-
Notifications
You must be signed in to change notification settings - Fork 0
/
Private.hpp
500 lines (406 loc) · 13.7 KB
/
Private.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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//A Smart Pointer to IMPLementation (i.e. Smart PIMPL or just SPIMPL).
//********************************************************************
//Copyright (c) 2015 Andrey Upadyshev ([email protected])
//Distributed under the Boost Software License, Version 1.0.
//See http://www.boost.org/LICENSE_1_0.txt
#pragma once
#include <memory>
#include <type_traits>
#include <cassert>
#if defined _MSC_VER && _MSC_VER < 1900 // MS Visual Studio before VS2015
#define SPIMPL_NO_CPP11_NOEXCEPT
#define SPIMPL_NO_CPP11_CONSTEXPR
#define SPIMPL_NO_CPP11_DEFAULT_MOVE_SPEC_FUNC
#endif
#if ! defined SPIMPL_NO_CPP11_NOEXCEPT
#define SPIMPL_NOEXCEPT noexcept
#else
#define SPIMPL_NOEXCEPT
#endif
#if ! defined SPIMPL_NO_CPP11_CONSTEXPR
#define SPIMPL_CONSTEXPR constexpr
#else
#define SPIMPL_CONSTEXPR
#endif
// define SPIMPL_HAS_AUTO_PTR to enable constructor and assignment operator that accept std::auto_ptr
// TODO: auto detect std::auto_ptr support
#ifndef PRIVATE_NS
#define PRIVATE_NS spimpl
#endif
#ifdef PRIVATE_NS
namespace PRIVATE_NS {
#endif
namespace details
{
template<class T>
T *default_copy(T *src)
{
static_assert(sizeof(T) > 0, "default_copy cannot copy incomplete type");
static_assert(!std::is_void<T>::value, "default_copy cannot copy incomplete type");
return new T(*src);
}
template<class T>
void default_delete(T *p) SPIMPL_NOEXCEPT
{
static_assert(sizeof(T) > 0, "default_delete cannot delete incomplete type");
static_assert(!std::is_void<T>::value, "default_delete cannot delete incomplete type");
delete p;
}
template<class T>
struct default_deleter
{
using type = void(*)(T*);
};
template<class T>
using default_deleter_t = typename default_deleter<T>::type;
template<class T>
struct default_copier
{
using type = T* (*)(T*);
};
template<class T>
using default_copier_t = typename default_copier<T>::type;
template<class T, class D, class C>
struct is_default_manageable : public std::integral_constant<bool,
std::is_same<D, default_deleter_t<T>>::value &&
std::is_same<C, default_copier_t<T>>::value>
{ };
}
template<class T, class Deleter = details::default_deleter_t<T>, class Copier = details::default_copier_t<T>>
class impl_ptr
{
private:
static_assert(!std::is_array<T>::value, "impl_ptr specialization for arrays is not implemented");
struct dummy_t_ { int dummy__; };
public:
using pointer = T*;
using element_type = T;
using copier_type = typename std::decay<Copier>::type;
using deleter_type = typename std::decay<Deleter>::type;
using unique_ptr_type = std::unique_ptr<T, deleter_type>;
using is_default_manageable = details::is_default_manageable<T, deleter_type, copier_type>;
SPIMPL_CONSTEXPR impl_ptr() SPIMPL_NOEXCEPT
: ptr_(nullptr, deleter_type{}), copier_(copier_type{}) {}
SPIMPL_CONSTEXPR impl_ptr(std::nullptr_t) SPIMPL_NOEXCEPT
: impl_ptr() {}
template<class D, class C>
impl_ptr(pointer p, D&& d, C&& c,
typename std::enable_if<
std::is_convertible<D, deleter_type>::value
&& std::is_convertible<C, copier_type>::value,
dummy_t_>::type = dummy_t_()) SPIMPL_NOEXCEPT
: ptr_(std::move(p), std::forward<D>(d)), copier_(std::forward<C>(c)) {}
template<class U>
impl_ptr(U *u,
typename std::enable_if<
std::is_convertible<U*, pointer>::value
&& is_default_manageable::value,
dummy_t_>::type = dummy_t_()) SPIMPL_NOEXCEPT
: impl_ptr(u, &details::default_delete<T>, &details::default_copy<T>) {}
impl_ptr(const impl_ptr& r)
: impl_ptr(r.clone()) {}
#ifndef SPIMPL_NO_CPP11_DEFAULT_MOVE_SPEC_FUNC
impl_ptr(impl_ptr&& r) SPIMPL_NOEXCEPT = default;
#else
impl_ptr(impl_ptr&& r) SPIMPL_NOEXCEPT
: ptr_(std::move(r.ptr_)), copier_(std::move(r.copier_)) {}
#endif
#ifdef SPIMPL_HAS_AUTO_PTR
template<class U>
impl_ptr(std::auto_ptr<U>&& u,
typename std::enable_if<
std::is_convertible<U*, pointer>::value
&& is_default_manageable::value,
dummy_t_
>::type = dummy_t_()) SPIMPL_NOEXCEPT
: ptr_(u.release(), &details::default_delete<T>), copier_(&details::default_copy<T>) {}
#endif
template<class U>
impl_ptr(std::unique_ptr<U>&& u,
typename std::enable_if<
std::is_convertible<U*, pointer>::value
&& is_default_manageable::value,
dummy_t_
>::type = dummy_t_()) SPIMPL_NOEXCEPT
: ptr_(u.release(), &details::default_delete<T>), copier_(&details::default_copy<T>) {}
template<class U, class D, class C>
impl_ptr(std::unique_ptr<U, D>&& u, C&& c,
typename std::enable_if<
std::is_convertible<U*, pointer>::value
&& std::is_convertible<D, deleter_type>::value
&& std::is_convertible<C, copier_type>::value,
dummy_t_
>::type = dummy_t_()) SPIMPL_NOEXCEPT
: ptr_(std::move(u)), copier_(std::forward<C>(c)) {}
template<class U, class D, class C>
impl_ptr(impl_ptr<U, D, C>&& u,
typename std::enable_if<
std::is_convertible<U*, pointer>::value
&& std::is_convertible<D, deleter_type>::value
&& std::is_convertible<C, copier_type>::value,
dummy_t_
>::type = dummy_t_()) SPIMPL_NOEXCEPT
: ptr_(std::move(u.ptr_)), copier_(std::move(u.copier_)) {}
impl_ptr& operator= (const impl_ptr& r)
{
if (this == &r)
return *this;
return operator=(r.clone());
}
#ifndef SPIMPL_NO_CPP11_DEFAULT_MOVE_SPEC_FUNC
impl_ptr& operator= (impl_ptr&& r) SPIMPL_NOEXCEPT = default;
#else
impl_ptr& operator= (impl_ptr&& r) SPIMPL_NOEXCEPT {
ptr_ = std::move(r.ptr_);
copier_ = std::move(r.copier_);
return *this;
}
#endif
template<class U, class D, class C>
typename std::enable_if<
std::is_convertible<U*, pointer>::value
&& std::is_convertible<D, deleter_type>::value
&& std::is_convertible<C, copier_type>::value,
impl_ptr&
>::type operator= (impl_ptr<U, D, C>&& u) SPIMPL_NOEXCEPT
{
ptr_ = std::move(u.ptr_);
copier_ = std::move(u.copier_);
return *this;
}
template<class U, class D, class C>
typename std::enable_if<
std::is_convertible<U*, pointer>::value
&& std::is_convertible<D, deleter_type>::value
&& std::is_convertible<C, copier_type>::value,
impl_ptr&
>::type operator= (const impl_ptr<U, D, C>& u)
{
return operator=(u.clone());
}
//
#ifdef SPIMPL_HAS_AUTO_PTR
template<class U>
typename std::enable_if<
std::is_convertible<U*, pointer>::value
&& is_default_manageable::value,
impl_ptr&
>::type operator= (std::auto_ptr<U>&& u) SPIMPL_NOEXCEPT
{
return operator=(impl_ptr(std::move(u)));
}
#endif
template<class U>
typename std::enable_if<
std::is_convertible<U*, pointer>::value
&& is_default_manageable::value,
impl_ptr&
>::type operator= (std::unique_ptr<U>&& u) SPIMPL_NOEXCEPT
{
return operator=(impl_ptr(std::move(u)));
}
impl_ptr clone() const
{
return impl_ptr(
ptr_ ? copier_(ptr_.get()) : nullptr,
ptr_.get_deleter(),
copier_);
}
typename std::remove_reference<T>::type & operator*() const { return *ptr_; }
pointer operator->() const SPIMPL_NOEXCEPT { return get(); }
pointer get() const SPIMPL_NOEXCEPT { return ptr_.get(); }
void swap(impl_ptr& u) SPIMPL_NOEXCEPT
{
using std::swap;
ptr_.swap(u.ptr_);
swap(copier_, u.copier_);
}
pointer release() SPIMPL_NOEXCEPT { return ptr_.release(); }
unique_ptr_type release_unique() SPIMPL_NOEXCEPT { return std::move(ptr_); }
explicit operator bool() const SPIMPL_NOEXCEPT { return static_cast<bool>(ptr_); }
typename std::remove_reference<deleter_type>::type& get_deleter() SPIMPL_NOEXCEPT { return ptr_.get_deleter(); }
const typename std::remove_reference<deleter_type>::type& get_deleter() const SPIMPL_NOEXCEPT { return ptr_.get_deleter(); }
typename std::remove_reference<copier_type>::type& get_copier() SPIMPL_NOEXCEPT { return copier_; }
const typename std::remove_reference<copier_type>::type& get_copier() const SPIMPL_NOEXCEPT { return copier_; }
private:
unique_ptr_type ptr_;
copier_type copier_;
};
template<class T, class D, class C>
inline void swap(impl_ptr<T, D, C>& l, impl_ptr<T, D, C>& r) SPIMPL_NOEXCEPT
{
l.swap(r);
}
template <class T1, class D1, class C1, class T2, class D2, class C2>
inline bool operator==(const impl_ptr<T1, D1, C1>& l, const impl_ptr<T2, D2, C2>& r)
{
return l.get() == r.get();
}
template <class T1, class D1, class C1, class T2, class D2, class C2>
inline bool operator!=(const impl_ptr<T1, D1, C1>& l, const impl_ptr<T2, D2, C2>& r)
{
return !(l == r);
}
template <class T1, class D1, class C1, class T2, class D2, class C2>
inline bool operator< (const impl_ptr<T1, D1, C1>& l, const impl_ptr<T2, D2, C2>& r)
{
using P1 = typename impl_ptr<T1, D1, C1>::pointer;
using P2 = typename impl_ptr<T2, D2, C2>::pointer;
using CT = typename std::common_type<P1, P2>::type;
return std::less<CT>()(l.get(), r.get());
}
template <class T1, class D1, class C1, class T2, class D2, class C2>
inline bool operator> (const impl_ptr<T1, D1, C1>& l, const impl_ptr<T2, D2, C2>& r)
{
return r < l;
}
template <class T1, class D1, class C1, class T2, class D2, class C2>
inline bool operator<=(const impl_ptr<T1, D1, C1>& l, const impl_ptr<T2, D2, C2>& r)
{
return !(r < l);
}
template <class T1, class D1, class C1, class T2, class D2, class C2>
inline bool operator>=(const impl_ptr<T1, D1, C1>& l, const impl_ptr<T2, D2, C2>& r)
{
return !(l < r);
}
template <class T, class D, class C>
inline bool operator==(const impl_ptr<T, D, C>& p, std::nullptr_t) SPIMPL_NOEXCEPT
{
return !p;
}
template <class T, class D, class C>
inline bool operator==(std::nullptr_t, const impl_ptr<T, D, C>& p) SPIMPL_NOEXCEPT
{
return !p;
}
template <class T, class D, class C>
inline bool operator!=(const impl_ptr<T, D, C>& p, std::nullptr_t) SPIMPL_NOEXCEPT
{
return static_cast<bool>(p);
}
template <class T, class D, class C>
inline bool operator!=(std::nullptr_t, const impl_ptr<T, D, C>& p) SPIMPL_NOEXCEPT
{
return static_cast<bool>(p);
}
template <class T, class D, class C>
inline bool operator< (const impl_ptr<T, D, C>& l, std::nullptr_t)
{
using P = typename impl_ptr<T, D, C>::pointer;
return std::less<P>()(l.get(), nullptr);
}
template <class T, class D, class C>
inline bool operator< (std::nullptr_t, const impl_ptr<T, D, C>& p)
{
using P = typename impl_ptr<T, D, C>::pointer;
return std::less<P>()(nullptr, p.get());
}
template <class T, class D, class C>
inline bool operator> (const impl_ptr<T, D, C>& p, std::nullptr_t)
{
return nullptr < p;
}
template <class T, class D, class C>
inline bool operator> (std::nullptr_t, const impl_ptr<T, D, C>& p)
{
return p < nullptr;
}
template <class T, class D, class C>
inline bool operator<=(const impl_ptr<T, D, C>& p, std::nullptr_t)
{
return !(nullptr < p);
}
template <class T, class D, class C>
inline bool operator<=(std::nullptr_t, const impl_ptr<T, D, C>& p)
{
return !(p < nullptr);
}
template <class T, class D, class C>
inline bool operator>=(const impl_ptr<T, D, C>& p, std::nullptr_t)
{
return !(p < nullptr);
}
template <class T, class D, class C>
inline bool operator>=(std::nullptr_t, const impl_ptr<T, D, C>& p)
{
return !(nullptr < p);
}
template<class T, class... Args>
inline impl_ptr<T> make_impl(Args&&... args)
{
return impl_ptr<T>(new T(std::forward<Args>(args)...), &details::default_delete<T>, &details::default_copy<T>);
}
// Helpers to manage unique impl, stored in std::unique_ptr
template<class T, class Deleter = void(*)(T*)>
using unique_impl_ptr = std::unique_ptr<T, Deleter>;
template<class T, class... Args>
inline unique_impl_ptr<T> make_impl_nocopy(Args&&... args)
{
static_assert(!std::is_array<T>::value, "unique_impl_ptr does not support arrays");
return unique_impl_ptr<T>(new T(std::forward<Args>(args)...), &details::default_delete<T>);
}
#ifdef PRIVATE_NS
}
#endif
namespace std {
template <class T, class D, class C>
struct hash<
#ifdef PRIVATE_NS
PRIVATE_NS::
#endif
impl_ptr<T, D, C>>
{
using argument_type =
#ifdef PRIVATE_NS
PRIVATE_NS::
#endif
impl_ptr<T, D, C>;
using result_type = size_t;
result_type operator()(const argument_type& p) const SPIMPL_NOEXCEPT
{
return hash<typename argument_type::pointer>()(p.get());
}
};
}
//********************************************************************
//********************************************************************
//********************************************************************
//********************************************************************
//Copyright (c) 2015 Aiden Koss
//Copyright (c) 2015 Open Systems International, Inc
//Distributed under the Boost Software License, Version 1.0.
//See http://www.boost.org/LICENSE_1_0.txt
#ifdef PRIVATE_NS
using namespace PRIVATE_NS;
#endif
#define ADDPRIVATE_STRUCT_COPYABLE \
private: \
struct Private; \
friend Private; \
impl_ptr<Private> impl; \
public: \
static bool isCopyable() {return true;}
#define ADDPRIVATE_CLASS_COPYABLE \
private: \
struct Private; \
friend Private; \
impl_ptr<Private> impl; \
public: \
static bool IsCopyable() {return true;} \
private:
#define ADDPRIVATE_STRUCT_NOCOPY \
private: \
struct Private; \
friend Private; \
unique_impl_ptr<Private> impl; \
public: \
static bool IsCopyable() {return false;}
#define ADDPRIVATE_CLASS_NOCOPY \
private: \
struct Private; \
friend Private; \
unique_impl_ptr<Private> impl; \
public: \
static bool IsCopyable() {return false;} \
private: