1 #ifndef POSIXX_BASIC_BUFFER_HPP_
2 #define POSIXX_BASIC_BUFFER_HPP_
4 #include <stdexcept> // std::bad_alloc, std::out_of_range
5 #include <limits> // std::numeric_limits
6 #include <tr1/type_traits> // std::tr1::is_integral, true_type, false_type
7 #include <cstring> // std::memcpy(), memset(), memcmp()
8 #include <cassert> // assert()
15 * This is pretty much like using a plain C-style array using dynamic memory,
16 * with a STL-ish interface.
18 * The buffer will use Allocator (which should be a function with realloc(3)
19 * semantics) for all storage management.
21 template< void* (*Allocator)(void*, size_t) >
25 //////////////////////////////////////////////////////////////////////
28 typedef unsigned char value_type; // TODO: use char to be more
29 // std::string-friendly?
32 typedef value_type& reference;
35 typedef const value_type& const_reference;
38 typedef value_type* iterator; // TODO: make a real iterator
41 typedef const value_type* const_iterator;
44 typedef size_t size_type;
47 typedef ptrdiff_t difference_type;
50 typedef value_type* pointer;
53 typedef const pointer const_pointer;
56 // TODO: reverse iterator
57 //typedef typename std::reverse_iterator< iterator > reverse_iterator;
60 //typedef typename std::reverse_iterator< const iterator >
61 // const_reverse_iterator;
64 // Construct/Copy/Destroy
65 //////////////////////////////////////////////////////////////////////
68 * Creates a buffer of length zero (the default constructor).
70 explicit basic_buffer(): _data(NULL), _size(0) {}
73 * Creates a buffer with a capacity() of n (uninitialized) elements.
75 * @throw std::bad_alloc
77 explicit basic_buffer(size_type n):
78 _data(_allocate(NULL, n * sizeof(value_type))), _size(n)
83 * Creates a buffer of length n, containing n copies of value.
85 * @throw std::bad_alloc
87 basic_buffer(size_type n, const_reference value):
91 std::memset(_data, value, n);
95 * Creates a copy of x.
97 * @throw std::bad_alloc
99 basic_buffer(const basic_buffer< Allocator >& x):
100 _data(_allocate(NULL, x.size() * sizeof(value_type))),
103 std::memcpy(_data, x.c_array(), x.size());
107 * Creates a buffer of length finish - start, filled with all values
108 * obtained by dereferencing the InputIterators on the range [start,
111 * @throw std::bad_alloc
113 template < typename InputIterator >
114 basic_buffer(InputIterator start, InputIterator finish):
115 _data(NULL), _size(0)
117 assign(start, finish);
121 * Erases all elements in self then inserts into self a copy of each
124 * Invalidates all references and iterators.
126 * @returns a reference to self.
128 * @throw std::bad_alloc
130 basic_buffer< Allocator >& operator = (
131 const basic_buffer< Allocator >& x)
135 std::memcpy(_data, x.c_array(), x.size());
141 * Frees the memory holded by the buffer
148 //////////////////////////////////////////////////////////////////////
151 * Returns a random access iterator that points to the first element.
157 * Returns a random access const_iterator that points to the first
160 const_iterator begin() const
164 * Returns a random access iterator that points to the past-the-end
168 { return _data ? _data + _size : NULL; }
171 * Returns a random access const_iterator that points to the
172 * past-the-end value.
174 const_iterator end() const
175 { return _data ? _data + _size : NULL; }
178 * Returns a random access reverse_iterator that points to the
179 * past-the-end value.
181 //reverse_iterator rbegin()
182 //{ return std::reverse_iterator< iterator >(end()); }
185 * Returns a random access const_reverse_iterator that points to the
186 * past-the-end value.
188 //const_reverse_iterator rbegin() const
189 //{ return std::reverse_iterator< const iterator >(end()); }
192 * Returns a random access reverse_iterator that points to the first
195 //reverse_iterator rend()
196 //{ return std::reverse_iterator< iterator >(begin()); }
199 * Returns a random access const_reverse_iterator that points to the
202 //const_reverse_iterator rend() const
203 //{ return std::reverse_iterator< const iterator >(begin()); }
207 //////////////////////////////////////////////////////////////////////
210 * Returns the number of elements.
212 size_type size() const
216 * Returns size() of the largest possible buffer.
218 size_type max_size() const
219 { return std::numeric_limits< size_type >::max(); }
222 * Alters the size of self.
224 * If the new size (sz) is greater than the current size, then
225 * sz-size() (uninitialized) elements are inserted at the end of the
226 * buffer. If the new size is smaller than the current capacity, then
227 * the buffer is truncated by erasing size()-sz elements off the end.
228 * If sz is equal to capacity then no action is taken.
230 * Invalidates all references and iterators.
232 * @throw std::bad_alloc
234 void resize(size_type sz)
236 pointer n_data = _allocate(_data, sz * sizeof(value_type));
242 * Alters the size of self.
244 * If the new size (sz) is greater than the current size, then
245 * sz-size() copies of value are inserted at the end of the buffer. If
246 * the new size is smaller than the current capacity, then the buffer
247 * is truncated by erasing size()-sz elements off the end. If sz is
248 * equal to capacity then no action is taken.
250 * Invalidates all references and iterators.
252 * @throw std::bad_alloc
254 void resize(size_type sz, value_type value)
256 size_type old_size = size();
259 std::memset(_data + old_size, value, sz - old_size);
265 size_type capacity() const
269 * Returns true if the size is zero.
275 * If sz > size() call resize(sz), otherwise do nothing.
277 * Invalidates all references and iterators in the first case.
279 * @throw std::bad_alloc
281 void reserve(size_type sz)
289 //////////////////////////////////////////////////////////////////////
292 * Returns a reference to element n of self.
294 * The result can be used as an lvalue. The index n must be between
295 * 0 and the size less one.
297 reference operator[](size_type n)
298 { assert(n < _size); return _data[n]; }
301 * Returns a constant reference to element n of self.
303 * The index n must be between 0 and the size less one.
305 const_reference operator[](size_type n) const
306 { assert(n < _size); return _data[n]; }
309 * Returns a reference to element n of self.
311 * The result can be used as an lvalue. If index n is not between
312 * 0 and the size less one, a std::out_of_range exception.
314 reference at(size_type n)
317 throw std::out_of_range("posixx::basic_buffer::at(n)");
322 * Returns a constant reference to element n of self.
324 * If index n is not between 0 and the size less one,
325 * a std::out_of_range exception.
327 const_reference at(size_type n) const
330 throw std::out_of_range("posixx::basic_buffer::at(n)");
335 * Returns a reference to the first element.
338 { assert(_size); return _data[0]; }
341 * Returns a constant reference to the first element.
343 const_reference front() const
344 { assert(_size); return _data[0]; }
347 * Returns a reference to the last element.
350 { assert(_size); return _data[_size - 1]; }
353 * Returns a constant reference to the last element.
355 const_reference back() const
356 { assert(_size); return _data[_size - 1]; }
359 * Returns a pointer to a C-style array.
365 * Returns a pointer to a read-only C-style array.
367 const_pointer c_array() const
372 //////////////////////////////////////////////////////////////////////
375 * Replaces elements with copies of those in the range [start, finish).
377 * The function invalidates all iterators and references to elements in
380 * Invalidates all references and iterators.
382 template < typename InputIterator >
383 void assign(InputIterator start, InputIterator finish)
385 // Check whether it's an integral type. If so, it's not an
387 typename std::tr1::is_integral< InputIterator >::type is_int;
388 _assign_dispatch(start, finish, is_int);
392 * Replaces elements in *this with n copies of value.
394 * The function invalidates all iterators and references to elements in
397 * Invalidates all references and iterators.
399 void assign(size_type n, const_reference value)
402 std::memset(_data, value, n);
406 * Efficiently swaps the contents of x and y.
408 * Invalidates all references and iterators.
410 void swap(basic_buffer< Allocator >& x)
412 pointer tmp_data = x._data;
413 size_type tmp_size = x._size;
421 * Deletes all elements from the buffer.
423 * Invalidates all references and iterators.
427 _data = _allocate(_data, 0);
433 // The underlying data
436 // Size in number of items
439 // Allocator wrapper for automatic casting
440 static pointer _allocate(void* ptr, size_t sz)
442 if (ptr == NULL && sz == 0)
444 void* new_ptr = Allocator(ptr, sz);
445 if (sz != 0 && new_ptr == NULL)
446 throw std::bad_alloc();
447 return static_cast< pointer >(new_ptr);
452 * Helper assign functions to disambiguate the Iterator based and the
453 * N-value copy assign() methods. The last arguments indicates if
454 * InputIterator is an integral type.
457 // This is the version for the integral types, we should use the
458 // "regular" N-value copy assign().
459 template < typename InputIterator >
460 void _assign_dispatch(InputIterator start, InputIterator finish,
463 assign(static_cast< size_type >(start),
464 static_cast< value_type >(finish));
467 // This is the version for the real iterators.
468 template < typename InputIterator >
469 void _assign_dispatch(InputIterator start, InputIterator finish,
470 std::tr1::false_type)
472 // TODO: provide an efficient version for random iterators
473 while (start != finish) {
475 (*this)[size() - 1] = *start;
482 // Nonmember Operators
483 //////////////////////////////////////////////////////////////////////
486 * Returns true if x hass the same contents as y.
488 template < void* (*Allocator)(void*, size_t) >
489 bool operator == (const basic_buffer< Allocator >& x, const basic_buffer < Allocator >& y)
493 if (x.size() != y.size())
495 if (x.empty() && y.empty())
497 if (x.empty() || y.empty())
499 int r = std::memcmp(x.c_array(), y.c_array(),
500 (x.size() < y.size()) ? x.size() : y.size());
502 return x.size() == y.size();
509 template < void* (*Allocator)(void*, size_t) >
510 bool operator != (const basic_buffer<Allocator>& x, const basic_buffer <Allocator>& y)
516 * Returns true if the elements contained in x are lexicographically less than
517 * the elements contained in y.
519 template < void* (*Allocator)(void*, size_t) >
520 bool operator < (const basic_buffer< Allocator >& x, const basic_buffer< Allocator >& y)
524 if (x.empty() && y.empty())
530 int r = std::memcmp(x.c_array(), y.c_array(),
531 (x.size() < y.size()) ? x.size() : y.size());
533 return x.size() < y.size();
540 template < void* (*Allocator)(void*, size_t) >
541 bool operator > (const basic_buffer< Allocator >& x, const basic_buffer< Allocator >& y)
549 template < void* (*Allocator)(void*, size_t) >
550 bool operator <= (const basic_buffer< Allocator >& x, const basic_buffer< Allocator >& y)
558 template < void* (*Allocator)(void*, size_t) >
559 bool operator >= (const basic_buffer< Allocator >& x, const basic_buffer< Allocator >& y)
565 // Specialized Algorithms
566 //////////////////////////////////////////////////////////////////////
569 * Exchanges self with x, by swapping all elements.
571 * Invalidates all references and iterators.
573 template < void* (*Allocator)(void*, size_t) >
574 void swap(basic_buffer< Allocator >& x, basic_buffer< Allocator >& y)
579 } // namespace posixx
581 #endif // POSIXX_BASIC_BUFFER_HPP_