diff options
author | Jörg Frings-Fürst <jff@merkur> | 2014-05-18 16:08:14 +0200 |
---|---|---|
committer | Jörg Frings-Fürst <jff@merkur> | 2014-05-18 16:08:14 +0200 |
commit | a15cf65c44d5c224169c32ef5495b68c758134b7 (patch) | |
tree | 3419f58fc8e1b315ba8171910ee044c5d467c162 /xsd/libxsd/xsd/cxx/tree/serialization/double.hxx |
Imported Upstream version 3.3.0.2upstream/3.3.0.2
Diffstat (limited to 'xsd/libxsd/xsd/cxx/tree/serialization/double.hxx')
-rw-r--r-- | xsd/libxsd/xsd/cxx/tree/serialization/double.hxx | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/xsd/libxsd/xsd/cxx/tree/serialization/double.hxx b/xsd/libxsd/xsd/cxx/tree/serialization/double.hxx new file mode 100644 index 0000000..6d21536 --- /dev/null +++ b/xsd/libxsd/xsd/cxx/tree/serialization/double.hxx @@ -0,0 +1,96 @@ +// file : xsd/cxx/tree/serialization/double.hxx +// author : Boris Kolpackov <boris@codesynthesis.com> +// copyright : Copyright (c) 2005-2010 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef XSD_CXX_TREE_SERIALIZATION_DOUBLE_HXX +#define XSD_CXX_TREE_SERIALIZATION_DOUBLE_HXX + +#include <limits> // std::numeric_limits +#include <locale> +#include <sstream> + +#include <xsd/cxx/tree/bits/literals.hxx> + +namespace xsd +{ + namespace cxx + { + namespace tree + { + namespace bits + { + // The formula for the number of decimla digits required is given in: + // + // http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1822.pdf + // + template <typename C> + std::basic_string<C> + insert (const as_double<double>& d) + { + std::basic_string<C> r; + + if (d.x == std::numeric_limits<double>::infinity ()) + r = bits::positive_inf<C> (); + else if (d.x == -std::numeric_limits<double>::infinity ()) + r = bits::negative_inf<C> (); + else if (!(d.x == d.x)) + r = bits::nan<C> (); + else + { + std::basic_ostringstream<C> os; + os.imbue (std::locale::classic ()); + + // Precision. + // +#if defined (XSD_CXX_TREE_DOUBLE_PRECISION_MAX) + os.precision (2 + std::numeric_limits<double>::digits * 301/1000); +#elif defined (XSD_CXX_TREE_DOUBLE_PRECISION) + os.precision (XSD_CXX_TREE_DOUBLE_PRECISION); +#else + os.precision (std::numeric_limits<double>::digits10); +#endif + // Format. + // +#if defined (XSD_CXX_TREE_DOUBLE_FIXED) + os << std::fixed << d.x; +#elif defined (XSD_CXX_TREE_DOUBLE_SCIENTIFIC) + os << std::scientific << d.x; +#else + os << d.x; +#endif + r = os.str (); + } + + return r; + } + } + + template <typename C> + inline void + operator<< (list_stream<C>& ls, const as_double<double>& d) + { + ls.os_ << bits::insert<C> (d); + } + } + } +} + +namespace XERCES_CPP_NAMESPACE +{ + inline void + operator<< (xercesc::DOMElement& e, + const xsd::cxx::tree::as_double<double>& d) + { + e << xsd::cxx::tree::bits::insert<char> (d); + } + + inline void + operator<< (xercesc::DOMAttr& a, + const xsd::cxx::tree::as_double<double>& d) + { + a << xsd::cxx::tree::bits::insert<char> (d); + } +} + +#endif // XSD_CXX_TREE_SERIALIZATION_DOUBLE_HXX |