mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 16:56:17 +08:00
76 lines
2.3 KiB
C++
76 lines
2.3 KiB
C++
#ifndef RFL_UUID_HPP_
|
|
#define RFL_UUID_HPP_
|
|
|
|
#include <ctime>
|
|
#include <iterator>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <regex>
|
|
|
|
#include "internal/StringLiteral.hpp"
|
|
#include "rfl/Literal.hpp"
|
|
#include "rfl/Result.hpp"
|
|
|
|
|
|
namespace rfl {
|
|
|
|
template <size_t N>
|
|
consteval const auto _get_pattern(const internal::StringLiteral<N> _v) {
|
|
if constexpr (_v == internal::StringLiteral{"1"}) {
|
|
return "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-1[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$";
|
|
} else if constexpr (_v == internal::StringLiteral{"2"}) {
|
|
return "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-2[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$";
|
|
} else if constexpr (_v == internal::StringLiteral{"3"}) {
|
|
return "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-3[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$";
|
|
}
|
|
else if constexpr (_v == internal::StringLiteral{"4"}) {
|
|
return "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$";
|
|
} else {
|
|
static_assert("UUID Version must be one the following: 1, 2, 3, 4.");
|
|
}
|
|
}
|
|
|
|
template <internal::StringLiteral _version>
|
|
class UUID {
|
|
using ReflectionType = std::string;
|
|
|
|
constexpr static const internal::StringLiteral version_ = _version;
|
|
|
|
constexpr static const char* version_pattern = _get_pattern(version_);
|
|
|
|
public:
|
|
UUID(const char* _str) {
|
|
std::regex uuid_regex(version_pattern);
|
|
if (!std::regex_match(uuid_regex, std::string(_str))) {
|
|
throw std::runtime_error(
|
|
"String '" + std::string(_str) + "' did not match format the UUIDv" + std::string(version_) + " format");
|
|
}
|
|
uuid_ = _str;
|
|
}
|
|
|
|
UUID(const std::string& _str) : UUID(_str.c_str()) {};
|
|
~UUID() = default;
|
|
|
|
ReflectionType reflection() const {
|
|
return std::string(uuid_);
|
|
}
|
|
|
|
/// Expresses the underlying timestamp as a string.
|
|
std::string str() const { return reflection; }
|
|
|
|
private:
|
|
//fixed size char instead of string
|
|
//
|
|
std::string uuid_;
|
|
//char uuid[36];
|
|
};
|
|
|
|
using UUIDv1 = UUID<internal::StringLiteral{"1"}>;
|
|
using UUIDv2 = UUID<internal::StringLiteral{"2"}>;
|
|
using UUIDv3 = UUID<internal::StringLiteral{"3"}>;
|
|
using UUIDv4 = UUID<internal::StringLiteral{"3"}>;
|
|
}
|
|
|
|
|
|
#endif
|