Apache Mesos
uuid.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License.
12 
13 #ifndef __STOUT_UUID_HPP__
14 #define __STOUT_UUID_HPP__
15 
16 #include <assert.h>
17 
18 #include <stdexcept>
19 #include <string>
20 
21 #include <boost/uuid/random_generator.hpp>
22 #include <boost/uuid/string_generator.hpp>
23 #include <boost/uuid/uuid.hpp>
24 #include <boost/uuid/uuid_io.hpp>
25 
26 #include <stout/error.hpp>
27 #include <stout/try.hpp>
28 
29 #ifdef __WINDOWS__
30 #include <stout/windows.hpp>
31 #endif // __WINDOWS__
32 
33 namespace id {
34 
35 struct UUID : boost::uuids::uuid
36 {
37 public:
38  static UUID random()
39  {
40  static thread_local boost::uuids::random_generator* generator = nullptr;
41 
42  if (generator == nullptr) {
43  generator = new boost::uuids::random_generator();
44  }
45 
46  return UUID((*generator)());
47  }
48 
49  static Try<UUID> fromBytes(const std::string& s)
50  {
51  const std::string error = "Not a valid UUID";
52 
53  if (s.size() != UUID::static_size()) {
54  return Error(error);
55  }
56 
57  boost::uuids::uuid uuid;
58  memcpy(&uuid, s.data(), s.size());
59 
60  if (uuid.version() == UUID::version_unknown) {
61  return Error(error);
62  }
63 
64  return UUID(uuid);
65  }
66 
67  static Try<UUID> fromString(const std::string& s)
68  {
69  try {
70  // NOTE: We don't use `thread_local` for the `string_generator`
71  // (unlike for the `random_generator` above), because it is cheap
72  // to construct one each time.
73  boost::uuids::string_generator gen;
74  boost::uuids::uuid uuid = gen(s);
75  return UUID(uuid);
76  } catch (const std::runtime_error& e) {
77  return Error(e.what());
78  }
79  }
80 
81  std::string toBytes() const
82  {
83  assert(sizeof(data) == size());
84  return std::string(reinterpret_cast<const char*>(data), sizeof(data));
85  }
86 
87  std::string toString() const
88  {
89  return to_string(*this);
90  }
91 
92 private:
93  explicit UUID(const boost::uuids::uuid& uuid)
94  : boost::uuids::uuid(uuid) {}
95 };
96 
97 } // namespace id {
98 
99 namespace std {
100 
101 template <>
102 struct hash<id::UUID>
103 {
104  typedef size_t result_type;
105 
107 
108  result_type operator()(const argument_type& uuid) const
109  {
110  return boost::uuids::hash_value(uuid);
111  }
112 };
113 
114 } // namespace std {
115 
116 #endif // __STOUT_UUID_HPP__
std::string toBytes() const
Definition: uuid.hpp:81
Definition: errorbase.hpp:36
Try< Bytes > size(const std::string &path, const FollowSymlink follow=FollowSymlink::FOLLOW_SYMLINK)
Definition: stat.hpp:121
Definition: check.hpp:33
id::UUID argument_type
Definition: uuid.hpp:106
result_type operator()(const argument_type &uuid) const
Definition: uuid.hpp:108
Definition: uuid.hpp:33
Definition: type_utils.hpp:510
static UUID random()
Definition: uuid.hpp:38
Definition: uuid.hpp:35
size_t result_type
Definition: uuid.hpp:104
static Try< UUID > fromBytes(const std::string &s)
Definition: uuid.hpp:49
std::string error(const std::string &msg, uint32_t code)
std::string toString() const
Definition: uuid.hpp:87
static Try< UUID > fromString(const std::string &s)
Definition: uuid.hpp:67