6 ///\brief Miscellaneous property maps
8 ///\todo This file has the same name as the concept file in skeletons,
9 /// and this is not easily detectable in docs...
15 /// Base class of maps.
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>
29 /// Null map. (a.k.a. DoNothingMap)
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>
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&) {}
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>
57 /// Default constructor
59 /// The value of the map will be uninitialized.
60 /// (More exactly it will be default constructed.)
64 /// \param _v The initial value of the map.
65 ConstMap(const T &_v) : v(_v) {}
67 T operator[](const K&) const { return v; }
68 void set(const K&, const T&) {}
72 typedef ConstMap<K,T1> other;
76 ConstMap(const ConstMap<K,T1> &, const T &_v) : v(_v) {}
80 template<typename T, T v>
83 template<typename K, typename V, V v>
84 class ConstMap<K, Const<V, v> > : public MapBase<K, V>
88 V operator[](const K&) const { return v; }
89 void set(const K&, const V&) { }
92 typedef Const<bool, true> True;
93 typedef Const<bool, false> False;
95 /// \c std::map wrapper
97 /// This is essentially a wrapper for \c std::map. With addition that
98 /// you can specify a default value different from \c ValueType() .
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;
105 typedef typename parent::value_type PairType;
110 typedef T& ReferenceType;
111 typedef const T& ConstReferenceType;
115 /// Constructor with specified default value
116 StdMap(const T& _v) : v(_v) {}
118 /// \brief Constructs the map from an appropriate std::map.
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.
125 /// \warning Inefficient: copies the content of \c m !
126 StdMap(const parent &m, const T& _v) : parent(m), v(_v) {}
128 template<typename T1, typename Comp1>
129 StdMap(const StdMap<Key,T1,Comp1> &m, const T &_v) {
133 ReferenceType operator[](const Key &k) {
134 return insert(PairType(k,v)).first -> second;
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))
142 void set(const Key &k, const T &t) {
143 parent::operator[](k) = t;
146 /// Changes the default value of the map.
147 /// \return Returns the previous default value.
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; }
153 template<typename T1>
155 typedef StdMap<Key,T1,Compare> other;
160 #endif // HUGO_MAPS_H