NazaraEngine/include/Nazara/Core/Serialization.hpp

121 lines
4.9 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/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;
struct NAZARA_CORE_API SerializationContext
{
public:
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>);
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<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>);
template <>
inline bool Unserialize(SerializationContext& context, std::string_view name, bool* value, TypeTag<bool>);
template <>
inline bool Unserialize(SerializationContext& context, std::string_view name, std::string* value, TypeTag<std::string>);
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>);
}
#include <Nazara/Core/Serialization.inl>
#endif // NAZARA_CORE_SERIALIZATION_HPP