OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
uuid.h
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, 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 
24 #pragma once
25 
26 #include <algorithm>
27 #include <array>
28 #include <iomanip>
29 #include <iterator>
30 #include <sstream>
31 
32 namespace heavyai {
33 
34 class UUID {
35  using value_type = uint8_t;
36 
37  public:
38  // Constructors
39  constexpr UUID() noexcept : data_({}) {}
40 
41  // copy from value_type[16] (Vulkan)
42  explicit UUID(const value_type (&arr)[16]) noexcept {
43  std::copy(std::cbegin(arr), std::cend(arr), std::begin(data_));
44  }
45 
46  // copy from char[16] (Cuda)
47  explicit UUID(char (&arr)[16]) noexcept {
49  std::cbegin(arr), std::cend(arr), std::begin(data_), [](char c) -> value_type {
50  return static_cast<value_type>(c);
51  });
52  }
53 
54  void swap(UUID& other) noexcept { data_.swap(other.data_); }
55 
56  // Access for underlying array data
57  constexpr value_type* data() noexcept { return data_.data(); }
58  constexpr const value_type* data() const noexcept { return data_.data(); }
59 
60  // clang-format off
61  friend std::ostream& operator<< (std::ostream& s, const UUID& id) {
62  auto fill = s.fill();
63  auto ff = s.flags();
64  s << std::hex << std::setfill('0')
65  << std::setw(2) << +id.data_[0]
66  << std::setw(2) << +id.data_[1]
67  << std::setw(2) << +id.data_[2]
68  << std::setw(2) << +id.data_[3]
69  << '-'
70  << std::setw(2) << +id.data_[4]
71  << std::setw(2) << +id.data_[5]
72  << '-'
73  << std::setw(2) << +id.data_[6]
74  << std::setw(2) << +id.data_[7]
75  << '-'
76  << std::setw(2) << +id.data_[8]
77  << std::setw(2) << +id.data_[9]
78  << '-'
79  << std::setw(2) << +id.data_[10]
80  << std::setw(2) << +id.data_[11]
81  << std::setw(2) << +id.data_[12]
82  << std::setw(2) << +id.data_[13]
83  << std::setw(2) << +id.data_[14]
84  << std::setw(2) << +id.data_[15]
85  << std::setfill(fill);
86  s.flags(ff);
87  return s;
88  }
89  // clang-format on
90 
91  private:
92  std::array<value_type, 16> data_;
93 
94  friend bool operator==(const UUID& lhs, const UUID& rhs) noexcept;
95  friend bool operator<(const UUID& lhs, const UUID& rhs) noexcept;
96 };
97 
98 inline bool operator==(const UUID& lhs, const UUID& rhs) noexcept {
99  return lhs.data_ == rhs.data_;
100 }
101 
102 inline bool operator!=(const UUID& lhs, const UUID& rhs) noexcept {
103  return !(lhs == rhs);
104 }
105 
106 inline bool operator<(const UUID& lhs, const UUID& rhs) noexcept {
107  return lhs.data_ < rhs.data_;
108 }
109 
110 inline std::string to_string(const UUID& uuid) {
111  std::stringstream ss;
112  ss << uuid;
113  return ss.str();
114 }
115 
116 constexpr UUID empty_uuid{};
117 
118 } // namespace heavyai
bool operator<(const UUID &lhs, const UUID &rhs) noexcept
Definition: uuid.h:106
std::ostream & operator<<(std::ostream &os, const SessionInfo &session_info)
Definition: SessionInfo.cpp:57
std::string to_string(const UUID &uuid)
Definition: uuid.h:110
DEVICE void fill(ARGS &&...args)
Definition: gpu_enabled.h:60
constexpr UUID empty_uuid
Definition: uuid.h:116
DEVICE auto copy(ARGS &&...args)
Definition: gpu_enabled.h:51
OUTPUT transform(INPUT const &input, FUNC const &func)
Definition: misc.h:329
constexpr UUID() noexcept
Definition: uuid.h:39
uint8_t value_type
Definition: uuid.h:35
bool operator==(const JSON &json1, const JSON &json2)
Definition: json.h:404
bool operator!=(const JSON &json1, const JSON &json2)
Definition: json.h:408
friend bool operator==(const UUID &lhs, const UUID &rhs) noexcept
Definition: uuid.h:98
DEVICE void swap(ARGS &&...args)
Definition: gpu_enabled.h:114
friend bool operator<(const UUID &lhs, const UUID &rhs) noexcept
Definition: uuid.h:106