libcommonc++  0.7
Blob.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_Blob_hxx
24 #define __ccxx_Blob_hxx
25 
26 #include <commonc++/Common.h++>
29 
30 #include <sstream>
31 
32 #ifdef _MSC_VER
33 #pragma warning(push)
34 #pragma warning(disable: 4251)
35 #endif // _MSC_VER
36 
37 namespace ccxx {
38 
46 {
47  public:
48 
50  Blob();
51 
60  Blob(byte_t* data, uint_t length, bool copy = true);
61 
63  Blob(const Blob& other);
64 
71  Blob(const byte_t* data, uint_t length);
72 
74  ~Blob();
75 
81  inline uint_t getLength() const
82  { return(_buf == NULL ? 0 : _buf->_length); };
83 
89  inline uint_t length() const
90  { return(getLength()); }
91 
100  void setLength(uint_t length);
101 
103  inline byte_t* getData()
104  { return(_buf == NULL ? NULL : _buf->_data); }
105 
107  inline const byte_t* getData() const
108  { return(_buf == NULL ? NULL : _buf->_data); }
109 
111  inline Blob& clear()
112  {
113  setLength(0);
114  return(*this);
115  }
116 
123  inline bool isEmpty() const
124  { return((_buf->_data == NULL) || (_buf->_length == 0)); }
125 
132  inline bool isNull() const
133  { return(_buf->_data == NULL); }
134 
141  Blob& append(const byte_t* data, uint_t length);
142 
151  Blob& append(const char* str, uint_t length = 0);
152 
161  int indexOf(byte_t b, uint_t fromIndex = 0) const;
162 
172  int indexOf(const byte_t* buf, uint_t length, uint_t fromIndex = 0) const;
173 
183  int lastIndexOf(byte_t b, uint_t fromIndex = END) const;
184 
195  int lastIndexOf(const byte_t* buf, uint_t length, uint_t fromIndex = END)
196  const;
197 
205  inline bool contains(byte_t b) const
206  { return(indexOf(b) >= 0); }
207 
216  int compareTo(const Blob& other) const;
217 
219  Blob& operator=(const Blob& other);
220 
229  byte_t& operator[](int index);
230 
239  byte_t operator[](int index) const;
240 
247  inline bool operator!() const
248  { return(isNull()); }
249 
255  inline Blob& operator+=(byte_t b)
256  { return(append(&b, 1)); }
257 
264  inline Blob& operator+=(int v)
265  { return(operator+=(static_cast<byte_t>(v & 0xFF))); }
266 
272  inline Blob& operator+=(const char *str)
273  { return(append(str)); }
274 
281  inline Blob& operator+=(const Blob& blob)
282  { return(append(blob.getData(), blob.getLength())); }
283 
285  inline Blob& operator<<(byte_t val)
286  { return(append(&val, 1)); }
287 
289  inline Blob& operator<<(int val)
290  { return(operator+=(static_cast<byte_t>(val & 0xFF))); }
291 
293  inline Blob& operator<<(const char* str)
294  { return(append(str)); }
295 
302  uint_t hash(uint_t modulo = 256) const;
303 
305  static const Blob null;
306 
308  static const uint_t END;
309 
310  private:
311 
312  class BlobBuf
313  {
314  friend class Blob;
315 
316  protected:
317 
318  byte_t *_data;
319  uint_t _capacity;
320  uint_t _length;
321  AtomicCounter _refs;
322 
323  BlobBuf(const BlobBuf& other, uint_t length);
324  BlobBuf(byte_t *data = NULL, uint_t offset = 0, uint_t length = 0,
325  bool copy = true);
326  BlobBuf(const byte_t *data, uint_t offset = 0, uint_t length = 0);
327  ~BlobBuf();
328 
329  void reserve(uint_t size);
330  void makeNull();
331  void makeCopy(const byte_t *data, uint_t offset, uint_t length);
332 
333  private:
334 
335  CCXX_COPY_DECLS(BlobBuf);
336  };
337 
338  BlobBuf *_buf;
339 
340  void _release();
341  void _makeCopy(uint_t size, bool unshareable = false);
342 };
343 
344 inline bool operator==(const Blob& b1, const Blob& b2)
345 { return(b1.compareTo(b2) == 0); }
346 
347 inline bool operator!=(const Blob& b1, const Blob& b2)
348 { return(b1.compareTo(b2) != 0); }
349 
350 inline bool operator<(const Blob& b1, const Blob& b2)
351 { return(b1.compareTo(b2) < 0); }
352 
353 inline bool operator>(const Blob& b1, const Blob& b2)
354 { return(b1.compareTo(b2) > 0); }
355 
356 inline bool operator<=(const Blob& b1, const Blob& b2)
357 { return(b1.compareTo(b2) <= 0); }
358 
359 inline bool operator>=(const Blob& b1, const Blob& b2)
360 { return(b1.compareTo(b2) >= 0); }
361 
362 inline std::ostream& operator<<(std::ostream& stream, const Blob& b)
363 {
364  return(stream.write(reinterpret_cast<const char *>(b.getData()),
365  static_cast<std::streamsize>(b.getLength())));
366 }
367 
368 } // namespace ccxx
369 
370 #ifdef _MSC_VER
371 #pragma warning(pop)
372 #endif // _MSC_VER
373 
374 #endif // __ccxx_Blob_hxx
Blob & operator<<(byte_t val)
Append operator.
Definition: Blob.h++:285
bool isEmpty() const
Test if the Blob is empty.
Definition: Blob.h++:123
uint_t getLength() const
Get the length of the Blob.
Definition: Blob.h++:81
bool contains(byte_t b) const
Determine if the Blob contains a given byte.
Definition: Blob.h++:205
bool operator<=(const Blob &b1, const Blob &b2)
Definition: Blob.h++:356
Blob & operator+=(byte_t b)
Append a byte onto the end of the Blob.
Definition: Blob.h++:255
Blob & operator+=(int v)
Append a value onto the end of the Blob.
Definition: Blob.h++:264
int compareTo(const Blob &other) const
Compare this Blob to another.
Definition: Blob.c++:132
bool operator==(const Blob &b1, const Blob &b2)
Definition: Blob.h++:344
bool operator!() const
Unary NOT operator.
Definition: Blob.h++:247
uint_t length() const
Get the length of the Blob.
Definition: Blob.h++:89
Blob & clear()
Clear the Blob.
Definition: Blob.h++:111
unsigned int uint_t
An alias for unsigned int.
Definition: Integers.h++:74
Blob & operator<<(const char *str)
Append operator.
Definition: Blob.h++:293
Blob & operator+=(const Blob &blob)
Append (a copy of) the contents of another Blob onto the end of the Blob.
Definition: Blob.h++:281
bool isNull() const
Test if the Blob is null.
Definition: Blob.h++:132
std::ostream & operator<<(std::ostream &stream, const BitSet &bs)
Definition: BitSet.h++:383
An integer counter whose value is modified in an atomic fashion.
Definition: AtomicCounter.h++:36
static const uint_t END
A pseudo-index indicating the end of the blob.
Definition: Blob.h++:308
#define COMMONCPP_API
Definition: Common.h++:126
static const Blob null
The null Blob.
Definition: Blob.h++:305
A reference-counting, copy-on-write, threadsafe container for arbitrary binary data.
Definition: Blob.h++:45
bool operator!=(const Blob &b1, const Blob &b2)
Definition: Blob.h++:347
Blob & operator+=(const char *str)
Append a character string onto the end of the Blob.
Definition: Blob.h++:272
bool operator<(const Blob &b1, const Blob &b2)
Definition: Blob.h++:350
#define CCXX_COPY_DECLS(CLASS)
Inlines declarations of a copy constructor and assignment operator for the class CLASS.
Definition: Common.h++:295
const byte_t * getData() const
Get a pointer to the beginning of the data in the Blob.
Definition: Blob.h++:107
bool operator>=(const Blob &b1, const Blob &b2)
Definition: Blob.h++:359
Blob & operator<<(int val)
Append operator.
Definition: Blob.h++:289
Definition: AllocationMap.c++:25
bool operator>(const Blob &b1, const Blob &b2)
Definition: Blob.h++:353
byte_t * getData()
Get a pointer to the beginning of the data in the Blob.
Definition: Blob.h++:103
unsigned char byte_t
An unsigned 8-bit value.
Definition: Integers.h++:68