gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Bug fixes in the ReferenceMap concept.
0 1 0
default
1 file changed with 4 insertions and 6 deletions:
↑ Collapse diff ↑
Ignore white space 96 line context
... ...
@@ -10,97 +10,96 @@
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_CONCEPT_MAPS_H
20 20
#define LEMON_CONCEPT_MAPS_H
21 21

	
22 22
#include <lemon/bits/utility.h>
23 23
#include <lemon/concept_check.h>
24 24

	
25 25
///\ingroup concept
26 26
///\file
27 27
///\brief Map concepts checking classes for testing and documenting.
28 28

	
29 29
namespace lemon {
30 30

	
31 31
  namespace concepts {
32 32
  
33 33
    /// \addtogroup concept
34 34
    /// @{
35 35

	
36 36
    /// Readable map concept
37 37

	
38 38
    /// Readable map concept.
39 39
    ///
40 40
    template<typename K, typename T>
41 41
    class ReadMap
42 42
    {
43 43
    public:
44 44
      /// The key type of the map.
45 45
      typedef K Key;    
46 46
      /// The value type of the map. (The type of objects associated with the keys).
47 47
      typedef T Value;
48 48

	
49 49
      /// Returns the value associated with a key.
50 50

	
51 51
      /// Returns the value associated with a key.
52 52
      /// \bug Value shouldn't need to be default constructible.
53 53
      ///
54 54
      Value operator[](const Key &) const {return Value();}
55 55

	
56 56
      template<typename _ReadMap>
57 57
      struct Constraints {
58

	
59 58
	void constraints() {
60 59
	  Value val = m[key];
61 60
	  val = m[key];
62 61
	  typename _ReadMap::Value own_val = m[own_key]; 
63 62
	  own_val = m[own_key]; 
64 63

	
65 64
	  ignore_unused_variable_warning(val);
66 65
	  ignore_unused_variable_warning(own_val);
67 66
	  ignore_unused_variable_warning(key);
68 67
	}
69 68
	Key& key;
70 69
	typename _ReadMap::Key& own_key;
71 70
	_ReadMap& m;
72 71
      };
73 72
      
74 73
    };
75 74

	
76 75

	
77 76
    /// Writable map concept
78 77
    
79 78
    /// Writable map concept.
80 79
    ///
81 80
    template<typename K, typename T>
82 81
    class WriteMap
83 82
    {
84 83
    public:
85 84
      /// The key type of the map.
86 85
      typedef K Key;    
87 86
      /// The value type of the map. (The type of objects associated with the keys).
88 87
      typedef T Value;
89 88

	
90 89
      /// Sets the value associated with a key.
91 90
      void set(const Key &,const Value &) {}
92 91

	
93 92
      ///Default constructor
94 93
      WriteMap() {}
95 94

	
96 95
      template <typename _WriteMap>
97 96
      struct Constraints {
98 97
	void constraints() {
99 98
	  // No constraints for constructor.
100 99
	  m.set(key, val);
101 100
	  m.set(own_key, own_val);
102 101
	  ignore_unused_variable_warning(key);
103 102
	  ignore_unused_variable_warning(val);
104 103
	  ignore_unused_variable_warning(own_key);
105 104
	  ignore_unused_variable_warning(own_val);
106 105
	}
... ...
@@ -130,79 +129,78 @@
130 129

	
131 130
      /// Returns the value associated with a key.
132 131
      Value operator[](const Key &) const {return Value();}
133 132
      /// Sets the value associated with a key.
134 133
      void set(const Key & ,const Value &) {}
135 134

	
136 135
      template<typename _ReadWriteMap>
137 136
      struct Constraints {
138 137
	void constraints() {
139 138
	  checkConcept<ReadMap<K, T>, _ReadWriteMap >();
140 139
	  checkConcept<WriteMap<K, T>, _ReadWriteMap >();
141 140
	}
142 141
      };
143 142
    };
144 143
  
145 144
  
146 145
    /// Dereferable map concept
147 146
    
148 147
    /// Dereferable map concept.
149 148
    ///
150 149
    /// \todo Rethink this concept.
151 150
    template<typename K, typename T, typename R, typename CR>
152 151
    class ReferenceMap : public ReadWriteMap<K,T>
153 152
    {
154 153
    public:
155 154
      /// Tag for reference maps.
156 155
      typedef True ReferenceMapTag;
157 156
      /// The key type of the map.
158 157
      typedef K Key;    
159 158
      /// The value type of the map. (The type of objects associated with the keys).
160 159
      typedef T Value;
161 160
      /// The reference type of the map.
162 161
      typedef R Reference;
163 162
      /// The const reference type of the map.
164 163
      typedef CR ConstReference;
165 164

	
166 165
    protected:
167 166
      Value tmp;
168 167
    public:
169 168

	
170 169
      ///Returns a reference to the value associated with a key.
171 170
      Reference operator[](const Key &) { return tmp; }
172 171
      ///Returns a const reference to the value associated with a key.
173 172
      ConstReference operator[](const Key &) const { return tmp; }
174 173
      /// Sets the value associated with a key.
175 174
      void set(const Key &k,const Value &t) { operator[](k)=t; }
176 175

	
177 176
      template<typename _ReferenceMap>
178
      struct ReferenceMapConcept {
179

	
177
      struct Constraints {
180 178
	void constraints() {
181
	  checkConcept<ReadWriteMap, _ReferenceMap >();
179
	  checkConcept<ReadWriteMap<K, T>, _ReferenceMap >();
182 180
	  m[key] = val;
183 181
	  val  = m[key];
184 182
	  m[key] = ref;
185 183
	  ref = m[key];
186 184
	  m[own_key] = own_val;
187 185
	  own_val  = m[own_key];
188 186
	  m[own_key] = own_ref;
189 187
	  own_ref = m[own_key];	  	  
190 188
	}
191 189

	
192 190
	typename _ReferenceMap::Key& own_key;
193 191
	typename _ReferenceMap::Value& own_val;
194
	typename _ReferenceMap::Reference& own_ref;
192
	typename _ReferenceMap::Reference own_ref;
195 193
	Key& key;
196 194
	Value& val;
197
	Reference& ref;
195
	Reference ref;
198 196
	_ReferenceMap& m;
199 197
      };
200 198
    };
201 199

	
202 200
    // @}
203 201

	
204 202
  } //namespace concepts
205 203

	
206 204
} //namespace lemon
207 205

	
208 206
#endif // LEMON_CONCEPT_MAPS_H
0 comments (0 inline)