-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathuuid.cpp
More file actions
77 lines (65 loc) · 2.46 KB
/
uuid.cpp
File metadata and controls
77 lines (65 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright (c) 2023-2024 Percona and/or its affiliates.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License, version 2.0,
// as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License, version 2.0, for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#include "binsrv/gtids/uuid.hpp"
#include <algorithm>
#include <cstddef>
#include <exception>
#include <iterator>
#include <ostream>
#include <stdexcept>
#include <string>
#include <string_view>
#include <tuple>
#include <boost/uuid/string_generator.hpp>
#include <boost/uuid/uuid_io.hpp>
#include "binsrv/gtids/common_types.hpp"
#include "util/byte_span_fwd.hpp"
#include "util/byte_span_inserters.hpp"
#include "util/exception_location_helpers.hpp"
namespace binsrv::gtids {
uuid::uuid(std::string_view value) {
try {
data_ =
boost::uuids::string_generator{}(std::cbegin(value), std::cend(value));
} catch (const std::exception &) {
util::exception_location().raise<std::invalid_argument>(
"cannot create uuid");
}
}
uuid::uuid(const uuid_storage &data) {
static_assert(std::tuple_size_v<uuid_storage> ==
boost::uuids::uuid::static_size());
std::copy_n(
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
reinterpret_cast<const boost::uuids::uuid::value_type *>(std::data(data)),
boost::uuids::uuid::static_size(), std::begin(data_));
}
[[nodiscard]] std::string uuid::str() const {
return boost::uuids::to_string(data_);
}
void uuid::encode_to(util::byte_span &destination) const {
const util::const_byte_span data_span{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
reinterpret_cast<const std::byte *>(std::begin(data_)),
boost::uuids::uuid::static_size()};
if (!util::insert_byte_span_to_byte_span_checked(destination, data_span)) {
util::exception_location().raise<std::invalid_argument>(
"cannot encode uuid");
}
}
std::ostream &operator<<(std::ostream &output, const uuid &obj) {
return output << obj.str();
}
} // namespace binsrv::gtids