Classdesc  3.D29
xml_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 XML_PACK_EPILOGUE_H
10 #define XML_PACK_EPILOGUE_H
11 #include <typeName_epilogue.h>
12 #include "polyAccessXMLPack.h"
13 
14 namespace classdesc
15 {
16  class xml_pack_t;
17  class xml_unpack_t;
18 }
19 
20 namespace classdesc_access
21 {
22 #ifdef XML_PACK_BASE_H
23  template <class T> struct access_xml_pack
24  {
25  //This routine uses operator << if defined
26  void operator()(classdesc::xml_pack_t& t,const classdesc::string& d, const T& a)
27  {t.pack_notag(d,a);}
28  };
29 #endif
30 
31 #ifdef XML_UNPACK_BASE_H
32  //This routine uses operator >> if defined
33  template <class T> struct access_xml_unpack
34  {
35  void operator()(classdesc::xml_unpack_t& t,const classdesc::string& d,T& a)
36  {t.unpack(d,a);}
37  };
38  // partial specialisation to discard unpacking to constant data types
39  template <class T> struct access_xml_unpack<const T>
40  {
41  void operator()(classdesc::xml_unpack_t& t,const classdesc::string& d, const T& a)
42  {
43  T tmp;
44  t.unpack(d,tmp);
45  }
46  };
47 #endif
48 }
49 
50 namespace classdesc
51 {
52  template <class T>
53  struct AllOtherXMLPackpTypes: public
54  Not<
55  Or<
56  Or<
57  Or< is_sequence<T>, is_associative_container<T> >,
58  is_fundamental<T>
59  >,
60  is_enum<T>
61  >
62  >
63  {};
64 }
65 
66 namespace classdesc
67 {
68 #ifdef XML_PACK_BASE_H
69  template <class T> typename
70  enable_if<AllOtherXMLPackpTypes<T>, void >::T
71  xml_packp(xml_pack_t& t,const string& d, T& a, dummy<0> dum=0)
72  {
73  xml_pack_t::Tag tag(t,d);
75  }
76 #endif
77 
78 #ifdef XML_UNPACK_BASE_H
79  template <class T> typename
80  enable_if<AllOtherXMLPackpTypes<T>, void >::T
81  xml_unpackp(xml_unpack_t& t,const string& d,T& a, dummy<0> dum=0)
82  {
84  }
85 
86  template <class T> typename
87  enable_if<is_enum<T>, void >::T
88  xml_unpackp(xml_unpack_t& t,const string& d,T& a, dummy<1> dum=0)
89  {
91  }
92 
93  // handle const arguments
94  template <class T>
95  void xml_unpackp(xml_unpack_t& t,const string& d, const T& a)
96  {}
97 #endif
98 }
99 
100 #ifdef XML_PACK_BASE_H
101 template <class T> void xml_pack(classdesc::xml_pack_t& t,const classdesc::string& d, T& a)
102 {
103  classdesc::xml_packp(t,d,a);
104 }
105 
106 template <class T> void xml_pack(classdesc::xml_pack_t& t,const classdesc::string& d, const T& a)
107 {
108  classdesc::xml_packp(t,d,const_cast<T&>(a));
109 }
110 
111 template <class T> classdesc::xml_pack_t& operator<<(classdesc::xml_pack_t& t, const T& a)
112 {xml_pack(t,"root",const_cast<T&>(a)); return t;}
113 
114 #endif
115 
116 #ifdef XML_UNPACK_BASE_H
117 template <class T> void xml_unpack(classdesc::xml_unpack_t& t,const classdesc::string& d,T& a)
118 {
119  classdesc::xml_unpackp(t,d,a);
120 }
121 
122 template <class T> classdesc::xml_unpack_t& operator>>(classdesc::xml_unpack_t& t, T& a)
123 {xml_unpack(t,"root",a); return t;}
124 #endif
125 
126 namespace classdesc_access
127 {
128  namespace cd=classdesc;
129  // support for polymorphic types, if loaded
130 #ifdef NEW_POLY_H
131 #ifdef XML_PACK_BASE_H
132  template <class T> struct access_xml_pack<cd::PolyBase<T> >:
133  public cd::NullDescriptor<cd::xml_pack_t> {};
134  template <class T, class B> struct access_xml_pack<cd::Poly<T,B> >
135  {
136  template <class U>
137  void operator()(cd::xml_pack_t& t, const cd::string& d, U& a)
138  {
139  xml_pack(t,d,cd::base_cast<B>::cast(a));
140  }
141  };
142 #endif
143 #ifdef XML_UNPACK_BASE_H
144  template <class T> struct access_xml_unpack<cd::PolyBase<T> >:
145  public cd::NullDescriptor<cd::xml_unpack_t> {};
146  template <class T, class B> struct access_xml_unpack<cd::Poly<T,B> >
147  {
148  template <class U>
149  void operator()(cd::xml_unpack_t& t, const cd::string& d, U& a)
150  {
151  xml_unpack(t,d,cd::base_cast<B>::cast(a));
152  }
153  };
154 #endif
155 #endif
156 
157 #ifdef POLYPACKBASE_H
158 #ifdef XML_PACK_BASE_H
159  template <> struct access_xml_pack<cd::PolyPackBase>:
160  public cd::NullDescriptor<cd::xml_pack_t> {};
161  template <class T> struct access_xml_pack<cd::PolyPack<T> >:
162  public cd::NullDescriptor<cd::xml_pack_t> {};
163 #endif
164 #ifdef XML_UNPACK_BASE_H
165  template <> struct access_xml_unpack<cd::PolyPackBase>:
166  public cd::NullDescriptor<cd::xml_unpack_t> {};
167  template <class T> struct access_xml_unpack<cd::PolyPack<T> >:
168  public cd::NullDescriptor<cd::xml_unpack_t> {};
169 #endif
170 #endif
171 
172 #ifdef POLYJSONBASE_H
173 #ifdef XML_PACK_BASE_H
174  template <> struct access_xml_pack<cd::PolyJsonBase>:
175  public cd::NullDescriptor<cd::xml_pack_t> {};
176  template <class T> struct access_xml_pack<cd::PolyJson<T> >:
177  public cd::NullDescriptor<cd::xml_pack_t> {};
178 #endif
179 #ifdef XML_UNPACK_BASE_H
180  template <> struct access_xml_unpack<cd::PolyJsonBase>:
181  public cd::NullDescriptor<cd::xml_unpack_t> {};
182  template <class T> struct access_xml_unpack<cd::PolyJson<T> >:
183  public cd::NullDescriptor<cd::xml_unpack_t> {};
184 #endif
185 #endif
186 
187 #ifdef POLYXMLBASE_H
188 #ifdef XML_PACK_BASE_H
189  template <> struct access_xml_pack<cd::PolyXMLBase>:
190  public cd::NullDescriptor<cd::xml_pack_t> {};
191  template <class T> struct access_xml_pack<cd::PolyXML<T> >:
192  public cd::NullDescriptor<cd::xml_pack_t> {};
193 #endif
194 #ifdef XML_UNPACK_BASE_H
195  template <> struct access_xml_unpack<cd::PolyXMLBase>:
196  public cd::NullDescriptor<cd::xml_unpack_t> {};
197  template <class T> struct access_xml_unpack<cd::PolyXML<T> >:
198  public cd::NullDescriptor<cd::xml_unpack_t> {};
199 #endif
200 #endif
201 
202 #ifdef REF_H
203 #ifdef XML_PACK_BASE_H
204  template <class T> struct access_xml_pack<cd::ref<T> >
205  {
206  void operator()(cd::xml_pack_t& x, const cd::string& d, cd::ref<T>& a)
207  {
208  if (a) xml_pack(x,d,*a);
209  }
210  };
211 #endif
212 
213 #ifdef XML_UNPACK_BASE_H
214  template <class T> struct access_xml_unpack<cd::ref<T> >
215  {
216  void operator()(cd::xml_unpack_t& x, const cd::string& d, cd::ref<T>& a)
217  {
218  if (x.exists(d))
219  xml_unpack(x,d,*a);
220  }
221  };
222 #endif
223 #endif
224 
225 #ifdef XML_PACK_BASE_H
226  template<class T>
227  struct access_xml_pack<T*>
228  {
229  void operator()(cd::xml_pack_t& targ, const cd::string& desc, T*&)
230  {
231  throw cd::exception("xml_pack of pointers not supported");
232  }
233  };
234 #endif
235 
236 #ifdef XML_UNPACK_BASE_H
237  template<class T>
238  struct access_xml_unpack<T*>
239  {
240  void operator()(cd::xml_unpack_t& targ, const cd::string& desc, T*&)
241  {
242  throw cd::exception("xml_unpack of pointers not supported");
243  }
244  };
245 #endif
246 }
247 
248 
249 
250 #endif
Definition: classdesc.h:351
Definition: classdesc.h:778
Definition: ref.h:27
Definition: classdesc_access.h:23
Definition: xml_unpack_base.h:268
helper for constructing null descriptors
Definition: classdesc.h:768
bool exists(const std::string &key)
checks for existence of token unpacked from XML stream
Definition: xml_unpack_base.h:358
Definition: xml_pack_epilogue.h:53
Definition: xml_pack_base.h:49
void unpack(std::string key, T &var)
simple data type deserialisation
Definition: xml_unpack_base.h:328
void pack_notag(const string &d, const T &x)
Definition: xml_pack_base.h:125
base class for exceptions thrown by classdesc
Definition: classdesc.h:391
Definition: classdesc_access.h:24