Rename component serialization macros so they can be used with non-component structs and classes

This commit is contained in:
S David 2023-05-17 11:46:11 -04:00
parent ffe33d1921
commit 2f2f748d37
3 changed files with 26 additions and 21 deletions

View File

@ -12,10 +12,12 @@
#include <sstream>
#include <string>
#include <nlohmann/json.hpp>
#include "framework/core/INonAssignable.hpp"
#include "framework/core/components/Metadata.hpp"
// #region Macros for child classes
// #region Macros for child classesbevers GTA V
#define COMPONENT(ClassName) \
public: \
static const TypeInfo& GetType() \
@ -24,9 +26,6 @@
return platonicInstance.GetMetadata().type; \
} \
\
template<typename Archive> \
void serialize(Archive& archive); \
\
private: \
inline virtual void initialize_metadata(const InstanceId& instance_id) \
override \
@ -43,23 +42,30 @@
this->component_info.instance.name = buffer.str(); \
}
#define COMPONENT_SERIALIZABLE(ClassName) \
#define SERIALIZABLE(ClassName) \
void to_json(nlohmann::json& json_object) const; \
void from_json(const nlohmann::json& json_object); \
\
friend void to_json(nlohmann::json& json_object, const ClassName& obj); \
friend void from_json(const nlohmann::json& json_object, ClassName& obj);
#define COMPONENT_SERIALIZERS(ClassName) \
inline void to_json(nlohmann::json& json_object, const ClassName& obj) \
#define REGISTER_JSON_SERIALIZERS(ClassName) \
namespace nlohmann { \
template<> \
struct adl_serializer<ClassName> \
{ \
obj.to_json(json_object); \
} \
inline void from_json(const nlohmann::json& json_object, \
ClassName& obj) \
{ \
obj.from_json(json_object); \
static ClassName from_json(const json& j) \
{ \
return { j.get<ClassName>() }; \
} \
\
static void to_json(json& j, ClassName t) \
{ \
t.to_json(j); \
} \
}; \
}
// #endregion
namespace QW::Core::Components {

View File

@ -59,7 +59,7 @@ class PlayerBehavior
void OnNotice(const Core::Observable& sender, std::any message) override;
/* #endregion */
COMPONENT_SERIALIZABLE(PlayerBehavior)
SERIALIZABLE(PlayerBehavior)
#ifdef UNIT_TEST // #region
void setPosition(PositionAttribute* pos) { this->position_ptr = pos; }
@ -74,7 +74,8 @@ class PlayerBehavior
private:
PositionAttribute* position_ptr;
std::pair<decimal, decimal> velocity;
std::pair<decimal, decimal> velocity; /// @todo: move this to a
/// PhysicalProperties object
Core::Input::InputMapper inputMapper;
@ -82,8 +83,8 @@ class PlayerBehavior
std::queue<SDL_Event> eventQueue;
};
COMPONENT_SERIALIZERS(PlayerBehavior)
}
REGISTER_JSON_SERIALIZERS(QW::Core::Components::PlayerBehavior)
// clang-format off
// vim: set foldmethod=marker foldmarker=#region,#endregion textwidth=80 ts=8 sts=0 sw=8 noexpandtab ft=cpp.doxygen :

View File

@ -42,16 +42,14 @@ class PositionAttribute : public BaseComponent
}
virtual ~PositionAttribute() = default;
COMPONENT_SERIALIZABLE(PositionAttribute)
SERIALIZABLE(PositionAttribute)
protected:
private:
std::unordered_map<std::string, decimal*> data_dictionary;
};
COMPONENT_SERIALIZERS(PositionAttribute)
} // end of namespace
} // end of namespace QW::Core::Components
REGISTER_JSON_SERIALIZERS(QW::Core::Components::PositionAttribute)
// clang-format off
// vim: set foldmethod=marker foldmarker=#region,#endregion textwidth=80 ts=8 sts=0 sw=8 noexpandtab ft=cpp.doxygen :