aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2011-03-28 11:40:14 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2011-03-28 11:40:14 +0200
commit74bd3bbc7edf0c067ba9eafe51b463d2e0e121bd (patch)
tree11ea18f50d426c8b021d5b8f849f42ef9035dc51
parent1ba1097cd1d71b945255d9401ffa4f04a036e94f (diff)
Factor out common buffer implementation
-rw-r--r--libcommon/common/buffer.hxx89
-rw-r--r--mysql/types/test.hxx81
-rw-r--r--mysql/types/traits.hxx2
-rw-r--r--sqlite/types/test.hxx79
-rw-r--r--sqlite/types/traits.hxx2
5 files changed, 94 insertions, 159 deletions
diff --git a/libcommon/common/buffer.hxx b/libcommon/common/buffer.hxx
new file mode 100644
index 0000000..97e8962
--- /dev/null
+++ b/libcommon/common/buffer.hxx
@@ -0,0 +1,89 @@
+// file : libcommon/common/buffer.hxx
+// author : Boris Kolpackov <boris@codesynthesis.com>
+// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
+// license : GNU GPL v2; see accompanying LICENSE file
+
+#ifndef LIBCOMMON_COMMON_BUFFER_HXX
+#define LIBCOMMON_COMMON_BUFFER_HXX
+
+#include <cstddef> // std::size_t
+#include <cstring> // std::{memcmp,memcpy}
+
+struct buffer
+{
+ ~buffer ()
+ {
+ delete[] data_;
+ }
+
+ buffer ()
+ : data_ (0), size_ (0)
+ {
+ }
+
+ buffer (const void* data, std::size_t size)
+ : data_ (0), size_ (size)
+ {
+ data_ = new char[size_];
+ std::memcpy (data_, data, size_);
+ }
+
+ buffer (const buffer& y)
+ : data_ (0), size_ (0)
+ {
+ assign (y.data_, y.size_);
+ }
+
+ buffer&
+ operator= (const buffer& y)
+ {
+ if (this != &y)
+ assign (y.data_, y.size_);
+
+ return *this;
+ }
+
+ void
+ assign (const void* data, std::size_t size)
+ {
+ if (size_ < size)
+ {
+ char* p (new char[size]);
+ delete[] data_;
+ data_ = p;
+ }
+
+ std::memcpy (data_, data, size);
+ size_ = size;
+ }
+
+ char*
+ data ()
+ {
+ return data_;
+ }
+
+ const char*
+ data () const
+ {
+ return data_;
+ }
+
+ std::size_t
+ size () const
+ {
+ return size_;
+ }
+
+ bool
+ operator== (const buffer& y) const
+ {
+ return size_ == y.size_ && std::memcmp (data_, y.data_, size_) == 0;
+ }
+
+private:
+ char* data_;
+ std::size_t size_;
+};
+
+#endif // LIBCOMMON_COMMON_BUFFER_HXX
diff --git a/mysql/types/test.hxx b/mysql/types/test.hxx
index 80d1423..207d8b3 100644
--- a/mysql/types/test.hxx
+++ b/mysql/types/test.hxx
@@ -9,11 +9,11 @@
#include <set>
#include <string>
#include <memory> // std::auto_ptr
-#include <cstddef> // std::size_t
-#include <cstring> // std::{memcmp,memcpy}
#include <odb/core.hxx>
+#include <common/buffer.hxx>
+
struct date_time
{
date_time ()
@@ -59,83 +59,6 @@ struct date_time
unsigned int second;
};
-struct buffer
-{
- ~buffer ()
- {
- delete[] data_;
- }
-
- buffer ()
- : data_ (0), size_ (0)
- {
- }
-
- buffer (const void* data, std::size_t size)
- : data_ (0), size_ (size)
- {
- data_ = new char[size_];
- std::memcpy (data_, data, size_);
- }
-
- buffer (const buffer& y)
- : data_ (0), size_ (0)
- {
- assign (y.data_, y.size_);
- }
-
- buffer&
- operator= (const buffer& y)
- {
- if (this != &y)
- assign (y.data_, y.size_);
-
- return *this;
- }
-
- void
- assign (const void* data, std::size_t size)
- {
- if (size_ < size)
- {
- char* p (new char[size]);
- delete[] data_;
- data_ = p;
- }
-
- std::memcpy (data_, data, size);
- size_ = size;
- }
-
- char*
- data ()
- {
- return data_;
- }
-
- const char*
- data () const
- {
- return data_;
- }
-
- std::size_t
- size () const
- {
- return size_;
- }
-
- bool
- operator== (const buffer& y) const
- {
- return size_ == y.size_ && std::memcmp (data_, y.data_, size_) == 0;
- }
-
-private:
- char* data_;
- std::size_t size_;
-};
-
struct bitfield
{
unsigned int a: 1;
diff --git a/mysql/types/traits.hxx b/mysql/types/traits.hxx
index df105e7..b05b2c4 100644
--- a/mysql/types/traits.hxx
+++ b/mysql/types/traits.hxx
@@ -10,7 +10,7 @@
#include <odb/mysql/traits.hxx>
-#include "test.hxx" // date_time, buffer
+#include "test.hxx" // date_time, buffer, string_ptr
namespace odb
{
diff --git a/sqlite/types/test.hxx b/sqlite/types/test.hxx
index 971a89f..37190a0 100644
--- a/sqlite/types/test.hxx
+++ b/sqlite/types/test.hxx
@@ -9,87 +9,10 @@
#include <set>
#include <string>
#include <memory> // std::auto_ptr
-#include <cstddef> // std::size_t
-#include <cstring> // std::{memcmp,memcpy}
#include <odb/core.hxx>
-struct buffer
-{
- ~buffer ()
- {
- delete[] data_;
- }
-
- buffer ()
- : data_ (0), size_ (0)
- {
- }
-
- buffer (const void* data, std::size_t size)
- : data_ (0), size_ (size)
- {
- data_ = new char[size_];
- std::memcpy (data_, data, size_);
- }
-
- buffer (const buffer& y)
- : data_ (0), size_ (0)
- {
- assign (y.data_, y.size_);
- }
-
- buffer&
- operator= (const buffer& y)
- {
- if (this != &y)
- assign (y.data_, y.size_);
-
- return *this;
- }
-
- void
- assign (const void* data, std::size_t size)
- {
- if (size_ < size)
- {
- char* p (new char[size]);
- delete[] data_;
- data_ = p;
- }
-
- std::memcpy (data_, data, size);
- size_ = size;
- }
-
- char*
- data ()
- {
- return data_;
- }
-
- const char*
- data () const
- {
- return data_;
- }
-
- std::size_t
- size () const
- {
- return size_;
- }
-
- bool
- operator== (const buffer& y) const
- {
- return size_ == y.size_ && std::memcmp (data_, y.data_, size_) == 0;
- }
-
-private:
- char* data_;
- std::size_t size_;
-};
+#include <common/buffer.hxx>
typedef std::auto_ptr<std::string> string_ptr;
diff --git a/sqlite/types/traits.hxx b/sqlite/types/traits.hxx
index 0f9c253..13eef34 100644
--- a/sqlite/types/traits.hxx
+++ b/sqlite/types/traits.hxx
@@ -10,7 +10,7 @@
#include <odb/sqlite/traits.hxx>
-#include "test.hxx" // buffer
+#include "test.hxx" // buffer, string_ptr
namespace odb
{