json_pack_epilogue.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 JSON_PACK_EPILOGUE_H
10 #define JSON_PACK_EPILOGUE_H
11 #include "classdesc.h"
12 #include <sstream>
13 
14 namespace classdesc
15 {
16  template <class T>
18  public Not< Or< Or< Or<is_fundamental<T>,is_string<T> >, is_sequence<T> >,
19  is_associative_container<T> > >
20  {};
21 
22  template <class T> typename
24  json_packp(json_pack_t& o, const string& d, T& a)
25  {
26  if (tail(d)!="")
27  {
28  //create the object, if it doesn't already exist
29  try
30  {
31  json_spirit::mValue& parent=json_find(o,head(d));
32  if (parent.type()!=json_spirit::obj_type)
33  throw json_pack_error("trying to create object %s in non-object",
34  d.c_str());
35  json_spirit::mObject::iterator member=parent.get_obj().find(tail(d));
36  if (member==parent.get_obj().end())
37  parent.get_obj().insert(make_pair(tail(d), json_spirit::mObject()));
38  }
39  catch (json_pack_error&)
40  {
41  // only throw if this flag is set
42  if (o.throw_on_error) throw;
43  }
44  }
46  }
47 
48  template <class T> typename
50  json_unpackp(json_pack_t& o, const string& d, T& a, dummy<3> dum=0)
52 
53  template <class T> void json_pack(json_pack_t& o, const string& d, T& a)
54  {json_packp(o,d,a);}
55 
56  template <class T> void json_unpack(json_unpack_t& o, const string& d, T& a)
57  {json_unpackp(o,d,a);}
58 
59 }
60 
61 namespace classdesc_access
62 {
63  namespace cd=classdesc;
64  // fall through to streaming operators
65  template <class T>
66  struct access_json_pack
67  {
68  public:
69  void operator()(cd::json_pack_t& b, const cd::string& d, T& a)
70  {
71  std::ostringstream o;
72  o<<a;
73  b<<o.str();
74  }
75  };
76 
77  template <class T>
78  struct access_json_unpack
79  {
80  public:
81  void operator()(cd::json_unpack_t& b, const cd::string& d,
82  T& a)
83  {
84  std::string s;
85  b>>s;
86  std::istringstream i(s);
87  i>>a;
88  }
89  };
90 
91  // support for polymorphic types, if loaded
92 //#ifdef NEW_POLY_H
93 // template <class T> struct access_json_pack<cd::PolyBase<T> >:
94 // public cd::NullDescriptor<cd::json_pack_t> {};
95 // template <class T> struct access_json_unpack<cd::PolyBase<T> >:
96 // public cd::NullDescriptor<cd::json_unpack_t> {};
97 // template <class T, class B> struct access_json_pack<cd::Poly<T,B> >
98 // {
99 // template <class U>
100 // void operator()(cd::json_pack_t& t, const cd::string& d, U& a)
101 // {
102 // json_pack(t,d,cd::base_cast<B>::cast(a));
103 // }
104 // };
105 // template <class T, class B> struct access_json_unpack<cd::Poly<T,B> >
106 // {
107 // template <class U>
108 // void operator()(cd::json_pack_t& t, const cd::string& d, U& a)
109 // {
110 // json_unpack(t,d,cd::base_cast<B>::cast(a));
111 // }
112 // };
113 //#endif
114 
115 #ifdef POLYPACKBASE_H
116  template <> struct access_json_pack<cd::PolyPackBase>:
117  public cd::NullDescriptor<cd::json_pack_t> {};
118  template <> struct access_json_unpack<cd::PolyPackBase>:
119  public cd::NullDescriptor<cd::json_unpack_t> {};
120  template <class T> struct access_json_pack<cd::PolyPack<T> >:
121  public cd::NullDescriptor<cd::json_pack_t> {};
122  template <class T> struct access_json_unpack<cd::PolyPack<T> >:
123  public cd::NullDescriptor<cd::json_unpack_t> {};
124 #endif
125 
126 #ifdef POLYJSONBASE_H
127  template <> struct access_json_pack<cd::PolyJsonBase>:
128  public cd::NullDescriptor<cd::json_pack_t> {};
129  template <> struct access_json_unpack<cd::PolyJsonBase>:
130  public cd::NullDescriptor<cd::json_unpack_t> {};
131  template <class T> struct access_json_pack<cd::PolyJson<T> >:
132  public cd::NullDescriptor<cd::json_pack_t> {};
133  template <class T> struct access_json_unpack<cd::PolyJson<T> >:
134  public cd::NullDescriptor<cd::json_unpack_t> {};
135 #endif
136 
137 #ifdef POLYXMLBASE_H
138  template <> struct access_json_pack<cd::PolyXMLBase>:
139  public cd::NullDescriptor<cd::json_pack_t> {};
140  template <> struct access_json_unpack<cd::PolyXMLBase>:
141  public cd::NullDescriptor<cd::json_unpack_t> {};
142  template <class T> struct access_json_pack<cd::PolyXML<T> >:
143  public cd::NullDescriptor<cd::json_pack_t> {};
144  template <class T> struct access_json_unpack<cd::PolyXML<T> >:
145  public cd::NullDescriptor<cd::json_unpack_t> {};
146 #endif
147 
148 }
149 
150 #include "polyAccessJsonPack.h"
151 #endif
Definition: json_pack_base.h:43
Definition: classdesc.h:326
void json_pack(json_pack_t &o, const string &d, T &a)
forward declare generic json operations
Definition: json_pack_epilogue.h:53
Definition: polyJsonBase.h:23
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
helper for constructing null descriptors
Definition: classdesc.h:784
Definition: json_pack_base.h:20
utility class for defining pack descriptors for polymorphic types
Definition: polyPackBase.h:45
Contains definitions related to classdesc functionality.
Definition: arrays.h:2514
Definition: polyXMLBase.h:25
Definition: polyXMLBase.h:17
controlled template specialisation: stolen from boost::enable_if.
Definition: classdesc.h:249
Definition: json_pack_epilogue.h:17
Definition: classdesc.h:266
interface for applying pack descriptors to polymorphic objects
Definition: polyPackBase.h:18
Contains access_* structs, and nothing else. These structs are used to gain access to private members...
Definition: accessor.h:55
Definition: polyJsonBase.h:15
json_spirit::mValue & json_find(json_spirit::mValue &x, std::string name)
find an object named by name within the json object x
Definition: json_pack_base.h:73