From f0510d2f90467de8e8f260b47d79a9baaf9bef17 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Thu, 17 Sep 2009 07:15:29 +0200 Subject: Start tracking XSD with git --- libxsd/xsd/cxx/tree/serialization/float.hxx | 94 +++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 libxsd/xsd/cxx/tree/serialization/float.hxx (limited to 'libxsd/xsd/cxx/tree/serialization/float.hxx') diff --git a/libxsd/xsd/cxx/tree/serialization/float.hxx b/libxsd/xsd/cxx/tree/serialization/float.hxx new file mode 100644 index 0000000..f5d8a1f --- /dev/null +++ b/libxsd/xsd/cxx/tree/serialization/float.hxx @@ -0,0 +1,94 @@ +// file : xsd/cxx/tree/serialization/float.hxx +// author : Boris Kolpackov +// copyright : Copyright (c) 2005-2009 Code Synthesis Tools CC +// license : GNU GPL v2 + exceptions; see accompanying LICENSE file + +#ifndef XSD_CXX_TREE_SERIALIZATION_FLOAT_HXX +#define XSD_CXX_TREE_SERIALIZATION_FLOAT_HXX + +#include // std::numeric_limits +#include +#include + +#include + +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 + std::basic_string + insert (float f) + { + std::basic_string r; + + if (f == std::numeric_limits::infinity ()) + r = bits::positive_inf (); + else if (f == -std::numeric_limits::infinity ()) + r = bits::negative_inf (); + else if (!(f == f)) + r = bits::nan (); + else + { + std::basic_ostringstream os; + os.imbue (std::locale::classic ()); + + // Precision. + // +#if defined (XSD_CXX_TREE_FLOAT_PRECISION_MAX) + os.precision (2 + std::numeric_limits::digits * 301/1000); +#elif defined (XSD_CXX_TREE_FLOAT_PRECISION) + os.precision (XSD_CXX_TREE_FLOAT_PRECISION); +#else + os.precision (std::numeric_limits::digits10); +#endif + // Format. + // +#if defined (XSD_CXX_TREE_FLOAT_FIXED) + os << std::fixed << f; +#elif defined (XSD_CXX_TREE_FLOAT_SCIENTIFIC) + os << std::scientific << f; +#else + os << f; +#endif + r = os.str (); + } + + return r; + } + } + + template + inline void + operator<< (list_stream& ls, float f) + { + ls.os_ << bits::insert (f); + } + } + } +} + +namespace XERCES_CPP_NAMESPACE +{ + inline void + operator<< (xercesc::DOMElement& e, float f) + { + e << xsd::cxx::tree::bits::insert (f); + } + + inline void + operator<< (xercesc::DOMAttr& a, float f) + { + a << xsd::cxx::tree::bits::insert (f); + } +} + +#endif // XSD_CXX_TREE_SERIALIZATION_FLOAT_HXX -- cgit v1.1