10 #ifndef __PION_PIONID_HEADER__
11 #define __PION_PIONID_HEADER__
18 #include <boost/functional/hash.hpp>
19 #include <boost/date_time/posix_time/posix_time.hpp>
20 #include <boost/random/uniform_int.hpp>
21 #include <boost/random/variate_generator.hpp>
22 #include <boost/random/mersenne_twister.hpp>
23 #include <boost/numeric/conversion/cast.hpp>
24 #include <pion/PionConfig.hpp>
42 PION_ID_DATA_BYTES = 16,
43 PION_ID_HEX_BYTES = 16 * 2 + 4
51 typedef boost::mt19937 gen_type;
52 typedef boost::uniform_int<unsigned long> dist_type;
53 typedef boost::variate_generator<gen_type,dist_type> die_type;
55 dist_type rng_dist((std::numeric_limits<unsigned long>::min)(), (std::numeric_limits<unsigned long>::max)());
56 die_type rng_die(rng_gen, rng_dist);
61 explicit PionId(
const std::string& str) {
71 template<
typename base_generator_type,
typename distribution_type>
72 explicit PionId(boost::variate_generator<base_generator_type, distribution_type>& rng) {
88 inline unsigned char operator[](
const std::size_t n)
const {
94 return (memcmp(
m_data,
id.
m_data, PION_ID_DATA_BYTES) == 0);
99 return (memcmp(
m_data,
id.
m_data, PION_ID_DATA_BYTES) != 0);
104 return (memcmp(
m_data,
id.
m_data, PION_ID_DATA_BYTES) < 0);
109 return (memcmp(
m_data,
id.
m_data, PION_ID_DATA_BYTES) > 0);
116 inline iterator
end(
void) {
return m_data + PION_ID_DATA_BYTES; }
122 inline const_iterator
end(
void)
const {
return m_data + PION_ID_DATA_BYTES; }
127 static const char hex[] =
"0123456789abcdef";
128 for (std::size_t i = 0; i < PION_ID_DATA_BYTES; i++) {
129 hex_str += hex[
m_data[i] >> 4];
130 hex_str += hex[m_data[i] & 0x0f];
131 if (i == 3 || i == 5 || i == 7 || i == 9)
139 std::size_t data_pos = 0;
142 while (*str !=
'\0' && data_pos < PION_ID_DATA_BYTES) {
143 if (isxdigit(*str)) {
145 if (*(++str) ==
'\0' || !isxdigit(*str))
148 m_data[data_pos++] = boost::numeric_cast<
unsigned char>(strtoul(buf, NULL, 16));
157 typedef boost::mt19937 gen_type;
158 typedef boost::uniform_int<unsigned long> dist_type;
159 typedef boost::variate_generator<gen_type,dist_type> die_type;
161 static boost::uint64_t seed_seed_64 = (time(NULL) * 1000000) + boost::posix_time::microsec_clock::local_time().time_of_day().total_microseconds();
163 static gen_type::result_type seed_seed_32 = boost::numeric_cast<gen_type::result_type>((seed_seed_64 >> 32) ^ (seed_seed_64 & 0xFFFFFFFF));
164 static gen_type rng_gen(seed_seed_32);
165 static dist_type rng_dist((std::numeric_limits<unsigned long>::min)(), (std::numeric_limits<unsigned long>::max)());
166 static die_type rng_die(rng_gen, rng_dist);
180 template<
typename base_generator_type,
typename distribution_type>
181 static inline void generate(
unsigned char *data, boost::variate_generator<base_generator_type, distribution_type>& rng) {
183 for (std::size_t i = 0; i < PION_ID_DATA_BYTES; i +=
sizeof(
unsigned long)) {
184 *
reinterpret_cast<unsigned long*
>(&data[i]) = rng();
199 unsigned char m_data[PION_ID_DATA_BYTES];
204 static inline std::size_t hash_value(
const PionId&
id) {
205 std::size_t seed = 0;
206 const unsigned char * data =
id.begin();
207 const unsigned char *
const end =
id.end();
209 boost::hash_combine(seed, *reinterpret_cast<const unsigned long*>(data));
210 data +=
sizeof(
unsigned long);
219 template <
typename BaseGeneratorType>
230 typedef boost::variate_generator<base_generator_type, distribution_type>
gen_type;
239 m_random_dist((
std::numeric_limits<unsigned long>::min)(), (
std::numeric_limits<unsigned long>::max)()),
PionId(const PionId &id)
copy constructor
BaseGeneratorType base_generator_type
make dynamic type for base generator available
PionId & operator=(const PionId &id)
assignment operator
PionId(boost::variate_generator< base_generator_type, distribution_type > &rng)
construction using an existing random number generator
PionId(const std::string &str)
construction using a string representation (bb49b9ca-e733-47c0-9a26-0f8f53ea1660) ...
const_iterator end(void) const
returns the ending iterator (const)
bool operator<(const PionId &id) const
returns true if id is less than this
const unsigned char * const_iterator
const data type for iterating PionId byte values
boost::uniform_int< unsigned long > distribution_type
random number distribution type
iterator begin(void)
returns the beginning iterator
gen_type m_random_die
random number die
void from_string(const char *str)
sets the data value based upon a null-terminated string representation (bb49b9ca-e733-47c0-9a26-0f8f5...
PionId(const char *str)
construction using a null-terminated c-style string (bb49b9ca-e733-47c0-9a26-0f8f53ea1660) ...
unsigned long getNumber(void)
return random number generator
unsigned char operator[](const std::size_t n) const
returns id value at byte offset
unsigned char * iterator
data type for iterating PionId byte values
static void generate(unsigned char *data, boost::variate_generator< base_generator_type, distribution_type > &rng)
const_iterator begin(void) const
returns the beginning iterator (const)
PionId operator()(void)
returns a newly generated PionId object
PionId(void)
default constructor
bool operator!=(const PionId &id) const
returns true if id does not equal this
base_generator_type m_random_gen
random number generator
static boost::uint32_t make_seed(void)
return a seed value for random number generators
boost::variate_generator< base_generator_type, distribution_type > gen_type
random number generator type
virtual ~PionIdGeneratorBase()
class may be extended (virtual destructor)
unsigned char m_data[PION_ID_DATA_BYTES]
sequence of bytes representing the unique identifier
bool operator==(const PionId &id) const
returns true if id equals this
PionIdGeneratorBase< boost::mt19937 > PionIdGenerator
data type for the default PionId generator class
distribution_type m_random_dist
random number distribution
PionIdGeneratorBase(void)
default constructor
the following enables use of the lock-free cache
gen_type & getRNG(void)
return random number generator
iterator end(void)
returns the ending iterator
virtual ~PionId()
class may be extended (virtual destructor)
bool operator>(const PionId &id) const
returns true if id is greater than this
std::string to_string(void) const
returns hexadecimal representation as a string (bb49b9ca-e733-47c0-9a26-0f8f53ea1660) ...