-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCWrapper.hpp
More file actions
347 lines (307 loc) · 9.82 KB
/
CWrapper.hpp
File metadata and controls
347 lines (307 loc) · 9.82 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#ifndef CWRAPPER_HPP_INCLUDED
#define CWRAPPER_HPP_INCLUDED
#include <utility>
#include <exception>
#define HAS_STATIC_MEMBER_DETECTOR(member) \
template<typename T> \
class HAS_STATIC_MEMBER_DETECTOR_CLASS_ ## member \
{ \
struct two_ints_type { int two_ints_1, two_ints_2; }; \
template<typename U> \
static two_ints_type has_func_helper(...) { } \
template<typename U> \
static int has_func_helper(int, decltype(U::member)* func = nullptr) \
{ return 0; } \
public: \
static constexpr bool value = \
sizeof(decltype(has_func_helper<T>(0))) == sizeof(int); \
}
#define HAS_STATIC_MEMBER(type, member) \
(HAS_STATIC_MEMBER_DETECTOR_CLASS_ ## member<type>::value)
#define HAS_NESTED_TYPE_DETECTOR(member) \
template<typename T> \
class HAS_NESTED_TYPE_DETECTOR_CLASS_ ## member \
{ \
struct two_ints_type { int two_ints_1, two_ints_2; }; \
template<typename U> \
static two_ints_type has_func_helper(...) { } \
template<typename U> \
static int has_func_helper(int, typename U::member* ty = nullptr) \
{ return 0; } \
public: \
static constexpr bool value = \
sizeof(decltype(has_func_helper<T>(0))) == sizeof(int); \
}
#define HAS_NESTED_TYPE(type, member) \
(HAS_NESTED_TYPE_DETECTOR_CLASS_ ## member<type>::value)
namespace CW {
enum class CWrapperType
{
Implicit,
Explicit,
Get,
};
struct CWrapperException : public std::exception
{
virtual const char * what() const noexcept override
{
return "CWrapperException";
}
};
template<
typename H,
typename F,
CWrapperType TY = CWrapperType::Get,
bool C = true>
class CWrapperFriend
{
template<
typename HANDLE_T,
typename BASE,
bool CONSTSAFE>
struct ArrowHandler
{ };
template<
typename PTR_T,
typename BASE>
struct ArrowHandler<PTR_T*, BASE, false>
{
PTR_T* operator->() const
{ return static_cast<const BASE*>(this)->ptr; }
};
template<
typename PTR_T,
typename BASE>
struct ArrowHandler<PTR_T*, BASE, true>
{
PTR_T* operator->()
{ return static_cast<BASE*>(this)->ptr; }
PTR_T const* operator->() const
{ return static_cast<const BASE*>(this)->ptr; }
};
template<
typename HANDLE_T,
typename BASE,
CWrapperType TYPE,
bool CONSTSAFE>
struct ConversionHandler : public ArrowHandler<HANDLE_T, BASE, CONSTSAFE>
{ };
template<
typename HANDLE_T,
typename BASE,
bool CONSTSAFE>
struct ConversionHandler<HANDLE_T, BASE, CWrapperType::Implicit, CONSTSAFE>
: public ArrowHandler<HANDLE_T, BASE, CONSTSAFE>
{
operator HANDLE_T() const
{ return static_cast<const BASE*>(this)->ptr; }
};
template<
typename HANDLE_T,
typename BASE,
bool CONSTSAFE>
struct ConversionHandler<HANDLE_T, BASE, CWrapperType::Explicit, CONSTSAFE>
: public ArrowHandler<HANDLE_T, BASE, CONSTSAFE>
{
explicit operator HANDLE_T() const
{ return static_cast<const BASE*>(this)->ptr; }
};
template<
typename HANDLE_T,
typename BASE,
bool CONSTSAFE>
struct ConversionHandler<HANDLE_T, BASE, CWrapperType::Get, CONSTSAFE>
: public ArrowHandler<HANDLE_T, BASE, CONSTSAFE>
{
HANDLE_T get() const
{ return static_cast<const BASE*>(this)->ptr; }
};
template<
typename PTR_T,
typename BASE>
struct ConversionHandler<PTR_T*, BASE, CWrapperType::Implicit, true>
: public ArrowHandler<PTR_T*, BASE, true>
{
operator PTR_T*()
{ return static_cast<BASE*>(this)->ptr; }
operator PTR_T const*() const
{ return static_cast<const BASE*>(this)->ptr; }
};
template<
typename PTR_T,
typename BASE>
struct ConversionHandler<PTR_T*, BASE, CWrapperType::Explicit, true>
: public ArrowHandler<PTR_T*, BASE, true>
{
explicit operator PTR_T*()
{ return static_cast<BASE*>(this)->ptr; }
explicit operator PTR_T const*() const
{ return static_cast<const BASE*>(this)->ptr; }
};
template<
typename PTR_T,
typename BASE>
struct ConversionHandler<PTR_T*, BASE, CWrapperType::Get, true>
: public ArrowHandler<PTR_T*, BASE, true>
{
PTR_T* get()
{ return static_cast<BASE*>(this)->ptr; }
PTR_T const* get() const
{ return static_cast<const BASE*>(this)->ptr; }
};
template<bool b, typename T>
struct hider
{
static constexpr bool value = b;
};
// Works exactly like std::enable if. I didn't want to include
// type_traits for this one and COND
template<bool cond, typename T = void>
struct enabler
{ };
template<typename T>
struct enabler<true, T>
{
using type = T;
};
template<bool condition, typename TRUE_TYPE, typename FALSE_TYPE>
struct COND
{
using type = TRUE_TYPE;
};
template<typename TRUE_TYPE, typename FALSE_TYPE>
struct COND<false, TRUE_TYPE, FALSE_TYPE>
{
using type = FALSE_TYPE;
};
template<
typename HANDLE_T,
typename FUNCTIONS,
CWrapperType TYPE,
bool CONSTSAFE,
typename EXCEPTION_T,
typename INVALID_T,
typename VALIDATE_T,
typename COPY_T,
bool HAS_COPY>
class CWrapperBase
: public ConversionHandler<
HANDLE_T,
CWrapperBase<HANDLE_T, FUNCTIONS, TYPE, CONSTSAFE, EXCEPTION_T,
INVALID_T, VALIDATE_T, COPY_T, HAS_COPY>,
TYPE,
CONSTSAFE>
{
friend ConversionHandler<
HANDLE_T,
CWrapperBase<HANDLE_T, FUNCTIONS, TYPE, CONSTSAFE, EXCEPTION_T,
INVALID_T, VALIDATE_T, COPY_T, HAS_COPY>,
TYPE,
CONSTSAFE>;
protected:
HANDLE_T ptr;
public:
explicit CWrapperBase(HANDLE_T ptr) :
ptr{ptr}
{
if(!VALIDATE_T::validate_func(ptr))
throw typename EXCEPTION_T::exception{};
}
template<typename DUMMY = void,
typename = typename enabler<hider<HAS_COPY, DUMMY>::value>::type>
CWrapperBase(CWrapperBase const& other) :
CWrapperBase{COPY_T::copy_func(other.ptr)}
{ }
// This one is needed to prevent variadic ctor to be
// called when you want to copy a non-const object.
template<typename DUMMY = void,
typename = typename enabler<hider<HAS_COPY, DUMMY>::value>::type>
CWrapperBase(CWrapperBase& other) :
CWrapperBase{COPY_T::copy_func(other.ptr)}
{ }
template<typename DUMMY = void,
typename = typename enabler<hider<!HAS_COPY, DUMMY>::value>::type>
CWrapperBase(CWrapperBase const& other, void ** ptr = nullptr) = delete;
// This one is needed to prevent variadic ctor to be
// called when you want to copy a non-const object.
template<typename DUMMY = void,
typename = typename enabler<hider<!HAS_COPY, DUMMY>::value>::type>
CWrapperBase(CWrapperBase& other, void ** ptr = nullptr) = delete;
template<typename... ARGS>
explicit CWrapperBase(ARGS&&... args) :
CWrapperBase{FUNCTIONS::ctor_func(std::forward<ARGS>(args)...)}
{ }
CWrapperBase(CWrapperBase&& old) :
CWrapperBase{old.ptr}
{
old.ptr = INVALID_T::invalid_value;
}
~CWrapperBase()
{
if(VALIDATE_T::validate_func(ptr))
FUNCTIONS::dtor_func(ptr);
}
CWrapperBase& operator=(CWrapperBase other)
{
std::swap(ptr, other.ptr);
return *this;
}
};
HAS_NESTED_TYPE_DETECTOR(exception);
HAS_STATIC_MEMBER_DETECTOR(copy_func);
HAS_STATIC_MEMBER_DETECTOR(invalid_value);
HAS_STATIC_MEMBER_DETECTOR(validate_func);
struct default_exception_type
{
using exception = CWrapperException;
};
template<typename T>
struct default_invalid_value
{
static constexpr T invalid_value = 0;
};
template<typename T>
struct default_invalid_value<T*>
{
static constexpr T* invalid_value = nullptr;
};
template<typename T, typename INVALID_T>
struct default_validate_func
{
static constexpr bool validate_func(T ptr)
{
return ptr != INVALID_T::invalid_value;
}
};
// this one is never called, but necessary to compile if copy ctor is deleted
template<typename T>
struct default_copy_func
{
static constexpr T copy_func(T const& other)
{
return other;
}
};
using E = typename COND< HAS_NESTED_TYPE(F, exception),
F, default_exception_type>::type;
using D = typename COND< HAS_STATIC_MEMBER(F, invalid_value),
F, default_invalid_value<H>>::type;
using V = typename COND< HAS_STATIC_MEMBER(F, validate_func),
F, default_validate_func<H, D>>::type;
using COPY = typename COND< HAS_STATIC_MEMBER(F, copy_func),
F, default_copy_func<H>>::type;
public:
using type = CWrapperBase<H, F, TY, C, E, D, V, COPY, HAS_STATIC_MEMBER(F, copy_func)>;
};
template<
typename HANDLE_T,
typename FUNCTIONS,
CWrapperType TYPE = CWrapperType::Get,
bool CONSTSAFE = true>
using CWrapper = typename CWrapperFriend<HANDLE_T, FUNCTIONS, TYPE, CONSTSAFE>::type;
}
#undef HAS_STATIC_MEMBER_DETECTOR
#undef HAS_STATIC_MEMBER
#undef HAS_NESTED_TYPE_DETECTOR
#undef HAS_NESTED_TYPE
#endif // CWRAPPER_HPP_INCLUDED