aboutsummaryrefslogtreecommitdiff
path: root/libxsd/xsd/cxx/tree/buffer.txx
blob: 201fcbb0d973d76c4fa609729b7b05c06a1fcfdd (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
// file      : xsd/cxx/tree/buffer.txx
// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

namespace xsd
{
  namespace cxx
  {
    namespace tree
    {
      template <typename C>
      buffer<C>::
      buffer (size_t size)
      {
        capacity (size);
        size_ = size;
      }

      template <typename C>
      buffer<C>::
      buffer (size_t size, size_t capacity)
      {
        if (size > capacity)
          throw bounds<C> ();

        this->capacity (capacity);
        size_ = size;
      }

      template <typename C>
      buffer<C>::
      buffer (const void* data, size_t size)
      {
        capacity (size);
        size_ = size;

        if (size_)
          std::memcpy (data_, data, size_);
      }

      template <typename C>
      buffer<C>::
      buffer (const void* data, size_t size, size_t capacity)
      {
        if (size > capacity)
          throw bounds<C> ();

        this->capacity (capacity);
        size_ = size;

        if (size_)
          std::memcpy (data_, data, size_);
      }

      template <typename C>
      buffer<C>::
      buffer (void* data, size_t size, size_t capacity, bool own)
      {
        if (size > capacity)
          throw bounds<C> ();

        data_ = reinterpret_cast<char*> (data);
        size_ = size;
        capacity_ = capacity;
        free_ = own;
      }

      template <typename C>
      buffer<C>::
      buffer (const buffer& other)
          : buffer_base ()
      {
        capacity (other.capacity_);
        size_ = other.size_;

        if (size_)
          std::memcpy (data_, other.data_, size_);
      }

      template <typename C>
      buffer<C>& buffer<C>::
      operator= (const buffer& other)
      {
        if (this != &other)
        {
          capacity (other.capacity_, false);
          size_ = other.size_;

          if (size_)
            std::memcpy (data_, other.data_, size_);
        }

        return *this;
      }

      template <typename C>
      void buffer<C>::
      swap (buffer& other)
      {
        char* tmp_data (data_);
        size_t tmp_size (size_);
        size_t tmp_capacity (capacity_);
        bool tmp_free (free_);

        data_ = other.data_;
        size_ = other.size_;
        capacity_ = other.capacity_;
        free_ = other.free_;

        other.data_ = tmp_data;
        other.size_ = tmp_size;
        other.capacity_ = tmp_capacity;
        other.free_ = tmp_free;
      }

      template <typename C>
      bool buffer<C>::
      capacity (size_t capacity, bool copy)
      {
        if (size_ > capacity)
          throw bounds<C> ();

        if (capacity <= capacity_)
        {
          return false; // Do nothing if shrinking is requested.
        }
        else
        {
          char* data (reinterpret_cast<char*> (operator new (capacity)));

          if (copy && size_ > 0)
            std::memcpy (data, data_, size_);

          if (free_ && data_)
            operator delete (data_);

          data_ = data;
          capacity_ = capacity;
          free_ = true;

          return true;
        }
      }
    }
  }
}