forked from sinojelly/mockcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValueHolder.h
More file actions
305 lines (249 loc) · 6.16 KB
/
ValueHolder.h
File metadata and controls
305 lines (249 loc) · 6.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/***
mockcpp is a C/C++ mock framework.
Copyright [2008] [Darwin Yuan <[email protected]>]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
***/
#ifndef __MOCKPP_VALUEHOLDER_H
#define __MOCKPP_VALUEHOLDER_H
#include <iostream>
#include <mockcpp/mockcpp.h>
#include <mockcpp/types/Holder.h>
#include <mockcpp/EqualityUtil.h>
#include <mockcpp/Formatter.h>
#include <mockcpp/IsEqual.h>
#include <mockcpp/Void.h>
#include <mockcpp/IsAnythingHelper.h>
#include <mockcpp/Ignore.h>
MOCKCPP_NS_START
namespace {
template <typename T>
Constraint* constraint(const T& value)
{
return new IsEqual<T>(value);
}
#ifndef _MSC_VER
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#endif
Constraint* constraint(Constraint* c)
{
return c == 0 ? any() : c;
}
Constraint* constraint(const Constraint* c)
{
return constraint(const_cast<Constraint*>(c));
}
#ifndef _MSC_VER
#pragma GCC diagnostic pop
#endif
Constraint* constraint(const Void& v)
{
return any();
}
}
template<typename ValueType>
struct ValueHolderBase : public Holder<ValueType>
{
Constraint* getConstraint() const
{
return constraint(getValue());
}
virtual const ValueType& getValue() const = 0;
};
template<typename ValueType>
struct ValueHolder : public ValueHolderBase<ValueType>
{
ValueHolder(const ValueType& value)
: held(value)
{
}
PlaceHolder * clone() const
{ return new ValueHolder(held); }
const ValueType& getValue() const
{
return held;
}
private:
ValueType held;
};
///////////////////////////////////////////////
template <typename ValueType>
struct UnsignedLongHolder : public ValueHolderBase<ValueType>
{
protected:
union Held{
unsigned long ul;
unsigned int ui;
unsigned short us;
unsigned char uc;
Held(const unsigned long value) : ul(value) {}
Held(const unsigned int value) : ul(value) {}
Held(const unsigned short value) : ul(value) {}
Held(const unsigned char value) : ul(value) {}
} held;
public:
UnsignedLongHolder(const ValueType& value)
: held(value)
{
}
};
///////////////////////////////////////////////
template <>
struct ValueHolder<unsigned long>
: public UnsignedLongHolder<unsigned long>
{
ValueHolder(unsigned long value)
: UnsignedLongHolder<unsigned long>(value)
{
}
const unsigned long& getValue() const
{
return held.ul;
}
PlaceHolder * clone() const
{ return new ValueHolder(held.ul); }
};
///////////////////////////////////////////////
template <>
struct ValueHolder<unsigned int>
: public UnsignedLongHolder<unsigned int>
{
ValueHolder(unsigned int value)
: UnsignedLongHolder<unsigned int>(value)
{
}
const unsigned int& getValue() const
{
return held.ui;
}
PlaceHolder * clone() const
{ return new ValueHolder(held.ui); }
};
///////////////////////////////////////////////
template <>
struct ValueHolder<unsigned short>
: public UnsignedLongHolder<unsigned short>
{
ValueHolder(unsigned short value)
: UnsignedLongHolder<unsigned short>(value)
{
}
const unsigned short& getValue() const
{
return held.us;
}
PlaceHolder * clone() const
{ return new ValueHolder(held.us); }
};
///////////////////////////////////////////////
template <>
struct ValueHolder<unsigned char>
: public UnsignedLongHolder<unsigned char>
{
ValueHolder(unsigned char value)
: UnsignedLongHolder<unsigned char>(value)
{
}
const unsigned char& getValue() const
{
return held.uc;
}
PlaceHolder * clone() const
{ return new ValueHolder(held.uc); }
};
///////////////////////////////////////////////
template <typename ValueType>
struct SignedLongHolder : public ValueHolderBase<ValueType>
{
protected:
union Held{
long sl;
int si;
short ss;
char sc;
Held(const long value) : sl(value) {}
Held(const int value) : sl(value) {}
Held(const short value) : sl(value) {}
Held(const char value) : sl(value) {}
} held;
public:
SignedLongHolder(const ValueType& value)
: held(value)
{
}
};
///////////////////////////////////////////////
template <>
struct ValueHolder<long>
: public SignedLongHolder<long>
{
ValueHolder(const long& value)
: SignedLongHolder<long>(value)
{
}
const long& getValue() const
{
return held.sl;
}
PlaceHolder * clone() const
{ return new ValueHolder(held.sl); }
};
///////////////////////////////////////////////
template <>
struct ValueHolder<int>
: public SignedLongHolder<int>
{
ValueHolder(const int& value)
: SignedLongHolder<int>(value)
{
}
const int& getValue() const
{
return held.si;
}
PlaceHolder * clone() const
{ return new ValueHolder(held.si); }
};
///////////////////////////////////////////////
template <>
struct ValueHolder<short>
: public SignedLongHolder<short>
{
ValueHolder(const short& value)
: SignedLongHolder<short>(value)
{
}
const short& getValue() const
{
return held.ss;
}
PlaceHolder * clone() const
{ return new ValueHolder(held.ss); }
};
///////////////////////////////////////////////
template <>
struct ValueHolder<char>
: public SignedLongHolder<char>
{
ValueHolder(const char& value)
: SignedLongHolder<char>(value)
{
}
const char& getValue() const
{
return held.sc;
}
PlaceHolder * clone() const
{ return new ValueHolder(held.sc); }
};
///////////////////////////////////////////////
MOCKCPP_NS_END
#endif // __MOCKPP_VALUEHOLDER_H