aboutsummaryrefslogtreecommitdiff
path: root/examples/cxx/tree/custom/double/double-custom.cxx
blob: 6aad908218f00a3a98f82941e4eeb994e702b266 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// file      : examples/cxx/tree/custom/double/double-custom.cxx
// copyright : not copyrighted - public domain

// Include xml-schema.hxx instead of double-custom.hxx here.
//
#include "xml-schema.hxx"

#include <limits>
#include <locale>
#include <sstream>

#include <xsd/cxx/ro-string.hxx>
#include <xsd/cxx/zc-istream.hxx>

using namespace std;

// Parsing.
//
namespace xsd
{
  namespace cxx
  {
    namespace tree
    {
      double traits<double, char, schema_type::double_>::
      create (const std::string& s,
              const xercesc::DOMElement*,
              flags,
              type*)
      {
        // This type cannot have whitespaces in its values. As result we
        // don't need to waste time collapsing whitespaces. All we need to
        // do is trim the string representation which can be done without
        // copying.
        //
        ro_string<char> tmp (s);
        trim (tmp);

        zc_istream<char> is (tmp);
        is.imbue (locale::classic ());

        double t;
        is >> t;

        return t;
      }
    }
  }
}

// Serialization.
//
namespace XERCES_CPP_NAMESPACE
{
  void
  operator<< (xercesc::DOMElement& e, const xml_schema::as_double& d)
  {
    ostringstream os;
    os.imbue (locale::classic ());

    os.precision (2);
    os << fixed << d.x;

    e << os.str ();
  }

  void
  operator<< (xercesc::DOMAttr& a, const xml_schema::as_double& d)
  {
    ostringstream os;
    os.imbue (locale::classic ());

    os.precision (2);
    os << fixed << d.x;

    a << os.str ();
  }
}

namespace xsd
{
  namespace cxx
  {
    namespace tree
    {
      void
      operator<< (xml_schema::list_stream& ls,
                  const xml_schema::as_double& d)
      {
        ls.os_.imbue (locale::classic ());
        ls.os_.precision (2);
        ls.os_ << fixed << d.x;
      }
    }
  }
}