diff --git a/include/Nazara/Core/Uuid.hpp b/include/Nazara/Core/Uuid.hpp index f984241b6..94dc94ca4 100644 --- a/include/Nazara/Core/Uuid.hpp +++ b/include/Nazara/Core/Uuid.hpp @@ -19,39 +19,36 @@ namespace Nz class NAZARA_CORE_API Uuid { public: - inline Uuid(); - inline Uuid(const std::array uuid); + constexpr Uuid(); + constexpr Uuid(const std::array& uuid); Uuid(const Uuid&) = default; - Uuid(Uuid&& generator) = default; + Uuid(Uuid&&) = default; ~Uuid() = default; - inline bool IsNull() const; + constexpr bool IsNull() const; - inline const std::array& ToArray() const; + constexpr const std::array& ToArray() const; inline std::string ToString() const; std::array ToStringArray() const; Uuid& operator=(const Uuid&) = default; Uuid& operator=(Uuid&&) = default; - static Uuid FromString(std::string_view str); + static constexpr Uuid FromString(std::string_view str); static Uuid Generate(); private: std::array m_uuid; }; - NAZARA_CORE_API std::ostream& operator<<(std::ostream& out, const Uuid& uuid); - inline bool operator==(const Uuid& lhs, const Uuid& rhs); - inline bool operator!=(const Uuid& lhs, const Uuid& rhs); - inline bool operator<(const Uuid& lhs, const Uuid& rhs); - inline bool operator<=(const Uuid& lhs, const Uuid& rhs); - inline bool operator>(const Uuid& lhs, const Uuid& rhs); - inline bool operator>=(const Uuid& lhs, const Uuid& rhs); -} + inline std::ostream& operator<<(std::ostream& out, const Uuid& uuid); + constexpr bool operator==(const Uuid& lhs, const Uuid& rhs); + constexpr bool operator!=(const Uuid& lhs, const Uuid& rhs); + constexpr bool operator<(const Uuid& lhs, const Uuid& rhs); + constexpr bool operator<=(const Uuid& lhs, const Uuid& rhs); + constexpr bool operator>(const Uuid& lhs, const Uuid& rhs); + constexpr bool operator>=(const Uuid& lhs, const Uuid& rhs); -namespace Nz -{ inline bool Serialize(SerializationContext& context, const Uuid& value, TypeTag); inline bool Unserialize(SerializationContext& context, Uuid* value, TypeTag); } diff --git a/include/Nazara/Core/Uuid.inl b/include/Nazara/Core/Uuid.inl index 77d4cd8cb..6747d4fec 100644 --- a/include/Nazara/Core/Uuid.inl +++ b/include/Nazara/Core/Uuid.inl @@ -7,23 +7,45 @@ namespace Nz { - inline Uuid::Uuid() + namespace Detail { - m_uuid.fill(0); + constexpr bool ParseHexadecimalPair(Pointer& str, UInt8& number) + { + number = 0; + + for (UInt8 mul : { UInt8(0x10), UInt8(1) }) + { + if (*str >= '0' && *str <= '9') + number += (*str - '0') * mul; + else if (((*str & 0x5F) >= 'A' && (*str & 0x5F) <= 'F')) + number += ((*str & 0x5F) - 'A' + 10) * mul; + else + return false; + + str++; + } + + return true; + } } - inline Uuid::Uuid(const std::array uuid) : + constexpr Uuid::Uuid() : + m_uuid{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } + { + } + + constexpr Uuid::Uuid(const std::array& uuid) : m_uuid(uuid) { } - inline bool Uuid::IsNull() const + constexpr bool Uuid::IsNull() const { Uuid nullUuid; return *this == nullUuid; } - inline const std::array& Uuid::ToArray() const + constexpr const std::array& Uuid::ToArray() const { return m_uuid; } @@ -35,39 +57,80 @@ namespace Nz return std::string(uuidStr.data(), uuidStr.size() - 1); } - bool operator==(const Uuid& lhs, const Uuid& rhs) + constexpr Uuid Uuid::FromString(std::string_view str) { - return lhs.ToArray() == rhs.ToArray(); + if (str.size() != 36) + return {}; + + const char* ptr = str.data(); + + std::array uuid = { 0 }; + UInt8* uuidPart = &uuid[0]; + + bool first = true; + for (std::size_t groupSize : { 4, 2, 2, 2, 6 }) + { + if (!first && *ptr++ != '-') + return {}; + + first = false; + + for (std::size_t i = 0; i < groupSize; ++i) + { + if (!Detail::ParseHexadecimalPair(ptr, *uuidPart++)) + return {}; + } + } + + return Uuid{ uuid }; } - bool operator!=(const Uuid& lhs, const Uuid& rhs) + constexpr bool operator==(const Uuid& lhs, const Uuid& rhs) { - return lhs.ToArray() != rhs.ToArray(); + const std::array& lhsArray = lhs.ToArray(); + const std::array& rhsArray = rhs.ToArray(); + for (std::size_t i = 0; i < lhsArray.size(); ++i) + { + if (lhsArray[i] != rhsArray[i]) + return false; + } + + return true; } - bool operator<(const Uuid& lhs, const Uuid& rhs) + constexpr bool operator!=(const Uuid& lhs, const Uuid& rhs) { - return lhs.ToArray() < rhs.ToArray(); + return !(lhs == rhs); } - bool operator<=(const Uuid& lhs, const Uuid& rhs) + constexpr bool operator<(const Uuid& lhs, const Uuid& rhs) { - return lhs.ToArray() <= rhs.ToArray(); + const std::array& lhsArray = lhs.ToArray(); + const std::array& rhsArray = rhs.ToArray(); + for (std::size_t i = 0; i < lhsArray.size(); ++i) + { + if (lhsArray[i] != rhsArray[i]) + return lhsArray[i] < rhsArray[i]; + } + + return false; } - bool operator>(const Uuid& lhs, const Uuid& rhs) + constexpr bool operator<=(const Uuid& lhs, const Uuid& rhs) { - return lhs.ToArray() > rhs.ToArray(); + return !(rhs < lhs); } - bool operator>=(const Uuid& lhs, const Uuid& rhs) + constexpr bool operator>(const Uuid& lhs, const Uuid& rhs) { - return lhs.ToArray() >= rhs.ToArray(); + return rhs < lhs; + } + + constexpr bool operator>=(const Uuid& lhs, const Uuid& rhs) + { + return !(lhs < rhs); } -} -namespace Nz -{ bool Serialize(SerializationContext& context, const Uuid& value, TypeTag) { const std::array& array = value.ToArray(); @@ -86,6 +149,13 @@ namespace Nz *value = Uuid(array); return true; } + + std::ostream& operator<<(std::ostream& out, const Uuid& guid) + { + std::array uuidStr = guid.ToStringArray(); + + return out << uuidStr.data(); + } } namespace std diff --git a/src/Nazara/Core/Uuid.cpp b/src/Nazara/Core/Uuid.cpp index 7be2ff6d7..4039d0360 100644 --- a/src/Nazara/Core/Uuid.cpp +++ b/src/Nazara/Core/Uuid.cpp @@ -15,28 +15,6 @@ namespace Nz { - namespace - { - bool ParseHexadecimalPair(Pointer& str, UInt8& number) - { - number = 0; - - for (UInt8 mul : { UInt8(0x10), UInt8(1) }) - { - if (*str >= '0' && *str <= '9') - number += (*str - '0') * mul; - else if (((*str & 0x5F) >= 'A' && (*str & 0x5F) <= 'F')) - number += ((*str & 0x5F) - 'A' + 10) * mul; - else - return false; - - str++; - } - - return true; - } - } - std::array Uuid::ToStringArray() const { std::array uuidStr; //< Including \0 @@ -47,34 +25,6 @@ namespace Nz return uuidStr; } - Uuid Uuid::FromString(std::string_view str) - { - if (str.size() != 36) - return {}; - - const char* ptr = str.data(); - - std::array uuid; - UInt8* uuidPart = &uuid[0]; - - bool first = true; - for (std::size_t groupSize : { 4, 2, 2, 2, 6 }) - { - if (!first && *ptr++ != '-') - return {}; - - first = false; - - for (std::size_t i = 0; i < groupSize; ++i) - { - if (!ParseHexadecimalPair(ptr, *uuidPart++)) - return {}; - } - } - - return Uuid{ uuid }; - } - Uuid Uuid::Generate() { std::array uuid; @@ -105,11 +55,4 @@ namespace Nz return uuid; } - - std::ostream& operator<<(std::ostream& out, const Uuid& guid) - { - std::array uuidStr = guid.ToStringArray(); - - return out << uuidStr.data(); - } }