1 /*
2  * Copyright (C) 2009 Codership Oy <info@codership.com>
3  */
4 
5 #ifndef _GCOMM_UUID_HPP_
6 #define _GCOMM_UUID_HPP_
7 
8 #include "gcomm/exception.hpp"
9 #include "gcomm/types.hpp"
10 
11 #include "gu_utils.hpp"
12 #include "gu_assert.hpp"
13 #include "gu_byteswap.h"
14 
15 #include "gu_uuid.hpp"
16 
17 #include <ostream>
18 #include <iomanip>
19 
20 namespace gcomm
21 {
22     class UUID;
23     std::ostream& operator<<(std::ostream&, const UUID&);
24 }
25 
26 class gcomm::UUID :
27         public gu::UUID
28 {
29 public:
30 
UUID()31     UUID() : gu::UUID() {}
32 
UUID(const void * node,const size_t node_len)33     UUID(const void* node, const size_t node_len) :
34             gu::UUID(node, node_len) {}
35 
UUID(const int32_t idx)36     UUID(const int32_t idx) : gu::UUID()
37     {
38         assert(idx > 0);
39         memcpy(&uuid_, &idx, sizeof(idx));
40     }
41 
nil()42     static const UUID& nil()
43     {
44         return uuid_nil_;
45     }
46 
to_stream(std::ostream & os,bool full) const47     std::ostream& to_stream(std::ostream& os, bool full) const
48     {
49         std::ios_base::fmtflags saved = os.flags();
50         if (full == true)
51         {
52             os << uuid_;
53         }
54         else
55         {
56             os << std::hex
57                << std::setfill('0') << std::setw(2)
58                << static_cast<int>(uuid_.data[0])
59                << std::setfill('0') << std::setw(2)
60                << static_cast<int>(uuid_.data[1])
61                << std::setfill('0') << std::setw(2)
62                << static_cast<int>(uuid_.data[2])
63                << std::setfill('0') << std::setw(2)
64                << static_cast<int>(uuid_.data[3]);
65         }
66         os.flags(saved);
67         return os;
68     }
69 
70     // Prefer the above function over this one
full_str() const71     std::string full_str() const
72     {
73         std::ostringstream os;
74         to_stream(os, true);
75         return os.str();
76     }
77 
78 private:
79     static const UUID uuid_nil_;
UUID(gu_uuid_t uuid)80     UUID(gu_uuid_t uuid) : gu::UUID(uuid) {}
81 };
82 
operator <<(std::ostream & os,const UUID & uuid)83 inline std::ostream& gcomm::operator<<(std::ostream& os, const UUID& uuid)
84 {
85     return uuid.to_stream (os, false);
86 }
87 
88 #endif // _GCOMM_UUID_HPP_
89