Classdesc 3.44
polyAccessJsonPack.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_JSON_PACK_H
10#define CLASSDESC_POLY_ACCESS_JSON_PACK_H
11
12namespace classdesc
13{
14 struct ResetThrowOnErrorOnExit
15 {
16 json_pack_t& x;
17 bool prev;
18 ResetThrowOnErrorOnExit(json_pack_t& x): x(x), prev(x.throw_on_error) {}
19 ~ResetThrowOnErrorOnExit() {x.throw_on_error=prev;}
20 };
21
22#ifdef CLASSDESC_POLYJSONBASE_H
23 // polymorphic version
24 template <class T>
26 json_pack_smart_ptr(json_pack_t& x, const string& d, T& a,
27 dummy<0> dum=0)
28 {
29 if (a.get())
30 {
31 ::json_pack(x,d+".type",a->type());
32 a->json_pack(x,d);
33 }
34 }
35
36 // non polymorphic version
37 template <class T>
39#else
40 template <class T>
41 void
42#endif
43 json_pack_smart_ptr(json_pack_t& x, const string& d, T& a,
44 dummy<1> dum=0)
45 {
46 if (a.get())
47 ::json_pack(x,d,*a);
48 }
49
50#ifdef CLASSDESC_POLYJSONBASE_H
51 template <class T>
53 public is_base_of<PolyJsonBase, typename T::element_type> {};
54#else
55 template <class T>
56 struct is_poly_constructible: public false_type {};
57#endif
58
59 // polymorphic version
60 template <class T>
62 json_unpack_smart_ptr(json_unpack_t& x, const string& d, T& a,
63 dummy<0> dum=0)
64 {
65 if (x.type()==RESTProcessType::object && x.get_obj().count("type"))
66 {
67 typename T::element_type::Type type;
68 ::json_unpack(x,d+".type",type);
69 a.reset(T::element_type::create(type));
70 a->json_unpack(x,d);
71 }
72 else
73 a.reset();
74 }
75
76 // non polymorphic version
77 template<class T>
78 typename enable_if<
79 And<
80 Not<is_poly_constructible<T> >, is_default_constructible<typename T::element_type> >,
81 void>::T
82 json_unpack_smart_ptr(json_pack_t& x, const string& d, T& a,
83 dummy<1> dum=0)
84 {
86 a.reset(new typename T::element_type);
87 try
88 {
89 ::json_unpack(x,d,*a);
90 }
91 catch (const std::exception&)
92 {
93 a.reset(); // data wasn't there
94 }
95 }
96
97 template<class T>
98 typename enable_if<
99 And<
101 >, void>::T
102 json_unpack_smart_ptr(json_pack_t& x, const string& d, T& a,
103 dummy<2> dum=0)
104 {
105 a.reset(); // cannot unpack
106 }
107
108}
109
110namespace classdesc_access
111{
112 namespace cd=classdesc;
113 template <class T>
114 struct access_json_pack<cd::shared_ptr<T> >
115 {
116 template <class U>
117 void operator()(cd::json_pack_t& x, const cd::string& d, U& a)
118 {json_pack_smart_ptr(x,d,a);}
119 };
120
121 template <class T>
122 struct access_json_unpack<cd::shared_ptr<T> >
123 {
124 template <class U>
125 void operator()(cd::json_unpack_t& x, const cd::string& d, U& a)
126 {json_unpack_smart_ptr(x,d,a);}
127 };
128
129 template <class T>
130 struct access_json_pack<cd::weak_ptr<T> >
131 {
132 template <class U>
133 void operator()(cd::json_pack_t& x, const cd::string& d, U& a)
134 {
135 if (cd::shared_ptr<T> sp=a.lock())
136 json_pack_smart_ptr(x,d,sp);
137 }
138 };
139
140 template <class T>
141 struct access_json_unpack<cd::weak_ptr<T> >
142 {
143 template <class U>
144 void operator()(cd::json_unpack_t& x, const cd::string& d, U& a)
145 {
146 if (cd::shared_ptr<T> sp=a.lock())
147 json_unpack_smart_ptr(x,d,sp);
148 }
149 };
150
151#if defined(__cplusplus) && __cplusplus<=201402
152#if defined(__GNUC__) && !defined(__ICC) && !defined(__clang__)
153#pragma GCC diagnostic push
154#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
155#endif
156 template <class T>
157 struct access_json_pack<std::auto_ptr<T> >
158 {
159 void operator()(cd::json_pack_t& x, const cd::string& d,
160 std::auto_ptr<T>& a)
161 {json_pack_smart_ptr(x,d,a);}
162 };
163
164 template <class T>
165 struct access_json_unpack<std::auto_ptr<T> >
166 {
167 void operator()(cd::json_unpack_t& x, const cd::string& d,
168 std::auto_ptr<T>& a)
169 {json_unpack_smart_ptr(x,d,a);}
170 };
171#if defined(__GNUC__) && !defined(__ICC) && !defined(__clang__)
172#pragma GCC diagnostic pop
173#endif
174#endif
175
176#if defined(__cplusplus) && __cplusplus >= 201103L
177 template <class T, class D>
178 struct access_json_pack<std::unique_ptr<T,D> >
179 {
180 void operator()(cd::json_pack_t& x, const cd::string& d,
181 std::unique_ptr<T,D>& a)
182 {json_pack_smart_ptr(x,d,a);}
183 };
184
185 template <class T, class D>
186 struct access_json_unpack<std::unique_ptr<T,D> >
187 {
188 void operator()(cd::json_unpack_t& x, const cd::string& d,
189 std::unique_ptr<T,D>& a)
190 {json_unpack_smart_ptr(x,d,a);}
191 };
192#endif
193
194}
195
196#endif
Definition json_pack_base.h:99
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 json_pack(json_pack_t &o, const string &d, T &a)
forward declare generic json operations
Definition json_pack_epilogue.h:61
STL namespace.
Definition classdesc.h:420
Definition classdesc.h:405
Definition polyAccessJsonPack.h:15
Definition classdesc.h:299
controlled template specialisation: stolen from boost::enable_if.
Definition classdesc.h:282
Definition polyAccessJsonPack.h:56
Definition json_pack_epilogue.h:92
Definition json_pack_epilogue.h:104