1 | // -*- C++ -*- // |
---|
2 | #ifndef HUGO_MAPS_H |
---|
3 | #define HUGO_MAPS_H |
---|
4 | |
---|
5 | ///\file |
---|
6 | ///\brief Miscellaneous property maps |
---|
7 | /// |
---|
8 | ///\todo This file has the same name as the concept file in skeletons, |
---|
9 | /// and this is not easily detectable in docs... |
---|
10 | |
---|
11 | #include <map> |
---|
12 | |
---|
13 | namespace hugo { |
---|
14 | |
---|
15 | /// Base class of maps. |
---|
16 | |
---|
17 | /// Base class of maps. |
---|
18 | /// It provides the necessary <tt>typedef</tt>s required by the map concept. |
---|
19 | template<typename K, typename T> |
---|
20 | class MapBase |
---|
21 | { |
---|
22 | public: |
---|
23 | /// . |
---|
24 | typedef K KeyType; |
---|
25 | /// . |
---|
26 | typedef T ValueType; |
---|
27 | }; |
---|
28 | |
---|
29 | /// Null map. (a.k.a. DoNothingMap) |
---|
30 | |
---|
31 | /// If you have to provide a map only for its type definitions, |
---|
32 | /// or if you have to provide a writable map, but |
---|
33 | /// data written to it will sent to <tt>/dev/null</tt>... |
---|
34 | template<typename K, typename T> |
---|
35 | class NullMap : public MapBase<K,T> |
---|
36 | { |
---|
37 | public: |
---|
38 | |
---|
39 | /// Gives back a default constructed element. |
---|
40 | T operator[](const K&) const { return T(); } |
---|
41 | /// Absorbs the value. |
---|
42 | void set(const K&, const T&) {} |
---|
43 | }; |
---|
44 | |
---|
45 | |
---|
46 | /// Constant map. |
---|
47 | |
---|
48 | /// This is a readable map which assigns a specified value to each key. |
---|
49 | /// In other aspects it is equivalent to the \ref NullMap. |
---|
50 | /// \todo set could be used to set the value. |
---|
51 | template<typename K, typename T> |
---|
52 | class ConstMap : public MapBase<K,T> |
---|
53 | { |
---|
54 | T v; |
---|
55 | public: |
---|
56 | |
---|
57 | /// Default constructor |
---|
58 | |
---|
59 | /// The value of the map will be uninitialized. |
---|
60 | /// (More exactly it will be default constructed.) |
---|
61 | ConstMap() {} |
---|
62 | /// . |
---|
63 | |
---|
64 | /// \param _v The initial value of the map. |
---|
65 | ConstMap(const T &_v) : v(_v) {} |
---|
66 | |
---|
67 | T operator[](const K&) const { return v; } |
---|
68 | void set(const K&, const T&) {} |
---|
69 | |
---|
70 | template<typename T1> |
---|
71 | struct rebind { |
---|
72 | typedef ConstMap<K,T1> other; |
---|
73 | }; |
---|
74 | |
---|
75 | template<typename T1> |
---|
76 | ConstMap(const ConstMap<K,T1> &, const T &_v) : v(_v) {} |
---|
77 | }; |
---|
78 | |
---|
79 | //to document later |
---|
80 | template<typename T, T v> |
---|
81 | struct Const { }; |
---|
82 | //to document later |
---|
83 | template<typename K, typename V, V v> |
---|
84 | class ConstMap<K, Const<V, v> > : public MapBase<K, V> |
---|
85 | { |
---|
86 | public: |
---|
87 | ConstMap() { } |
---|
88 | V operator[](const K&) const { return v; } |
---|
89 | void set(const K&, const V&) { } |
---|
90 | }; |
---|
91 | //to document later |
---|
92 | typedef Const<bool, true> True; |
---|
93 | typedef Const<bool, false> False; |
---|
94 | |
---|
95 | /// \c std::map wrapper |
---|
96 | |
---|
97 | /// This is essentially a wrapper for \c std::map. With addition that |
---|
98 | /// you can specify a default value different from \c ValueType() . |
---|
99 | /// |
---|
100 | /// \todo Provide allocator parameter... |
---|
101 | template <typename Key, typename T, typename Compare = std::less<Key> > |
---|
102 | class StdMap : public std::map<Key,T,Compare> { |
---|
103 | typedef std::map<Key,T,Compare> parent; |
---|
104 | T v; |
---|
105 | typedef typename parent::value_type PairType; |
---|
106 | |
---|
107 | public: |
---|
108 | typedef Key KeyType; |
---|
109 | typedef T ValueType; |
---|
110 | typedef T& ReferenceType; |
---|
111 | typedef const T& ConstReferenceType; |
---|
112 | |
---|
113 | |
---|
114 | StdMap() : v() {} |
---|
115 | /// Constructor with specified default value |
---|
116 | StdMap(const T& _v) : v(_v) {} |
---|
117 | |
---|
118 | /// \brief Constructs the map from an appropriate std::map. |
---|
119 | /// |
---|
120 | /// \warning Inefficient: copies the content of \c m ! |
---|
121 | StdMap(const parent &m) : parent(m) {} |
---|
122 | /// \brief Constructs the map from an appropriate std::map, and explicitly |
---|
123 | /// specifies a default value. |
---|
124 | /// |
---|
125 | /// \warning Inefficient: copies the content of \c m ! |
---|
126 | StdMap(const parent &m, const T& _v) : parent(m), v(_v) {} |
---|
127 | |
---|
128 | template<typename T1, typename Comp1> |
---|
129 | StdMap(const StdMap<Key,T1,Comp1> &m, const T &_v) { |
---|
130 | //FIXME; |
---|
131 | } |
---|
132 | |
---|
133 | ReferenceType operator[](const Key &k) { |
---|
134 | return insert(PairType(k,v)).first -> second; |
---|
135 | } |
---|
136 | ConstReferenceType operator[](const Key &k) const { |
---|
137 | typename parent::iterator i = lower_bound(k); |
---|
138 | if (i == parent::end() || parent::key_comp()(k, (*i).first)) |
---|
139 | return v; |
---|
140 | return (*i).second; |
---|
141 | } |
---|
142 | void set(const Key &k, const T &t) { |
---|
143 | parent::operator[](k) = t; |
---|
144 | } |
---|
145 | |
---|
146 | /// Changes the default value of the map. |
---|
147 | /// \return Returns the previous default value. |
---|
148 | /// |
---|
149 | /// \warning The value of some keys (which has already been queried, but |
---|
150 | /// the value has been unchanged from the default) may change! |
---|
151 | T setDefault(const T &_v) { T old=v; v=_v; return old; } |
---|
152 | |
---|
153 | template<typename T1> |
---|
154 | struct rebind { |
---|
155 | typedef StdMap<Key,T1,Compare> other; |
---|
156 | }; |
---|
157 | }; |
---|
158 | |
---|
159 | } |
---|
160 | #endif // HUGO_MAPS_H |
---|