Classdesc 3.44
polyAccessPack.h
1/*
2 @copyright Russell Standish 2000-2013
3 @author Russell Standish
4 This file is part of Classdesc
5
6 Open source licensed under the MIT license. See LICENSE for details.
7*/
8
9#ifndef CLASSDESC_POLY_ACCESS_PACK_H
10#define CLASSDESC_POLY_ACCESS_PACK_H
11#include "polyPackBase.h"
12
13namespace classdesc
14{
15#ifdef CLASSDESC_POLYPACKBASE_H
16 template <class T>
18 pack_smart_ptr(pack_t& x, const string& d, const T& a,
19 dummy<0> dum=0)
20 {
21 bool valid=a.get();
22 ::pack(x,d,valid);
23 if (valid)
24 {
25 typename T::element_type::Type t=a->type();
26 ::pack(x,d,t);
27 a->pack(x,d);
28 }
29 }
30
31 template <class T>
33#else
34 template <class T>
35 void //if Poly not defined, just define shared_ptr non-polymorphically
36#endif
37 pack_smart_ptr(pack_t& x, const string& d, const T& a,
38 dummy<1> dum=0)
39 {
40 bool valid=a.get();
41 pack(x,d,valid);
42 if (valid)
43 pack(x,d,*a);
44 }
45
46#ifdef CLASSDESC_POLYPACKBASE_H
47 template <class T>
49 unpack_smart_ptr(unpack_t& x, const string& d, T& a, dummy<0> dum=0)
50 {
51 bool valid;
52 unpack(x,d,valid);
53 if (valid)
54 {
55 typename T::element_type::Type t;
56 unpack(x,d,t);
57 a.reset(T::element_type::create(t));
58 a->unpack(x,d);
59 }
60 else
61 a.reset();
62 }
63
64 template <class T>
66#else
67 template <class T>
68 void //if Poly not defined, just define smart_ptr non-polymorphically
69#endif
70 unpack_smart_ptr(pack_t& x, const string& d, T& a, dummy<1> dum=0)
71 {
72 bool valid;
73 unpack(x,d,valid);
74 if (valid)
75 {
76 a.reset(new typename T::element_type);
77 unpack(x,d,*a);
78 }
79 else
80 a.reset();
81 }
82
83}
84
85namespace classdesc_access
86{
87 namespace cd = classdesc;
88
89 template <class T>
90 struct access_pack<cd::shared_ptr<T> >
91 {
92 template <class U>
93 void operator()(cd::pack_t& x, const cd::string& d, U& a)
94 {pack_smart_ptr(x,d,a);}
95 };
96
97 template <class T>
98 struct access_unpack<cd::shared_ptr<T> >
99 {
100 template <class U>
101 void operator()(cd::unpack_t& x, const cd::string& d, U& a)
102 {unpack_smart_ptr(x,d,a);}
103 };
104
105 template <class T>
106 struct access_pack<cd::weak_ptr<T> >
107 {
108 template <class U>
109 void operator()(cd::pack_t& x, const cd::string& d, U& a)
110 {pack_smart_ptr(x,d,a.lock());}
111 };
112
113 template <class T>
114 struct access_unpack<cd::weak_ptr<T> >
115 {
116 template <class U>
117 void operator()(cd::unpack_t& x, const cd::string& d, U& a)
118 {
119 // cannot deserialise to a weak reference, but need to process stream
120 cd::shared_ptr<T> tmp;
121 unpack_smart_ptr(x,d,tmp);
122 if (cd::shared_ptr<T> target=a.lock())
123 *target=*tmp; // best effort, polymorphism not supported
124 }
125 };
126
127
128#if defined(__cplusplus) && __cplusplus<=201402
129#if defined(__GNUC__) && !defined(__ICC) && !defined(__clang__)
130#pragma GCC diagnostic push
131#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
132#endif
133 template <class T>
134 struct access_pack<std::auto_ptr<T> >
135 {
136 template <class U>
137 void operator()(cd::pack_t& x, const cd::string& d, U& a)
138 {pack_smart_ptr(x,d,a);}
139 };
140
141 template <class T>
142 struct access_unpack<std::auto_ptr<T> >
143 {
144 template <class U>
145 void operator()(cd::unpack_t& x, const cd::string& d, U& a)
146 {unpack_smart_ptr(x,d,a);}
147 };
148#if defined(__GNUC__) && !defined(__ICC) && !defined(__clang__)
149#pragma GCC diagnostic pop
150#endif
151#endif
152
153#if defined(__cplusplus) && __cplusplus >= 201103L
154 template <class T, class D>
155 struct access_pack<std::unique_ptr<T,D>>
156 {
157 template <class U>
158 void operator()(cd::pack_t& x, const cd::string& d, U& a)
159 {pack_smart_ptr(x,d,a);}
160 };
161
162 template <class T, class D>
163 struct access_unpack<std::unique_ptr<T,D>>
164 {
165 template <class U>
166 void operator()(cd::unpack_t& x, const cd::string& d, U& a)
167 {unpack_smart_ptr(x,d,a);}
168 };
169#endif
170
171}
172
173#endif
Definition pack_base.h:138
Contains access_* structs, and nothing else. These structs are used to gain access to private members...
Definition classdesc_access.h:20
Contains definitions related to classdesc functionality.
void pack(pack_t &targ, const string &desc, is_treenode dum, const T *const &arg)
serialise a tree (or DAG)
Definition pack_graph.h:28
void unpack(unpack_t &targ, const string &desc, is_treenode dum, T *&arg)
unserialise a tree.
Definition pack_graph.h:44
STL namespace.
Definition classdesc.h:299
controlled template specialisation: stolen from boost::enable_if.
Definition classdesc.h:282
class to allow access to private members
Definition classdesc_access.h:21
class to allow access to private members
Definition classdesc_access.h:22