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 POLY_ACCESS_JSON_PACK_H
10 #define POLY_ACCESS_JSON_PACK_H
11 
12 namespace classdesc
13 {
15  {
16  json_pack_t& x;
17  bool prev;
20  };
21 
22 #ifdef 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 POLYJSONBASE_H
51  // polymorphic version
52  template <class T>
54  void>::T
55  json_unpack_smart_ptr(json_unpack_t& x, const string& d, T& a,
56  dummy<0> dum=0)
57  {
58  if (x.type()==json_spirit::obj_type && x.get_obj().count("type"))
59  {
60  typename T::element_type::Type type;
61  ::json_unpack(x,d+".type",type);
62  a.reset(T::element_type::create(type));
63  a->json_unpack(x,d);
64  }
65  else
66  a.reset();
67  }
68 
69  // non polymorphic version
70  template<class T>
72  void>::T
73 #else
74  template<class T>
75  void
76 #endif
77  json_unpack_smart_ptr(json_pack_t& x, const string& d, T& a,
78  dummy<1> dum=0)
79  {
81  a.reset(new typename T::element_type);
82  try
83  {
84  ::json_unpack(x,d,*a);
85  }
86  catch (const std::exception&)
87  {
88  a.reset(); // data wasn't there
89  }
90  }
91 
92 
93 }
94 
95 namespace classdesc_access
96 {
97  namespace cd=classdesc;
98  template <class T>
99  struct access_json_pack<cd::shared_ptr<T> >
100  {
101  void operator()(cd::json_pack_t& x, const cd::string& d,
102  cd::shared_ptr<T>& a)
103  {json_pack_smart_ptr(x,d,a);}
104  };
105 
106  template <class T>
107  struct access_json_unpack<cd::shared_ptr<T> >
108  {
109  void operator()(cd::json_unpack_t& x, const cd::string& d,
110  cd::shared_ptr<T>& a)
111  {json_unpack_smart_ptr(x,d,a);}
112  };
113 
114 #if defined(__cplusplus) && __cplusplus<=201402
115 #if defined(__GNUC__) && !defined(__ICC) && !defined(__clang__)
116 #pragma GCC diagnostic push
117 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
118 #endif
119  template <class T>
120  struct access_json_pack<std::auto_ptr<T> >
121  {
122  void operator()(cd::json_pack_t& x, const cd::string& d,
123  std::auto_ptr<T>& a)
124  {json_pack_smart_ptr(x,d,a);}
125  };
126 
127  template <class T>
128  struct access_json_unpack<std::auto_ptr<T> >
129  {
130  void operator()(cd::json_unpack_t& x, const cd::string& d,
131  std::auto_ptr<T>& a)
132  {json_unpack_smart_ptr(x,d,a);}
133  };
134 #if defined(__GNUC__) && !defined(__ICC) && !defined(__clang__)
135 #pragma GCC diagnostic pop
136 #endif
137 #endif
138 
139 #if defined(__cplusplus) && __cplusplus >= 201103L
140  template <class T, class D>
141  struct access_json_pack<std::unique_ptr<T,D> >
142  {
143  void operator()(cd::json_pack_t& x, const cd::string& d,
144  std::unique_ptr<T,D>& a)
145  {json_pack_smart_ptr(x,d,a);}
146  };
147 
148  template <class T, class D>
149  struct access_json_unpack<std::unique_ptr<T,D> >
150  {
151  void operator()(cd::json_unpack_t& x, const cd::string& d,
152  std::unique_ptr<T,D>& a)
153  {json_unpack_smart_ptr(x,d,a);}
154  };
155 #endif
156 
157 }
158 
159 #endif
Definition: json_pack_base.h:43
void json_pack(json_pack_t &o, const string &d, T &a)
forward declare generic json operations
Definition: json_pack_epilogue.h:53
Definition: polyAccessJsonPack.h:14
Definition: classdesc_access.h:26
bool throw_on_error
enable exceptions on error conditions
Definition: json_pack_base.h:46
Definition: classdesc_access.h:25
Contains definitions related to classdesc functionality.
Definition: arrays.h:2514
controlled template specialisation: stolen from boost::enable_if.
Definition: classdesc.h:249
Definition: classdesc.h:266
Contains access_* structs, and nothing else. These structs are used to gain access to private members...
Definition: accessor.h:55