/
policy.hpp
188 lines (177 loc) · 4.9 KB
/
policy.hpp
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
/* This software is distributed under BSD 3-clause license (see LICENSE file).
*
* Copyright (c) 2012-2013 Sergey Lisitsyn
*/
#ifndef TAPKEE_POLICY_H_
#define TAPKEE_POLICY_H_
namespace tapkee
{
namespace tapkee_internal
{
struct TypePolicyBase
{
virtual ~TypePolicyBase() {}
virtual void copyFromValue(void const*, void**) const = 0;
virtual void* getValue(void**) const = 0;
virtual void free(void**) const = 0;
virtual void clone(void* const*, void**) const = 0;
virtual void move(void* const*, void**) const = 0;
};
template <typename T>
struct PointerTypePolicyImpl : public TypePolicyBase
{
inline virtual void copyFromValue(void const* src, void** dest) const
{
*dest = new T(*reinterpret_cast<T const*>(src));
}
inline virtual void* getValue(void** src) const
{
return *src;
}
inline virtual void free(void** src) const
{
if (*src)
delete (*reinterpret_cast<T**>(src));
*src = NULL;
}
virtual void clone(void* const* src, void** dest) const
{
if (*dest)
(*reinterpret_cast<T**>(dest))->~T();
*dest = new T(**reinterpret_cast<T* const*>(src));
}
inline virtual void move(void* const* src, void** dest) const
{
(*reinterpret_cast<T**>(dest))->~T();
**reinterpret_cast<T**>(dest) = **reinterpret_cast<T* const*>(src);
}
};
template <typename T>
TypePolicyBase* getPolicy()
{
static PointerTypePolicyImpl<T> policy;
return &policy;
}
struct CheckerPolicyBase
{
virtual ~CheckerPolicyBase() {}
virtual bool isInRange(void* const*, void*, void*) const = 0;
virtual bool isEqual(void* const*, void*) const = 0;
virtual bool isNotEqual(void* const*, void*) const = 0;
virtual bool isPositive(void* const*) const = 0;
virtual bool isNonNegative(void * const*) const = 0;
virtual bool isNegative(void* const*) const = 0;
virtual bool isNonPositive(void * const*) const = 0;
virtual bool isGreater(void* const*, void*) const = 0;
virtual bool isLesser(void* const*, void*) const = 0;
};
template <typename T>
struct PointerCheckerPolicyImpl : public CheckerPolicyBase
{
virtual bool isInRange(void* const*, void*, void*) const
{
return false;
}
virtual bool isEqual(void* const*, void*) const
{
return false;
}
virtual bool isNotEqual(void* const*, void*) const
{
return false;
}
virtual bool isPositive(void* const*) const
{
return false;
}
virtual bool isNonNegative(void* const*) const
{
return false;
}
virtual bool isNegative(void* const*) const
{
return false;
}
virtual bool isNonPositive(void* const*) const
{
return false;
}
virtual bool isGreater(void* const*, void*) const
{
return false;
}
virtual bool isLesser(void* const*, void*) const
{
return false;
}
};
#define default_policy_for(Type) \
template <> \
struct PointerCheckerPolicyImpl<Type> : public CheckerPolicyBase \
{ \
inline Type value(void* v) const \
{ \
return *reinterpret_cast<Type*>(v); \
} \
virtual bool isInRange(void* const* src, void* lower, void* upper) const \
{ \
Type v = value(*src); \
Type l = value(lower); \
Type u = value(upper); \
return (v>=l) && (v<u); \
} \
virtual bool isEqual(void* const* src, void* other_src) const \
{ \
Type v = value(*src); \
Type ov = value(other_src); \
return (v==ov); \
} \
virtual bool isNotEqual(void* const* src, void* other_src) const \
{ \
Type v = value(*src); \
Type ov = value(other_src); \
return (v!=ov); \
} \
virtual bool isPositive(void* const* src) const \
{ \
Type v = value(*src); \
return (v>0); \
} \
virtual bool isNonNegative(void* const* src) const \
{ \
Type v = value(*src); \
return (v>=0); \
} \
virtual bool isNegative(void* const* src) const \
{ \
Type v = value(*src); \
return (v<0); \
} \
virtual bool isNonPositive(void* const* src) const \
{ \
Type v = value(*src); \
return (v<=0); \
} \
virtual bool isGreater(void* const* src, void* lower) const \
{ \
Type v = value(*src); \
return (v>value(lower)); \
} \
virtual bool isLesser(void* const* src, void* upper) const \
{ \
Type v = value(*src); \
return (v<value(upper)); \
} \
}
default_policy_for(double);
default_policy_for(float);
default_policy_for(int);
template <typename T>
CheckerPolicyBase* getCheckerPolicy()
{
static PointerCheckerPolicyImpl<T> policy;
return &policy;
}
}
}
#endif