[286] | 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 | /// Null map. (aka DoNothingMap) |
---|
| 16 | |
---|
| 17 | /// If you have to provide a map only for its type definitions, |
---|
| 18 | /// or if you have to provide a writable map, but will not use the |
---|
| 19 | /// data written to it... |
---|
| 20 | template<typename K, typename T> |
---|
| 21 | class NullMap |
---|
| 22 | { |
---|
| 23 | public: |
---|
| 24 | typedef K KeyType; |
---|
| 25 | typedef T ValueType; |
---|
| 26 | |
---|
| 27 | T operator[](const K&) const { return T(); } |
---|
| 28 | void set(const K&, const T&) {} |
---|
[529] | 29 | ///\bug when update is removed from map concepts by being dynamic |
---|
| 30 | ///stuffs, this line have to be removed. |
---|
| 31 | void update() { } |
---|
[286] | 32 | }; |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | /// Constant map. |
---|
| 36 | |
---|
| 37 | /// This is a readable map which assignes a specified value to each key. |
---|
| 38 | /// In other aspects it is equivalent to the \ref NullMap |
---|
| 39 | template<typename K, typename T> |
---|
| 40 | class ConstMap |
---|
| 41 | { |
---|
| 42 | T v; |
---|
| 43 | public: |
---|
| 44 | typedef K KeyType; |
---|
| 45 | typedef T ValueType; |
---|
| 46 | |
---|
| 47 | ConstMap() {} |
---|
| 48 | ConstMap(const T &_v) : v(_v) {} |
---|
| 49 | |
---|
| 50 | T operator[](const K&) const { return v; } |
---|
| 51 | void set(const K&, const T&) {} |
---|
| 52 | |
---|
| 53 | template<typename T1> |
---|
| 54 | struct rebind { |
---|
| 55 | typedef ConstMap<K,T1> other; |
---|
| 56 | }; |
---|
| 57 | |
---|
| 58 | template<typename T1> |
---|
| 59 | ConstMap(const ConstMap<K,T1> &, const T &_v) : v(_v) {} |
---|
| 60 | }; |
---|
| 61 | |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | /// \c std::map wrapper |
---|
| 65 | |
---|
| 66 | /// This is essentially a wrapper for \c std::map. With addition that |
---|
| 67 | /// you can specify a default value different from \c ValueType() . |
---|
| 68 | /// |
---|
| 69 | /// \todo Provide allocator parameter... |
---|
| 70 | template <typename Key, typename T, typename Compare = std::less<Key> > |
---|
| 71 | class StdMap : public std::map<Key,T,Compare> { |
---|
| 72 | typedef std::map<Key,T,Compare> parent; |
---|
| 73 | T v; |
---|
| 74 | typedef typename parent::value_type PairType; |
---|
| 75 | |
---|
| 76 | public: |
---|
| 77 | typedef Key KeyType; |
---|
| 78 | typedef T ValueType; |
---|
| 79 | typedef T& ReferenceType; |
---|
| 80 | typedef const T& ConstReferenceType; |
---|
| 81 | |
---|
| 82 | |
---|
[345] | 83 | StdMap() : v() {} |
---|
[286] | 84 | /// Constructor with specified default value |
---|
| 85 | StdMap(const T& _v) : v(_v) {} |
---|
| 86 | |
---|
| 87 | /// \brief Constructs the map from an appropriate std::map. |
---|
| 88 | /// |
---|
| 89 | /// \warning Inefficient: copies the content of \c m ! |
---|
| 90 | StdMap(const parent &m) : parent(m) {} |
---|
| 91 | /// \brief Constructs the map from an appropriate std::map, and explicitly |
---|
| 92 | /// specifies a default value. |
---|
| 93 | /// |
---|
| 94 | /// \warning Inefficient: copies the content of \c m ! |
---|
| 95 | StdMap(const parent &m, const T& _v) : parent(m), v(_v) {} |
---|
| 96 | |
---|
| 97 | template<typename T1, typename Comp1> |
---|
[389] | 98 | StdMap(const StdMap<Key,T1,Comp1> &m, const T &_v) { |
---|
| 99 | //FIXME; |
---|
| 100 | } |
---|
[286] | 101 | |
---|
| 102 | ReferenceType operator[](const Key &k) { |
---|
[346] | 103 | return insert(PairType(k,v)).first -> second; |
---|
[286] | 104 | } |
---|
| 105 | ConstReferenceType operator[](const Key &k) const { |
---|
[389] | 106 | typename parent::iterator i = lower_bound(k); |
---|
[391] | 107 | if (i == parent::end() || parent::key_comp()(k, (*i).first)) |
---|
[286] | 108 | return v; |
---|
| 109 | return (*i).second; |
---|
| 110 | } |
---|
[345] | 111 | void set(const Key &k, const T &t) { |
---|
[346] | 112 | parent::operator[](k) = t; |
---|
[345] | 113 | } |
---|
[286] | 114 | |
---|
| 115 | /// Changes the default value of the map. |
---|
| 116 | /// \return Returns the previous default value. |
---|
| 117 | /// |
---|
| 118 | /// \warning The value of some keys (which has alredy been queried, but |
---|
| 119 | /// the value has been unchanged from the default) may change! |
---|
| 120 | T setDefault(const T &_v) { T old=v; v=_v; return old; } |
---|
| 121 | |
---|
| 122 | template<typename T1> |
---|
| 123 | struct rebind { |
---|
| 124 | typedef StdMap<Key,T1,Compare> other; |
---|
| 125 | }; |
---|
| 126 | }; |
---|
| 127 | |
---|
| 128 | } |
---|
| 129 | #endif // HUGO_MAPS_H |
---|