10 #ifndef __PION_PIONBLOB_HEADER__ 11 #define __PION_PIONBLOB_HEADER__ 14 #include <boost/detail/atomic_count.hpp> 15 #include <boost/functional/hash.hpp> 16 #include <pion/PionConfig.hpp> 26 template <
typename CharType,
typename AllocType>
33 BlobData(AllocType& blob_alloc,
const std::size_t len) :
36 *((CharType*)(
this) +
sizeof(
struct BlobData) + len) =
'\0';
40 inline const CharType *
get(void)
const {
41 return ((CharType*)(
this) +
sizeof(
struct BlobData));
45 inline CharType *
get(void) {
46 return ((CharType*)(
this) +
sizeof(
struct BlobData));
70 static inline BlobData *
create(AllocType& blob_alloc,
const std::size_t len) {
108 BlobParams(AllocType& blob_alloc,
const CharType *ptr,
const std::size_t len)
109 : m_alloc(blob_alloc), m_ptr(ptr),
m_len(len)
113 const CharType * m_ptr;
133 m_blob_ptr(blob.
grab())
144 m_blob_ptr =
create(p.m_alloc, p.m_len);
145 memcpy(m_blob_ptr->
get(), p.m_ptr, p.m_len);
155 PionBlob(AllocType& blob_alloc,
const CharType* ptr,
const std::size_t len) :
158 m_blob_ptr =
create(blob_alloc, len);
159 memcpy(m_blob_ptr->
get(), ptr, len);
168 PionBlob(AllocType& blob_alloc,
const std::string& str) :
171 m_blob_ptr =
create(blob_alloc, str.size());
172 memcpy(m_blob_ptr->
get(), str.c_str(), str.size());
184 m_blob_ptr = blob.
grab();
195 m_blob_ptr =
create(p.m_alloc, p.m_len);
196 memcpy(m_blob_ptr->
get(), p.m_ptr, p.m_len);
206 inline void set(AllocType& blob_alloc,
const CharType* ptr,
const std::size_t len) {
208 m_blob_ptr =
create(blob_alloc, len);
209 memcpy(m_blob_ptr->
get(), ptr, len);
218 inline void set(AllocType& blob_alloc,
const std::string& str) {
220 m_blob_ptr =
create(blob_alloc, str.size());
221 memcpy(m_blob_ptr->
get(), str.c_str(), str.size());
232 inline CharType *
reserve(AllocType& blob_alloc,
const std::size_t len) {
234 m_blob_ptr =
create(blob_alloc, len);
235 return m_blob_ptr->
get();
239 inline const CharType *
get(void)
const {
240 return (m_blob_ptr ? m_blob_ptr->
get() :
"");
244 inline std::size_t
size(
void)
const {
245 return (m_blob_ptr ? (m_blob_ptr->
m_len) : 0);
255 return (m_blob_ptr == NULL || m_blob_ptr->
m_len == 0);
260 return (m_blob_ptr == NULL ? 0 : m_blob_ptr->
m_copies + 1);
265 return (m_blob_ptr == NULL || m_blob_ptr->
m_copies == 0);
283 if (
size() != str.size())
285 return (
empty() || memcmp(
get(), str.c_str(), m_blob_ptr->
m_len)==0);
302 const int val = memcmp(
get(), blob.
get(), len);
315 const int val = memcmp(
get(), blob.
get(), len);
326 const std::size_t len = (
size() < str.size() ?
size() : str.size());
328 const int val = memcmp(
get(), str.c_str(), len);
334 return (
size() < str.size());
339 const std::size_t len = (
size() < str.size() ?
size() : str.size());
341 const int val = memcmp(
get(), str.c_str(), len);
347 return (
size() > str.size());
353 template <
typename CharType,
typename AllocType>
355 return (blob.
empty() ? 0 : boost::hash_range(blob.
get(), blob.
get() + blob.
size()));
362 inline unsigned long getValue(
unsigned char c)
const {
363 unsigned long result;
365 case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
368 case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
371 case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
382 template <
typename CharType,
typename AllocType>
384 if (blob.
size() != 36)
385 return hash_value(blob);
387 const char *
const data = blob.
get();
389 std::size_t seed = 0;
392 n = (getValue(data[0]) << 28);
393 n |= (getValue(data[1]) << 24);
394 n |= (getValue(data[2]) << 20);
395 n |= (getValue(data[3]) << 16);
396 n |= (getValue(data[4]) << 12);
397 n |= (getValue(data[5]) << 8);
398 n |= (getValue(data[6]) << 4);
399 n |= getValue(data[7]);
400 boost::hash_combine(seed, n);
403 n = (getValue(data[9]) << 28);
404 n |= (getValue(data[10]) << 24);
405 n |= (getValue(data[11]) << 20);
406 n |= (getValue(data[12]) << 16);
407 n |= (getValue(data[14]) << 12);
408 n |= (getValue(data[15]) << 8);
409 n |= (getValue(data[16]) << 4);
410 n |= getValue(data[17]);
411 boost::hash_combine(seed, n);
414 n = (getValue(data[19]) << 28);
415 n |= (getValue(data[20]) << 24);
416 n |= (getValue(data[21]) << 20);
417 n |= (getValue(data[22]) << 16);
418 n |= (getValue(data[24]) << 12);
419 n |= (getValue(data[25]) << 8);
420 n |= (getValue(data[26]) << 4);
421 n |= getValue(data[27]);
422 boost::hash_combine(seed, n);
425 n = (getValue(data[28]) << 28);
426 n |= (getValue(data[29]) << 24);
427 n |= (getValue(data[30]) << 20);
428 n |= (getValue(data[31]) << 16);
429 n |= (getValue(data[32]) << 12);
430 n |= (getValue(data[33]) << 8);
431 n |= (getValue(data[34]) << 4);
432 n |= getValue(data[35]);
433 boost::hash_combine(seed, n);
446 template <
typename CharType,
typename AllocType>
448 return _Keyval1 < _Keyval2;
data type used to initialize blobs in variants without copy construction
bool unique(void) const
returns true if this is a unique instance or if this is null
unsigned long getValue(unsigned char c) const
helper for hex->int conversion
void clear(void)
alias for release() -> switch to empty state
bool operator>(const PionBlob &blob) const
returns true if this is greater than blob
PionBlob(AllocType &blob_alloc, const std::string &str)
std::size_t length(void) const
returns length of the BLOB in octets (alias for size())
bool operator>(const std::string &str) const
returns true if this is greater than str
BlobData * grab(void) const
bool operator==(const PionBlob &blob) const
returns true if str is equal to this (BLOB matches string)
PionBlob(const PionBlob &blob)
PionBlob & operator=(const PionBlob &blob)
optimized hash function object for PionBlob objects which contain PionId string representations (bb49...
std::size_t size(void) const
returns size of the BLOB in octets
const CharType * get(void) const
returns (const) reference to the BLOB payload
bool operator!=(const std::string &str) const
returns true if str is not equal to this (BLOB does not match string)
bool empty(void) const
returns true if the BLOB is empty (undefined or size == 0)
bool operator<(const PionBlob &blob) const
returns true if this is less than blob
structure used to store BLOB metadata; payload starts immediately following this
void reset(void)
alias for release() -> switch to empty state
bool operator==(const std::string &str) const
returns true if str is equal to this (BLOB matches string)
std::size_t operator()(const PionBlob< CharType, AllocType > &blob) const
returns hash value for the blob provided
AllocType *const m_alloc_ptr
pointer to the allocator used by the BLOB
const CharType * get(void) const
returns (const) reference to the BLOB payload
BlobData(AllocType &blob_alloc, const std::size_t len)
constructor takes allocator and size (in octets) of BLOB
CharType * reserve(AllocType &blob_alloc, const std::size_t len)
long use_count(void) const
returns the number of reference to this BLOB (or 0 if this is null)
bool operator!=(const PionBlob &blob) const
returns true if blob is not equal to this (two BLOBs do not match)
the following enables use of the lock-free cache
PionBlob(const BlobParams &p)
boost::detail::atomic_count m_copies
number of copies referencing this BLOB
const std::size_t m_len
size of the BLOB, in octets
static BlobData * create(AllocType &blob_alloc, const std::size_t len)
PionBlob(AllocType &blob_alloc, const CharType *ptr, const std::size_t len)
BlobData * m_blob_ptr
pointer to the BLOB metadata structure (payload follows the structure)
BlobParams(AllocType &blob_alloc, const CharType *ptr, const std::size_t len)
constructor requires all parameters
virtual ~PionBlob()
virtual destructor
bool operator<(const std::string &str) const
returns true if this is less than str
PionBlob(void)
default constructor