Blob.h
1/* Copyright (C) 2007 by Tommi Maekitalo
2 Copyright (C) 2007-2026 by Marc Duerner
3 SPDX-License-Identifier: LGPL-2.1-or-later WITH mif-exception
4*/
5
6#ifndef PT_BLOB_H
7#define PT_BLOB_H
8
9#include <Pt/SmartPtr.h>
10#include <Pt/RefCounted.h>
11#include <cstddef>
12#include <cstring>
13#include <cstdlib>
14
15namespace Pt {
16
17namespace Db {
18
21class IBlob : public RefCounted
22{
23 public:
26 virtual ~IBlob() {}
27
30 virtual void assign(const char* data, std::size_t len) = 0;
31
34 virtual IBlob* create() const = 0;
35
38 virtual void destroy() = 0;
39
42 std::size_t size() const
43 { return _size; }
44
47 const char* data() const
48 { return _data; }
49
52 bool operator==(const IBlob& other) const
53 {
54 return _size == other._size &&
55 ( std::strncmp(_data, other._data, _size) == 0 );
56 }
57
58 protected:
59 IBlob()
60 : _data(0)
61 , _size(0)
62 { }
63
64 char* _data;
65 std::size_t _size;
66};
67
70class BlobImpl : public IBlob
71{
72 public:
73 BlobImpl()
74 { }
75
76 ~BlobImpl()
77 {
78 delete[] _data;
79 }
80
81 virtual void assign(const char* data, std::size_t len)
82 {
83 if (len == 0)
84 {
85 delete[] _data;
86 _data = 0;
87 _size = 0;
88 return;
89 }
90
91 if( len > this->size() )
92 {
93 delete[] _data;
94 _data = new char[len];
95 }
96
97 std::memcpy(_data, data, len);
98 _size = len;
99 }
100
101 virtual IBlob* create() const
102 { return new BlobImpl(); }
103
104 virtual void destroy()
105 { delete this; }
106
107 static BlobImpl* emptyInstance()
108 {
109 static BlobImpl empty(1);
110 return &empty;
111 }
112
113 protected:
114 BlobImpl(std::size_t n)
115 { RefCounted::addRef(); }
116};
117
118
121static struct BlobStaticInitializer
122{
123 BlobStaticInitializer()
124 {
125 BlobImpl::emptyInstance();
126 }
127} pt_blob_static_initializer;
128
129
143class Blob
144{
145 template <typename T>
146 struct Release
147 {
148 void destroy(T* blob)
149 { blob->destroy(); }
150 };
151
152 SmartPtr<IBlob, InternalRefCounted<IBlob>, Release<IBlob> > m_data;
153
154public:
158 : m_data( BlobImpl::emptyInstance() )
159 { }
160
163 Blob(const char* data, std::size_t len)
164 : m_data( new BlobImpl() )
165 {
166 m_data->assign(data, len);
167 }
168
171 explicit Blob(IBlob* b)
172 : m_data(b)
173 { }
174
177 void assign(const char* data, std::size_t len)
178 {
179 // copy-on-write
180 if ( m_data->refs() > 1 )
181 {
182 m_data.reset( m_data->create() );
183 }
184
185 m_data->assign(data, len);
186 }
187
190 bool operator==(const Blob& b) const
191 {
192 return *m_data == *b.m_data;
193 }
194
197 bool operator!=(const Blob& b) const
198 {
199 return !operator==(b);
200 }
201
204 const char* data() const
205 {
206 return m_data->data();
207 }
208
211 std::size_t size() const
212 {
213 return m_data->size();
214 }
215};
216
217} // namespace Db
218
219} // namespace Pt
220
221#endif //PT_BLOB_H
Default blob implementation using new and delete.
Definition Blob.h:71
virtual void assign(const char *data, std::size_t len)
Copies len bytes from data into this blob.
Definition Blob.h:81
virtual void destroy()
Destroys an instance created by create().
Definition Blob.h:104
virtual IBlob * create() const
Creates a new implementation of the same type.
Definition Blob.h:101
bool operator==(const Blob &b) const
Returns true if both blobs contain the same data.
Definition Blob.h:190
Blob()
Creates an empty blob.
Definition Blob.h:157
Blob(const char *data, std::size_t len)
Copies len bytes from data.
Definition Blob.h:163
void assign(const char *data, std::size_t len)
Replaces the bytes with len bytes from data.
Definition Blob.h:177
Blob(IBlob *b)
Takes ownership of the implementation b.
Definition Blob.h:171
const char * data() const
Returns a pointer to the data, or 0 if empty.
Definition Blob.h:204
bool operator!=(const Blob &b) const
Returns true if the blobs do not contain the same data.
Definition Blob.h:197
std::size_t size() const
Returns the size of the data.
Definition Blob.h:211
Shared implementation of blob data.
Definition Blob.h:22
virtual ~IBlob()
Destructor.
Definition Blob.h:26
virtual void assign(const char *data, std::size_t len)=0
Copies len bytes from data into this blob.
const char * data() const
Returns a pointer to the blob data, or 0 if empty.
Definition Blob.h:47
std::size_t size() const
Returns the size of the blob data.
Definition Blob.h:42
virtual IBlob * create() const =0
Creates a new implementation of the same type.
bool operator==(const IBlob &other) const
Returns true if both instances contain the same data.
Definition Blob.h:52
virtual void destroy()=0
Destroys an instance created by create().
Policy based smart pointer.
Definition SmartPtr.h:462
Portable SQL database access.
Core module.
Definition Allocator.h:33