aboutsummaryrefslogtreecommitdiff
path: root/odb/details/buffer.hxx
blob: 28c9cfadc0cc8976985fbc2c5df249143ab6a145 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// file      : odb/details/buffer.hxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2009-2011 Code Synthesis Tools CC
// license   : GNU GPL v2; see accompanying LICENSE file

#ifndef ODB_BUFFER_DETAILS_HXX
#define ODB_BUFFER_DETAILS_HXX

#include <odb/pre.hxx>

#include <new>
#include <cstddef> // std::size_t

#include <odb/details/export.hxx>

namespace odb
{
  namespace details
  {
    class LIBODB_EXPORT basic_buffer_base
    {
    public:
      ~basic_buffer_base ()
      {
        if (data_)
          operator delete (data_);
      }

      basic_buffer_base ()
        : capacity_ (512)
      {
        data_ = operator new (capacity_);
      }

      std::size_t
      capacity () const
      {
        return capacity_;
      }

      void capacity (std::size_t, std::size_t data_size = 0);

    protected:
      void* data_;
      std::size_t capacity_;
    };

    template <typename T>
    class basic_buffer: public basic_buffer_base
    {
    public:

      T*
      data ()
      {
        return static_cast<T*> (data_);
      }

      const T*
      data () const
      {
        return static_cast<T*> (data_);
      }
    };

    typedef basic_buffer<char> buffer;
    typedef basic_buffer<unsigned char> ubuffer;
  }
}

#include <odb/post.hxx>

#endif // ODB_BUFFER_DETAILS_HXX