Core: Add Uuid class

This commit is contained in:
Jérôme Leclercq
2022-03-04 20:40:41 +01:00
parent 36aea2ca0c
commit 72b664f42c
6 changed files with 306 additions and 2 deletions

View File

@@ -93,5 +93,6 @@
#include <Nazara/Core/TypeTag.hpp>
#include <Nazara/Core/Unicode.hpp>
#include <Nazara/Core/Updatable.hpp>
#include <Nazara/Core/Uuid.hpp>
#endif // NAZARA_GLOBAL_CORE_HPP

View File

@@ -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

View File

@@ -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>