pstore2
uuid.hpp
Go to the documentation of this file.
1 //===- include/pstore/core/uuid.hpp -----------------------*- mode: C++ -*-===//
2 //* _ _ *
3 //* _ _ _ _(_) __| | *
4 //* | | | | | | | |/ _` | *
5 //* | |_| | |_| | | (_| | *
6 //* \__,_|\__,_|_|\__,_| *
7 //* *
8 //===----------------------------------------------------------------------===//
9 //
10 // Part of the pstore project, under the Apache License v2.0 with LLVM Exceptions.
11 // See https://github.com/SNSystems/pstore/blob/master/LICENSE.txt for license
12 // information.
13 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 //
15 //===----------------------------------------------------------------------===//
18 
19 #ifndef PSTORE_CORE_UUID_HPP
20 #define PSTORE_CORE_UUID_HPP
21 
22 #include <array>
23 #include <cstdint>
24 #include <string>
25 
26 #include "pstore/support/maybe.hpp"
27 
28 #if defined(_WIN32)
29 # define WIN32_LEAN_AND_MEAN
30 # include <Rpc.h>
31 #elif defined(__APPLE__)
32 # include <uuid/uuid.h>
33 #endif
34 
35 namespace pstore {
36 
40  class uuid {
41  friend bool operator== (uuid const & lhs, uuid const & rhs) noexcept;
42  friend bool operator!= (uuid const & lhs, uuid const & rhs) noexcept;
43  friend bool operator< (uuid const & lhs, uuid const & rhs) noexcept;
44  friend bool operator<= (uuid const & lhs, uuid const & rhs) noexcept;
45  friend bool operator> (uuid const & lhs, uuid const & rhs) noexcept;
46  friend bool operator>= (uuid const & lhs, uuid const & rhs) noexcept;
47 
48  public:
49  static constexpr auto elements = std::size_t{16};
50 
53  static constexpr auto string_length = elements * 2 + 4;
54 
55  using container_type = std::array<std::uint8_t, elements>;
56  using value_type = container_type::value_type;
57  using size_type = container_type::size_type;
58  using difference_type = container_type::difference_type;
59 
60  using reference = container_type::reference;
61  using const_reference = container_type::const_reference;
62  using iterator = container_type::iterator;
63  using const_iterator = container_type::const_iterator;
64 
65  uuid ();
68  explicit uuid (std::string const & s);
69 
71  explicit constexpr uuid (container_type const & c)
72  : data_{c} {}
73 
74 #if defined(_WIN32)
75  explicit uuid (UUID const & u);
77 #elif defined(__APPLE__)
78  explicit uuid (uuid_t const & bytes);
79 #endif
80 
87  static maybe<uuid> from_string (std::string const & s);
88 
89  iterator begin () noexcept { return std::begin (data_); }
90  const_iterator begin () const noexcept { return std::begin (data_); }
91  iterator end () noexcept { return std::end (data_); }
92  const_iterator end () const noexcept { return std::end (data_); }
93 
94  container_type const & array () const noexcept { return data_; }
95 
96  enum class variant_type {
97  ncs, // NCS backward compatibility
98  rfc_4122, // Defined by RFC 4122
99  microsoft, // Microsoft Corporation backward compatibility
100  future // Future definition
101  };
102  enum class version_type {
103  time_based = 1,
104  dce_security = 2,
105  name_based_md5 = 3,
106  random_number_based = 4,
107  name_based_sha1 = 5,
108  unknown,
109  };
110 
111  auto variant () const noexcept -> variant_type;
112  auto version () const noexcept -> version_type;
113  bool is_null () const noexcept;
114 
116  std::string str () const;
117 
118  private:
119  container_type data_{};
120 
121  enum {
122  version_octet = 6,
123  variant_octet = 8,
124  };
125 
129  template <typename Ty>
130  static std::uint8_t get_byte (Ty const t, unsigned const num) noexcept {
131  PSTORE_ASSERT (num < sizeof (Ty));
132  return static_cast<std::uint8_t> ((t >> (num * 8)) & 0xff);
133  }
134  };
135 
136  PSTORE_STATIC_ASSERT (std::is_standard_layout<uuid>::value);
137  PSTORE_STATIC_ASSERT (sizeof (uuid) == 16);
138 
139  std::ostream & operator<< (std::ostream & os, uuid::version_type v);
140  std::ostream & operator<< (std::ostream & os, uuid::variant_type v);
141  std::ostream & operator<< (std::ostream & os, uuid const & m);
142 
143  inline bool operator== (uuid const & lhs, uuid const & rhs) noexcept {
144  return lhs.data_ == rhs.data_;
145  }
146  inline bool operator< (uuid const & lhs, uuid const & rhs) noexcept {
147  return lhs.data_ < rhs.data_;
148  }
149  inline bool operator!= (uuid const & lhs, uuid const & rhs) noexcept {
150  return lhs.data_ != rhs.data_;
151  }
152  inline bool operator> (uuid const & lhs, uuid const & rhs) noexcept {
153  return lhs.data_ > rhs.data_;
154  }
155  inline bool operator<= (uuid const & lhs, uuid const & rhs) noexcept {
156  return lhs.data_ <= rhs.data_;
157  }
158  inline bool operator>= (uuid const & lhs, uuid const & rhs) noexcept {
159  return lhs.data_ >= rhs.data_;
160  }
161 
162 } // end namespace pstore
163 
164 #endif // PSTORE_CORE_UUID_HPP
static constexpr auto string_length
RFC4122 defines the UUID string representation which includes 16 two-digit hex numbers and 4 hyphens...
Definition: uuid.hpp:53
constexpr uuid(container_type const &c)
A constructor used to construct a specific UUID from its binary value.
Definition: uuid.hpp:71
An implementation of the Haskell Maybe type.
auto variant() const noexcept -> variant_type
Definition: uuid.cpp:172
Definition: nonpod2.cpp:40
The uuid class is used to represent Universally Unique Identifiers (UUID) as defined by RFC 4122...
Definition: uuid.hpp:40
static maybe< uuid > from_string(std::string const &s)
Converts a string to a UUID following the convention defined by RFC4122.
Definition: uuid.cpp:134
Definition: maybe.hpp:49
std::string str() const
Yields a string representation of the UUID following the convention defined by RFC4122.
Definition: uuid.cpp:213