5 #ifndef _GCOMM_UUID_HPP_
6 #define _GCOMM_UUID_HPP_
9 #include "gcomm/types.hpp"
11 #include "gu_utils.hpp"
12 #include "gu_assert.hpp"
13 #include "gu_byteswap.h"
23 std::ostream& operator<<(std::ostream&,
const UUID&);
30 UUID() : uuid_(GU_UUID_NIL) {}
32 UUID(
const void* node,
const size_t node_len) : uuid_()
34 gu_uuid_generate(&uuid_, node, node_len);
37 UUID(
const int32_t idx) : uuid_()
41 memcpy(&uuid_, &idx,
sizeof(idx));
44 static const UUID& nil()
49 size_t unserialize(
const gu::byte_t* buf,
const size_t buflen,
const size_t offset)
51 if (buflen < offset +
sizeof(gu_uuid_t))
52 gu_throw_error (EMSGSIZE) <<
sizeof(gu_uuid_t) <<
" > "
55 memcpy(&uuid_, buf + offset,
sizeof(gu_uuid_t));
57 return offset +
sizeof(gu_uuid_t);
60 size_t serialize(gu::byte_t* buf,
const size_t buflen,
const size_t offset)
const
62 if (buflen < offset +
sizeof(gu_uuid_t))
63 gu_throw_error (EMSGSIZE) <<
sizeof(gu_uuid_t) <<
" > "
66 memcpy(buf + offset, &uuid_,
sizeof(gu_uuid_t));
68 return offset +
sizeof(gu_uuid_t);
71 static size_t serial_size()
73 return sizeof(gu_uuid_t);
76 const gu_uuid_t* uuid_ptr()
const
81 bool operator<(
const UUID& cmp)
const
83 return (gu_uuid_compare(&uuid_, &cmp.uuid_) < 0);
86 bool operator==(
const UUID& cmp)
const
88 return (gu_uuid_compare(&uuid_, &cmp.uuid_) == 0);
91 bool older(
const UUID& cmp)
const
93 return (gu_uuid_older(&uuid_, &cmp.uuid_) > 0);
96 std::ostream& to_stream(std::ostream& os)
const
98 static const char buf[37] = { 0, };
99 const uint32_t* i =
reinterpret_cast<const uint32_t*
>(uuid_.data);
102 memcmp(i + 1, buf,
sizeof(uuid_) -
sizeof(*i)) == 0)
109 const uint16_t* s =
reinterpret_cast<const uint16_t*
>(uuid_.data);
111 std::ios_base::fmtflags saved = os.flags();
114 << std::setfill(
'0') << std::setw(8) << gu_be32(i[0]) <<
'-'
115 << std::setfill(
'0') << std::setw(4) << gu_be16(s[2]) <<
'-'
116 << std::setfill(
'0') << std::setw(4) << gu_be16(s[3]) <<
'-'
117 << std::setfill(
'0') << std::setw(4) << gu_be16(s[4]) <<
'-'
118 << std::setfill(
'0') << std::setw(4) << gu_be16(s[5])
119 << std::setfill(
'0') << std::setw(8) << gu_be32(i[3]);
128 std::string _str()
const
130 std::ostringstream os;
138 static const UUID uuid_nil_;
139 UUID(gu_uuid_t uuid) : uuid_(uuid) {}
144 inline std::ostream& gcomm::operator<<(std::ostream& os,
const UUID& uuid)
146 return uuid.to_stream (os);
149 #endif // _GCOMM_UUID_HPP_
GComm exception definitions.