aboutsummaryrefslogtreecommitdiff
path: root/cutl/xml
diff options
context:
space:
mode:
Diffstat (limited to 'cutl/xml')
-rw-r--r--cutl/xml/serializer.cxx8
-rw-r--r--cutl/xml/serializer.hxx37
-rw-r--r--cutl/xml/serializer.ixx30
-rw-r--r--cutl/xml/value-traits.hxx43
-rw-r--r--cutl/xml/value-traits.txx24
5 files changed, 130 insertions, 12 deletions
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 <typename T>
+ void
+ attribute (const qname_type& qname, const T& value);
+
void
attribute (const std::string& name, const std::string& value);
+ template <typename T>
+ void
+ attribute (const std::string& name, const T& value);
+
void
attribute (const std::string& ns,
const std::string& name,
const std::string& value);
+ template <typename T>
+ void
+ attribute (const std::string& ns,
+ const std::string& name,
+ const T& value);
+
// Characters.
//
void
characters (const std::string& value);
+ template <typename T>
+ 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 <cutl/xml/value-traits.hxx>
+
namespace cutl
{
namespace xml
@@ -36,10 +38,38 @@ namespace cutl
attribute (qname.namespace_ (), qname.name (), value);
}
+ template <typename T>
+ inline void serializer::
+ attribute (const qname_type& qname, const T& value)
+ {
+ attribute (qname, value_traits<T>::serialize (value, *this));
+ }
+
inline void serializer::
attribute (const std::string& name, const std::string& value)
{
attribute (std::string (), name, value);
}
+
+ template <typename T>
+ inline void serializer::
+ attribute (const std::string& name, const T& value)
+ {
+ attribute (name, value_traits<T>::serialize (value, *this));
+ }
+
+ template <typename T>
+ inline void serializer::
+ attribute (const std::string& ns, const std::string& name, const T& value)
+ {
+ attribute (ns, name, value_traits<T>::serialize (value, *this));
+ }
+
+ template <typename T>
+ inline void serializer::
+ characters (const T& value)
+ {
+ characters (value_traits<T>::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 <string>
+
+#include <cutl/details/export.hxx>
+
+namespace cutl
+{
+ namespace xml
+ {
+ class parser;
+ class serializer;
+
+ template <typename T>
+ struct default_value_traits
+ {
+ static std::string
+ serialize (const T&, const serializer&);
+ };
+
+ template <>
+ struct LIBCUTL_EXPORT default_value_traits<bool>
+ {
+ static std::string
+ serialize (bool v, const serializer&)
+ {
+ return v ? "true" : "false";
+ }
+ };
+
+ template <typename T>
+ struct value_traits: default_value_traits<T> {};
+ }
+}
+
+#include <cutl/xml/value-traits.txx>
+
+#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 <sstream>
+
+#include <cutl/xml/parser.hxx>
+#include <cutl/xml/serializer.hxx>
+
+namespace cutl
+{
+ namespace xml
+ {
+ template <typename T>
+ std::string default_value_traits<T>::
+ serialize (const T& v, const serializer& s)
+ {
+ std::ostringstream os;
+ if (!(os << v))
+ throw serialization (s.output_name (), "invalid value");
+ return os.str ();
+ }
+ }
+}