blob.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2008 Tommi Maekitalo
3  * Copyright (C) 2007-2008 Marc Boris Duerner
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * As a special exception, you may use this file as part of a free
11  * software library without restriction. Specifically, if other files
12  * instantiate templates or use macros or inline functions from this
13  * file, or you compile this file and link it with other files to
14  * produce an executable, this file does not by itself cause the
15  * resulting executable to be covered by the GNU General Public
16  * License. This exception does not however invalidate any other
17  * reasons why the executable file might be covered by the GNU Library
18  * General Public License.
19  *
20  * This library is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  * Lesser General Public License for more details.
24  *
25  * You should have received a copy of the GNU Lesser General Public
26  * License along with this library; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28  */
29 
30 #ifndef TNTDB_BITS_BLOB_H
31 #define TNTDB_BITS_BLOB_H
32 
33 #include <tntdb/iface/iblob.h>
34 #include <tntdb/impl/blob.h>
35 #include <cxxtools/smartptr.h>
36 
37 namespace tntdb
38 {
40  class Blob
41  {
42  public:
44  template <typename T>
45  struct Release
46  {
47  void destroy(IBlob* blob)
48  { blob->destroy(); }
49  };
50 
51  private:
54 
55  public:
56  Blob()
57  : _data(BlobImpl::emptyInstance())
58  { }
59 
66  Blob(const char* data, std::size_t len)
67  : _data(new BlobImpl())
68  { _data->assign(data, len); }
69 
71  explicit Blob(IBlob* b)
72  : _data(b)
73  { }
74 
76  void assign(const char* data, std::size_t len)
77  {
78  // copy-on-write
79  if (_data->refs() > 1)
80  _data = _data->create();
81 
82  _data->assign(data, len);
83  }
84 
90  char* reserve(std::size_t len, bool shrink = false)
91  { return _data->reserve(len, shrink); }
92 
93  bool operator==(const Blob& b) const
94  { return *_data == *b._data; }
95 
96  bool operator!=(const Blob& b) const
97  { return !operator==(b); }
98 
100  const char* data() const
101  { return _data->data(); }
102 
104  std::size_t size() const
105  { return _data->size(); }
106  };
107 }
108 
109 #endif // TNTDB_BITS_BLOB_H
110