XTL  0.1
eXtended Template Library
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
unique_id.hpp
Go to the documentation of this file.
1 
7 #pragma once
8 #include <xtd/xtd.hpp>
9 #include <xtd/string.hpp>
10 #include <ios>
11 #include <fstream>
12 #include "exception.hpp"
13 
14 #if (XTD_COMPILER_MSVC & XTD_COMPILER)
15  #pragma comment(lib, "rpcrt4")
16 #endif
17 
18 #if (XTD_HAS_LIBUUID)
19  #include <uuid/uuid.h>
20 #endif
21 
22 namespace xtd{
23 #if ((XTD_OS_MSYS|XTD_OS_LINUX|XTD_OS_CYGWIN) & XTD_OS)
24  #if (XTD_HAS_LIBUUID)
25  class unique_id{
26  public:
27  unique_id(){
28  uuid_generate(_uuid);
29  }
30  unique_id(const unique_id& src){
31  uuid_copy(_uuid, src._uuid);
32  }
33  explicit unique_id(const uuid_t& src){
34  uuid_copy(_uuid, src);
35  }
36  unique_id& operator=(const unique_id& src){
37  uuid_copy(_uuid, src._uuid);
38  return *this;
39  }
40  unique_id& operator=(const uuid_t& src){
41  uuid_copy(_uuid, src);
42  return *this;
43  }
44  bool operator<(const unique_id& rhs) const{
45  return -1 == uuid_compare(_uuid, rhs._uuid);
46  }
47  bool operator<(const uuid_t& rhs) const{
48  return -1 == uuid_compare(_uuid, rhs);
49  }
50  static const unique_id& nullid(){
51  static uuid_t null_id = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
52  static unique_id oRet(null_id);
53  return oRet;
54  }
55  private:
56  template <typename> friend class _::xstring_format;
57  uuid_t _uuid;
58  };
59 
60  #else
61 
62  struct unique_id{
63  using uuid_t = uint8_t[16];
64  uuid_t _uuid;
65  public:
66  unique_id(){
67  std::ifstream oFile;
68  oFile.open("/proc/sys/kernel/random/uuid", std::ios::binary);
69  if (oFile.bad()) throw std::runtime_error("Failure generating uuid");
70  std::string sTemp;
71  std::getline(oFile, sTemp);
72  //289248a1-379e-4775-bcf5-c6348cf5272b
73  sscanf(sTemp.c_str(), "%08hX-%04hX-%04hX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX",
74  (uint32_t*)&_uuid[0], (uint16_t*)&_uuid[4], (uint16_t*)&_uuid[6],
75  &_uuid[8], &_uuid[9], &_uuid[10], &_uuid[11], &_uuid[12], &_uuid[13], &_uuid[14], &_uuid[15]);
76 
77  }
78  unique_id(const uuid_t& oID) {
79  memcpy(_uuid, &oID, sizeof(uuid_t));
80  }
81  unique_id(const unique_id& src){
82  memcpy(_uuid, src._uuid, sizeof(uuid_t));
83  }
84  unique_id& operator=(const unique_id& src){
85  memcpy(_uuid, src._uuid, sizeof(uuid_t));
86  return *this;
87  }
88  bool operator<(const unique_id& rhs) const{
89  return -1 == memcmp(_uuid, rhs._uuid, sizeof(uuid_t));
90  }
91  static const unique_id& nullid(){
92  static uuid_t null_id = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
93  static unique_id oRet(null_id);
94  return oRet;
95  }
96  };
97 
98  #endif
99  namespace _{
100  template <> class xstring_format<char, const unique_id&>{
101  public:
102  inline static string format(const unique_id& value){
103  xtd::string oRet(36, 0);
104  sprintf(&oRet[0], "%08hX-%04hX-%04hX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX",
105  *(uint32_t*)&value._uuid[0], *(uint16_t*)&value._uuid[4], *(uint16_t*)&value._uuid[6],
106  value._uuid[8], value._uuid[9], value._uuid[10], value._uuid[11],
107  value._uuid[12], value._uuid[13], value._uuid[14], value._uuid[15]);
108  return oRet;
109  }
110  };
111  }
112 
113 #elif ((XTD_OS_WINDOWS & XTD_OS) || (XTD_OS_MINGW & XTD_OS))
114 
115  class unique_id : uuid_t{
116  template <typename, typename> friend class _::xstring_format;
117  public:
118  unique_id(){
119  xtd::exception::throw_if(UuidCreate(this), [](RPC_STATUS x){return (RPC_S_OK != x && RPC_S_UUID_LOCAL_ONLY != x && RPC_S_UUID_NO_ADDRESS != x); });
120  }
121  unique_id(const unique_id& src){
122  std::memcpy(this, &src, sizeof(unique_id));
123  }
124  unique_id(const uuid_t& src){
125  std::memcpy(this, &src, sizeof(uuid_t));
126  }
127  unique_id& operator=(const unique_id& src){
128  if (this != &src){
129  std::memcpy(this, &src, sizeof(unique_id));
130  }
131  return *this;
132  }
133  unique_id& operator=(const uuid_t& src){
134  if (this != &src){
135  std::memcpy(this, &src, sizeof(uuid_t));
136  }
137  return *this;
138  }
139  bool operator<(const uuid_t& rhs) const{
140  return ((Data1 < rhs.Data1) ||
141  (Data2 < rhs.Data2) ||
142  (Data3 < rhs.Data3) ||
143  (Data4[0] < rhs.Data4[0]) ||
144  (Data4[1] < rhs.Data4[1]) ||
145  (Data4[2] < rhs.Data4[2]) ||
146  (Data4[3] < rhs.Data4[3]) ||
147  (Data4[4] < rhs.Data4[4]) ||
148  (Data4[5] < rhs.Data4[5]) ||
149  (Data4[6] < rhs.Data4[6]) ||
150  (Data4[7] < rhs.Data4[7]));
151  }
152  bool operator<(const unique_id& rhs) const{
153  return *this < static_cast<const uuid_t&>(rhs);
154  }
155  static unique_id nullid(){
156  uuid_t oRet = { 0, 0, 0,{ 0, 0, 0, 0, 0, 0, 0, 0 } };
157  return oRet;
158  }
159  };
160 
161  namespace _{
162  template <> class xstring_format<char, const unique_id&>{
163  public:
164  inline static string format(const unique_id& value){
165  xtd::string oRet(36, 0);
166  sprintf(&oRet[0], "%08lX-%04hX-%04hX-%02hhX%02hhX-%02hhX%02hhX%02hhX%02hhX%02hhX%02hhX",
167  value.Data1, value.Data2, value.Data3,
168  value.Data4[0], value.Data4[1], value.Data4[2], value.Data4[3],
169  value.Data4[4], value.Data4[5], value.Data4[6], value.Data4[7]);
170  return oRet;
171  }
172  };
173  }
174 
175 #endif
176 
177 
178 
179 }
specializations of std::basic_string for advanced and common string handling
host, target and build configurations and settings Various components are purpose built for specific ...
#define throw_if(_test, _expression)
Simplifies use of exception::_throw_if.
Definition: exception.hpp:22
generic and special purpose exceptions