#ifndef RFL_UUID_HPP_ #define RFL_UUID_HPP_ #include #include #include #include #include #include "internal/StringLiteral.hpp" #include "rfl/Literal.hpp" #include "rfl/Result.hpp" namespace rfl { template consteval const auto _get_pattern(const internal::StringLiteral _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 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; using UUIDv2 = UUID; using UUIDv3 = UUID; using UUIDv4 = UUID; } #endif