CJCombrink | 4b90909 | 2024-04-27 19:51:39 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | #ifndef _THRIFT_TUUID_H_ |
| 21 | #define _THRIFT_TUUID_H_ 1 |
| 22 | |
| 23 | #include <thrift/Thrift.h> |
| 24 | |
| 25 | #ifdef THRIFT_TUUID_SUPPORT_BOOST_UUID |
| 26 | #include <boost/uuid/uuid.hpp> |
| 27 | #endif // THRIFT_TUUID_SUPPORT_BOOST_UUID |
| 28 | |
| 29 | #include <algorithm> |
Sven Roederer | d80bb57 | 2024-07-17 15:21:14 +0200 | [diff] [blame] | 30 | #include <sstream> |
CJCombrink | 4b90909 | 2024-04-27 19:51:39 +0200 | [diff] [blame] | 31 | |
| 32 | namespace apache { |
| 33 | namespace thrift { |
| 34 | |
| 35 | /** |
| 36 | * Thrift wrapper class for a UUID type. |
| 37 | * |
| 38 | * The UUID is stored as a 16 byte buffer. |
| 39 | * This class stores the UUID in network order when assigned from a string. |
| 40 | */ |
| 41 | class TUuid { |
| 42 | public: |
| 43 | typedef uint8_t value_type; |
| 44 | typedef uint8_t* iterator; |
| 45 | typedef uint8_t const* const_iterator; |
| 46 | typedef std::size_t size_type; |
| 47 | typedef std::ptrdiff_t difference_type; |
| 48 | |
| 49 | TUuid() = default; |
| 50 | TUuid(const TUuid& other) = default; |
| 51 | TUuid(TUuid&& other) = default; |
| 52 | TUuid& operator=(const TUuid&) = default; |
| 53 | TUuid& operator=(TUuid&&) = default; |
| 54 | ~TUuid() = default; |
| 55 | |
| 56 | /** |
| 57 | * Construct the object from a 16 byte buffer. |
| 58 | */ |
| 59 | explicit TUuid(const uint8_t (&data)[16]) noexcept |
| 60 | { |
| 61 | std::copy(std::begin(data), std::end(data), std::begin(this->data_)); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Construct the object from the specified string. |
| 66 | * |
| 67 | * Supported string formats are: |
| 68 | * - "hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh" |
| 69 | * - "{hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh}" |
| 70 | * - "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh" |
| 71 | * - "{hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh}" |
| 72 | * |
| 73 | * If the string is invalid, the object will be set to a |
| 74 | * nil (empty) UUID. |
| 75 | */ |
| 76 | explicit TUuid(const std::string& str) noexcept; |
| 77 | |
| 78 | #ifdef THRIFT_TUUID_SUPPORT_BOOST_UUID |
| 79 | /** |
| 80 | * Construct the TUuid from a boost::uuids::uuid. |
| 81 | * |
| 82 | * This constructor will only be available if the <tt>THRIFT_TUUID_SUPPORT_BOOST_UUID</tt> |
| 83 | * compiler directive is set when this file is included. |
| 84 | * |
| 85 | * This constructor is by default implicit. It can be made explicit by defining the |
| 86 | * <tt>THRIFT_TUUID_BOOST_CONSTRUCTOR_EXPLICIT</tt> compiler directive. |
| 87 | */ |
| 88 | #ifdef THRIFT_TUUID_BOOST_CONSTRUCTOR_EXPLICIT |
| 89 | explicit |
| 90 | #endif // THRIFT_TUUID_BOOST_CONSTRUCTOR_EXPLICIT |
| 91 | TUuid(const boost::uuids::uuid& buuid) noexcept |
| 92 | { |
Carel Combrink | 3867d68 | 2024-09-03 22:01:48 +0200 | [diff] [blame] | 93 | std::copy(buuid.begin(), buuid.end(), std::begin(this->data_)); |
CJCombrink | 4b90909 | 2024-04-27 19:51:39 +0200 | [diff] [blame] | 94 | } |
| 95 | #endif // THRIFT_TUUID_SUPPORT_BOOST_UUID |
| 96 | |
| 97 | /** |
| 98 | * Check if the UUID is nil. |
| 99 | */ |
| 100 | bool is_nil() const noexcept; |
| 101 | |
| 102 | /** |
| 103 | * Compare two TUuid objects for equality. |
| 104 | */ |
| 105 | inline bool operator==(const TUuid& other) const; |
| 106 | |
| 107 | /** |
| 108 | * Compare two TUuid objects for inequality. |
| 109 | */ |
| 110 | inline bool operator!=(const TUuid& other) const; |
| 111 | |
| 112 | iterator begin() noexcept { return data_; } |
| 113 | const_iterator begin() const noexcept { return data_; } |
| 114 | iterator end() noexcept { return data_ + size(); } |
| 115 | const_iterator end() const noexcept { return data_ + size(); } |
| 116 | size_type size() const noexcept { return 16; } |
| 117 | inline const_iterator data() const { return data_; } |
| 118 | inline iterator data() { return data_; } |
| 119 | |
| 120 | void swap(TUuid& other) noexcept { std::swap(data_, other.data_); } |
| 121 | |
| 122 | private: |
| 123 | /** |
| 124 | * The UUID data. |
| 125 | */ |
| 126 | uint8_t data_[16] = {}; |
| 127 | }; |
| 128 | |
| 129 | /** |
| 130 | * Get the String representation of a TUUID. |
| 131 | * |
| 132 | * The format returned is: |
| 133 | * - "hhhhhhhh-hhhh-hhhh-hhhh-hhhhhhhhhhhh" |
| 134 | */ |
| 135 | std::string to_string(const TUuid& uuid) noexcept(false); |
| 136 | |
| 137 | /** |
| 138 | * Swap two TUuid objects |
| 139 | */ |
| 140 | inline void swap(TUuid& lhs, TUuid& rhs) noexcept { |
| 141 | lhs.swap(rhs); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * TUuid equality comparison operator implementation |
| 146 | */ |
| 147 | inline bool TUuid::operator==(const TUuid& other) const { |
| 148 | // Compare using temporary strings. |
| 149 | // Can't use strcmp() since we expect embeded zeros |
| 150 | // Perhaps the reason we should use std::array instead |
| 151 | return std::string(this->begin(), this->end()) == std::string(other.begin(), other.end()); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * TUuid inequality comparison operator implementation |
| 156 | */ |
| 157 | inline bool TUuid::operator!=(const TUuid& other) const { |
| 158 | return !(*this == other); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * TUuid ostream stream operator implementation |
| 163 | */ |
| 164 | inline std::ostream& operator<<(std::ostream& out, const TUuid& obj) { |
| 165 | out << to_string(obj); |
| 166 | return out; |
| 167 | } |
| 168 | |
| 169 | } // namespace thrift |
| 170 | } // namespace apache |
| 171 | |
| 172 | #endif // #ifndef _THRIFT_TUUID_H_ |