blob: 27cd05eae05ff7b6c079d7421b1b0d864059f0fa (
plain)
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
|
/**
** \file misc/unique.hh
** \brief Definition of misc::unique.
*/
#pragma once
#include <iosfwd>
#include <set>
namespace misc
{
/** \brief Define \c unique class.
**
** Implementation of the flyweight pattern.
** Map identical objects to a unique reference.
*/
template <typename T, class C = std::less<T>> class unique
{
protected:
/// The unique's object type.
using object_set_type = std::set<T, C>;
/// The type for the size of the unique set.
using object_size_type = typename object_set_type::size_type;
public:
using value_type = unique;
using data_type = T;
/** \name Ctor & Dtor.
** \{ */
/** \brief Construct a \c unique.
** \param s referenced object */
unique(const data_type& s);
/** \brief Construct a \c unique.
** \param u \c unique to copy. */
constexpr unique(const unique& u) = default;
virtual ~unique() = default;
/** \} */
/** \name Accessors.
** \{ */
/// The object referenced by \c this.
const data_type& get() const;
operator const data_type&() const;
/// The number of referenced objects.
static object_size_type object_map_size();
/** \} */
/** \name Operators.
** \{ */
/** \brief Assign a \c unique to this \c unique.
** \param rhs \c unique to copy. */
value_type& operator=(const value_type& rhs);
/** \brief Compare two \c unique for equality.
** \param rhs \c unique to compare with. */
bool operator==(const value_type& rhs) const;
/** \brief Compare two \c unique for inequality.
** \param rhs \c unique to compare with. */
bool operator!=(const value_type& rhs) const;
/** \brief Compare two \c unique for order.
** \param rhs \c unique to compare with. */
bool operator<(const value_type& rhs) const;
/** \} */
protected:
/// Return the set of uniques.
static object_set_type& object_set_instance();
/// Pointer to the unique referenced object.
const data_type* obj_;
};
/** \brief Intercept output stream redirection.
** \param ostr the destination output stream
** \param u a reference to the unique to redirect
*/
template <typename T, class C>
std::ostream& operator<<(std::ostream& ostr, const unique<T, C>& u);
} // namespace misc
#include <misc/unique.hxx>
|