| 1 | // file : xsd/cxx/tree/serialization/double.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_DOUBLE_HXX |
|---|
| 7 | #define XSD_CXX_TREE_SERIALIZATION_DOUBLE_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, double d) |
|---|
| 23 | { |
|---|
| 24 | if (d == std::numeric_limits<double>::infinity ()) |
|---|
| 25 | e << "INF"; |
|---|
| 26 | else if (d == -std::numeric_limits<double>::infinity ()) |
|---|
| 27 | e << "-INF"; |
|---|
| 28 | else if (!(d == d)) |
|---|
| 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<double>::digits * 301/1000); |
|---|
| 37 | #else |
|---|
| 38 | os.precision (std::numeric_limits<double>::digits10); |
|---|
| 39 | #endif |
|---|
| 40 | |
|---|
| 41 | // We map both xsd:double and xsd:decimal to double and decimal |
|---|
| 42 | // cannot be in scientific notation. @@ Plus decimal cannot be |
|---|
| 43 | // INF/NaN which is not handled at the moment. |
|---|
| 44 | // |
|---|
| 45 | os << std::fixed << d; |
|---|
| 46 | std::string s (os.str ()); |
|---|
| 47 | |
|---|
| 48 | // Remove the trailing zeros and the decimal point if necessary. |
|---|
| 49 | // |
|---|
| 50 | std::string::size_type size (s.size ()), n (size); |
|---|
| 51 | |
|---|
| 52 | for (; n > 0 && s[n - 1] == '0'; --n); |
|---|
| 53 | |
|---|
| 54 | if (n > 0 && s[n - 1] == '.') |
|---|
| 55 | --n; |
|---|
| 56 | |
|---|
| 57 | if (n != size) |
|---|
| 58 | s.resize (n); |
|---|
| 59 | |
|---|
| 60 | e << s; |
|---|
| 61 | } |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | inline void |
|---|
| 65 | operator<< (xercesc::DOMAttr& a, double d) |
|---|
| 66 | { |
|---|
| 67 | if (d == std::numeric_limits<double>::infinity ()) |
|---|
| 68 | a << "INF"; |
|---|
| 69 | else if (d == -std::numeric_limits<double>::infinity ()) |
|---|
| 70 | a << "-INF"; |
|---|
| 71 | else if (!(d == d)) |
|---|
| 72 | a << "NaN"; |
|---|
| 73 | else |
|---|
| 74 | { |
|---|
| 75 | std::basic_ostringstream<char> os; |
|---|
| 76 | os.imbue (std::locale::classic ()); |
|---|
| 77 | |
|---|
| 78 | #ifdef XSD_FP_ALL_DIGITS |
|---|
| 79 | os.precision (2 + std::numeric_limits<double>::digits * 301/1000); |
|---|
| 80 | #else |
|---|
| 81 | os.precision (std::numeric_limits<double>::digits10); |
|---|
| 82 | #endif |
|---|
| 83 | |
|---|
| 84 | // We map both xsd:double and xsd:decimal to double and decimal |
|---|
| 85 | // cannot be in scientific notation. |
|---|
| 86 | // |
|---|
| 87 | os << std::fixed << d; |
|---|
| 88 | std::string s (os.str ()); |
|---|
| 89 | |
|---|
| 90 | // Remove the trailing zeros and the decimal point if necessary. |
|---|
| 91 | // |
|---|
| 92 | std::string::size_type size (s.size ()), n (size); |
|---|
| 93 | |
|---|
| 94 | for (; n > 0 && s[n - 1] == '0'; --n); |
|---|
| 95 | |
|---|
| 96 | if (n > 0 && s[n - 1] == '.') |
|---|
| 97 | --n; |
|---|
| 98 | |
|---|
| 99 | if (n != size) |
|---|
| 100 | s.resize (n); |
|---|
| 101 | |
|---|
| 102 | a << s; |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | namespace xsd |
|---|
| 108 | { |
|---|
| 109 | namespace cxx |
|---|
| 110 | { |
|---|
| 111 | namespace tree |
|---|
| 112 | { |
|---|
| 113 | template <typename C> |
|---|
| 114 | inline void |
|---|
| 115 | operator<< (list_stream<C>& ls, double d) |
|---|
| 116 | { |
|---|
| 117 | if (d == std::numeric_limits<double>::infinity ()) |
|---|
| 118 | ls.os_ << bits::positive_inf<C> (); |
|---|
| 119 | else if (d == -std::numeric_limits<double>::infinity ()) |
|---|
| 120 | ls.os_ << bits::negative_inf<C> (); |
|---|
| 121 | else if (!(d == d)) |
|---|
| 122 | ls.os_ << bits::nan<C> (); |
|---|
| 123 | else |
|---|
| 124 | { |
|---|
| 125 | std::basic_ostringstream<C> os; |
|---|
| 126 | os.imbue (std::locale::classic ()); |
|---|
| 127 | |
|---|
| 128 | #ifdef XSD_FP_ALL_DIGITS |
|---|
| 129 | os.precision (2 + std::numeric_limits<double>::digits * 301/1000); |
|---|
| 130 | #else |
|---|
| 131 | os.precision (std::numeric_limits<double>::digits10); |
|---|
| 132 | #endif |
|---|
| 133 | |
|---|
| 134 | // We map both xsd:double and xsd:decimal to double and decimal |
|---|
| 135 | // cannot be in scientific notation. @@ Plus decimal cannot be |
|---|
| 136 | // INF/NaN which is not handled at the moment. |
|---|
| 137 | // |
|---|
| 138 | os << std::fixed << d; |
|---|
| 139 | std::basic_string<C> s (os.str ()); |
|---|
| 140 | |
|---|
| 141 | // Remove the trailing zeros and the decimal point if necessary. |
|---|
| 142 | // |
|---|
| 143 | typename std::basic_string<C>::size_type size (s.size ()), n (size); |
|---|
| 144 | |
|---|
| 145 | for (; n > 0 && s[n - 1] == '0'; --n); |
|---|
| 146 | |
|---|
| 147 | if (n > 0 && s[n - 1] == '.') |
|---|
| 148 | --n; |
|---|
| 149 | |
|---|
| 150 | if (n != size) |
|---|
| 151 | s.resize (n); |
|---|
| 152 | |
|---|
| 153 | ls.os_ << s; |
|---|
| 154 | } |
|---|
| 155 | } |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | #endif // XSD_CXX_TREE_SERIALIZATION_DOUBLE_HXX |
|---|