1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
/**
** \file misc/unique.hxx
** \brief Inline implementation of misc::unique.
*/
#pragma once
#include <misc/contract.hh>
#include <misc/unique.hh>
namespace misc
{
template <typename T, class C> unique<T, C>::unique(const data_type& s)
// FIXME DONE: Some code was deleted here (Initializations).
/** \brief Following the Flyweight design pattern, set the attribute to a
unique reference of value s. You might want to check out std::set methods
on cppreference.com. */
{
if (object_set_instance().insert(s).second)
{
obj_ = &(*(object_set_instance().find(s)));
}
else
{
obj_ = &(*(object_set_instance().find(s)));
}
}
template <typename T, class C>
typename unique<T, C>::object_set_type& unique<T, C>::object_set_instance()
{
// FIXME DONE: Some code was deleted here (Classical Singleton pattern, a la Scott Meyers').
/** \brief Create a persistent instance of a set which would hold each value. */
static object_set_type res{};
return res;
}
template <typename T, class C>
typename unique<T, C>::object_size_type unique<T, C>::object_map_size()
{
// FIXME DONE: Some code was deleted here.
return object_set_instance().size();
}
template <typename T, class C>
inline const typename unique<T, C>::data_type& unique<T, C>::get() const
{
// FIXME DONE: Some code was deleted here.
return *obj_;
}
template <typename T, class C>
inline unique<T, C>::operator const data_type&() const
{
// FIXME DONE: Some code was deleted here.
return *obj_;
}
template <typename T, class C>
inline typename unique<T, C>::value_type&
unique<T, C>::operator=(const value_type& rhs)
{
if (this != &rhs)
obj_ = rhs.obj_;
return *this;
}
template <typename T, class C>
inline bool unique<T, C>::operator==(const value_type& rhs) const
{
return obj_ == rhs.obj_;
}
template <typename T, class C>
inline bool unique<T, C>::operator!=(const value_type& rhs) const
{
return !operator==(rhs);
}
template <typename T, class C>
inline bool unique<T, C>::operator<(const value_type& rhs) const
{
C cmp;
assertion(obj_);
assertion(rhs.obj_);
return cmp(*obj_, *rhs.obj_);
}
template <typename T, class C>
inline std::ostream& operator<<(std::ostream& ostr, const unique<T, C>& the)
{
return ostr << the.get();
}
} // namespace misc
|