add enum serialization
This commit is contained in:
@@ -9,6 +9,8 @@ namespace Nz
|
||||
: public SerializationContext
|
||||
{
|
||||
public:
|
||||
EnumSerializationMode GetEnumSerializationMode() const override { return EnumSerializationMode::Binary; }
|
||||
|
||||
bool PushObject(std::string_view name) override;
|
||||
bool PopObject() override;
|
||||
bool PushArray(std::string_view name) override;
|
||||
|
||||
64
include/Nazara/Core/EnumSerialization.hpp
Normal file
64
include/Nazara/Core/EnumSerialization.hpp
Normal file
@@ -0,0 +1,64 @@
|
||||
#pragma once
|
||||
|
||||
#include <Nazara/Core/Enums.hpp>
|
||||
#include <Nazara/Core/Export.hpp>
|
||||
|
||||
#include <string_view>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
template <typename T>
|
||||
concept Enum = std::is_enum_v<T>;
|
||||
|
||||
template <Enum TEnum> TEnum EnumFromString(std::string_view) { return TEnum{}; }
|
||||
|
||||
#define DEF_ENUM(Type) \
|
||||
NAZARA_CORE_API std::string_view EnumToString(Type t); \
|
||||
NAZARA_CORE_API Type EnumFromString_##Type(std::string_view); \
|
||||
template<> inline Type EnumFromString(std::string_view v) { return EnumFromString_##Type(v); }
|
||||
|
||||
DEF_ENUM(AnimationType);
|
||||
DEF_ENUM(BlendEquation);
|
||||
DEF_ENUM(BlendFunc);
|
||||
DEF_ENUM(BufferAccess);
|
||||
DEF_ENUM(BufferType);
|
||||
DEF_ENUM(BufferUsage);
|
||||
DEF_ENUM(ComponentType);
|
||||
DEF_ENUM(CoordSys);
|
||||
DEF_ENUM(CursorPosition);
|
||||
DEF_ENUM(CubemapFace);
|
||||
DEF_ENUM(DataStorage);
|
||||
DEF_ENUM(ErrorMode);
|
||||
DEF_ENUM(ErrorType);
|
||||
DEF_ENUM(FaceFilling);
|
||||
DEF_ENUM(FaceCulling);
|
||||
DEF_ENUM(FrontFace);
|
||||
DEF_ENUM(ImageType);
|
||||
DEF_ENUM(IndexType);
|
||||
DEF_ENUM(HashType);
|
||||
DEF_ENUM(OpenMode);
|
||||
DEF_ENUM(ParameterType);
|
||||
DEF_ENUM(PixelFormatContent);
|
||||
DEF_ENUM(PixelFormat);
|
||||
DEF_ENUM(PixelFormatSubType);
|
||||
DEF_ENUM(PixelFlipping);
|
||||
DEF_ENUM(PrimitiveMode);
|
||||
DEF_ENUM(PrimitiveType);
|
||||
DEF_ENUM(ProcessorCap);
|
||||
DEF_ENUM(ProcessorVendor);
|
||||
DEF_ENUM(RendererComparison);
|
||||
DEF_ENUM(ResourceLoadingError);
|
||||
DEF_ENUM(SamplerFilter);
|
||||
DEF_ENUM(SamplerMipmapMode);
|
||||
DEF_ENUM(SamplerWrap);
|
||||
DEF_ENUM(SphereType);
|
||||
DEF_ENUM(StencilOperation);
|
||||
DEF_ENUM(StreamOption);
|
||||
DEF_ENUM(TextAlign);
|
||||
DEF_ENUM(TextStyle);
|
||||
DEF_ENUM(VertexComponent);
|
||||
DEF_ENUM(VertexInputRate);
|
||||
DEF_ENUM(VertexLayout);
|
||||
|
||||
#undef DEF_ENUM
|
||||
}
|
||||
@@ -8,6 +8,7 @@
|
||||
#define NAZARA_CORE_SERIALIZATION_HPP
|
||||
|
||||
#include <NazaraUtils/Prerequisites.hpp>
|
||||
#include <Nazara/Core/EnumSerialization.hpp>
|
||||
#include <Nazara/Core/Export.hpp>
|
||||
#include <Nazara/Core/Stream.hpp>
|
||||
#include <NazaraUtils/Endianness.hpp>
|
||||
@@ -21,9 +22,17 @@ namespace Nz
|
||||
template <typename T>
|
||||
concept Numeric = std::is_arithmetic<T>::value;
|
||||
|
||||
enum class EnumSerializationMode
|
||||
{
|
||||
Binary,
|
||||
String,
|
||||
};
|
||||
|
||||
struct NAZARA_CORE_API SerializationContext
|
||||
{
|
||||
public:
|
||||
virtual EnumSerializationMode GetEnumSerializationMode() const = 0;
|
||||
|
||||
virtual bool PushObject(std::string_view name) = 0;
|
||||
virtual bool PopObject() = 0;
|
||||
virtual bool PushArray(std::string_view name) = 0;
|
||||
@@ -83,6 +92,9 @@ namespace Nz
|
||||
template<Numeric T>
|
||||
inline bool Serialize(SerializationContext& context, std::string_view name, const T& value, TypeTag<T>);
|
||||
|
||||
template<Enum T>
|
||||
inline bool Serialize(SerializationContext& context, std::string_view name, const T& value, TypeTag<T>);
|
||||
|
||||
|
||||
template<typename T>
|
||||
inline bool Unserialize(SerializationContext&, T*, TypeTag<T>) { return false; }
|
||||
@@ -113,6 +125,9 @@ namespace Nz
|
||||
|
||||
template<Numeric T>
|
||||
inline bool Unserialize(SerializationContext& context, std::string_view name, T* value, TypeTag<T>);
|
||||
|
||||
template<Enum T>
|
||||
bool Unserialize(SerializationContext& context, std::string_view name, T* value, TypeTag<T>);
|
||||
}
|
||||
|
||||
#include <Nazara/Core/Serialization.inl>
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// This file is part of the "Nazara Engine - Core module"
|
||||
// For conditions of distribution and use, see copyright notice in Export.hpp
|
||||
|
||||
#include <Nazara/Core/EnumSerialization.hpp>
|
||||
|
||||
namespace Nz
|
||||
{
|
||||
@@ -127,6 +128,19 @@ namespace Nz
|
||||
return context.Write(name, value);
|
||||
}
|
||||
|
||||
template<Enum T>
|
||||
inline bool Serialize(SerializationContext& context, std::string_view name, const T& value, TypeTag<T>)
|
||||
{
|
||||
switch (context.GetEnumSerializationMode())
|
||||
{
|
||||
case EnumSerializationMode::Binary:
|
||||
return Serialize(context, name, std::underlying_type_t<T>(value), TypeTag<std::decay_t<std::underlying_type_t<T>>>());
|
||||
case EnumSerializationMode::String:
|
||||
return Serialize(context, name, std::string(EnumToString(value)), TypeTag<std::string>());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
inline bool Unserialize(SerializationContext& context, std::string_view name, T* value)
|
||||
@@ -250,5 +264,33 @@ namespace Nz
|
||||
{
|
||||
return context.Read(name, value);
|
||||
}
|
||||
|
||||
template<Enum T>
|
||||
bool Unserialize(SerializationContext& context, std::string_view name, T* value, TypeTag<T>)
|
||||
{
|
||||
switch (context.GetEnumSerializationMode())
|
||||
{
|
||||
case EnumSerializationMode::Binary:
|
||||
{
|
||||
std::underlying_type_t<T> v = 0;
|
||||
if (!Unserialize(context, name, &v, TypeTag<std::decay_t<std::underlying_type_t<T>>>()))
|
||||
return false;
|
||||
|
||||
*value = static_cast<T>(v);
|
||||
return true;
|
||||
}
|
||||
case EnumSerializationMode::String:
|
||||
{
|
||||
std::string str;
|
||||
if (!Unserialize(context, name, &str, TypeTag<std::string>()))
|
||||
return false;
|
||||
|
||||
*value = EnumFromString<T>(str);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user