13 #ifndef XML_PACK_BASE_H 14 #define XML_PACK_BASE_H 21 #include <classdesc.h> 22 #include <xml_common.h> 27 inline std::string xml_quote(
char c)
31 case '&':
return "&";
32 case '<':
return "<";
33 case '>':
return ">";
34 case '\'':
return "'";
35 case '"':
return """;
43 return std::string(1,c);
54 int level(
const string& xx) {
55 const char* x=xx.c_str();
57 if (*x==
'\0')
return 0;
58 for (l=1; *x!=
'\0'; x++)
if (*x==
'.') l++;
62 void pretty(
const string& d) {
if (prettyPrint) *o << std::setw(level(d)) <<
"";}
63 void endpretty() {
if (prettyPrint) *o<<std::endl;}
69 bool tag(
const string& d) {
71 bool ret = taglevel < l;
76 if (l==1 && !schema.empty())
77 *o<<
" xmlns=\""<<schema<<
"\"";
82 assert(taglevel==level(d));
86 void endtag(
const string& d) {
89 *o<<
"</"<<tail(d)<<
">";
99 o(&o), taglevel(0), schema(schema), prettyPrint(false) {}
106 Tag(
xml_pack_t& t,
const string& d): t(t.tag(d)? &t: 0), d(d) {}
107 ~
Tag() {
if (t) t->endtag(d);}
114 void pack(
const string& d,
const T&x)
116 std::string tag=tail(d);
118 *o <<
"<"<<tag<<
">" << x <<
"</"<<tag<<
">";
135 template <>
inline void xml_packp(
xml_pack_t& x,
const string& d,
bool& a)
136 {x.
pack(d, a?
"true":
"false");}
151 template <
class T>
struct access_xml_pack;
164 for (std::string::size_type i=0; i<a.length(); i++) tmp+=classdesc::xml_quote(a[i]);
169 {xml_pack(x,d,const_cast<std::string&>(a));}
175 T& a,
int dims,
size_t ncopies,...)
178 va_start(ap,ncopies);
179 for (
int i=1; i<dims; i++) ncopies*=va_arg(ap,
int);
184 classdesc::string eName=classdesc::typeName<T>().c_str();
186 const char *e=eName.c_str()+eName.length();
187 while (e!=eName.c_str() && *(e-1)!=
' ' && *(e-1)!=
':') e--;
189 for (
size_t i=0; i<ncopies; i++) xml_pack(x,d+
"."+e,(&a)[i]);
192 template <
class T1,
class T2>
194 const std::pair<T1,T2>& arg)
197 xml_pack(x,d+
".first",arg.first);
198 xml_pack(x,d+
".second",arg.second);
203 template <
class T>
typename 209 string eName=typeName<typename T::value_type>().c_str();
210 eName=eName.substr(0,eName.find(
'<'));
212 const char *e=eName.c_str()+eName.length();
213 while (e!=eName.c_str() && *(e-1)!=
' ' && *(e-1)!=
':') e--;
215 for (
typename T::const_iterator i=arg.begin(); i!=arg.end(); ++i)
216 ::xml_pack(x,d+
"."+e,*i);
220 void xml_pack_onbase(
xml_pack_t& x,
const string& d,T& a)
221 {::xml_pack(x,d+basename<T>(),a);}
225 using classdesc::xml_pack_onbase;
228 template<
class C,
class T>
236 template<
class T,
class U>
247 classdesc::shared_ptr<T>& a);
254 throw exception(
"xml_pack of arbitrary graphs not supported");
Definition: classdesc.h:631
Definition: classdesc.h:623
xml_pack_t(std::ostream &o, const string &schema="")
if true, the layout XML in more human friendly form
Definition: xml_pack_base.h:98
void pack(const string &d, const T &x)
Definition: xml_pack_base.h:114
Definition: classdesc.h:626
Definition: xml_pack_base.h:49
Definition: classdesc.h:588
Contains definitions related to classdesc functionality.
Definition: arrays.h:2514
<utility structure for handling tag/endtag
Definition: xml_pack_base.h:101
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
base class for exceptions thrown by classdesc
Definition: classdesc.h:366
determines if T is a standard associative container
Definition: classdesc.h:288
Definition: classdesc.h:704
void pack_notag(const string &d, const T &x)
Definition: xml_pack_base.h:125