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>
354 static inline std::size_t hash_value(
const PionBlob<CharType,AllocType>& blob) {
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;
400 boost::hash_combine(seed, n);
411 boost::hash_combine(seed, n);
422 boost::hash_combine(seed, n);
433 boost::hash_combine(seed, n);
446 template <
typename CharType,
typename AllocType>
447 bool operator()(
const PionBlob<CharType, AllocType>& _Keyval1,
const PionBlob<CharType, AllocType>& _Keyval2)
const {
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)
void set(AllocType &blob_alloc, const CharType *ptr, const std::size_t len)
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)
void set(AllocType &blob_alloc, const std::string &str)
void set(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