141 lines
5.8 KiB
C++
141 lines
5.8 KiB
C++
// Copyright (C) 2024 Jérôme "SirLynix" Leclercq (lynix680@gmail.com)
|
|
// This file is part of the "Nazara Engine - Core module"
|
|
// For conditions of distribution and use, see copyright notice in Export.hpp
|
|
|
|
#pragma once
|
|
|
|
#ifndef NAZARA_CORE_SERIALIZATION_HPP
|
|
#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>
|
|
#include <NazaraUtils/MovablePtr.hpp>
|
|
#include <NazaraUtils/TypeTag.hpp>
|
|
#include <NazaraUtils/EnumArray.hpp>
|
|
#include <string>
|
|
|
|
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;
|
|
virtual bool PopArray() = 0;
|
|
|
|
virtual bool Write(std::string_view name, bool value) = 0;
|
|
virtual bool Write(std::string_view name, Nz::Int8 value) = 0;
|
|
virtual bool Write(std::string_view name, Nz::Int16 value) = 0;
|
|
virtual bool Write(std::string_view name, Nz::Int32 value) = 0;
|
|
virtual bool Write(std::string_view name, Nz::Int64 value) = 0;
|
|
virtual bool Write(std::string_view name, Nz::UInt8 value) = 0;
|
|
virtual bool Write(std::string_view name, Nz::UInt16 value) = 0;
|
|
virtual bool Write(std::string_view name, Nz::UInt32 value) = 0;
|
|
virtual bool Write(std::string_view name, Nz::UInt64 value) = 0;
|
|
virtual bool Write(std::string_view name, float value) = 0;
|
|
virtual bool Write(std::string_view name, double value) = 0;
|
|
virtual bool Write(std::string_view name, const std::string& value) = 0;
|
|
|
|
virtual bool Read(std::string_view name, bool* value) = 0;
|
|
virtual bool Read(std::string_view name, Nz::Int8* value) = 0;
|
|
virtual bool Read(std::string_view name, Nz::Int16* value) = 0;
|
|
virtual bool Read(std::string_view name, Nz::Int32* value) = 0;
|
|
virtual bool Read(std::string_view name, Nz::Int64* value) = 0;
|
|
virtual bool Read(std::string_view name, Nz::UInt8* value) = 0;
|
|
virtual bool Read(std::string_view name, Nz::UInt16* value) = 0;
|
|
virtual bool Read(std::string_view name, Nz::UInt32* value) = 0;
|
|
virtual bool Read(std::string_view name, Nz::UInt64* value) = 0;
|
|
virtual bool Read(std::string_view name, float* value) = 0;
|
|
virtual bool Read(std::string_view name, double* value) = 0;
|
|
virtual bool Read(std::string_view name, std::string* value) = 0;
|
|
};
|
|
|
|
template<typename T>
|
|
inline bool Serialize(SerializationContext&, T, TypeTag<T>) { return false; }
|
|
|
|
template<typename T>
|
|
bool Serialize(SerializationContext& context, std::string_view name, const T& value);
|
|
|
|
template<typename T>
|
|
inline bool Serialize(SerializationContext& context, std::string_view name, const T& value, TypeTag<T>);
|
|
|
|
inline bool Serialize(SerializationContext& context, std::string_view name, const bool& value, TypeTag<bool>);
|
|
inline bool Serialize(SerializationContext& context, std::string_view name, const std::string& value, TypeTag<std::string>);
|
|
inline bool Serialize(SerializationContext& context, std::string_view name, const std::filesystem::path& value, TypeTag<std::filesystem::path>);
|
|
|
|
template<typename T, size_t N>
|
|
bool Serialize(SerializationContext& context, std::string_view name, const T (&values)[N]);
|
|
|
|
template<typename T, size_t N>
|
|
bool Serialize(SerializationContext& context, std::string_view name, const std::array<T, N>& values);
|
|
|
|
template<typename T>
|
|
bool Serialize(SerializationContext& context, std::string_view name, const std::vector<T>& values);
|
|
|
|
template<typename E, typename T>
|
|
bool Serialize(SerializationContext& context, std::string_view name, const Nz::EnumArray<E, T>& values);
|
|
|
|
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 Serialize(SerializationContext& context, std::string_view name, const Flags<T>& flags, TypeTag<Flags<T>>);
|
|
|
|
|
|
template<typename T>
|
|
inline bool Unserialize(SerializationContext&, T*, TypeTag<T>) { return false; }
|
|
|
|
template<typename T>
|
|
inline bool Unserialize(SerializationContext& context, std::string_view name, T* value);
|
|
|
|
template<typename T>
|
|
bool Unserialize(SerializationContext& context, std::string_view name, T* value, TypeTag<T>);
|
|
|
|
inline bool Unserialize(SerializationContext& context, std::string_view name, bool* value, TypeTag<bool>);
|
|
inline bool Unserialize(SerializationContext& context, std::string_view name, std::string* value, TypeTag<std::string>);
|
|
inline bool Unserialize(SerializationContext& context, std::string_view name, std::filesystem::path* value, TypeTag<std::filesystem::path>);
|
|
|
|
template<typename T, size_t N>
|
|
bool Unserialize(SerializationContext& context, std::string_view name, T (*values)[N]);
|
|
|
|
template<typename T, size_t N>
|
|
bool Unserialize(SerializationContext& context, std::string_view name, std::array<T, N>* values);
|
|
|
|
template<typename T>
|
|
bool Unserialize(SerializationContext& context, std::string_view name, std::vector<T>* values);
|
|
|
|
template<typename E, typename T>
|
|
bool Unserialize(SerializationContext& context, std::string_view name, Nz::EnumArray<E, T>* values);
|
|
|
|
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>);
|
|
|
|
template <typename T>
|
|
inline bool Unserialize(SerializationContext& context, std::string_view name, Flags<T>* flags, TypeTag<Flags<T>>);
|
|
}
|
|
|
|
#include <Nazara/Core/Serialization.inl>
|
|
|
|
#endif // NAZARA_CORE_SERIALIZATION_HPP
|