Core: Add Uuid class
This commit is contained in:
parent
36aea2ca0c
commit
72b664f42c
|
|
@ -93,5 +93,6 @@
|
||||||
#include <Nazara/Core/TypeTag.hpp>
|
#include <Nazara/Core/TypeTag.hpp>
|
||||||
#include <Nazara/Core/Unicode.hpp>
|
#include <Nazara/Core/Unicode.hpp>
|
||||||
#include <Nazara/Core/Updatable.hpp>
|
#include <Nazara/Core/Updatable.hpp>
|
||||||
|
#include <Nazara/Core/Uuid.hpp>
|
||||||
|
|
||||||
#endif // NAZARA_GLOBAL_CORE_HPP
|
#endif // NAZARA_GLOBAL_CORE_HPP
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,66 @@
|
||||||
|
// Copyright (C) 2022 Full Cycle Games
|
||||||
|
// This file is part of the "Nazara Engine - Core module"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifndef NAZARA_CORE_UUID_HPP
|
||||||
|
#define NAZARA_CORE_UUID_HPP
|
||||||
|
|
||||||
|
#include <Nazara/Prerequisites.hpp>
|
||||||
|
#include <Nazara/Core/Algorithm.hpp>
|
||||||
|
#include <Nazara/Core/Config.hpp>
|
||||||
|
#include <array>
|
||||||
|
#include <functional>
|
||||||
|
#include <iosfwd>
|
||||||
|
|
||||||
|
namespace Nz
|
||||||
|
{
|
||||||
|
class NAZARA_CORE_API Uuid
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
inline Uuid();
|
||||||
|
inline Uuid(const std::array<UInt8, 16> guid);
|
||||||
|
Uuid(const Uuid&) = default;
|
||||||
|
Uuid(Uuid&& generator) = default;
|
||||||
|
~Uuid() = default;
|
||||||
|
|
||||||
|
inline bool IsNull() const;
|
||||||
|
|
||||||
|
inline const std::array<UInt8, 16>& ToArray() const;
|
||||||
|
inline std::string ToString() const;
|
||||||
|
std::array<char, 37> ToStringArray() const;
|
||||||
|
|
||||||
|
Uuid& operator=(const Uuid&) = default;
|
||||||
|
Uuid& operator=(Uuid&&) = default;
|
||||||
|
|
||||||
|
static Uuid Generate();
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::array<UInt8, 16> m_guid;
|
||||||
|
};
|
||||||
|
|
||||||
|
NAZARA_CORE_API std::ostream& operator<<(std::ostream& out, const Uuid& guid);
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace Nz
|
||||||
|
{
|
||||||
|
inline bool Serialize(SerializationContext& context, const Uuid& value, TypeTag<Uuid>);
|
||||||
|
inline bool Unserialize(SerializationContext& context, Uuid* value, TypeTag<Uuid>);
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace std
|
||||||
|
{
|
||||||
|
template<>
|
||||||
|
struct hash<Nz::Uuid>;
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <Nazara/Core/Uuid.inl>
|
||||||
|
|
||||||
|
#endif // NAZARA_CORE_UUID_HPP
|
||||||
|
|
@ -0,0 +1,110 @@
|
||||||
|
// Copyright (C) 2022 Full Cycle Games
|
||||||
|
// This file is part of the "Nazara Engine - Core module"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
|
#include <Nazara/Core/Uuid.hpp>
|
||||||
|
#include <Nazara/Core/Debug.hpp>
|
||||||
|
|
||||||
|
namespace Nz
|
||||||
|
{
|
||||||
|
inline Uuid::Uuid()
|
||||||
|
{
|
||||||
|
m_guid.fill(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Uuid::Uuid(const std::array<UInt8, 16> guid) :
|
||||||
|
m_guid(guid)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
inline bool Uuid::IsNull() const
|
||||||
|
{
|
||||||
|
Uuid NullGuid;
|
||||||
|
return *this == NullGuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline const std::array<UInt8, 16>& Uuid::ToArray() const
|
||||||
|
{
|
||||||
|
return m_guid;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::string Uuid::ToString() const
|
||||||
|
{
|
||||||
|
std::array<char, 37> guidStr = ToStringArray();
|
||||||
|
|
||||||
|
return std::string(guidStr.data(), guidStr.size() - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator==(const Uuid& lhs, const Uuid& rhs)
|
||||||
|
{
|
||||||
|
return lhs.ToArray() == rhs.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator!=(const Uuid& lhs, const Uuid& rhs)
|
||||||
|
{
|
||||||
|
return lhs.ToArray() != rhs.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator<(const Uuid& lhs, const Uuid& rhs)
|
||||||
|
{
|
||||||
|
return lhs.ToArray() < rhs.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator<=(const Uuid& lhs, const Uuid& rhs)
|
||||||
|
{
|
||||||
|
return lhs.ToArray() <= rhs.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator>(const Uuid& lhs, const Uuid& rhs)
|
||||||
|
{
|
||||||
|
return lhs.ToArray() > rhs.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool operator>=(const Uuid& lhs, const Uuid& rhs)
|
||||||
|
{
|
||||||
|
return lhs.ToArray() >= rhs.ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace Nz
|
||||||
|
{
|
||||||
|
bool Serialize(SerializationContext& context, const Uuid& value, TypeTag<Uuid>)
|
||||||
|
{
|
||||||
|
const std::array<Nz::UInt8, 16>& array = value.ToArray();
|
||||||
|
if (context.stream->Write(array.data(), array.size()) != array.size())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Unserialize(SerializationContext& context, Uuid* value, TypeTag<Uuid>)
|
||||||
|
{
|
||||||
|
std::array<Nz::UInt8, 16> array;
|
||||||
|
if (context.stream->Read(array.data(), array.size()) != array.size())
|
||||||
|
return false;
|
||||||
|
|
||||||
|
*value = Uuid(array);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace std
|
||||||
|
{
|
||||||
|
template <>
|
||||||
|
struct hash<Nz::Uuid>
|
||||||
|
{
|
||||||
|
size_t operator()(const Nz::Uuid& guid) const
|
||||||
|
{
|
||||||
|
// DJB2 algorithm http://www.cse.yorku.ca/~oz/hash.html
|
||||||
|
size_t h = 5381;
|
||||||
|
|
||||||
|
const array<Nz::UInt8, 16>& data = guid.ToArray();
|
||||||
|
for (size_t i = 0; i < data.size(); ++i)
|
||||||
|
h = ((h << 5) + h) + data[i];
|
||||||
|
|
||||||
|
return h;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <Nazara/Core/DebugOff.hpp>
|
||||||
|
|
@ -0,0 +1,65 @@
|
||||||
|
// Copyright (C) 2022 Full Cycle Games
|
||||||
|
// This file is part of the "Nazara Engine - Core module"
|
||||||
|
// For conditions of distribution and use, see copyright notice in Config.hpp
|
||||||
|
|
||||||
|
#include <Nazara/Core/Uuid.hpp>
|
||||||
|
#include <ostream>
|
||||||
|
|
||||||
|
#ifdef NAZARA_PLATFORM_WINDOWS
|
||||||
|
#include <objbase.h>
|
||||||
|
#elif defined(NAZARA_PLATFORM_LINUX) || defined(NAZARA_PLATFORM_MACOSX)
|
||||||
|
#include <uuid/uuid.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <Nazara/Core/Debug.hpp>
|
||||||
|
|
||||||
|
namespace Nz
|
||||||
|
{
|
||||||
|
std::array<char, 37> Uuid::ToStringArray() const
|
||||||
|
{
|
||||||
|
std::array<char, 37> guidStr; //< Including \0
|
||||||
|
std::snprintf(guidStr.data(), guidStr.size(), "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
|
||||||
|
m_guid[0], m_guid[1], m_guid[2], m_guid[3], m_guid[4], m_guid[5], m_guid[6], m_guid[7],
|
||||||
|
m_guid[8], m_guid[9], m_guid[10], m_guid[11], m_guid[12], m_guid[13], m_guid[14], m_guid[15]);
|
||||||
|
|
||||||
|
return guidStr;
|
||||||
|
}
|
||||||
|
|
||||||
|
Uuid Uuid::Generate()
|
||||||
|
{
|
||||||
|
std::array<UInt8, 16> guid;
|
||||||
|
|
||||||
|
#ifdef NAZARA_PLATFORM_WINDOWS
|
||||||
|
GUID id;
|
||||||
|
CoCreateGuid(&id);
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < 4; ++i)
|
||||||
|
guid[i] = static_cast<UInt8>(id.Data1 >> ((3 - i) * 8 & 0xFF));
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < 2; ++i)
|
||||||
|
guid[4 + i] = static_cast<UInt8>(id.Data2 >> ((1 - i) * 8 & 0xFF));
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < 2; ++i)
|
||||||
|
guid[6 + i] = static_cast<UInt8>(id.Data3 >> ((1 - i) * 8 & 0xFF));
|
||||||
|
|
||||||
|
for (unsigned int i = 0; i < 8; ++i)
|
||||||
|
guid[8 + i] = static_cast<UInt8>(id.Data4[i]);
|
||||||
|
#elif defined(NAZARA_PLATFORM_LINUX) || defined(NAZARA_PLATFORM_MACOSX)
|
||||||
|
uuid_t id;
|
||||||
|
uuid_generate(id);
|
||||||
|
|
||||||
|
std::copy(std::begin(id), std::end(id), guid.begin());
|
||||||
|
#else
|
||||||
|
#error Missing platform support
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return guid;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ostream& operator<<(std::ostream& out, const Uuid& guid)
|
||||||
|
{
|
||||||
|
std::array<char, 37> guidStr = guid.ToStringArray();
|
||||||
|
|
||||||
|
return out << guidStr.data();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,60 @@
|
||||||
|
#include <Nazara/Core/Uuid.hpp>
|
||||||
|
#include <catch2/catch.hpp>
|
||||||
|
#include <regex>
|
||||||
|
#include <set>
|
||||||
|
#include <unordered_set>
|
||||||
|
|
||||||
|
SCENARIO("Uuid", "[CORE][UUID]")
|
||||||
|
{
|
||||||
|
WHEN("Generating a null UUID")
|
||||||
|
{
|
||||||
|
Nz::Uuid nullUuid;
|
||||||
|
CHECK(nullUuid.IsNull());
|
||||||
|
CHECK(nullUuid.ToArray() == std::array<Nz::UInt8, 16>{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
|
||||||
|
CHECK(nullUuid.ToString() == "00000000-0000-0000-0000-000000000000");
|
||||||
|
CHECK(nullUuid.ToStringArray() == std::array<char, 37>{"00000000-0000-0000-0000-000000000000"});
|
||||||
|
CHECK(nullUuid == Nz::Uuid{});
|
||||||
|
CHECK(nullUuid >= Nz::Uuid{});
|
||||||
|
CHECK(nullUuid <= Nz::Uuid{});
|
||||||
|
CHECK_FALSE(nullUuid > Nz::Uuid{});
|
||||||
|
CHECK_FALSE(nullUuid < Nz::Uuid{});
|
||||||
|
CHECK(nullUuid != Nz::Uuid::Generate());
|
||||||
|
}
|
||||||
|
|
||||||
|
WHEN("Generating a UUID")
|
||||||
|
{
|
||||||
|
// https://stackoverflow.com/questions/136505/searching-for-uuids-in-text-with-regex
|
||||||
|
std::regex uuidRegex(R"(^\b[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\b[0-9a-f]{12}\b$)");
|
||||||
|
|
||||||
|
Nz::Uuid uuid = Nz::Uuid::Generate();
|
||||||
|
CHECK_FALSE(uuid.IsNull());
|
||||||
|
CHECK(std::regex_match(uuid.ToString(), uuidRegex));
|
||||||
|
CHECK(uuid == uuid);
|
||||||
|
CHECK(uuid == Nz::Uuid{uuid.ToArray()});
|
||||||
|
CHECK(uuid >= uuid);
|
||||||
|
CHECK(uuid <= uuid);
|
||||||
|
CHECK_FALSE(uuid > uuid);
|
||||||
|
CHECK_FALSE(uuid < uuid);
|
||||||
|
}
|
||||||
|
|
||||||
|
WHEN("Generating multiple UUID, they are unique")
|
||||||
|
{
|
||||||
|
std::set<Nz::Uuid> uuidSet;
|
||||||
|
std::unordered_set<Nz::Uuid> uuidUnorderedset;
|
||||||
|
|
||||||
|
auto InsertUniqueUuid = [](auto& container, const Nz::Uuid& uuid)
|
||||||
|
{
|
||||||
|
auto it = container.find(uuid);
|
||||||
|
REQUIRE(it == container.end());
|
||||||
|
container.insert(uuid);
|
||||||
|
};
|
||||||
|
|
||||||
|
for (std::size_t i = 0; i < 1'000; ++i)
|
||||||
|
{
|
||||||
|
auto uuid = Nz::Uuid::Generate();
|
||||||
|
|
||||||
|
InsertUniqueUuid(uuidSet, uuid);
|
||||||
|
InsertUniqueUuid(uuidUnorderedset, uuid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -12,8 +12,10 @@ local modules = {
|
||||||
-- NazaraMath is header-only, make it part of the core project
|
-- NazaraMath is header-only, make it part of the core project
|
||||||
add_headerfiles("include/(Nazara/Math/**.hpp)", "include/(Nazara/Math/**.inl)")
|
add_headerfiles("include/(Nazara/Math/**.hpp)", "include/(Nazara/Math/**.inl)")
|
||||||
|
|
||||||
if is_plat("linux") then
|
if is_plat("windows") then
|
||||||
add_syslinks("dl", "pthread")
|
add_syslinks("ole32")
|
||||||
|
elseif is_plat("linux") then
|
||||||
|
add_syslinks("dl", "pthread", "uuid")
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
Packages = {"entt"}
|
Packages = {"entt"}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue