-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpolyAccessPack.h
173 lines (154 loc) · 4.11 KB
/
polyAccessPack.h
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
/*
@copyright Russell Standish 2000-2013
@author Russell Standish
This file is part of Classdesc
Open source licensed under the MIT license. See LICENSE for details.
*/
#ifndef CLASSDESC_POLY_ACCESS_PACK_H
#define CLASSDESC_POLY_ACCESS_PACK_H
#include "polyPackBase.h"
namespace classdesc
{
#ifdef CLASSDESC_POLYPACKBASE_H
template <class T>
typename enable_if<is_base_of<PolyPackBase, typename T::element_type> >::T
pack_smart_ptr(pack_t& x, const string& d, const T& a,
dummy<0> dum=0)
{
bool valid=a.get();
::pack(x,d,valid);
if (valid)
{
typename T::element_type::Type t=a->type();
::pack(x,d,t);
a->pack(x,d);
}
}
template <class T>
typename enable_if<Not<is_base_of<PolyPackBase, typename T::element_type> > >::T
#else
template <class T>
void //if Poly not defined, just define shared_ptr non-polymorphically
#endif
pack_smart_ptr(pack_t& x, const string& d, const T& a,
dummy<1> dum=0)
{
bool valid=a.get();
pack(x,d,valid);
if (valid)
pack(x,d,*a);
}
#ifdef CLASSDESC_POLYPACKBASE_H
template <class T>
typename enable_if<is_base_of<PolyPackBase, typename T::element_type> >::T
unpack_smart_ptr(unpack_t& x, const string& d, T& a, dummy<0> dum=0)
{
bool valid;
unpack(x,d,valid);
if (valid)
{
typename T::element_type::Type t;
unpack(x,d,t);
a.reset(T::element_type::create(t));
a->unpack(x,d);
}
else
a.reset();
}
template <class T>
typename enable_if<Not<is_base_of<PolyPackBase, typename T::element_type> > >::T
#else
template <class T>
void //if Poly not defined, just define smart_ptr non-polymorphically
#endif
unpack_smart_ptr(pack_t& x, const string& d, T& a, dummy<1> dum=0)
{
bool valid;
unpack(x,d,valid);
if (valid)
{
a.reset(new typename T::element_type);
unpack(x,d,*a);
}
else
a.reset();
}
}
namespace classdesc_access
{
namespace cd = classdesc;
template <class T>
struct access_pack<cd::shared_ptr<T> >
{
template <class U>
void operator()(cd::pack_t& x, const cd::string& d, U& a)
{pack_smart_ptr(x,d,a);}
};
template <class T>
struct access_unpack<cd::shared_ptr<T> >
{
template <class U>
void operator()(cd::unpack_t& x, const cd::string& d, U& a)
{unpack_smart_ptr(x,d,a);}
};
template <class T>
struct access_pack<cd::weak_ptr<T> >
{
template <class U>
void operator()(cd::pack_t& x, const cd::string& d, U& a)
{pack_smart_ptr(x,d,a.lock());}
};
template <class T>
struct access_unpack<cd::weak_ptr<T> >
{
template <class U>
void operator()(cd::unpack_t& x, const cd::string& d, U& a)
{
// cannot deserialise to a weak reference, but need to process stream
cd::shared_ptr<T> tmp;
unpack_smart_ptr(x,d,tmp);
if (cd::shared_ptr<T> target=a.lock())
*target=*tmp; // best effort, polymorphism not supported
}
};
#if defined(__cplusplus) && __cplusplus<=201402
#if defined(__GNUC__) && !defined(__ICC) && !defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
template <class T>
struct access_pack<std::auto_ptr<T> >
{
template <class U>
void operator()(cd::pack_t& x, const cd::string& d, U& a)
{pack_smart_ptr(x,d,a);}
};
template <class T>
struct access_unpack<std::auto_ptr<T> >
{
template <class U>
void operator()(cd::unpack_t& x, const cd::string& d, U& a)
{unpack_smart_ptr(x,d,a);}
};
#if defined(__GNUC__) && !defined(__ICC) && !defined(__clang__)
#pragma GCC diagnostic pop
#endif
#endif
#if defined(__cplusplus) && __cplusplus >= 201103L
template <class T, class D>
struct access_pack<std::unique_ptr<T,D>>
{
template <class U>
void operator()(cd::pack_t& x, const cd::string& d, U& a)
{pack_smart_ptr(x,d,a);}
};
template <class T, class D>
struct access_unpack<std::unique_ptr<T,D>>
{
template <class U>
void operator()(cd::unpack_t& x, const cd::string& d, U& a)
{unpack_smart_ptr(x,d,a);}
};
#endif
}
#endif