From 1f93c243ca820bae633c0d2c98233e00fd2dc316 Mon Sep 17 00:00:00 2001 From: Jeremy Perez Date: Fri, 6 Oct 2023 21:15:11 +0200 Subject: [PATCH] first draft for UUID implementation. --- include/rfl/UUID.hpp | 62 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 include/rfl/UUID.hpp diff --git a/include/rfl/UUID.hpp b/include/rfl/UUID.hpp new file mode 100644 index 00000000..1b4c2868 --- /dev/null +++ b/include/rfl/UUID.hpp @@ -0,0 +1,62 @@ +#ifndef RFL_TIMESTAMP_HPP_ +#define RFL_TIMESTAMP_HPP_ + +#include +#include +#include +#include +#include + +#include "internal/StringLiteral.hpp" +#include "rfl/Literal.hpp" +#include "rfl/Result.hpp" + + +namespace rfl { + + +template +class UUID { + using ReflectionType = std::string; + + constexpr static const internal::StringLiteral version_ = _version; + static constexpr const char* version_pattern = [] { + if constexpr (version_ == "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 (version_ == "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 (version_ == "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 (version_ == "3") { + 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."); + } + }(); + + 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()) {}; + + ReflectionType reflection() const { + return std::string(uuid); + } + + //std::string str() const {return reflection; } + + private: + //fixed size char instead of string + // + std::string uuid; + //char uuid[36]; +}; +}