Coverage Report

Created: 2026-07-30 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/thrift/lib/cpp/src/thrift/TUuid.h
Line
Count
Source
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>
30
#include <sstream>
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
84.7M
  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
0
  {
61
0
    std::copy(std::begin(data), std::end(data), std::begin(this->data_));
62
0
  }
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
  {
93
    std::copy(buuid.begin(), buuid.end(), std::begin(this->data_));
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
8.04k
  iterator begin() noexcept { return data_; }
113
0
  const_iterator begin() const noexcept { return data_; }
114
7.73k
  iterator end() noexcept { return data_ + size(); }
115
0
  const_iterator end() const noexcept { return data_ + size(); }
116
7.73k
  size_type size() const noexcept { return 16; }
117
0
  inline const_iterator data() const { return data_; }
118
0
  inline iterator data() { return data_; }
119
120
0
  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
0
inline void swap(TUuid& lhs, TUuid& rhs) noexcept {
141
0
  lhs.swap(rhs);
142
0
}
143
144
/**
145
 * TUuid equality comparison operator implementation
146
 */
147
0
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
0
  return std::string(this->begin(), this->end()) == std::string(other.begin(), other.end());
152
0
}
153
154
/**
155
 * TUuid inequality comparison operator implementation
156
 */
157
0
inline bool TUuid::operator!=(const TUuid& other) const {
158
0
  return !(*this == other);
159
0
}
160
161
/**
162
 * TUuid ostream stream operator implementation
163
 */
164
0
inline std::ostream& operator<<(std::ostream& out, const TUuid& obj) {
165
0
  out << to_string(obj);
166
0
  return out;
167
0
}
168
169
} // namespace thrift
170
} // namespace apache
171
172
#endif // #ifndef _THRIFT_TUUID_H_