UUID.hpp
Go to the documentation of this file.
1 // @formatter:off
2 //
3 // Balau core C++ library
4 //
5 // Copyright (C) 2008 Bora Software (contact@borasoftware.com)
6 //
7 // Licensed under the Boost Software License - Version 1.0 - August 17th, 2003.
8 // See the LICENSE file for the full license text.
9 //
10 
16 
17 #ifndef COM_BORA_SOFTWARE__BALAU_TYPE__UUID
18 #define COM_BORA_SOFTWARE__BALAU_TYPE__UUID
19 
20 #include <Balau/Type/ToString.hpp>
21 
22 #include <boost/uuid/uuid.hpp>
23 #include <boost/uuid/uuid_generators.hpp>
24 #include <boost/uuid/uuid_io.hpp>
25 
26 #include <sstream>
27 
28 namespace Balau {
29 
33 class UUID {
37  public: UUID() : uuid(boost::uuids::random_generator()()) {}
38 
42  public: UUID(const UUID & copy) = default;
43 
49  public: std::array<unsigned char, 16> asArray() const {
50  std::array<unsigned char, 16> a {};
51  memcpy(a.data(), uuid.data, 16);
52  return a;
53  }
54 
60  public: const unsigned char * asBytes() const {
61  return uuid.data;
62  }
63 
69  public: template <typename AllocatorT> Balau::U8String<AllocatorT> asString() const {
71  stream << uuid;
72  return stream.str();
73  }
74 
80  public: std::string asString() const {
81  std::ostringstream stream;
82  stream << uuid;
83  return stream.str();
84  }
85 
91  public: const boost::uuids::uuid & asUUID() const {
92  return uuid;
93  }
94 
98  public: bool operator == (const UUID & rhs) const {
99  return uuid == rhs.uuid;
100  }
101 
105  public: bool operator != (const UUID & rhs) const {
106  return uuid != rhs.uuid;
107  }
108 
112  public: bool operator < (const UUID & rhs) const {
113  return uuid < rhs.uuid;
114  }
115 
119  public: bool operator > (const UUID & rhs) const {
120  return uuid > rhs.uuid;
121  }
122 
126  public: bool operator <= (const UUID & rhs) const {
127  return uuid <= rhs.uuid;
128  }
129 
133  public: bool operator >= (const UUID & rhs) const {
134  return uuid >= rhs.uuid;
135  }
136 
138 
139  private: const boost::uuids::uuid uuid;
140 };
141 
147 template <typename AllocatorT>
149  return uuid.asString<AllocatorT>();
150 }
151 
157 inline std::string toString(const UUID & uuid) {
158  return uuid.asString();
159 }
160 
161 } // namespace Balau
162 
163 #endif // COM_BORA_SOFTWARE__BALAU_TYPE__UUID
std::string asString() const
Get the UUID as a UTF-8 string.
Definition: UUID.hpp:80
Pre-defined universal to-string functions.
Definition: NetworkTypes.hpp:39
UUID()
Construct a new UUID with a random value.
Definition: UUID.hpp:37
bool operator==(const UUID &rhs) const
Are the two UUIDs equal?
Definition: UUID.hpp:98
The root Balau namespace.
Definition: ApplicationConfiguration.hpp:23
bool operator<(const UUID &rhs) const
Is this UUID less than the supplied UUID?
Definition: UUID.hpp:112
bool operator>(const UUID &rhs) const
Is this UUID greater than the supplied UUID?
Definition: UUID.hpp:119
std::basic_ostringstream< char, std::char_traits< char >, AllocatorT > U8OStringStream
UTF-8 output string stream type with selectable allocator.
Definition: ToStringA.hpp:59
bool operator<=(const UUID &rhs) const
Is this UUID less or equal to the supplied UUID?
Definition: UUID.hpp:126
const unsigned char * asBytes() const
Get the UUID as raw bytes.
Definition: UUID.hpp:60
bool operator>=(const UUID &rhs) const
Is this UUID greater or equal to the supplied UUID?
Definition: UUID.hpp:133
std::basic_string< char, std::char_traits< char >, AllocatorT > U8String
UTF-8 string type with selectable allocator.
Definition: ToStringA.hpp:41
const boost::uuids::uuid & asUUID() const
Get the UUID as the underlying Boost UUID.
Definition: UUID.hpp:91
std::array< unsigned char, 16 > asArray() const
Get the UUID as a std::array of bytes.
Definition: UUID.hpp:49
bool operator!=(const UUID &rhs) const
Are the two UUIDs not equal?
Definition: UUID.hpp:105
UUID class, using the Boost uuid implementation.
Definition: UUID.hpp:33
Balau::U8String< AllocatorT > toString(LoggingLevel level)
Print the logging level as a UTF-8 string.
Definition: LoggingLevel.hpp:73
Balau::U8String< AllocatorT > asString() const
Get the UUID as a UTF-8 string.
Definition: UUID.hpp:69