| 1 | // file : xsd/cxx/tree/serialization/float.hxx |
|---|
| 2 | // author : Boris Kolpackov <boris@codesynthesis.com> |
|---|
| 3 | // copyright : Copyright (c) 2005-2008 Code Synthesis Tools CC |
|---|
| 4 | // license : GNU GPL v2 + exceptions; see accompanying LICENSE file |
|---|
| 5 | |
|---|
| 6 | #ifndef XSD_CXX_TREE_SERIALIZATION_FLOAT_HXX |
|---|
| 7 | #define XSD_CXX_TREE_SERIALIZATION_FLOAT_HXX |
|---|
| 8 | |
|---|
| 9 | #include <limits> // std::numeric_limits |
|---|
| 10 | #include <locale> |
|---|
| 11 | #include <sstream> |
|---|
| 12 | |
|---|
| 13 | #include <xsd/cxx/tree/bits/literals.hxx> |
|---|
| 14 | |
|---|
| 15 | // The formula for the number of decimla digits required is given in: |
|---|
| 16 | // |
|---|
| 17 | // http://www2.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1822.pdf |
|---|
| 18 | // |
|---|
| 19 | namespace XERCES_CPP_NAMESPACE |
|---|
| 20 | { |
|---|
| 21 | inline void |
|---|
| 22 | operator<< (xercesc::DOMElement& e, float f) |
|---|
| 23 | { |
|---|
| 24 | if (f == std::numeric_limits<float>::infinity ()) |
|---|
| 25 | e << "INF"; |
|---|
| 26 | else if (f == -std::numeric_limits<float>::infinity ()) |
|---|
| 27 | e << "-INF"; |
|---|
| 28 | else if (!(f == f)) |
|---|
| 29 | e << "NaN"; |
|---|
| 30 | else |
|---|
| 31 | { |
|---|
| 32 | std::basic_ostringstream<char> os; |
|---|
| 33 | os.imbue (std::locale::classic ()); |
|---|
| 34 | |
|---|
| 35 | #ifdef XSD_FP_ALL_DIGITS |
|---|
| 36 | os.precision (2 + std::numeric_limits<float>::digits * 301/1000); |
|---|
| 37 | #else |
|---|
| 38 | os.precision (std::numeric_limits<float>::digits10); |
|---|
| 39 | #endif |
|---|
| 40 | |
|---|
| 41 | os << f; |
|---|
| 42 | e << os.str (); |
|---|
| 43 | } |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | inline void |
|---|
| 47 | operator<< (xercesc::DOMAttr& a, float f) |
|---|
| 48 | { |
|---|
| 49 | if (f == std::numeric_limits<float>::infinity ()) |
|---|
| 50 | a << "INF"; |
|---|
| 51 | else if (f == -std::numeric_limits<float>::infinity ()) |
|---|
| 52 | a << "-INF"; |
|---|
| 53 | else if (!(f == f)) |
|---|
| 54 | a << "NaN"; |
|---|
| 55 | else |
|---|
| 56 | { |
|---|
| 57 | std::basic_ostringstream<char> os; |
|---|
| 58 | os.imbue (std::locale::classic ()); |
|---|
| 59 | |
|---|
| 60 | #ifdef XSD_FP_ALL_DIGITS |
|---|
| 61 | os.precision (2 + std::numeric_limits<float>::digits * 301/1000); |
|---|
| 62 | #else |
|---|
| 63 | os.precision (std::numeric_limits<float>::digits10); |
|---|
| 64 | #endif |
|---|
| 65 | |
|---|
| 66 | os << f; |
|---|
| 67 | a << os.str (); |
|---|
| 68 | } |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | namespace xsd |
|---|
| 73 | { |
|---|
| 74 | namespace cxx |
|---|
| 75 | { |
|---|
| 76 | namespace tree |
|---|
| 77 | { |
|---|
| 78 | template <typename C> |
|---|
| 79 | inline void |
|---|
| 80 | operator<< (list_stream<C>& ls, float f) |
|---|
| 81 | { |
|---|
| 82 | if (f == std::numeric_limits<float>::infinity ()) |
|---|
| 83 | ls.os_ << bits::positive_inf<C> (); |
|---|
| 84 | else if (f == -std::numeric_limits<float>::infinity ()) |
|---|
| 85 | ls.os_ << bits::negative_inf<C> (); |
|---|
| 86 | else if (!(f == f)) |
|---|
| 87 | ls.os_ << bits::nan<C> (); |
|---|
| 88 | else |
|---|
| 89 | { |
|---|
| 90 | // We don't need to restore the original locale or precision |
|---|
| 91 | // because items in the list are all of the same type. |
|---|
| 92 | // |
|---|
| 93 | ls.os_.imbue (std::locale::classic ()); |
|---|
| 94 | |
|---|
| 95 | #ifdef XSD_FP_ALL_DIGITS |
|---|
| 96 | ls.os_.precision (2 + std::numeric_limits<float>::digits * 301/1000); |
|---|
| 97 | #else |
|---|
| 98 | ls.os_.precision (std::numeric_limits<float>::digits10); |
|---|
| 99 | #endif |
|---|
| 100 | ls.os_ << f; |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | #endif // XSD_CXX_TREE_SERIALIZATION_FLOAT_HXX |
|---|