Line data Source code
1 : // file : xsd/cxx/tree/buffer.txx
2 : // copyright : Copyright (c) 2005-2017 Code Synthesis Tools CC
3 : // license : GNU GPL v2 + exceptions; see accompanying LICENSE file
4 :
5 : namespace xsd
6 : {
7 : namespace cxx
8 : {
9 : namespace tree
10 : {
11 : template <typename C>
12 0 : buffer<C>::
13 0 : buffer (size_t size)
14 : {
15 0 : capacity (size);
16 0 : size_ = size;
17 0 : }
18 :
19 : template <typename C>
20 : buffer<C>::
21 : buffer (size_t size, size_t capacity)
22 : {
23 : if (size > capacity)
24 : throw bounds<C> ();
25 :
26 : this->capacity (capacity);
27 : size_ = size;
28 : }
29 :
30 : template <typename C>
31 0 : buffer<C>::
32 0 : buffer (const void* data, size_t size)
33 : {
34 0 : capacity (size);
35 0 : size_ = size;
36 :
37 0 : if (size_)
38 0 : std::memcpy (data_, data, size_);
39 0 : }
40 :
41 : template <typename C>
42 : buffer<C>::
43 : buffer (const void* data, size_t size, size_t capacity)
44 : {
45 : if (size > capacity)
46 : throw bounds<C> ();
47 :
48 : this->capacity (capacity);
49 : size_ = size;
50 :
51 : if (size_)
52 : std::memcpy (data_, data, size_);
53 : }
54 :
55 : template <typename C>
56 0 : buffer<C>::
57 0 : buffer (void* data, size_t size, size_t capacity, bool own)
58 : {
59 0 : if (size > capacity)
60 0 : throw bounds<C> ();
61 :
62 0 : data_ = reinterpret_cast<char*> (data);
63 0 : size_ = size;
64 0 : capacity_ = capacity;
65 0 : free_ = own;
66 0 : }
67 :
68 : template <typename C>
69 0 : buffer<C>::
70 : buffer (const buffer& other)
71 0 : : buffer_base ()
72 : {
73 0 : capacity (other.capacity_);
74 0 : size_ = other.size_;
75 :
76 0 : if (size_)
77 0 : std::memcpy (data_, other.data_, size_);
78 0 : }
79 :
80 : template <typename C>
81 0 : buffer<C>& buffer<C>::
82 : operator= (const buffer& other)
83 : {
84 0 : if (this != &other)
85 : {
86 0 : capacity (other.capacity_, false);
87 0 : size_ = other.size_;
88 :
89 0 : if (size_)
90 0 : std::memcpy (data_, other.data_, size_);
91 : }
92 :
93 0 : return *this;
94 : }
95 :
96 : template <typename C>
97 0 : void buffer<C>::
98 : swap (buffer& other)
99 : {
100 0 : char* tmp_data (data_);
101 0 : size_t tmp_size (size_);
102 0 : size_t tmp_capacity (capacity_);
103 0 : bool tmp_free (free_);
104 :
105 0 : data_ = other.data_;
106 0 : size_ = other.size_;
107 0 : capacity_ = other.capacity_;
108 0 : free_ = other.free_;
109 :
110 0 : other.data_ = tmp_data;
111 0 : other.size_ = tmp_size;
112 0 : other.capacity_ = tmp_capacity;
113 0 : other.free_ = tmp_free;
114 0 : }
115 :
116 : template <typename C>
117 0 : bool buffer<C>::
118 : capacity (size_t capacity, bool copy)
119 : {
120 0 : if (size_ > capacity)
121 0 : throw bounds<C> ();
122 :
123 0 : if (capacity <= capacity_)
124 : {
125 0 : return false; // Do nothing if shrinking is requested.
126 : }
127 : else
128 : {
129 0 : char* data (reinterpret_cast<char*> (operator new (capacity)));
130 :
131 0 : if (copy && size_ > 0)
132 0 : std::memcpy (data, data_, size_);
133 :
134 0 : if (free_ && data_)
135 0 : operator delete (data_);
136 :
137 0 : data_ = data;
138 0 : capacity_ = capacity;
139 0 : free_ = true;
140 :
141 0 : return true;
142 : }
143 : }
144 : }
145 : }
146 : }
|