From: Leandro Lucarella Date: Mon, 1 Dec 2008 19:01:55 +0000 (-0200) Subject: Generalize basic_buffer's value_type X-Git-Url: https://git.llucax.com/software/posixx.git/commitdiff_plain/25e37c946dfa92e9a3676a022177afef06588a01 Generalize basic_buffer's value_type Now the basic_buffer has a new template parameter to choose the type to use as value_type (i.e. the type of the elements stored in the array). Note that only POD types are supported, since no constructors or destructors get called. --- diff --git a/src/basic_buffer.hpp b/src/basic_buffer.hpp index 1e53f96..14b880c 100644 --- a/src/basic_buffer.hpp +++ b/src/basic_buffer.hpp @@ -4,6 +4,7 @@ #include // std::bad_alloc, std::out_of_range #include // std::numeric_limits #include // std::tr1::is_integral, true_type, false_type +#include // std::realloc() #include // std::memcpy(), memset(), memcmp() #include // assert() #include // std::size_t, ptrdiff_t @@ -14,20 +15,20 @@ namespace posixx { * A low-level buffer. * * This is pretty much like using a plain C-style array using dynamic memory, - * with a STL-ish interface. + * with a STL-ish interface. Only POD should be stored, since no constructors + * or destructors are called. * * The buffer will use Allocator (which should be a function with realloc(3) * semantics) for all storage management. */ -template< void* (*Allocator)(void*, size_t) > +template< typename T, void* (*Allocator)(void*, size_t) = &std::realloc > struct basic_buffer { // Types ////////////////////////////////////////////////////////////////////// /// - typedef unsigned char value_type; // TODO: use char to be more - // std::string-friendly? + typedef T value_type; /// typedef value_type& reference; @@ -95,7 +96,7 @@ struct basic_buffer { * * @throw std::bad_alloc */ - basic_buffer(const basic_buffer< Allocator >& x): + basic_buffer(const basic_buffer< T, Allocator >& x): _data(_allocate(NULL, x.size() * sizeof(value_type))), _size(x.size()) { @@ -126,8 +127,8 @@ struct basic_buffer { * * @throw std::bad_alloc */ - basic_buffer< Allocator >& operator = ( - const basic_buffer< Allocator >& x) + basic_buffer< T, Allocator >& operator = ( + const basic_buffer< T, Allocator >& x) { if (this != &x) { resize(x.size()); @@ -406,7 +407,7 @@ struct basic_buffer { * * Invalidates all references and iterators. */ - void swap(basic_buffer< Allocator >& x) + void swap(basic_buffer< T, Allocator >& x) { pointer tmp_data = x._data; size_type tmp_size = x._size; @@ -484,9 +485,9 @@ protected: /** * Returns true if x hass the same contents as y. */ -template < void* (*Allocator)(void*, std::size_t) > -bool operator == (const basic_buffer< Allocator >& x, - const basic_buffer < Allocator >& y) +template < typename T, void* (*Allocator)(void*, std::size_t) > +bool operator == (const basic_buffer< T, Allocator >& x, + const basic_buffer < T, Allocator >& y) { if (&x == &y) return true; @@ -506,9 +507,9 @@ bool operator == (const basic_buffer< Allocator >& x, /** * Returns !(x==y). */ -template < void* (*Allocator)(void*, std::size_t) > -bool operator != (const basic_buffer& x, - const basic_buffer & y) +template < typename T, void* (*Allocator)(void*, std::size_t) > +bool operator != (const basic_buffer< T, Allocator >& x, + const basic_buffer < T, Allocator >& y) { return !(x == y); } @@ -517,9 +518,9 @@ bool operator != (const basic_buffer& x, * Returns true if the elements contained in x are lexicographically less than * the elements contained in y. */ -template < void* (*Allocator)(void*, std::size_t) > -bool operator < (const basic_buffer< Allocator >& x, - const basic_buffer< Allocator >& y) +template < typename T, void* (*Allocator)(void*, std::size_t) > +bool operator < (const basic_buffer< T, Allocator >& x, + const basic_buffer< T, Allocator >& y) { if (&x == &y) return false; @@ -539,9 +540,9 @@ bool operator < (const basic_buffer< Allocator >& x, /** * Returns y < x. */ -template < void* (*Allocator)(void*, std::size_t) > -bool operator > (const basic_buffer< Allocator >& x, - const basic_buffer< Allocator >& y) +template < typename T, void* (*Allocator)(void*, std::size_t) > +bool operator > (const basic_buffer< T, Allocator >& x, + const basic_buffer< T, Allocator >& y) { return y < x; } @@ -549,9 +550,9 @@ bool operator > (const basic_buffer< Allocator >& x, /** * Returns !(y < x). */ -template < void* (*Allocator)(void*, std::size_t) > -bool operator <= (const basic_buffer< Allocator >& x, - const basic_buffer< Allocator >& y) +template < typename T, void* (*Allocator)(void*, std::size_t) > +bool operator <= (const basic_buffer< T, Allocator >& x, + const basic_buffer< T, Allocator >& y) { return !(y < x); } @@ -559,9 +560,9 @@ bool operator <= (const basic_buffer< Allocator >& x, /** * Returns !(x < y). */ -template < void* (*Allocator)(void*, std::size_t) > -bool operator >= (const basic_buffer< Allocator >& x, - const basic_buffer< Allocator >& y) +template < typename T, void* (*Allocator)(void*, std::size_t) > +bool operator >= (const basic_buffer< T, Allocator >& x, + const basic_buffer< T, Allocator >& y) { return !(x < y); } @@ -575,8 +576,8 @@ bool operator >= (const basic_buffer< Allocator >& x, * * Invalidates all references and iterators. */ -template < void* (*Allocator)(void*, std::size_t) > -void swap(basic_buffer< Allocator >& x, basic_buffer< Allocator >& y) +template < typename T, void* (*Allocator)(void*, std::size_t) > +void swap(basic_buffer< T, Allocator >& x, basic_buffer< T, Allocator >& y) { x.swap(y); } diff --git a/src/buffer.hpp b/src/buffer.hpp index 8613d23..b3b8c11 100644 --- a/src/buffer.hpp +++ b/src/buffer.hpp @@ -3,12 +3,10 @@ #include "basic_buffer.hpp" // posixx::basic_buffer -#include // std::realloc() - namespace posixx { /// A buffer that uses realloc(3) for memory management. -typedef basic_buffer< &std::realloc > buffer; +typedef basic_buffer< unsigned char > buffer; } // namespace posixx