HOPS
HOPS class reference
MHO_UUID.hh
Go to the documentation of this file.
1 #ifndef MHO_UUID_HH__
2 #define MHO_UUID_HH__
3 
4 #include <algorithm>
5 #include <cstdlib>
6 #include <iomanip>
7 #include <sstream>
8 #include <string>
9 
10 #include "MHO_Message.hh"
11 #include "MHO_Types.hh"
12 
13 namespace hops
14 {
15 
24 #define MHO_UUID_LENGTH 16
25 
26 class MHO_UUID
27 {
28 
29  public:
31  {
32  for(std::size_t i = 0; i < MHO_UUID_LENGTH; i++)
33  {
34  fBytes[i] = 0;
35  }
36  }
37 
38  virtual ~MHO_UUID(){};
39 
40  MHO_UUID(const MHO_UUID& copy)
41  {
42  for(std::size_t i = 0; i < MHO_UUID_LENGTH; i++)
43  {
44  fBytes[i] = copy[i];
45  }
46  }
47 
49  {
50  for(std::size_t i = 0; i < MHO_UUID_LENGTH; i++)
51  {
52  fBytes[i] = rhs[i];
53  }
54  return *this;
55  }
56 
57  //access operator
58  uint8_t& operator[](std::size_t i) { return fBytes[i]; }
59 
60  const uint8_t& operator[](std::size_t i) const { return fBytes[i]; }
61 
62  bool operator==(const MHO_UUID& rhs) const
63  {
64  for(std::size_t i = 0; i < MHO_UUID_LENGTH; i++)
65  {
66  if(fBytes[i] != rhs[i])
67  {
68  return false;
69  }
70  }
71  return true;
72  }
73 
74  bool operator!=(const MHO_UUID& rhs) const { return !(*this == rhs); }
75 
76  bool operator<(const MHO_UUID& rhs) const
77  {
78  //use STL lexi compare
79  return std::lexicographical_compare(&(fBytes[0]), &(fBytes[MHO_UUID_LENGTH - 1]), &(rhs[0]),
80  &(rhs[MHO_UUID_LENGTH - 1]));
81  }
82 
83  /*!*
84  * Truncate the UUID byte array to the first (last) 8 bytes and convert into a 64 bit int
85  * @return A uint64_t composed of the first or last 8 bytes of the UUID
86  */
87  uint64_t as_truncated_long(bool first_half = true) const
88  {
89  byte2ints b2i;
90  for(std::size_t i = 0; i < MHO_UUID_LENGTH; i++)
91  {
92  b2i.byte_values[i] = fBytes[i];
93  }
94  if(first_half)
95  {
96  return b2i.uint_values[0];
97  }
98  else
99  {
100  return b2i.uint_values[1];
101  }
102  }
103 
104  /*!*
105  * Split the UUID byte array into two halves conver to uint64_t and return the sum
106  * @return A uint64_t composed of the sum of the two halves of the uuid
107  */
108  uint64_t as_long() const
109  {
110  //stone knives and bear skins...
111  byte2ints b2i;
112  for(std::size_t i = 0; i < MHO_UUID_LENGTH; i++)
113  {
114  b2i.byte_values[i] = fBytes[i];
115  }
116  uint64_t result = b2i.uint_values[0];
117  result += b2i.uint_values[1];
118  return result;
119  }
120 
121  /*!*
122  * Convert the UUID byte array into a string
123  * @return A std::string containing the hexadecimal digits of the UUID.
124  */
125  std::string as_string() const
126  {
127  std::stringstream ss;
128  for(unsigned int i = 0; i < MHO_UUID_LENGTH; i++)
129  {
130  uint32_t tmp = fBytes[i];
131  std::stringstream hss;
132  hss << std::setw(2) << std::setfill('0') << std::hex << (int)(tmp);
133  std::string hexstr = hss.str();
134  ss << hexstr;
135  }
136  return ss.str();
137  }
138 
139  /*!*
140  * Convert a formatted string into a UUID byte array and fill this object
141  * @return update the uuid object from std::string containing the hexadecimal digits of the UUID.
142  */
143  bool from_string(const std::string& uuid_str)
144  {
145  if(uuid_str.size() == 2 * MHO_UUID_LENGTH)
146  {
147  for(int i = 0; i < MHO_UUID_LENGTH; i++)
148  {
149  //one byte at a time
150  std::stringstream ss;
151  ss << uuid_str[2 * i];
152  ss << uuid_str[2 * i + 1];
153  uint32_t val = std::strtoul(ss.str().c_str(), 0, 16);
154  fBytes[i] = val;
155  }
156  return true;
157  }
158  else
159  {
160  msg_error("utility", "could not convert string to uuid, length of "
161  << uuid_str.size() << " != " << 2 * MHO_UUID_LENGTH << " is incorrect" << eom);
162  return false;
163  }
164  }
165 
166  uint64_t size() const { return MHO_UUID_LENGTH; }
167 
168  bool is_empty() const
169  {
170  for(std::size_t i = 0; i < MHO_UUID_LENGTH; i++)
171  {
172  if(fBytes[i] != 0)
173  {
174  return false;
175  }
176  }
177  return true;
178  }
179 
180  static uint64_t ByteSize() { return MHO_UUID_LENGTH * sizeof(uint8_t); };
181 
182  protected:
183  typedef union
184  {
185  uint8_t byte_values[MHO_UUID_LENGTH];
186  uint64_t uint_values[2];
187  } byte2ints;
188 
190 };
191 
192 template< typename XStream > XStream& operator>>(XStream& s, MHO_UUID& uuid)
193 {
194  for(std::size_t i = 0; i < MHO_UUID_LENGTH; i++)
195  {
196  s >> uuid[i];
197  }
198  return s;
199 };
200 
201 template< typename XStream > XStream& operator<<(XStream& s, const MHO_UUID& uuid)
202 {
203  for(std::size_t i = 0; i < MHO_UUID_LENGTH; i++)
204  {
205  s << uuid[i];
206  }
207  return s;
208 };
209 
210 } // namespace hops
211 
212 #endif
#define msg_error(xKEY, xCONTENT)
Definition: MHO_Message.hh:244
#define MHO_UUID_LENGTH
Definition: MHO_UUID.hh:24
Class MHO_UUID - a class for a 16 byte UUID (for object and type identification)
Definition: MHO_UUID.hh:27
uint64_t as_long() const
Definition: MHO_UUID.hh:108
uint64_t uint_values[2]
Definition: MHO_UUID.hh:186
uint64_t as_truncated_long(bool first_half=true) const
Definition: MHO_UUID.hh:87
uint8_t byte_values[MHO_UUID_LENGTH]
Definition: MHO_UUID.hh:185
uint8_t fBytes[MHO_UUID_LENGTH]
Definition: MHO_UUID.hh:189
bool operator!=(const MHO_UUID &rhs) const
Definition: MHO_UUID.hh:74
bool operator<(const MHO_UUID &rhs) const
Definition: MHO_UUID.hh:76
virtual ~MHO_UUID()
Definition: MHO_UUID.hh:38
const uint8_t & operator[](std::size_t i) const
Definition: MHO_UUID.hh:60
uint8_t & operator[](std::size_t i)
Definition: MHO_UUID.hh:58
MHO_UUID()
Definition: MHO_UUID.hh:30
std::string as_string() const
Definition: MHO_UUID.hh:125
static uint64_t ByteSize()
Definition: MHO_UUID.hh:180
bool is_empty() const
Definition: MHO_UUID.hh:168
MHO_UUID & operator=(const MHO_UUID &rhs)
Definition: MHO_UUID.hh:48
bool operator==(const MHO_UUID &rhs) const
Definition: MHO_UUID.hh:62
bool from_string(const std::string &uuid_str)
Definition: MHO_UUID.hh:143
uint64_t size() const
Definition: MHO_UUID.hh:166
MHO_UUID(const MHO_UUID &copy)
Definition: MHO_UUID.hh:40
Definition: MHO_UUID.hh:184
Definition: MHO_ChannelLabeler.hh:17
std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const hops_time< Duration > &t)
Definition: MHO_Clock.hh:587
XStream & operator>>(XStream &s, MHO_UUID &uuid)
Definition: MHO_UUID.hh:192