-
Notifications
You must be signed in to change notification settings - Fork 0
/
cppmeta.hpp
3909 lines (3252 loc) · 119 KB
/
cppmeta.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
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef CPPMETA_H
#define CPPMETA_H
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <stdexcept>
#include <vector>
#include <string>
#include <cassert>
#include <algorithm>
#ifdef _MSC_VER
#pragma warning( push )
#pragma warning (disable: 4913)
#endif
#ifndef NULL
#define CPPMETA_NULL 0
#else
#define CPPMETA_NULL NULL
#endif
namespace cppmeta
{
namespace detail
{
struct void_type {};
template<int>
class disabled {};
}
struct reflection
{
enum {
compile_time,
run_time
};
};
template<class T = detail::void_type, int Reflection = reflection::run_time>
struct reflect;
template<class T>
struct reflect<T, reflection::compile_time>
{
template<class meta_info>
void operator()() const {}
};
template<class T = detail::void_type, int Reflection = reflection::run_time>
struct resolve;
namespace type_traits
{
template<class T>
struct is_const
{
static const bool value = false;
};
template<class T>
struct is_const <const T>
{
static const bool value = true;
};
template<class T>
struct add_const
{
typedef const T type;
};
template <bool, class IfTrueT, class IfFalseT>
struct conditional
{
typedef IfTrueT type;
};
template <class IfTrueT, class IfFalseT>
struct conditional<false, IfTrueT, IfFalseT>
{
typedef IfFalseT type;
};
template< class T >
struct remove_reference
{
typedef T type;
};
template<class T>
struct remove_const
{ // remove top level const qualifier
typedef T type;
};
template<class T>
struct remove_const<const T>
{ // remove top level const qualifier
typedef T type;
};
template<class T>
struct remove_const<const volatile T>
{ // remove top level const qualifier
typedef volatile T type;
};
// remove_volatile
template<class T>
struct remove_volatile
{ // remove top level volatile qualifier
typedef T type;
};
template<class T>
struct remove_volatile<volatile T>
{ // remove top level volatile qualifier
typedef T type;
};
// remove_cv
template<class T>
struct remove_cv
{ // remove top level const and volatile qualifiers
typedef typename remove_const<typename remove_volatile<T>::type>::type
type;
};
template< class T >
struct remove_reference<T&>
{
typedef T type;
};
template<class T, class>
struct remove_pointer_helper
{
typedef T type;
};
template<class T, class U>
struct remove_pointer_helper<T, U*>
{
typedef U type;
};
// remove_pointer
template<class T>
struct remove_pointer
: public remove_pointer_helper<T, typename remove_cv<T>::type>
{ };
namespace detail
{
typedef char yes_type;
struct no_type
{
char d[8];
};
}
template<class T>
T& declref();
struct any { template<class T> any(const T&) {} };
template<class>
struct templated_any { template<class T> templated_any(const T&) {} };
struct no_to_from_conversion {};
struct has_to_from_conversion {};
no_to_from_conversion operator,(no_to_from_conversion, has_to_from_conversion);
template<class T>
detail::yes_type has_conversion_tester(T);
detail::no_type has_conversion_tester(no_to_from_conversion);
template<class U>
detail::yes_type is_simple_type_tester(void (U::*)());
template<class U>
detail::no_type is_simple_type_tester(...);
template<class T>
struct is_simple_type
{
static const bool value =
sizeof(is_simple_type_tester<T>(0)) == sizeof(detail::no_type);
};
template<>
struct is_simple_type<void>
{
static const bool value = true;
};
template<class, class>
struct is_same
{
static const bool value = false;
};
template<class T>
struct is_same<T, T>
{
static const bool value = true;
};
template<class>
struct is_reference
{
static const bool value = false;
};
template<class T>
struct is_reference<T&>
{
static const bool value = true;
};
template<class>
struct is_pointer
{
static const bool value = false;
};
template<class T>
struct is_pointer<T*>
{
static const bool value = true;
};
template<unsigned N> struct priority_tag : priority_tag < N - 1 > {};
template<> struct priority_tag<0> {};
template<class T>
detail::yes_type is_convertable_tester(T, priority_tag<1>);
template<class T>
detail::no_type is_convertable_tester(any, priority_tag<0>);
template<class FromT, class ToT>
struct is_convertable
{
static const bool value =
sizeof(is_convertable_tester<ToT>(declref<FromT>(), priority_tag<1>())) == sizeof(detail::yes_type);
};
template<class T>
struct is_function
{
static const bool value =
is_convertable<typename remove_pointer<T>::type*, const void*>::value == bool(false);
};
template<class>
struct is_void
{
static const bool value = false;
};
template<>
struct is_void<void>
{
static const bool value = true;
};
template <bool, class T>
struct enable_if
{
private:
struct enable_if_dummy;
public:
typedef enable_if_dummy(&type)[1];
};
template <class T>
struct enable_if<true, T>
{
typedef T type;
};
// add_pointer
template<class T>
struct add_pointer
{
typedef
typename
conditional<
is_reference<T>::value,
typename remove_reference<T>::type,
T
>::type * type;
};
template<class T, template<class> class RemoverT>
struct first_level
{
typedef
typename
conditional<
is_reference<T>::value,
typename
RemoverT<
typename remove_reference<T>::type
>::type&,
typename
conditional<
is_pointer<T>::value,
typename
add_pointer<
typename RemoverT<typename remove_pointer<T>::type>::type
>::type,
typename RemoverT<T>::type
>::type
>::type type;
};
}
class any
{
public:
/// Constructs an object of type any with an empty state.
any()
: vtable(CPPMETA_NULL)
, size_()
{ }
/// Constructs an object of type any with an equivalent state as other.
any(const any& other)
: vtable(other.vtable)
, size_(other.size_)
{
if (!other.empty())
{
other.vtable->copy(other.storage, this->storage);
}
}
/// Same effect as this->clear().
~any()
{
this->clear();
}
/// Constructs an object of type any that contains an object of type T direct-initialized with std::forward<ValueType>(value).
template<class ValueType>
any(const ValueType& value)
: size_(sizeof(value))
{
this->construct(value);
}
/// Has the same effect as any(rhs).swap(*this). No effects if an exception is thrown.
any& operator=(const any& rhs)
{
any(rhs).swap(*this);
return *this;
}
/// Has the same effect as any(std::forward<ValueType>(value)).swap(*this). No effect if a exception is thrown.
template<class ValueType>
any& operator=(const ValueType& value)
{
any tmp(value);
tmp.swap(*this);
return *this;
}
/// If not empty, destroys the contained object.
void clear() throw()
{
if (!empty())
{
this->vtable->destroy(storage);
this->vtable = CPPMETA_NULL;
}
}
/// Returns true if *this has no contained object, otherwise false.
bool empty() const throw()
{
return this->vtable == CPPMETA_NULL;
}
/// Exchange the states of *this and rhs.
void swap(any& rhs) throw()
{
std::swap(size_, rhs.size_);
if (this->vtable != rhs.vtable)
{
any tmp(rhs);
// move from *this to rhs.
rhs.vtable = this->vtable;
if (this->vtable != CPPMETA_NULL)
{
this->vtable->move(this->storage, rhs.storage);
//this->vtable = nullptr; -- unneeded, see below
}
// move from tmp (previously rhs) to *this.
this->vtable = tmp.vtable;
if (tmp.vtable != CPPMETA_NULL)
{
tmp.vtable->move(tmp.storage, this->storage);
tmp.vtable = CPPMETA_NULL;
}
}
else // same types
{
if (this->vtable != CPPMETA_NULL)
this->vtable->swap(this->storage, rhs.storage);
}
}
/// non-standard
std::size_t size() const
{
return this->size_;
}
const char* data() const
{
return cast<const char>();
}
private: // Storage and Virtual Method Table
union storage_union
{
struct stack_storage_t
{
unsigned char data[sizeof(void*)];
};
stack_storage_t stack;
void* dynamic;
};
/// Base VTable specification.
struct vtable_type
{
// Note: The caller is responssible for doing .vtable = nullptr after destructful operations
// such as destroy() and/or move().
/// Destroys the object in the union.
/// The state of the union after this call is unspecified, caller must ensure not to use src anymore.
void(*destroy)(storage_union&);
/// Copies the **inner** content of the src union into the yet unitialized dest union.
/// As such, both inner objects will have the same state, but on separate memory locations.
void(*copy)(const storage_union& src, storage_union& dest);
/// Moves the storage from src to the yet unitialized dest union.
/// The state of src after this call is unspecified, caller must ensure not to use src anymore.
void(*move)(storage_union& src, storage_union& dest);
/// Exchanges the storage between lhs and rhs.
void(*swap)(storage_union& lhs, storage_union& rhs);
};
/// VTable for dynamically allocated storage.
template<class T>
struct vtable_dynamic
{
static void destroy(storage_union& storage)
{
//assert(reinterpret_cast<T*>(storage.dynamic));
delete reinterpret_cast<T*>(storage.dynamic);
}
static void copy(const storage_union& src, storage_union& dest)
{
const T& src_storage = *reinterpret_cast<const T*>(src.dynamic);
T& dst_storage = *(new T(src_storage));
dest.dynamic = &dst_storage;
}
static void move(storage_union& src, storage_union& dest)
{
dest.dynamic = src.dynamic;
src.dynamic = 0;
}
static void swap(storage_union& lhs, storage_union& rhs)
{
// just exchage the storage pointers.
std::swap(lhs.dynamic, rhs.dynamic);
}
};
/// VTable for stack allocated storage.
template<class T>
struct vtable_stack
{
static void destroy(storage_union& storage) throw()
{
reinterpret_cast<T*>(&storage.stack)->~T();
}
static void copy(const storage_union& src, storage_union& dest)
{
const T& src_storage = reinterpret_cast<const T&>(src.stack);
T& dst_storage = *(new (&dest.stack) T(src_storage));
}
static void move(storage_union& src, storage_union& dest) throw()
{
copy(src, dest);
destroy(src);
}
static void swap(storage_union& lhs, storage_union& rhs) throw()
{
storage_union tmp_storage;
move(rhs, tmp_storage);
move(lhs, rhs);
move(tmp_storage, lhs);
}
};
/// Whether the type T must be dynamically allocated or can be stored on the stack.
template<class T>
struct requires_allocation
{
static const bool value =
(sizeof(T) > sizeof(storage_union().stack))
|| type_traits::is_simple_type<T>::value == bool(false);
};
/// Returns the pointer to the vtable of the type T.
template<class T>
static vtable_type* vtable_for_type()
{
typedef typename type_traits::conditional<
requires_allocation<T>::value, vtable_dynamic<T>, vtable_stack<T>
>::type VTableType;
static vtable_type table = {
VTableType::destroy,
VTableType::copy, VTableType::move,
VTableType::swap,
};
return &table;
}
protected:
/// Casts (with no type_meta checks) the storage pointer as const T*.
template<class T>
const T* cast() const
{
const T* result =
requires_allocation<T>::value
? reinterpret_cast<const T*>(storage.dynamic)
: reinterpret_cast<const T*>(&storage.stack);
return result;
}
/// Casts (with no type_meta checks) the storage pointer as T*.
template<class T>
T* cast()
{
T* result =
requires_allocation<T>::value
? reinterpret_cast<T*>(storage.dynamic)
: reinterpret_cast<T*>(&storage.stack);
return result;
}
private:
storage_union storage; // on offset(0) so no padding for align
vtable_type* vtable;
std::size_t size_;
template<class ValueType, class T>
void do_construct(
typename
type_traits::conditional<
requires_allocation<T>::value,
const ValueType,
detail::disabled<__LINE__>
>::type &value)
{
storage.dynamic = new T(value);
}
template<class ValueType, class T>
void do_construct(
typename
type_traits::conditional<
requires_allocation<T>::value,
detail::disabled<__LINE__>,
const ValueType
>::type &value)
{
new (&storage.stack) T(value);
}
/// Chooses between stack and dynamic allocation for the type decay_t<ValueType>,
/// assigns the correct vtable, and constructs the object on our storage.
template<class ValueType>
void construct(const ValueType& value)
{
typedef
//typename
//type_traits::remove_cv<
typename type_traits::remove_reference<ValueType>::type
//>::type
T;
this->vtable = vtable_for_type<T>();
do_construct<ValueType, T>(value);
}
template<class T>
struct remove_const_from_ptr_or_value
{
typedef
typename
type_traits::conditional<
type_traits::is_pointer<T>::value == bool(true) &&
type_traits::is_function<T>::value == bool(false),
typename type_traits::remove_const<typename type_traits::remove_pointer<T>::type>::type*,
typename type_traits::remove_const<T>::type
>::type type;
};
template<class T>
struct remove_volatile_from_ptr_or_value
{
typedef
typename
type_traits::conditional<
type_traits::is_pointer<T>::value == bool(true) &&
type_traits::is_function<T>::value == bool(false),
typename type_traits::remove_volatile<typename type_traits::remove_pointer<T>::type>::type*,
typename type_traits::remove_volatile<T>::type
>::type type;
};
template<class T>
struct remove_cv_from_ptr_or_value
{
typedef
typename
type_traits::conditional<
type_traits::is_pointer<T>::value == bool(true) &&
type_traits::is_function<T>::value == bool(false),
typename type_traits::remove_cv<typename type_traits::remove_pointer<T>::type>::type*,
typename type_traits::remove_cv<T>::type
>::type type;
};
public:
template<class ValueType>
static inline ValueType* any_cast(
any* operand)
{
using type_traits::remove_reference;
using type_traits::remove_pointer;
using type_traits::remove_const;
using type_traits::remove_volatile;
using type_traits::remove_cv;
typedef
typename remove_reference<ValueType>::type type_no_ref;
typedef
typename remove_pointer<ValueType>::type type_no_ptr;
typedef
typename remove_const_from_ptr_or_value<type_no_ref>::type type_no_const;
typedef
typename remove_volatile_from_ptr_or_value<type_no_ref>::type type_no_volatile;
typedef
typename remove_cv_from_ptr_or_value<type_no_ref>::type type_no_cv;
//void(*tmp1)(type_no_ref);
//void(*tmp2)(type_no_const);
//void(*tmp3)(type_no_volatile);
//void(*tmp4)(type_no_cv);
if (operand->vtable == vtable_for_type<type_no_ref>())
return operand->cast<ValueType>();
if (operand->vtable == vtable_for_type<type_no_const>())
return operand->cast<ValueType>();
if (operand->vtable == vtable_for_type<type_no_volatile>())
return operand->cast<ValueType>();
if (operand->vtable == vtable_for_type<type_no_cv>())
return operand->cast<ValueType>();
if (operand->vtable == vtable_for_type<type_no_ref*>())
return *operand->cast<ValueType*>();
if (operand->vtable == vtable_for_type<type_no_const*>())
return *operand->cast<ValueType*>();
if (operand->vtable == vtable_for_type<type_no_volatile*>())
return *operand->cast<ValueType*>();
if (operand->vtable == vtable_for_type<type_no_cv*>())
return *operand->cast<ValueType*>();
return CPPMETA_NULL;
}
template<class ValueType>
static inline const ValueType* any_cast_const(
const any* operand)
{
using type_traits::remove_reference;
using type_traits::remove_pointer;
using type_traits::remove_const;
using type_traits::remove_volatile;
using type_traits::remove_cv;
typedef
typename remove_reference<const ValueType>::type type_no_ref;
typedef
typename remove_const_from_ptr_or_value<type_no_ref>::type type_no_const;
typedef
typename remove_volatile_from_ptr_or_value<type_no_ref>::type type_no_volatile;
typedef
typename remove_cv_from_ptr_or_value<type_no_ref>::type type_no_cv;
if (operand->vtable == vtable_for_type<type_no_ref>())
return operand->cast<ValueType>();
if (operand->vtable == vtable_for_type<type_no_const>())
return operand->cast<ValueType>();
if (operand->vtable == vtable_for_type<type_no_volatile>())
return operand->cast<ValueType>();
if (operand->vtable == vtable_for_type<type_no_cv>())
return operand->cast<ValueType>();
if (operand->vtable == vtable_for_type<type_no_ref*>())
return *operand->cast<ValueType*>();
if (operand->vtable == vtable_for_type<type_no_const*>())
return *operand->cast<ValueType*>();
if (operand->vtable == vtable_for_type<type_no_volatile*>())
return *operand->cast<ValueType*>();
if (operand->vtable == vtable_for_type<type_no_cv*>())
return *operand->cast<ValueType*>();
return CPPMETA_NULL;
}
};
class bad_any_cast :
public std::bad_cast
{
std::string what_;
public:
bad_any_cast(const std::string &msg)
: what_("cppmeta::bad_any_cast: " + msg) {}
const char * what() const
{
return what_.c_str();
}
};
template<class ValueType>
inline ValueType any_cast(
any& operand)
{
typedef typename type_traits::remove_reference<ValueType>::type cast_type;
cast_type* result = any::any_cast<cast_type>(&operand);
if (!result) throw(std::bad_cast());
return *result;
}
template<class ValueType>
inline ValueType any_cast(
const any &operand)
{
typedef typename type_traits::add_const<typename type_traits::remove_reference<ValueType>::type>::type cast_type;
cast_type* result = any::any_cast_const<cast_type>(&operand);
if (!result) throw(std::bad_cast());
return *result;
}
template<class ValueType>
inline ValueType* any_cast(
any* operand)
{
if (!operand)
return CPPMETA_NULL;
typedef ValueType cast_type;
cast_type* result = any::any_cast<cast_type>(operand);
return result;
}
template<class ValueType>
inline const ValueType* any_cast(
const any* operand)
{
if (!operand)
return CPPMETA_NULL;
typedef ValueType cast_type;
const cast_type* result = any::any_cast_const<cast_type>(operand);
return result;
}
template <class T>
class smart_ptr;
template <class T>
struct smart_ptrref_ { // proxy reference for smart_ptr copying
explicit smart_ptrref_(T* right_) : ref_(right_) {} // construct from generic pointer to smart_ptr ptr
T* ref_; // generic pointer to smart_ptr ptr
};
template <class T>
class smart_ptr { // wrap an object pointer to ensure destruction
public:
typedef T element_type;
explicit smart_ptr(T* ptr_ = CPPMETA_NULL) throw() : _ptr(ptr_) {} // construct from object pointer
smart_ptr(smart_ptr& right_) throw() : _ptr(right_.release()) {
// construct by assuming pointer from right_ smart_ptr
}
smart_ptr(smart_ptrref_<T> right_) throw() { // construct by assuming pointer from right_ smart_ptrref_
T* ptr = right_.ref_;
right_.ref_ = CPPMETA_NULL; // release old
_ptr = ptr; // reset this
}
template <class OtherT>
operator smart_ptr<OtherT>() throw() { // convert to compatible smart_ptr
return smart_ptr<OtherT>(*this);
}
template <class OtherT>
operator smart_ptrref_<OtherT>() throw() { // convert to compatible smart_ptrref_
OtherT* cvtptr = _ptr; // test implicit conversion
smart_ptrref_<OtherT> ans(cvtptr);
_ptr = CPPMETA_NULL; // pass ownership to smart_ptrref_
return ans;
}
template <class OtherT>
smart_ptr& operator=(smart_ptr<OtherT>& right_) throw() { // assign compatible right_ (assume pointer)
reset(right_.release());
return *this;
}
template <class OtherT>
smart_ptr(smart_ptr<OtherT>& right_) throw() : _ptr(right_.release()) {
// construct by assuming pointer from right_
}
smart_ptr& operator=(smart_ptr& right_) throw() { // assign compatible right_ (assume pointer)
reset(right_.release());
return *this;
}
smart_ptr& operator=(smart_ptrref_<T> right_) throw() { // assign compatible right_.ref_ (assume pointer)
T* ptr = right_.ref_;
right_.ref_ = 0; // release old
reset(ptr); // set new
return *this;
}
~smart_ptr() throw() {
delete _ptr;
}
T& operator*() const throw() {
return *get();
}
T* operator->() const throw() {
return get();
}
T* get() const throw() { // return wrapped pointer
return _ptr;
}
T* release() throw() { // return wrapped pointer and give up ownership
T* tmp = _ptr;
_ptr = CPPMETA_NULL;
return tmp;
}
void reset(T* _Ptr = CPPMETA_NULL) { // destroy designated object and store new pointer
if (_Ptr != _ptr) {
delete _ptr;
}
_ptr = _Ptr;
}
private:
T* _ptr; // the wrapped object pointer
};
template <>
class smart_ptr<void> {
public:
typedef void element_type;
};
template<class ClassT, class T>
struct entity_types
{
typedef T value_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef value_type ClassT::*member_ptr_type;
typedef const_reference(ClassT::*ref_getter_member_func_ptr_type)() const;
typedef void (ClassT::*ref_setter_member_func_ptr_type)(const_reference);
typedef value_type(ClassT::*val_getter_member_func_ptr_type)() const;
typedef void (ClassT::*val_setter_member_func_ptr_type)(value_type);
typedef reference(ClassT::*nonconst_ref_getter_member_func_ptr_type)();
typedef const_reference(*ref_getter_func_ptr_type)(const ClassT&);
typedef void (*ref_setter_func_ptr_type)(ClassT&, const_reference);
typedef value_type(*val_getter_func_ptr_type)(const ClassT&);
typedef void (*val_setter_func_ptr_type)(ClassT&, value_type);
typedef reference(*nonconst_ref_getter_func_ptr_type)(ClassT&);
};
template<class T>
struct entity_types<T, T>
{
typedef T value_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef value_type* member_ptr_type;
typedef const_reference(*ref_getter_func_ptr_type)();
typedef void (*ref_setter_func_ptr_type)(const_reference);
typedef value_type(*val_getter_func_ptr_type)();
typedef void (*val_setter_func_ptr_type)(value_type);
typedef reference(*nonconst_ref_getter_func_ptr_type)();
};
template<class ClassT, int Size, class T>
struct entity_types<ClassT, T[Size]>
{
typedef T(value_type)[Size];
typedef value_type& reference;
typedef const value_type& const_reference;
typedef value_type ClassT::*member_ptr_type;
typedef const_reference(ClassT::*ref_getter_member_func_ptr_type)() const;
typedef void (ClassT::*ref_setter_member_func_ptr_type)(const_reference);
typedef ref_getter_member_func_ptr_type val_getter_member_func_ptr_type;
typedef ref_setter_member_func_ptr_type val_setter_member_func_ptr_type;
typedef reference(ClassT::*nonconst_ref_getter_member_func_ptr_type)();
typedef const_reference(*ref_getter_func_ptr_type)(const ClassT&) ;
typedef void (*ref_setter_func_ptr_type)(ClassT&, const_reference);
typedef ref_getter_func_ptr_type val_getter_func_ptr_type;
typedef ref_setter_func_ptr_type val_setter_func_ptr_type;
typedef reference(*nonconst_ref_getter_func_ptr_type)(ClassT&);
};
namespace detail
{
template <
class ResultT,
class ClassT,
class Arg0T = void_type,