OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
uuid.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019 OmniSci, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
25 #pragma once
26 
27 #include <algorithm>
28 #include <array>
29 #include <iomanip>
30 #include <iterator>
31 #include <sstream>
32 
33 namespace omnisci {
34 
35 class UUID {
36  using value_type = uint8_t;
37 
38  public:
39  // Constructors
40  constexpr UUID() noexcept : data_({}) {}
41 
42  // copy from value_type[16] (OpenGL / Vulkan)
43  explicit UUID(const value_type (&arr)[16]) noexcept {
44  std::copy(std::cbegin(arr), std::cend(arr), std::begin(data_));
45  }
46 
47  // copy from char[16] (Cuda)
48  explicit UUID(char (&arr)[16]) noexcept {
49  std::transform(
50  std::cbegin(arr), std::cend(arr), std::begin(data_), [](char c) -> value_type {
51  return static_cast<value_type>(c);
52  });
53  }
54 
55  void swap(UUID& other) noexcept { data_.swap(other.data_); }
56 
57  // Access for underlying array data
58  constexpr value_type* data() noexcept { return data_.data(); }
59  constexpr const value_type* data() const noexcept { return data_.data(); }
60 
61  // clang-format off
62  friend std::ostream& operator<< (std::ostream& s, const UUID& id) {
63  auto fill = s.fill();
64  auto ff = s.flags();
65  s << std::hex << std::setfill('0')
66  << std::setw(2) << +id.data_[0]
67  << std::setw(2) << +id.data_[1]
68  << std::setw(2) << +id.data_[2]
69  << std::setw(2) << +id.data_[3]
70  << '-'
71  << std::setw(2) << +id.data_[4]
72  << std::setw(2) << +id.data_[5]
73  << '-'
74  << std::setw(2) << +id.data_[6]
75  << std::setw(2) << +id.data_[7]
76  << '-'
77  << std::setw(2) << +id.data_[8]
78  << std::setw(2) << +id.data_[9]
79  << '-'
80  << std::setw(2) << +id.data_[10]
81  << std::setw(2) << +id.data_[11]
82  << std::setw(2) << +id.data_[12]
83  << std::setw(2) << +id.data_[13]
84  << std::setw(2) << +id.data_[14]
85  << std::setw(2) << +id.data_[15]
86  << std::setfill(fill);
87  s.flags(ff);
88  return s;
89  }
90  // clang-format on
91 
92  private:
93  std::array<value_type, 16> data_;
94 
95  friend bool operator==(const UUID& lhs, const UUID& rhs) noexcept;
96  friend bool operator<(const UUID& lhs, const UUID& rhs) noexcept;
97 };
98 
99 inline bool operator==(const UUID& lhs, const UUID& rhs) noexcept {
100  return lhs.data_ == rhs.data_;
101 }
102 
103 inline bool operator!=(const UUID& lhs, const UUID& rhs) noexcept {
104  return !(lhs == rhs);
105 }
106 
107 inline bool operator<(const UUID& lhs, const UUID& rhs) noexcept {
108  return lhs.data_ < rhs.data_;
109 }
110 
111 inline std::string to_string(const UUID& uuid) {
112  std::stringstream ss;
113  ss << uuid;
114  return ss.str();
115 }
116 
117 constexpr UUID empty_uuid{};
118 
119 } // namespace omnisci
bool operator!=(const UUID &lhs, const UUID &rhs) noexcept
Definition: uuid.h:103
std::ostream & operator<<(std::ostream &os, const SessionInfo &session_info)
Definition: SessionInfo.cpp:53
constexpr UUID() noexcept
Definition: uuid.h:40
std::string to_string(const UUID &uuid)
Definition: uuid.h:111
bool operator==(const UUID &lhs, const UUID &rhs) noexcept
Definition: uuid.h:99
constexpr UUID empty_uuid
Definition: uuid.h:117
uint8_t value_type
Definition: uuid.h:36
friend bool operator==(const UUID &lhs, const UUID &rhs) noexcept
Definition: uuid.h:99
friend bool operator<(const UUID &lhs, const UUID &rhs) noexcept
Definition: uuid.h:107
bool operator<(const UUID &lhs, const UUID &rhs) noexcept
Definition: uuid.h:107