13#ifndef CLASSDESC_XML_PACK_BASE_H
14#define CLASSDESC_XML_PACK_BASE_H
25#include <xml_common.h>
30 inline std::string xml_quote(
char c)
34 case '&':
return "&";
35 case '<':
return "<";
36 case '>':
return ">";
37 case '\'':
return "'";
38 case '"':
return """;
43 s<<
"&#"<<std::setfill(
'0')<<std::setw(4)<<int(c)<<
";";
46 return std::string(1,c);
49 inline std::ostream& operator<<(std::ostream& o,
const CDATA& x)
50 {
return o<<
"<![CDATA["<<
static_cast<const std::string&
>(x)<<
"]]>";}
60 int level(
const string& xx) {
61 const char* x=xx.c_str();
63 if (*x==
'\0')
return 0;
64 for (l=1; *x!=
'\0'; x++)
if (*x==
'.') l++;
68 void pretty(
const string& d) {
if (prettyPrint) *o << std::setw(level(d)) <<
"";}
69 void endpretty() {
if (prettyPrint) *o<<std::endl;}
75 bool tag(
const string& d) {
77 bool ret = taglevel < l;
82 if (l==1 && !schema.empty())
83 *o<<
" xmlns=\""<<schema<<
"\"";
91 void endtag(
const string& d) {
94 *o<<
"</"<<tail(d)<<
">";
106 xml_pack_t(std::ostream& o,
const string& schema=
""):
107 o(&o), taglevel(0), schema(schema), prettyPrint(false),
abort(false) {}
114 Tag(xml_pack_t& t,
const string& d): t(t.tag(d)? &t: 0), d(d) {}
115 ~Tag() {
if (t) t->endtag(d);}
121 put(std::ostream& o,
T x)
125 else if (std::isinf(x))
136 put(std::ostream& o,
const T& x)
146 void pack(
const string& d,
const T&x)
149 std::string tag=tail(d);
152 put(*o,x) <<
"</"<<tag<<
">";
154 if (!*o)
throw std::runtime_error(
"failed to serialise");
164 if (!*o)
throw std::runtime_error(
"failed to serialise");
170 typename enable_if<is_fundamental<T>,
void>::T
171 xml_packp(xml_pack_t& x,
const string& d,T& a)
174 template <>
inline void xml_packp(
xml_pack_t& x,
const string& d,
bool& a)
175 {x.pack(d, a?
"true":
"false");}
177 template <>
inline void xml_packp(
xml_pack_t& x,
const string& d,
char& a)
178 {x.pack(d,classdesc::xml_quote(a));}
191 template <
class T>
void xml_pack(xml_pack_t&,
const string&,
const T&);
193 template <
class T>
void xml_pack(xml_pack_t&,
const string&, T&);
195 template <
class T> xml_pack_t& operator<<(xml_pack_t& t,
const T& a);
197 inline void xml_pack(xml_pack_t& x,
const string& d, std::string& a)
200 for (std::string::size_type i=0; i<a.length() && !x.abort; i++) tmp+=classdesc::xml_quote(a[i]);
204 inline void xml_pack(
xml_pack_t& x,
const string& d,
const std::string& a)
205 {xml_pack(x,d,
const_cast<std::string&
>(a));}
209 T& a,
int dims,
size_t ncopies,...)
212 va_start(ap,ncopies);
213 for (
int i=1; i<dims; i++) ncopies*=va_arg(ap,
int);
218 string eName=typeName<T>().c_str();
220 const char *e=eName.c_str()+eName.length();
221 while (e!=eName.c_str() && *(e-1)!=
' ' && *(e-1)!=
':') e--;
223 for (
size_t i=0; i<ncopies; i++) xml_pack(x,d+
"."+e,(&a)[i]);
226 template <
class T1,
class T2>
227 void xml_pack(
xml_pack_t& x,
const string& d,
const std::pair<T1,T2>& arg)
230 xml_pack(x,d+
".first",arg.first);
231 xml_pack(x,d+
".second",arg.second);
234 template <
class T>
typename
240 string eName=typeName<typename T::value_type>().c_str();
241 eName=eName.substr(0,eName.find(
'<'));
243 const char *e=eName.c_str()+eName.length();
244 while (e!=eName.c_str() && *(e-1)!=
' ' && *(e-1)!=
':') e--;
246 for (
typename T::const_iterator i=arg.begin(); i!=arg.end(); ++i)
247 xml_pack(x,d+
"."+e,*i);
251 void xml_pack_onbase(
xml_pack_t& x,
const string& d,
T& a)
265 void xml_pack(
xml_pack_t& x,
const string& d, shared_ptr<T>& a);
270 throw exception(
"xml_pack of arbitrary graphs not supported");
275#include "use_mbr_pointers.h"
276CLASSDESC_USE_OLDSTYLE_MEMBER_OBJECTS(xml_pack)
277CLASSDESC_FUNCTION_NOP(xml_pack)
279using classdesc::xml_pack;
280using classdesc::xml_pack_onbase;
Definition classdesc.h:868
Definition classdesc.h:920
Definition classdesc.h:923
Definition classdesc.h:931
<utility structure for handling tag/endtag
Definition xml_pack_base.h:110
Definition xml_pack_base.h:56
void pack_notag(const string &d, const T &x)
Definition xml_pack_base.h:161
volatile bool abort
if true, the layout XML in more human friendly form
Definition xml_pack_base.h:103
void pack(const string &d, const T &x)
Definition xml_pack_base.h:146
descriptor access to a class's privates
#define CLASSDESC_ACCESS(type)
add friend statements for each accessor function
Definition classdesc_access.h:36
Contains definitions related to classdesc functionality.
string basename()
returns a valid identifier to append to the descriptor of a base class
Definition classdesc.h:1127
used to transfer contents of CDATA sections.
Definition xml_common.h:23
Definition classdesc.h:1012
Definition classdesc.h:299
controlled template specialisation: stolen from boost::enable_if.
Definition classdesc.h:282
base class for exceptions thrown by classdesc
Definition classdesc.h:546
determines if T is a standard associative container
Definition classdesc.h:321
set to true to cancel packing from another thread
Definition xml_pack_base.h:104