1 | // file : xsd/cxx/tree/buffer.hxx |
---|
2 | // author : Boris Kolpackov <boris@codesynthesis.com> |
---|
3 | // copyright : Copyright (c) 2005-2008 Code Synthesis Tools CC |
---|
4 | // license : GNU GPL v2 + exceptions; see accompanying LICENSE file |
---|
5 | |
---|
6 | /** |
---|
7 | * @file |
---|
8 | * |
---|
9 | * @brief Contains a simple binary buffer abstraction that is used to |
---|
10 | * implement the base64Binary and hexBinary XML Schema built-in types. |
---|
11 | * |
---|
12 | * This is an internal header and is included by the generated code. You |
---|
13 | * normally should not include it directly. |
---|
14 | * |
---|
15 | */ |
---|
16 | |
---|
17 | #ifndef XSD_CXX_TREE_BUFFER_HXX |
---|
18 | #define XSD_CXX_TREE_BUFFER_HXX |
---|
19 | |
---|
20 | #include <new> // operator new/delete |
---|
21 | #include <cstddef> // std::size_t |
---|
22 | #include <cstring> // std::memcpy, std::memcmp |
---|
23 | |
---|
24 | #include <xsd/cxx/tree/exceptions.hxx> |
---|
25 | |
---|
26 | namespace xsd |
---|
27 | { |
---|
28 | namespace cxx |
---|
29 | { |
---|
30 | /** |
---|
31 | * @brief C++/Tree mapping runtime namespace. |
---|
32 | * |
---|
33 | * This is an internal namespace and normally should not be referenced |
---|
34 | * directly. Instead you should use the aliases for types in this |
---|
35 | * namespaces that are created in the generated code. |
---|
36 | * |
---|
37 | */ |
---|
38 | namespace tree |
---|
39 | { |
---|
40 | //@cond |
---|
41 | |
---|
42 | class buffer_base |
---|
43 | { |
---|
44 | protected: |
---|
45 | virtual |
---|
46 | ~buffer_base () |
---|
47 | { |
---|
48 | operator delete (data_); |
---|
49 | } |
---|
50 | |
---|
51 | buffer_base () |
---|
52 | : data_ (0), size_ (0), capacity_ (0) |
---|
53 | { |
---|
54 | } |
---|
55 | |
---|
56 | protected: |
---|
57 | char* data_; |
---|
58 | size_t size_; |
---|
59 | size_t capacity_; |
---|
60 | }; |
---|
61 | |
---|
62 | //@endcond |
---|
63 | |
---|
64 | /** |
---|
65 | * @brief Simple binary %buffer abstraction |
---|
66 | * |
---|
67 | * The %buffer class manages a continuous binary %buffer. The base |
---|
68 | * concepts are data (actual memory region), size (the portion of |
---|
69 | * the %buffer that contains useful information), and capacity (the |
---|
70 | * actual size of the underlying memory region). The bounds |
---|
71 | * %exception is thrown from the constructors and modifier functions |
---|
72 | * if the (size <= capacity) constraint is violated. |
---|
73 | * |
---|
74 | * Note that the template parameter is only used to instantiate |
---|
75 | * %exception types. The underlying %buffer type is always @c char. |
---|
76 | * |
---|
77 | * @nosubgrouping |
---|
78 | */ |
---|
79 | template<typename C> |
---|
80 | class buffer: protected buffer_base |
---|
81 | { |
---|
82 | public: |
---|
83 | /** |
---|
84 | * @brief Size type |
---|
85 | */ |
---|
86 | typedef std::size_t size_t; |
---|
87 | |
---|
88 | public: |
---|
89 | /** |
---|
90 | * @name Constructors |
---|
91 | */ |
---|
92 | //@{ |
---|
93 | |
---|
94 | /** |
---|
95 | * @brief Allocate a %buffer of the specified size. |
---|
96 | * |
---|
97 | * The resulting %buffer has the same size and capacity. |
---|
98 | * |
---|
99 | * @param size A %buffer size in bytes. |
---|
100 | */ |
---|
101 | explicit |
---|
102 | buffer (size_t size = 0); |
---|
103 | |
---|
104 | /** |
---|
105 | * @brief Allocate a %buffer of the specified size and capacity. |
---|
106 | * |
---|
107 | * @param size A %buffer size in bytes. |
---|
108 | * @param capacity A %buffer capacity in bytes. |
---|
109 | * @throw bounds If @a size exceeds @a capacity |
---|
110 | */ |
---|
111 | buffer (size_t size, size_t capacity); |
---|
112 | |
---|
113 | /** |
---|
114 | * @brief Allocate a %buffer of the specified size and copy |
---|
115 | * the data. |
---|
116 | * |
---|
117 | * The resulting %buffer has the same size and capacity with |
---|
118 | * @a size bytes copied from @a data. |
---|
119 | * |
---|
120 | * @param data A %buffer to copy the data from. |
---|
121 | * @param size A %buffer size in bytes. |
---|
122 | */ |
---|
123 | buffer (const void* data, size_t size); |
---|
124 | |
---|
125 | /** |
---|
126 | * @brief Allocate a %buffer of the specified size and capacity |
---|
127 | * and copy the data. |
---|
128 | * |
---|
129 | * @a size bytes are copied from @a data to the resulting |
---|
130 | * %buffer. |
---|
131 | * |
---|
132 | * @param data A %buffer to copy the data from. |
---|
133 | * @param size A %buffer size in bytes. |
---|
134 | * @param capacity A %buffer capacity in bytes. |
---|
135 | * @throw bounds If @a size exceeds @a capacity |
---|
136 | */ |
---|
137 | buffer (const void* data, size_t size, size_t capacity); |
---|
138 | |
---|
139 | /** |
---|
140 | * @brief Assume ownership of the specified %buffer. |
---|
141 | * |
---|
142 | * If the @a assume_ownership argument is true, the %buffer will |
---|
143 | * assume ownership of @a data and will release the memory |
---|
144 | * by calling @c operator @c delete(). |
---|
145 | * |
---|
146 | * @param data A %buffer to assume ownership of. |
---|
147 | * @param size A %buffer size in bytes. |
---|
148 | * @param capacity A %buffer capacity in bytes. |
---|
149 | * @param assume_ownership A boolean value indication whether to |
---|
150 | * assume ownership. |
---|
151 | * @throw bounds If @a size exceeds @a capacity |
---|
152 | */ |
---|
153 | buffer (void* data, |
---|
154 | size_t size, |
---|
155 | size_t capacity, |
---|
156 | bool assume_ownership); |
---|
157 | |
---|
158 | /** |
---|
159 | * @brief Copy constructor. |
---|
160 | * |
---|
161 | * The copy constructor performs a deep copy of the underlying |
---|
162 | * memory %buffer. |
---|
163 | * |
---|
164 | * @param x An instance to make a copy of. |
---|
165 | */ |
---|
166 | buffer (const buffer& x); |
---|
167 | |
---|
168 | //@} |
---|
169 | |
---|
170 | public: |
---|
171 | /** |
---|
172 | * @brief Copy assignment operator. |
---|
173 | * |
---|
174 | * The copy assignment operator changes the buffer's capacity |
---|
175 | * to @c x.capacity() and copies @c x.size() bytes from @a x. |
---|
176 | * |
---|
177 | * @param x An instance to assign. |
---|
178 | * @return A reference to the instance. |
---|
179 | */ |
---|
180 | buffer& |
---|
181 | operator= (const buffer& x); |
---|
182 | |
---|
183 | public: |
---|
184 | /** |
---|
185 | * @brief Get buffer's capacity. |
---|
186 | * |
---|
187 | * @return A number of bytes that the %buffer can hold without |
---|
188 | * reallocation. |
---|
189 | */ |
---|
190 | size_t |
---|
191 | capacity () const |
---|
192 | { |
---|
193 | return capacity_; |
---|
194 | } |
---|
195 | |
---|
196 | /** |
---|
197 | * @brief Set buffer's capacity. |
---|
198 | * |
---|
199 | * @param c The new capacity in bytes. |
---|
200 | * @return True if the underlying %buffer has moved, false otherwise. |
---|
201 | */ |
---|
202 | bool |
---|
203 | capacity (size_t c) |
---|
204 | { |
---|
205 | return this->capacity (c, true); |
---|
206 | } |
---|
207 | |
---|
208 | public: |
---|
209 | /** |
---|
210 | * @brief Get buffer's size. |
---|
211 | * |
---|
212 | * @return A number of bytes that the %buffer holds. |
---|
213 | */ |
---|
214 | size_t |
---|
215 | size () const {return size_;} |
---|
216 | |
---|
217 | /** |
---|
218 | * @brief Set buffer's size. |
---|
219 | * |
---|
220 | * @param s The new size in bytes. |
---|
221 | * @return True if the underlying %buffer has moved, false otherwise. |
---|
222 | */ |
---|
223 | bool |
---|
224 | size (size_t s) |
---|
225 | { |
---|
226 | bool r (false); |
---|
227 | |
---|
228 | if (s > capacity_) |
---|
229 | r = capacity (s); |
---|
230 | |
---|
231 | size_ = s; |
---|
232 | |
---|
233 | return r; |
---|
234 | } |
---|
235 | |
---|
236 | public: |
---|
237 | /** |
---|
238 | * @brief Get the underlying memory region. |
---|
239 | * |
---|
240 | * @return A constant pointer to the underlying memory region. |
---|
241 | */ |
---|
242 | const char* |
---|
243 | data () const {return data_;} |
---|
244 | |
---|
245 | /** |
---|
246 | * @brief Get the underlying memory region. |
---|
247 | * |
---|
248 | * @return A pointer to the underlying memory region. |
---|
249 | */ |
---|
250 | char* |
---|
251 | data () {return data_;} |
---|
252 | |
---|
253 | /** |
---|
254 | * @brief Get the beginning of the underlying memory region. |
---|
255 | * |
---|
256 | * @return A constant pointer to the first byte of the underlying |
---|
257 | * memory region. |
---|
258 | */ |
---|
259 | const char* |
---|
260 | begin () const {return data_;} |
---|
261 | |
---|
262 | /** |
---|
263 | * @brief Get the beginning of the underlying memory region. |
---|
264 | * |
---|
265 | * @return A pointer to the first byte of the underlying memory |
---|
266 | * region. |
---|
267 | */ |
---|
268 | char* |
---|
269 | begin () {return data_;} |
---|
270 | |
---|
271 | /** |
---|
272 | * @brief Get the end of the underlying memory region. |
---|
273 | * |
---|
274 | * @return A constant pointer to the one past last byte of the |
---|
275 | * underlying memory region (that is @c %begin() @c + @c %size() ). |
---|
276 | */ |
---|
277 | const char* |
---|
278 | end () const {return data_ + size_;} |
---|
279 | |
---|
280 | /** |
---|
281 | * @brief Get the end of the underlying memory region. |
---|
282 | * |
---|
283 | * @return A pointer to the one past last byte of the underlying |
---|
284 | * memory region (that is @c %begin() @c + @c %size() ). |
---|
285 | */ |
---|
286 | char* |
---|
287 | end () {return data_ + size_;} |
---|
288 | |
---|
289 | public: |
---|
290 | /** |
---|
291 | * @brief Swap data with another %buffer. |
---|
292 | * |
---|
293 | * @param x A %buffer to swap with. |
---|
294 | */ |
---|
295 | void |
---|
296 | swap (buffer& x); |
---|
297 | |
---|
298 | private: |
---|
299 | bool |
---|
300 | capacity (size_t capacity, bool copy); |
---|
301 | }; |
---|
302 | |
---|
303 | /** |
---|
304 | * @brief %buffer comparison operator. |
---|
305 | * |
---|
306 | * @return True if the buffers have the same sizes and the same |
---|
307 | * data. |
---|
308 | */ |
---|
309 | template <typename C> |
---|
310 | inline bool |
---|
311 | operator== (const buffer<C>& a, const buffer<C>& b) |
---|
312 | { |
---|
313 | return a.size () == b.size () && |
---|
314 | std::memcmp (a.data (), b.data (), a.size ()) == 0; |
---|
315 | } |
---|
316 | |
---|
317 | /** |
---|
318 | * @brief %buffer comparison operator. |
---|
319 | * |
---|
320 | * @return True if the buffers have different sizes or different |
---|
321 | * data. |
---|
322 | */ |
---|
323 | template <typename C> |
---|
324 | inline bool |
---|
325 | operator!= (const buffer<C>& a, const buffer<C>& b) |
---|
326 | { |
---|
327 | return !(a == b); |
---|
328 | } |
---|
329 | } |
---|
330 | } |
---|
331 | } |
---|
332 | |
---|
333 | #include <xsd/cxx/tree/buffer.txx> |
---|
334 | |
---|
335 | #endif // XSD_CXX_TREE_BUFFER_HXX |
---|