Blob.h++
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef __ccxx_Blob_hxx
00024 #define __ccxx_Blob_hxx
00025
00026 #include <commonc++/Common.h++>
00027 #include <commonc++/AtomicCounter.h++>
00028 #include <commonc++/OutOfBoundsException.h++>
00029
00030 #include <sstream>
00031
00032 #ifdef _MSC_VER
00033 #pragma warning(push)
00034 #pragma warning(disable: 4251)
00035 #endif // _MSC_VER
00036
00037 namespace ccxx {
00038
00046 class COMMONCPP_API Blob
00047 {
00048 public:
00049
00051 Blob() throw();
00052
00060 Blob(byte_t *data, size_t length, bool copy = true);
00061
00063 Blob(const Blob& other);
00064
00070 Blob(const byte_t *data, size_t length);
00071
00073 ~Blob() throw();
00074
00079 inline size_t getLength() const throw()
00080 { return(_buf == NULL ? 0 : _buf->_length); };
00081
00086 inline size_t length() const throw()
00087 { return(getLength()); }
00088
00096 void setLength(uint_t length);
00097
00099 inline byte_t * getData() throw()
00100 { return(_buf == NULL ? NULL : _buf->_data); }
00101
00103 inline const byte_t * getData() const throw()
00104 { return(_buf == NULL ? NULL : _buf->_data); }
00105
00107 inline Blob & clear()
00108 {
00109 setLength(0);
00110 return(*this);
00111 }
00112
00118 inline bool isEmpty() const throw()
00119 { return((_buf->_data == NULL) || (_buf->_length == 0)); }
00120
00126 inline bool isNull() const throw()
00127 { return(_buf->_data == NULL); }
00128
00134 Blob& append(const byte_t *data, size_t len);
00135
00143 Blob& append(const char *str, size_t len = 0);
00144
00152 int indexOf(byte_t b, uint_t fromIndex = 0) const throw();
00153
00162 int indexOf(const byte_t *buf, size_t len, uint_t fromIndex = 0) const
00163 throw();
00164
00173 int lastIndexOf(byte_t b, uint_t fromIndex = END) const throw();
00174
00184 int lastIndexOf(const byte_t *buf, size_t len, uint_t fromIndex = END)
00185 const throw();
00186
00193 inline bool contains(byte_t b) const throw()
00194 { return(indexOf(b) >= 0); }
00195
00203 int compareTo(const Blob &other) const throw();
00204
00206 Blob& operator=(const Blob &other);
00207
00215 byte_t& operator[](int index) throw(OutOfBoundsException);
00216
00224 byte_t operator[](int index) const throw(OutOfBoundsException);
00225
00231 inline bool operator!() const throw()
00232 { return(isNull()); }
00233
00238 inline Blob& operator+=(byte_t b)
00239 { return(append(&b, 1)); }
00240
00246 inline Blob& operator+=(int v)
00247 { return(operator+=(static_cast<byte_t>(v & 0xFF))); }
00248
00253 inline Blob& operator+=(const char *str)
00254 { return(append(str)); }
00255
00261 inline Blob& operator+=(const Blob &blob)
00262 { return(append(blob.getData(), blob.getLength())); }
00263
00265 inline Blob& operator<<(byte_t val)
00266 { return(append(&val, 1)); }
00267
00269 inline Blob& operator<<(int val)
00270 { return(operator+=(static_cast<byte_t>(val & 0xFF))); }
00271
00273 inline Blob& operator<<(const char *str)
00274 { return(append(str)); }
00275
00281 uint_t hash(uint_t modulo = 256) const throw();
00282
00284 static const Blob null;
00285
00287 static const uint_t END;
00288
00289 private:
00290
00291 class BlobBuf
00292 {
00293 friend class Blob;
00294
00295 protected:
00296
00297 byte_t *_data;
00298 size_t _capacity;
00299 size_t _length;
00300 AtomicCounter _refs;
00301
00302 BlobBuf(const BlobBuf& other, size_t length);
00303 BlobBuf(byte_t *data = NULL, size_t offset = 0, size_t length = 0,
00304 bool copy = true);
00305 BlobBuf(const byte_t *data, size_t offset = 0, size_t length = 0);
00306 ~BlobBuf() throw();
00307
00308 void reserve(size_t size);
00309 void makeNull();
00310 void makeCopy(const byte_t *data, size_t offset, size_t length);
00311
00312 private:
00313
00314 CCXX_COPY_DECLS(BlobBuf);
00315 };
00316
00317 BlobBuf *_buf;
00318
00319 void _release();
00320 void _makeCopy(size_t size, bool unshareable = false);
00321 };
00322
00323 inline bool operator==(const Blob& b1, const Blob& b2) throw()
00324 { return(b1.compareTo(b2) == 0); }
00325
00326 inline bool operator!=(const Blob& b1, const Blob& b2) throw()
00327 { return(b1.compareTo(b2) != 0); }
00328
00329 inline bool operator<(const Blob& b1, const Blob& b2) throw()
00330 { return(b1.compareTo(b2) < 0); }
00331
00332 inline bool operator>(const Blob& b1, const Blob& b2) throw()
00333 { return(b1.compareTo(b2) > 0); }
00334
00335 inline bool operator<=(const Blob& b1, const Blob& b2) throw()
00336 { return(b1.compareTo(b2) <= 0); }
00337
00338 inline bool operator>=(const Blob& b1, const Blob& b2) throw()
00339 { return(b1.compareTo(b2) >= 0); }
00340
00341 inline std::ostream& operator<<(std::ostream& stream, const Blob& b)
00342 {
00343 return(stream.write(reinterpret_cast<const char *>(b.getData()),
00344 static_cast<std::streamsize>(b.getLength())));
00345 }
00346
00347 };
00348
00349 #ifdef _MSC_VER
00350 #pragma warning(pop)
00351 #endif // _MSC_VER
00352
00353 #endif // __ccxx_Blob_hxx
00354
00355