gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Fixes in the map concepts - Now Value type needn't be default constructible. - Extend the test file to check this.
0 2 0
default
2 files changed with 22 insertions and 9 deletions:
↑ Collapse diff ↑
Ignore white space 6 line context
... ...
@@ -34,36 +34,35 @@
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 the given key.
50

	
51
      /// Returns the value associated with the given key.
52
      /// \bug Value shouldn't need to be default constructible. 
53
      Value operator[](const Key &) const { return Value(); }
50
      Value operator[](const Key &) const { 
51
        return *static_cast<Value *>(0);
52
      }
54 53

	
55 54
      template<typename _ReadMap>
56 55
      struct Constraints {
57 56
	void constraints() {
58 57
	  Value val = m[key];
59 58
	  val = m[key];
60 59
	  typename _ReadMap::Value own_val = m[own_key];
61 60
	  own_val = m[own_key];
62 61

	
63 62
	  ignore_unused_variable_warning(key);
64 63
	  ignore_unused_variable_warning(val);
65 64
	  ignore_unused_variable_warning(own_key);
66 65
	  ignore_unused_variable_warning(own_val);
67 66
	}
68 67
	const Key& key;
69 68
	const typename _ReadMap::Key& own_key;
... ...
@@ -113,33 +112,35 @@
113 112

	
114 113
    /// Read/writable map concept
115 114

	
116 115
    /// Read/writable map concept.
117 116
    ///
118 117
    template<typename K, typename T>
119 118
    class ReadWriteMap : public ReadMap<K,T>,
120 119
			 public WriteMap<K,T>
121 120
    {
122 121
    public:
123 122
      /// The key type of the map.
124 123
      typedef K Key;
125 124
      /// The value type of the map. (The type of objects associated with the keys).
126 125
      typedef T Value;
127 126

	
128 127
      /// Returns the value associated with the given key.
129
      Value operator[](const Key &) const { return Value(); }
128
      Value operator[](const Key &) const { 
129
        return *static_cast<Value *>(0);
130
      }
130 131

	
131 132
      /// Sets the value associated with the given key.
132 133
      void set(const Key &, const Value &) {}
133 134

	
134 135
      template<typename _ReadWriteMap>
135 136
      struct Constraints {
136 137
	void constraints() {
137 138
	  checkConcept<ReadMap<K, T>, _ReadWriteMap >();
138 139
	  checkConcept<WriteMap<K, T>, _ReadWriteMap >();
139 140
	}
140 141
      };
141 142
    };
142 143

	
143 144

	
144 145
    /// Dereferable map concept
145 146

	
... ...
@@ -147,41 +148,43 @@
147 148
    ///
148 149
    template<typename K, typename T, typename R, typename CR>
149 150
    class ReferenceMap : public ReadWriteMap<K,T>
150 151
    {
151 152
    public:
152 153
      /// Tag for reference maps.
153 154
      typedef True ReferenceMapTag;
154 155
      /// The key type of the map.
155 156
      typedef K Key;
156 157
      /// The value type of the map. (The type of objects associated with the keys).
157 158
      typedef T Value;
158 159
      /// The reference type of the map.
159 160
      typedef R Reference;
160 161
      /// The const reference type of the map.
161 162
      typedef CR ConstReference;
162 163

	
163
    protected:
164
      Value tmp;
165 164
    public:
166 165

	
167 166
      /// Returns a reference to the value associated with the given key.
168
      Reference operator[](const Key &) { return tmp; }
167
      Reference operator[](const Key &) { 
168
        return *static_cast<Value *>(0);
169
      }
169 170

	
170 171
      /// Returns a const reference to the value associated with the given key.
171
      ConstReference operator[](const Key &) const { return tmp; }
172
      ConstReference operator[](const Key &) const {
173
        return *static_cast<Value *>(0);
174
      }
172 175

	
173 176
      /// Sets the value associated with the given key.
174 177
      void set(const Key &k,const Value &t) { operator[](k)=t; }
175 178

	
176 179
      template<typename _ReferenceMap>
177 180
      struct Constraints {
178 181
	void constraints() {
179 182
	  checkConcept<ReadWriteMap<K, T>, _ReferenceMap >();
180 183
	  ref = m[key];
181 184
	  m[key] = val;
182 185
	  m[key] = ref;
183 186
	  m[key] = cref;
184 187
	  own_ref = m[own_key];
185 188
	  m[own_key] = own_val;
186 189
	  m[own_key] = own_ref;
187 190
	  m[own_key] = own_cref;
Ignore white space 32 line context
... ...
@@ -19,61 +19,71 @@
19 19
#include <deque>
20 20
#include <set>
21 21

	
22 22
#include <lemon/concept_check.h>
23 23
#include <lemon/concepts/maps.h>
24 24
#include <lemon/maps.h>
25 25

	
26 26
#include "test_tools.h"
27 27

	
28 28
using namespace lemon;
29 29
using namespace lemon::concepts;
30 30

	
31 31
struct A {};
32 32
inline bool operator<(A, A) { return true; }
33 33
struct B {};
34 34

	
35
class C {
36
  int x;
37
public:
38
  C(int _x) : x(_x) {}
39
};
40

	
35 41
class F {
36 42
public:
37 43
  typedef A argument_type;
38 44
  typedef B result_type;
39 45

	
40 46
  B operator()(const A&) const { return B(); }
41 47
private:
42 48
  F& operator=(const F&);
43 49
};
44 50

	
45 51
int func(A) { return 3; }
46 52

	
47 53
int binc(int a, B) { return a+1; }
48 54

	
49 55
typedef ReadMap<A, double> DoubleMap;
50 56
typedef ReadWriteMap<A, double> DoubleWriteMap;
51 57
typedef ReferenceMap<A, double, double&, const double&> DoubleRefMap;
52 58

	
53 59
typedef ReadMap<A, bool> BoolMap;
54 60
typedef ReadWriteMap<A, bool> BoolWriteMap;
55 61
typedef ReferenceMap<A, bool, bool&, const bool&> BoolRefMap;
56 62

	
57 63
int main()
58 64
{
59 65
  // Map concepts
60 66
  checkConcept<ReadMap<A,B>, ReadMap<A,B> >();
67
  checkConcept<ReadMap<A,C>, ReadMap<A,C> >();
61 68
  checkConcept<WriteMap<A,B>, WriteMap<A,B> >();
69
  checkConcept<WriteMap<A,C>, WriteMap<A,C> >();
62 70
  checkConcept<ReadWriteMap<A,B>, ReadWriteMap<A,B> >();
71
  checkConcept<ReadWriteMap<A,C>, ReadWriteMap<A,C> >();
63 72
  checkConcept<ReferenceMap<A,B,B&,const B&>, ReferenceMap<A,B,B&,const B&> >();
73
  checkConcept<ReferenceMap<A,C,C&,const C&>, ReferenceMap<A,C,C&,const C&> >();
64 74

	
65 75
  // NullMap
66 76
  {
67 77
    checkConcept<ReadWriteMap<A,B>, NullMap<A,B> >();
68 78
    NullMap<A,B> map1;
69 79
    NullMap<A,B> map2 = map1;
70 80
    map1 = nullMap<A,B>();
71 81
  }
72 82

	
73 83
  // ConstMap
74 84
  {
75 85
    checkConcept<ReadWriteMap<A,B>, ConstMap<A,B> >();
76 86
    ConstMap<A,B> map1;
77 87
    ConstMap<A,B> map2(B());
78 88
    ConstMap<A,B> map3 = map1;
79 89
    map1 = constMap<A>(B());
0 comments (0 inline)