aboutsummaryrefslogtreecommitdiff
path: root/libxsde/xsde/cxx/hybrid/any-type.cxx
blob: 09ce62a5b81c60ea1eccb96ca8358beb6c9de649 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// file      : xsde/cxx/hybrid/any-type.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#include <xsde/cxx/config.hxx>

#ifdef XSDE_CUSTOM_ALLOCATOR
#  include <xsde/cxx/allocator.hxx>
#endif

#include <xsde/cxx/hybrid/any-type.hxx>

namespace xsde
{
  namespace cxx
  {
    namespace hybrid
    {
      // any_type
      //
      any_type::
      ~any_type ()
      {
#ifndef XSDE_CUSTOM_ALLOCATOR
        delete data_;
#else
        if (data_ != 0)
        {
          data_->~data_sequence ();
          cxx::free (data_);
        }
#endif
      }

#ifdef XSDE_EXCEPTIONS
      void any_type::
      allocate_custom_data ()
      {
        if (data_ != 0)
          return;

#ifndef XSDE_CUSTOM_ALLOCATOR
        data_ = new data_sequence;
#else
        // Default data_sequence c-tor cannot throw so we don't need a guard.
        //
        data_ = static_cast<data_sequence*> (
          cxx::alloc (sizeof (data_sequence)));
        new (data_) data_sequence;
#endif
      }

      void any_type::
      _copy (any_type& c) const
      {
        if (data_ != 0)
        {
          c.allocate_custom_data ();
          data_->copy (c.custom_data ());
        }
      }

      struct any_type_guard
      {
        any_type_guard (any_type* p) : p_ (p) {}

        ~any_type_guard ()
        {
#ifndef XSDE_CUSTOM_ALLOCATOR
          delete p_;
#else
          if (p_ != 0)
          {
            p_->~any_type ();
            cxx::free (p_);
          }
#endif
        }

        void
        release () { p_ = 0; }

      private:
        any_type* p_;
      };

      any_type* any_type::
      _clone () const
      {
#ifndef XSDE_CUSTOM_ALLOCATOR
        any_type* c = new any_type;
#else
        // Default any_type c-tor cannot throw so we don't need alloc_guard.
        //
        any_type* c = static_cast<any_type*> (cxx::alloc (sizeof (any_type)));
        new (c) any_type;
#endif
        any_type_guard g (c);
        _copy (*c);
        g.release ();
        return c;
      }
#else
      bool any_type::
      allocate_custom_data ()
      {
        if (data_ != 0)
          return true;

#ifndef XSDE_CUSTOM_ALLOCATOR
        data_ = new data_sequence;
#else
        data_ = static_cast<data_sequence*> (
          cxx::alloc (sizeof (data_sequence)));
#endif
        if (data_ == 0)
          return false;

#ifdef XSDE_CUSTOM_ALLOCATOR
        new (data_) data_sequence;
#endif
        return true;
      }

      bool any_type::
      _copy (any_type& c) const
      {
        if (data_ != 0)
        {
          if (!c.allocate_custom_data () ||
              !data_->copy (c.custom_data ()))
            return false;
        }

        return true;
      }

      any_type* any_type::
      _clone () const
      {
#ifndef XSDE_CUSTOM_ALLOCATOR
        any_type* c = new any_type;
#else
        any_type* c = static_cast<any_type*> (cxx::alloc (sizeof (any_type)));
#endif

        if (c == 0)
          return 0;

#ifdef XSDE_CUSTOM_ALLOCATOR
        new (c) any_type;
#endif

        if (!_copy (*c))
        {
#ifndef XSDE_CUSTOM_ALLOCATOR
          delete c;
#else
          c->~any_type ();
          cxx::free (c);
#endif
          return 0;
        }

        return c;
      }
#endif

#ifdef XSDE_POLYMORPHIC
#ifdef XSDE_STL
      const std::string& any_type::
      _dynamic_type () const
      {
        return _static_type ();
      }

      static const std::string any_type_static_type_ =
        "anyType http://www.w3.org/2001/XMLSchema";

      const std::string& any_type::
      _static_type ()
      {
        return any_type_static_type_;
      }
#else
      const char* any_type::
      _dynamic_type () const
      {
        return _static_type ();
      }

      const char* any_type::
      _static_type ()
      {
        return "anyType http://www.w3.org/2001/XMLSchema";
      }
#endif
#endif
    }
  }
}