Classdesc  3.D29
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>
38  typename enable_if<Not<is_base_of<PolyJsonBase, typename T::element_type> >, void>::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>
53  typename enable_if<is_base_of<PolyJsonBase, typename T::element_type>,
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>
71  typename enable_if<Not<is_base_of<PolyJsonBase, typename T::element_type> >,
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  {
80  ResetThrowOnErrorOnExit r(x);
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  template <class T>
115  struct access_json_pack<std::auto_ptr<T> >
116  {
117  void operator()(cd::json_pack_t& x, const cd::string& d,
118  std::auto_ptr<T>& a)
119  {json_pack_smart_ptr(x,d,a);}
120  };
121 
122  template <class T>
123  struct access_json_unpack<std::auto_ptr<T> >
124  {
125  void operator()(cd::json_unpack_t& x, const cd::string& d,
126  std::auto_ptr<T>& a)
127  {json_unpack_smart_ptr(x,d,a);}
128  };
129 
130 #if defined(__cplusplus) && __cplusplus >= 201103L
131  template <class T, class D>
132  struct access_json_pack<std::unique_ptr<T,D> >
133  {
134  void operator()(cd::json_pack_t& x, const cd::string& d,
135  std::unique_ptr<T,D>& a)
136  {json_pack_smart_ptr(x,d,a);}
137  };
138 
139  template <class T, class D>
140  struct access_json_unpack<std::unique_ptr<T,D> >
141  {
142  void operator()(cd::json_unpack_t& x, const cd::string& d,
143  std::unique_ptr<T,D>& a)
144  {json_unpack_smart_ptr(x,d,a);}
145  };
146 #endif
147 
148 }
149 
150 #endif
Definition: json_pack_base.h:37
Definition: polyAccessJsonPack.h:14
bool throw_on_error
enable exceptions on error conditions
Definition: json_pack_base.h:40
Definition: classdesc_access.h:26
void json_pack(json_pack_t &o, const string &d, T &a)
forward declare generic json operations
Definition: json_pack_epilogue.h:53
Definition: classdesc_access.h:25
controlled template specialisation: stolen from boost::enable_if.
Definition: classdesc.h:274
Definition: classdesc.h:291