From ce3dfdff0d03697bf0c9e0cc9fb45dc6ef7c4ba9 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 13 Mar 2013 12:42:37 +0200 Subject: Add support for XML serializing values other than std::string --- cutl/xml/serializer.cxx | 8 ++++---- cutl/xml/serializer.hxx | 37 +++++++++++++++++++++++++++++-------- cutl/xml/serializer.ixx | 30 ++++++++++++++++++++++++++++++ cutl/xml/value-traits.hxx | 43 +++++++++++++++++++++++++++++++++++++++++++ cutl/xml/value-traits.txx | 24 ++++++++++++++++++++++++ 5 files changed, 130 insertions(+), 12 deletions(-) create mode 100644 cutl/xml/value-traits.hxx create mode 100644 cutl/xml/value-traits.txx (limited to 'cutl') diff --git a/cutl/xml/serializer.cxx b/cutl/xml/serializer.cxx index 66c78a4..3919635 100644 --- a/cutl/xml/serializer.cxx +++ b/cutl/xml/serializer.cxx @@ -78,8 +78,8 @@ namespace cutl } serializer:: - serializer (ostream& os, const string& name, unsigned short ind) - : os_ (os), os_state_ (os.exceptions ()), name_ (name), depth_ (0) + serializer (ostream& os, const string& oname, unsigned short ind) + : os_ (os), os_state_ (os.exceptions ()), oname_ (oname), depth_ (0) { // Temporarily disable exceptions on the stream. // @@ -106,7 +106,7 @@ namespace cutl { string m (genxGetErrorMessage (s_, e)); genxDispose (s_); - throw serialization (name, m); + throw serialization (oname, m); } } @@ -126,7 +126,7 @@ namespace cutl os_.exceptions (os_state_); // Fall through. default: - throw serialization (name_, genxGetErrorMessage (s_, e)); + throw serialization (oname_, genxGetErrorMessage (s_, e)); } } diff --git a/cutl/xml/serializer.hxx b/cutl/xml/serializer.hxx index e3fef54..591a20a 100644 --- a/cutl/xml/serializer.hxx +++ b/cutl/xml/serializer.hxx @@ -50,17 +50,20 @@ namespace cutl typedef xml::qname qname_type; - // Serialize to std::ostream. Name is used in diagnostics to identify - // the document being serialized. std::ios_base::failure exception is - // used to report io errors (badbit and failbit). The indentation - // argument specifies the number of indentation spaces that should - // be used for pretty-printing. If 0 is passed, no pretty-printing - // is performed. + // Serialize to std::ostream. Output name is used in diagnostics to + // identify the document being serialized. std::ios_base::failure + // exception is used to report io errors (badbit and failbit). The + // indentation argument specifies the number of indentation spaces + // that should be used for pretty-printing. If 0 is passed, no + // pretty-printing is performed. // serializer (std::ostream&, - const std::string& name, + const std::string& output_name, unsigned short indentation = 2); + const std::string& + output_name () const {return oname_;} + // Serialization functions. // public: @@ -96,19 +99,37 @@ namespace cutl void attribute (const qname_type& qname, const std::string& value); + template + void + attribute (const qname_type& qname, const T& value); + void attribute (const std::string& name, const std::string& value); + template + void + attribute (const std::string& name, const T& value); + void attribute (const std::string& ns, const std::string& name, const std::string& value); + template + void + attribute (const std::string& ns, + const std::string& name, + const T& value); + // Characters. // void characters (const std::string& value); + template + void + characters (const T& value); + // Namespaces declaration. If prefix is empty, then the default // namespace is declared. If both prefix and namespace are empty, // then the default namespace declaration is cleared (xmlns=""). @@ -140,7 +161,7 @@ namespace cutl private: std::ostream& os_; std::ostream::iostate os_state_; // Original exception state. - const std::string name_; + const std::string oname_; genxWriter s_; genxSender sender_; diff --git a/cutl/xml/serializer.ixx b/cutl/xml/serializer.ixx index 115b77b..11ff33a 100644 --- a/cutl/xml/serializer.ixx +++ b/cutl/xml/serializer.ixx @@ -2,6 +2,8 @@ // copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC // license : MIT; see accompanying LICENSE file +#include + namespace cutl { namespace xml @@ -36,10 +38,38 @@ namespace cutl attribute (qname.namespace_ (), qname.name (), value); } + template + inline void serializer:: + attribute (const qname_type& qname, const T& value) + { + attribute (qname, value_traits::serialize (value, *this)); + } + inline void serializer:: attribute (const std::string& name, const std::string& value) { attribute (std::string (), name, value); } + + template + inline void serializer:: + attribute (const std::string& name, const T& value) + { + attribute (name, value_traits::serialize (value, *this)); + } + + template + inline void serializer:: + attribute (const std::string& ns, const std::string& name, const T& value) + { + attribute (ns, name, value_traits::serialize (value, *this)); + } + + template + inline void serializer:: + characters (const T& value) + { + characters (value_traits::serialize (value, *this)); + } } } diff --git a/cutl/xml/value-traits.hxx b/cutl/xml/value-traits.hxx new file mode 100644 index 0000000..f79f67f --- /dev/null +++ b/cutl/xml/value-traits.hxx @@ -0,0 +1,43 @@ +// file : cutl/xml/value-traits.hxx +// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +#ifndef CUTL_XML_VALUE_TRAITS_HXX +#define CUTL_XML_VALUE_TRAITS_HXX + +#include + +#include + +namespace cutl +{ + namespace xml + { + class parser; + class serializer; + + template + struct default_value_traits + { + static std::string + serialize (const T&, const serializer&); + }; + + template <> + struct LIBCUTL_EXPORT default_value_traits + { + static std::string + serialize (bool v, const serializer&) + { + return v ? "true" : "false"; + } + }; + + template + struct value_traits: default_value_traits {}; + } +} + +#include + +#endif // CUTL_XML_VALUE_TRAITS_HXX diff --git a/cutl/xml/value-traits.txx b/cutl/xml/value-traits.txx new file mode 100644 index 0000000..000d6be --- /dev/null +++ b/cutl/xml/value-traits.txx @@ -0,0 +1,24 @@ +// file : cutl/xml/value-traits.txx +// copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC +// license : MIT; see accompanying LICENSE file + +#include + +#include +#include + +namespace cutl +{ + namespace xml + { + template + std::string default_value_traits:: + serialize (const T& v, const serializer& s) + { + std::ostringstream os; + if (!(os << v)) + throw serialization (s.output_name (), "invalid value"); + return os.str (); + } + } +} -- cgit v1.1