UUID system iteration 2

This commit is contained in:
2026-01-03 13:16:53 +02:00
parent 7470f01dc4
commit cb275cd7b1
3 changed files with 47 additions and 5 deletions

View File

@@ -10,7 +10,6 @@
#include <bit>
#include <cstdint>
#include <ctime>
#include <format>
using namespace Tourmaline::Systems;
@@ -23,5 +22,5 @@ Tourmaline::Type::UUID Random::GenerateUUID() {
random_ab = std::rotl(hold, 12);
random_c = ((random_c >> 2) << 2) + 2;
return std::format("{:X}{:X}", random_ab, random_c);
return Tourmaline::Type::UUID(random_ab, random_c);
}

View File

@@ -8,8 +8,19 @@
*/
#ifndef GUARD_TOURMALINE_TYPES_H
#define GUARD_TOURMALINE_TYPES_H
#include <string>
#include <cstdint>
#include <memory>
namespace Tourmaline::Type {
using UUID = std::string;
}
struct UUID {
[[nodiscard]]
std::string asString() const;
UUID(uint64_t firstHalf, uint64_t secondHalf);
UUID(const std::string &uuid);
UUID(UUID &&uuid) noexcept;
private:
std::unique_ptr<uint64_t[]> data = std::make_unique<uint64_t[]>(2);
};
} // namespace Tourmaline::Type
#endif

32
sourceCode/Types/UUID.cpp Normal file
View File

@@ -0,0 +1,32 @@
/*
* SPDX-FileCopyrightText: Dora "cat" <cat@thenight.club>
* SPDX-License-Identifier: MPL-2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "Types.hpp"
#include <algorithm>
#include <format>
#include <memory>
#include <string>
using namespace Tourmaline::Type;
std::string UUID::asString() const {
return std::format("{:016X}{:016X}", data[0], data[1]);
}
UUID::UUID(UUID &&uuid) noexcept { data.swap(uuid.data); }
UUID::UUID(uint64_t firstHalf, uint64_t secondHalf) {
data[0] = firstHalf;
data[1] = secondHalf;
}
UUID::UUID(const std::string &uuid) {
// We are assuming that it is a valid UUID, if not then somewhere else this
// UUID should cause an error
std::copy(uuid.begin(), uuid.begin() + 32, data.get());
}