summaryrefslogtreecommitdiff
path: root/libxsd/xsd/cxx/tree/buffer.hxx
blob: 45a1a82fc4dc13f8dc40af64260f2202c26d86e2 (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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
// file      : xsd/cxx/tree/buffer.hxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2005-2010 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

/**
 * @file
 *
 * @brief Contains a simple binary buffer abstraction that is used to
 * implement the base64Binary and hexBinary XML Schema built-in types.
 *
 * This is an internal header and is included by the generated code. You
 * normally should not include it directly.
 *
 */

#ifndef XSD_CXX_TREE_BUFFER_HXX
#define XSD_CXX_TREE_BUFFER_HXX

#include <new>     // operator new/delete
#include <cstddef> // std::size_t
#include <cstring> // std::memcpy, std::memcmp

#include <xsd/cxx/tree/exceptions.hxx>

namespace xsd
{
  namespace cxx
  {
    /**
     * @brief C++/Tree mapping runtime namespace.
     *
     * This is an internal namespace and normally should not be referenced
     * directly. Instead you should use the aliases for types in this
     * namespaces that are created in the generated code.
     *
     */
    namespace tree
    {
      //@cond

      class buffer_base
      {
      protected:
        virtual
        ~buffer_base ()
        {
          if (data_)
            operator delete (data_);
        }

        buffer_base ()
            : data_ (0), size_ (0), capacity_ (0)
        {
        }

      protected:
        char* data_;
        size_t size_;
        size_t capacity_;
      };

      //@endcond

      /**
       * @brief Simple binary %buffer abstraction
       *
       * The %buffer class manages a continuous binary %buffer. The base
       * concepts are data (actual memory region), size (the portion of
       * the %buffer that contains useful information), and capacity (the
       * actual size of the underlying memory region). The bounds
       * %exception is thrown from the constructors and modifier functions
       * if the (size <= capacity) constraint is violated.
       *
       * Note that the template parameter is only used to instantiate
       * %exception types. The underlying %buffer type is always @c char.
       *
       * @nosubgrouping
       */
      template<typename C>
      class buffer: protected buffer_base
      {
      public:
        /**
         * @brief Size type
         */
        typedef std::size_t size_t;

      public:
        /**
         * @name Constructors
         */
        //@{

        /**
         * @brief Allocate a %buffer of the specified size.
         *
         * The resulting %buffer has the same size and capacity.
         *
         * @param size A %buffer size in bytes.
         */
        explicit
        buffer (size_t size = 0);

        /**
         * @brief Allocate a %buffer of the specified size and capacity.
         *
         * @param size A %buffer size in bytes.
         * @param capacity A %buffer capacity in bytes.
         * @throw bounds If @a size exceeds @a capacity
         */
        buffer (size_t size, size_t capacity);

        /**
         * @brief Allocate a %buffer of the specified size and copy
         * the data.
         *
         * The resulting %buffer has the same size and capacity with
         * @a size bytes copied from @a data.
         *
         * @param data A %buffer to copy the data from.
         * @param size A %buffer size in bytes.
         */
        buffer (const void* data, size_t size);

        /**
         * @brief Allocate a %buffer of the specified size and capacity
         * and copy the data.
         *
         * @a size bytes are copied from @a data to the resulting
         * %buffer.
         *
         * @param data A %buffer to copy the data from.
         * @param size A %buffer size in bytes.
         * @param capacity A %buffer capacity in bytes.
         * @throw bounds If @a size exceeds @a capacity
         */
        buffer (const void* data, size_t size, size_t capacity);

        /**
         * @brief Assume ownership of the specified %buffer.
         *
         * If the @a assume_ownership argument is true, the %buffer will
         * assume ownership of @a data and will release the memory
         * by calling @c operator @c delete().
         *
         * @param data A %buffer to assume ownership of.
         * @param size A %buffer size in bytes.
         * @param capacity A %buffer capacity in bytes.
         * @param assume_ownership A boolean value indication whether to
         * assume ownership.
         * @throw bounds If @a size exceeds @a capacity
         */
        buffer (void* data,
                size_t size,
                size_t capacity,
                bool assume_ownership);

        /**
         * @brief Copy constructor.
         *
         * The copy constructor performs a deep copy of the underlying
         * memory %buffer.
         *
         * @param x An instance to make a copy of.
         */
        buffer (const buffer& x);

        //@}

      public:
        /**
         * @brief Copy assignment operator.
         *
         * The copy assignment operator changes the buffer's capacity
         * to @c x.capacity() and copies @c x.size() bytes from @a x.
         *
         * @param x An instance to assign.
         * @return A reference to the instance.
         */
        buffer&
        operator= (const buffer& x);

      public:
        /**
         * @brief Get buffer's capacity.
         *
         * @return A number of bytes that the %buffer can hold without
         * reallocation.
         */
        size_t
        capacity () const
	{
	  return capacity_;
	}

        /**
         * @brief Set buffer's capacity.
         *
         * @param c The new capacity in bytes.
         * @return True if the underlying %buffer has moved, false otherwise.
         */
        bool
        capacity (size_t c)
        {
          return this->capacity (c, true);
        }

      public:
        /**
         * @brief Get buffer's size.
         *
         * @return A number of bytes that the %buffer holds.
         */
        size_t
        size () const {return size_;}

        /**
         * @brief Set buffer's size.
         *
         * @param s The new size in bytes.
         * @return True if the underlying %buffer has moved, false otherwise.
         */
        bool
        size (size_t s)
        {
          bool r (false);

          if (s > capacity_)
            r = capacity (s);

          size_ = s;

          return r;
        }

      public:
        /**
         * @brief Get the underlying memory region.
         *
         * @return A constant pointer to the underlying memory region.
         */
        const char*
        data () const {return data_;}

        /**
         * @brief Get the underlying memory region.
         *
         * @return A pointer to the underlying memory region.
         */
        char*
        data () {return data_;}

        /**
         * @brief Get the beginning of the underlying memory region.
         *
         * @return A constant pointer to the first byte of the underlying
         * memory region.
         */
        const char*
        begin () const {return data_;}

        /**
         * @brief Get the beginning of the underlying memory region.
         *
         * @return A pointer to the first byte of the underlying memory
         * region.
         */
        char*
        begin () {return data_;}

        /**
         * @brief Get the end of the underlying memory region.
         *
         * @return A constant pointer to the one past last byte of the
         * underlying memory region (that is @c %begin() @c + @c %size() ).
         */
        const char*
        end () const {return data_ + size_;}

        /**
         * @brief Get the end of the underlying memory region.
         *
         * @return A pointer to the one past last byte of the underlying
         * memory region (that is @c %begin() @c + @c %size() ).
         */
        char*
        end () {return data_ + size_;}

      public:
        /**
         * @brief Swap data with another %buffer.
         *
         * @param x A %buffer to swap with.
         */
        void
        swap (buffer& x);

      private:
        bool
        capacity (size_t capacity, bool copy);
      };

      /**
       * @brief %buffer comparison operator.
       *
       * @return True if the buffers have the same sizes and the same
       * data.
       */
      template <typename C>
      inline bool
      operator== (const buffer<C>& a, const buffer<C>& b)
      {
        return a.size () == b.size () &&
          std::memcmp (a.data (), b.data (), a.size ()) == 0;
      }

      /**
       * @brief %buffer comparison operator.
       *
       * @return True if the buffers have different sizes or different
       * data.
       */
      template <typename C>
      inline bool
      operator!= (const buffer<C>& a, const buffer<C>& b)
      {
        return !(a == b);
      }
    }
  }
}

#include <xsd/cxx/tree/buffer.txx>

#endif  // XSD_CXX_TREE_BUFFER_HXX