aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--libcommon/common/buffer.hxx69
1 files changed, 43 insertions, 26 deletions
diff --git a/libcommon/common/buffer.hxx b/libcommon/common/buffer.hxx
index 97e8962..ada73a1 100644
--- a/libcommon/common/buffer.hxx
+++ b/libcommon/common/buffer.hxx
@@ -6,36 +6,37 @@
#ifndef LIBCOMMON_COMMON_BUFFER_HXX
#define LIBCOMMON_COMMON_BUFFER_HXX
+#include <new>
#include <cstddef> // std::size_t
#include <cstring> // std::{memcmp,memcpy}
-struct buffer
+struct basic_buffer_base
{
- ~buffer ()
+ ~basic_buffer_base ()
{
- delete[] data_;
+ operator delete (data_);
}
- buffer ()
+ basic_buffer_base ()
: data_ (0), size_ (0)
{
}
- buffer (const void* data, std::size_t size)
+ basic_buffer_base (const void* data, std::size_t size)
: data_ (0), size_ (size)
{
- data_ = new char[size_];
+ data_ = operator new (size_);
std::memcpy (data_, data, size_);
}
- buffer (const buffer& y)
+ basic_buffer_base (const basic_buffer_base& y)
: data_ (0), size_ (0)
{
assign (y.data_, y.size_);
}
- buffer&
- operator= (const buffer& y)
+ basic_buffer_base&
+ operator= (const basic_buffer_base& y)
{
if (this != &y)
assign (y.data_, y.size_);
@@ -48,8 +49,8 @@ struct buffer
{
if (size_ < size)
{
- char* p (new char[size]);
- delete[] data_;
+ void *p (operator new (size));
+ operator delete (data_);
data_ = p;
}
@@ -57,18 +58,6 @@ struct buffer
size_ = size;
}
- char*
- data ()
- {
- return data_;
- }
-
- const char*
- data () const
- {
- return data_;
- }
-
std::size_t
size () const
{
@@ -76,14 +65,42 @@ struct buffer
}
bool
- operator== (const buffer& y) const
+ operator== (const basic_buffer_base& y) const
{
return size_ == y.size_ && std::memcmp (data_, y.data_, size_) == 0;
}
-private:
- char* data_;
+protected:
+ void* data_;
std::size_t size_;
};
+template <typename T>
+struct basic_buffer: basic_buffer_base
+{
+ basic_buffer ()
+ {
+ }
+
+ basic_buffer (const T* data, std::size_t size)
+ : basic_buffer_base (data, size)
+ {
+ }
+
+ T*
+ data ()
+ {
+ return static_cast<T*> (data_);
+ }
+
+ const T*
+ data () const
+ {
+ return static_cast<const T*> (data_);
+ }
+};
+
+typedef basic_buffer<char> buffer;
+typedef basic_buffer<unsigned char> ubuffer;
+
#endif // LIBCOMMON_COMMON_BUFFER_HXX