blob: 062b286d8b587d077719352fe30909bec71e391c [file] [log] [blame]
CJCombrink4b909092024-04-27 19:51:39 +02001/*
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#include <thrift/TUuid.h>
21
22#include <boost/uuid/string_generator.hpp>
23#include <boost/uuid/uuid.hpp>
24#include <boost/uuid/uuid_io.hpp>
25
26namespace apache {
27namespace thrift {
28
29namespace {
30static const boost::uuids::string_generator gen;
31}
32
33TUuid::TUuid(const std::string& str) noexcept {
34 std::fill(this->begin(), this->end(), 0);
35 if (str.empty()) {
36 return ;
37 }
38
39 try {
Carel Combrinked5c5a42024-09-03 22:01:48 +020040 const boost::uuids::uuid uuid = gen(str);
CJCombrink4b909092024-04-27 19:51:39 +020041 std::copy(uuid.begin(), uuid.end(), this->begin());
42 } catch (const std::runtime_error&) {
43 // Invalid string most probably
44 }
45}
46
47bool TUuid::is_nil() const noexcept {
48 boost::uuids::uuid uuid_tmp{};
49 std::copy(this->begin(), this->end(), std::begin(uuid_tmp));
50 return uuid_tmp.is_nil();
51}
52
53std::string to_string(const TUuid& in) {
54 boost::uuids::uuid uuid_tmp{};
55 std::copy(std::begin(in), std::end(in), std::begin(uuid_tmp));
56 return boost::uuids::to_string(uuid_tmp);
57}
58
59
60}
61} // apache::thrift