Botcraft 1.21.4
Loading...
Searching...
No Matches
AutoSerializedMacros.hpp File Reference

Go to the source code of this file.

Macros

#define DEFINE_CONDITION(Name, ...)   private: bool Name() const { return __VA_ARGS__; } static_assert(true, "Forcing ;")
 Define a condition that can be used later inside an Internal::Conditioned type.
 
#define _OFFSETS_DECLARATION(Name)
 
#define _OFFSETS_FIELDS_DATA_DECLARATION(Name)
 
#define SERIALIZED_FIELD_WITHOUT_GETTER_SETTER(Name, ...)
 Define a field that will be automatically integrated in Read/Write and Json serialization We start by retrieving the SerializedType of the given type and create the actual class member.
 
#define GETTER(Name)
 
#define SETTER(Name)
 
#define GETTER_SETTER(Name)   GETTER(Name); SETTER(Name)
 
#define SERIALIZED_FIELD(Name, ...)
 
#define DECLARE_READ   protected: virtual void ReadImpl(ReadIterator& iter, size_t& length) override
 Declare ReadImpl virtual function for auto serializable types.
 
#define DECLARE_WRITE   protected: virtual void WriteImpl(WriteContainer& container) const override
 Declare WriteImpl virtual function for auto serializable types.
 
#define DECLARE_SERIALIZE   protected: virtual Json::Value SerializeImpl() const override
 Declare SerializeImpl virtual function for auto serializable types.
 
#define _DEFINE_OFFSETS_UTILITIES
 
#define DEFINE_UTILITIES
 Define some constexpr utilities used to deal with the auto serializable fields We use void as the unique identifier type template parameter for FieldsData as we are sure it'll be different from what was used for the fields.
 
#define DECLARE_READ_WRITE_SERIALIZE   DEFINE_UTILITIES; DECLARE_READ; DECLARE_WRITE; DECLARE_SERIALIZE
 Define auto serializable utilities and declare ReadImpl, WriteImpl and SerializeImpl virtual functions.
 
#define _OFFSETS_POINTERS
 
#define DEFINE_READ(ClassName)
 Define ReadImpl virtual function that loops through all auto serializable fields.
 
#define DEFINE_WRITE(ClassName)
 Define WriteImpl virtual function that loops through all auto serializable fields.
 
#define DEFINE_SERIALIZE(ClassName)
 
#define DEFINE_NETWORK_TYPE(ClassName)
 
#define DEFINE_MESSAGE_CLASS(ClassName)
 

Macro Definition Documentation

◆ _DEFINE_OFFSETS_UTILITIES

#define _DEFINE_OFFSETS_UTILITIES

Definition at line 100 of file AutoSerializedMacros.hpp.

◆ _OFFSETS_DECLARATION

#define _OFFSETS_DECLARATION (   Name)

Definition at line 19 of file AutoSerializedMacros.hpp.

◆ _OFFSETS_FIELDS_DATA_DECLARATION

#define _OFFSETS_FIELDS_DATA_DECLARATION (   Name)

Definition at line 20 of file AutoSerializedMacros.hpp.

◆ _OFFSETS_POINTERS

#define _OFFSETS_POINTERS

Definition at line 128 of file AutoSerializedMacros.hpp.

◆ DECLARE_READ

#define DECLARE_READ   protected: virtual void ReadImpl(ReadIterator& iter, size_t& length) override

Declare ReadImpl virtual function for auto serializable types.

Definition at line 82 of file AutoSerializedMacros.hpp.

◆ DECLARE_READ_WRITE_SERIALIZE

#define DECLARE_READ_WRITE_SERIALIZE   DEFINE_UTILITIES; DECLARE_READ; DECLARE_WRITE; DECLARE_SERIALIZE

Define auto serializable utilities and declare ReadImpl, WriteImpl and SerializeImpl virtual functions.

Definition at line 123 of file AutoSerializedMacros.hpp.

◆ DECLARE_SERIALIZE

#define DECLARE_SERIALIZE   protected: virtual Json::Value SerializeImpl() const override

Declare SerializeImpl virtual function for auto serializable types.

Definition at line 86 of file AutoSerializedMacros.hpp.

◆ DECLARE_WRITE

#define DECLARE_WRITE   protected: virtual void WriteImpl(WriteContainer& container) const override

Declare WriteImpl virtual function for auto serializable types.

Definition at line 84 of file AutoSerializedMacros.hpp.

◆ DEFINE_CONDITION

#define DEFINE_CONDITION (   Name,
  ... 
)    private: bool Name() const { return __VA_ARGS__; } static_assert(true, "Forcing ;")

Define a condition that can be used later inside an Internal::Conditioned type.

Definition at line 4 of file AutoSerializedMacros.hpp.

◆ DEFINE_MESSAGE_CLASS

#define DEFINE_MESSAGE_CLASS (   ClassName)
Value:
DEFINE_READ(ClassName); \
DEFINE_WRITE(ClassName); \
DEFINE_SERIALIZE(ClassName); \
template class BaseMessage<ClassName>
#define DEFINE_READ(ClassName)
Define ReadImpl virtual function that loops through all auto serializable fields.

Definition at line 239 of file AutoSerializedMacros.hpp.

◆ DEFINE_NETWORK_TYPE

#define DEFINE_NETWORK_TYPE (   ClassName)
Value:
DEFINE_READ(ClassName); \
DEFINE_WRITE(ClassName); \
DEFINE_SERIALIZE(ClassName)

Definition at line 233 of file AutoSerializedMacros.hpp.

◆ DEFINE_READ

#define DEFINE_READ (   ClassName)

Define ReadImpl virtual function that loops through all auto serializable fields.

Definition at line 131 of file AutoSerializedMacros.hpp.

◆ DEFINE_SERIALIZE

#define DEFINE_SERIALIZE (   ClassName)
Value:
Json::Value ClassName::SerializeImpl() const { \
Json::Value output; \
Internal::loop<num_fields>([&](auto i) { \
const auto& field = this->GetField<i>(); \
std::optional<Json::Value> serialized = std::nullopt; \
if constexpr (Internal::IsCustomType<field_type<i>>) { \
serialized = field_type<i>::Serialize( \
this, field _OFFSETS_POINTERS); \
} \
else if constexpr (Internal::IsConditioned<field_type<i>>) { \
if (field_type<i>::Evaluate(this)) { \
serialized = SerializeType<field_storage_type<i>>( \
} \
} \
else { \
serialized = SerializeType<field_storage_type<i>>( \
} \
if (serialized.has_value()) { \
output[std::string(field_name<i>)] = serialized.value(); \
} \
}); \
return output; \
} static_assert(true, "Forcing ;")
#define _OFFSETS_POINTERS

Definition at line 205 of file AutoSerializedMacros.hpp.

◆ DEFINE_UTILITIES

#define DEFINE_UTILITIES
Value:
private: \
template <size_t, typename> struct FieldsData; \
static constexpr size_t num_fields = Internal::field_index<0, void, FieldsData>; \
template <size_t i> static constexpr std::string_view field_name = FieldsData<i, void>::field_name; \
template <size_t i> using field_type = typename FieldsData<i, void>::field_type; \
template <size_t i> using field_storage_type = typename FieldsData<i, void>::field_storage_type; \
template <size_t i> using field_serialization_type = \
typename FieldsData<i, void>::field_serialization_type; \
template <size_t i> auto& GetField() { \
return this->*FieldsData<i, void>::template field_ptr<std::remove_reference_t<decltype(*this)>>; } \
template <size_t i> const auto& GetField() const { \
return this->*FieldsData<i, void>::template field_ptr<std::remove_reference_t<decltype(*this)>>; } \
static_assert(true, "Forcing ;")
#define _DEFINE_OFFSETS_UTILITIES

Define some constexpr utilities used to deal with the auto serializable fields We use void as the unique identifier type template parameter for FieldsData as we are sure it'll be different from what was used for the fields.

Definition at line 106 of file AutoSerializedMacros.hpp.

◆ DEFINE_WRITE

#define DEFINE_WRITE (   ClassName)
Value:
void ClassName::WriteImpl(WriteContainer& container) const { \
Internal::loop<num_fields>([&](auto i) { \
const auto& field = this->GetField<i>(); \
if constexpr (Internal::IsCustomType<field_type<i>>) { \
field_type<i>::Write(this, field, container); \
} \
else if constexpr (Internal::IsConditioned<field_type<i>>) { \
if (field_type<i>::Evaluate(this)) { \
if constexpr (field_type<i>::stored_as_optional) { \
WriteData< \
typename field_storage_type<i>::value_type, \
field_serialization_type<i>>(field.value(), container); \
} \
else { \
field_storage_type<i>, \
field_serialization_type<i>>(field, container); \
} \
} \
} \
else { \
field_storage_type<i>, \
field_serialization_type<i>>(field, container); \
} \
}); \
} static_assert(true, "Forcing ;")
void WriteData(typename std::conditional_t< std::is_arithmetic_v< StorageType >||std::is_enum_v< StorageType >, StorageType, const StorageType & > value, WriteContainer &container)

Define WriteImpl virtual function that loops through all auto serializable fields.

Definition at line 175 of file AutoSerializedMacros.hpp.

◆ GETTER

#define GETTER (   Name)
Value:
public: \
std::conditional_t<std::is_arithmetic_v<Name##_type::storage_type> || std::is_enum_v<Name##_type::storage_type>, \
Name##_type::storage_type, const Name##_type::storage_type&> Get##Name() const { return Name; } \
static_assert(true, "Forcing ;")

Definition at line 62 of file AutoSerializedMacros.hpp.

◆ GETTER_SETTER

#define GETTER_SETTER (   Name)    GETTER(Name); SETTER(Name)

Definition at line 75 of file AutoSerializedMacros.hpp.

◆ SERIALIZED_FIELD

#define SERIALIZED_FIELD (   Name,
  ... 
)
Value:
GETTER_SETTER(Name)
#define SERIALIZED_FIELD_WITHOUT_GETTER_SETTER(Name,...)
Define a field that will be automatically integrated in Read/Write and Json serialization We start by...

Definition at line 77 of file AutoSerializedMacros.hpp.

◆ SERIALIZED_FIELD_WITHOUT_GETTER_SETTER

#define SERIALIZED_FIELD_WITHOUT_GETTER_SETTER (   Name,
  ... 
)
Value:
private: \
using Name##_type = typename Internal::SerializedType<__VA_ARGS__>; \
Name##_type::storage_type Name{}; \
_OFFSETS_DECLARATION(Name); \
template <size_t, typename> struct FieldsData; \
static constexpr size_t Name##_index = \
Internal::field_index<0, struct FieldDummy##Name, FieldsData>; \
template <typename T> \
struct FieldsData<Name##_index, T> { \
static constexpr std::array name_array = \
Internal::ToSnakeCase<Internal::GetSnakeCaseSize(#Name)>(#Name); \
static constexpr std::string_view field_name = \
Internal::ToStringView<std::size(name_array)>(name_array); \
using field_type = __VA_ARGS__; \
using field_storage_type = Name##_type::storage_type; \
using field_serialization_type = Name##_type::serialization_type; \
template <typename Parent> static constexpr field_storage_type \
Parent::*field_ptr = &Parent::Name; \
_OFFSETS_FIELDS_DATA_DECLARATION(Name); \
}; \
static_assert(true, "Forcing ;")

Define a field that will be automatically integrated in Read/Write and Json serialization We start by retrieving the SerializedType of the given type and create the actual class member.

Then we find the index of this field by counting how many FieldsData<size_t, T> template class have already been defined. Last part defines a templated FieldsData struct with all the name/type/member pointer info for this field. For the name, we want to define it in PascalCase but we also need a camel_case litteral version for json serialization, hence the constexpr string processing For field_ptr we don't know the name of the current class so to avoid passing it to the macro we use a template class instead. See GetField in DEFINE_UTILITIES for example of how to use it.

Parameters
namefield name
...field type (passed as VA_ARGS because it can contain some ,) /!\ if Name == Type, use the fully qualified type instead (like ProtocolCraft::Type instead of Type)

Definition at line 35 of file AutoSerializedMacros.hpp.

◆ SETTER

#define SETTER (   Name)
Value:
public: \
auto& Set##Name( \
std::conditional_t<std::is_arithmetic_v<Name##_type::storage_type> || std::is_enum_v<Name##_type::storage_type>, \
Name##_type::storage_type, const Name##_type::storage_type&> Name##_) { Name = Name##_; return *this; } \
static_assert(true, "Forcing ;")

Definition at line 68 of file AutoSerializedMacros.hpp.