Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(data): Allow wrapped fields to be read as normal object with a single field #1425

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ set(CUBOS_CORE_SOURCE
"src/reflection/traits/enum.cpp"
"src/reflection/traits/mask.cpp"
"src/reflection/traits/inherits.cpp"
"src/reflection/traits/wrapper.cpp"
"src/reflection/external/primitives.cpp"
"src/reflection/external/cstring.cpp"
"src/reflection/external/string.cpp"
Expand Down
16 changes: 15 additions & 1 deletion core/include/cubos/core/ecs/reflection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <cubos/core/reflection/traits/constructible.hpp>
#include <cubos/core/reflection/traits/fields.hpp>
#include <cubos/core/reflection/traits/wrapper.hpp>
#include <cubos/core/reflection/type.hpp>

namespace cubos::core::ecs
Expand Down Expand Up @@ -88,7 +89,7 @@ namespace cubos::core::ecs
/// @param pointer Field pointer.
/// @return Builder.
template <typename F>
TypeBuilder&& withField(std::string name, F T::*pointer) &&
TypeBuilder&& withField(std::string name, F T::* pointer) &&
{
mFields.addField(std::move(name), pointer);
return std::move(*this);
Expand All @@ -102,6 +103,19 @@ namespace cubos::core::ecs
return mType;
}

/// @brief Creates a type with a single field.
/// @tparam F Field type.
/// @param pointer Field pointer.
/// @return Type.
template <typename F>
reflection::Type& wrap(F T::* pointer) &&
{
CUBOS_ASSERT(mFields.size() == 0);
mType.with(reflection::WrapperTrait(pointer));
CUBOS_ERROR("We wrapping");
return mType;
}

private:
reflection::Type& mType;
reflection::FieldsTrait mFields;
Expand Down
64 changes: 64 additions & 0 deletions core/include/cubos/core/reflection/traits/wrapper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/// @file
/// @brief Class @ref cubos::core::reflection::WrapperTrait.
/// @ingroup core-reflection

#pragma once

#include <cstdint>
#include <string>

#include <cubos/core/reflection/reflect.hpp>
#include <cubos/core/reflection/traits/fields.hpp>
#include <cubos/core/reflection/type.hpp>
#include <cubos/core/tel/logging.hpp>

namespace cubos::core::reflection
{
/// @brief Describes the single field of a reflected type.
/// @ingroup core-reflection
class CUBOS_CORE_API WrapperTrait
{
public:
CUBOS_REFLECT;

~WrapperTrait()
{
delete mAddressOf;
};

/// @brief Constructs.
WrapperTrait(WrapperTrait&& other)
: mType(other.mType)
, mAddressOf(other.mAddressOf)
{
other.mAddressOf = nullptr;
}

/// @brief Constructs.
template <typename F, typename O>
WrapperTrait(F O::* pointer)
: mType(reflect<F>())
, mAddressOf(new FieldsTrait::AddressOfImpl<O, F>(pointer))
{
}

/// @brief Gets the type of the single field.
/// @return Type.
const Type& type() const
{
return mType;
}

/// @brief Gets a pointer to the field on a given instance of the reflected type.
/// @param instance Pointer to the instance.
/// @return Pointer to the field on the given instance.
void* value(const void* instance) const
{
return reinterpret_cast<void*>(this->mAddressOf->get(instance));
}

private:
const Type& mType;
const FieldsTrait::AddressOf* mAddressOf;
};
} // namespace cubos::core::reflection
11 changes: 11 additions & 0 deletions core/src/data/des/binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <cubos/core/reflection/traits/fields.hpp>
#include <cubos/core/reflection/traits/mask.hpp>
#include <cubos/core/reflection/traits/string_conversion.hpp>
#include <cubos/core/reflection/traits/wrapper.hpp>
#include <cubos/core/reflection/type.hpp>
#include <cubos/core/tel/logging.hpp>

Expand All @@ -21,6 +22,7 @@ using cubos::core::reflection::FieldsTrait;
using cubos::core::reflection::MaskTrait;
using cubos::core::reflection::StringConversionTrait;
using cubos::core::reflection::Type;
using cubos::core::reflection::WrapperTrait;

// NOLINTBEGIN(bugprone-macro-parentheses)
#define AUTO_HOOK(casted, type) \
Expand Down Expand Up @@ -193,6 +195,15 @@ bool BinaryDeserializer::decompose(const Type& type, void* value)
}
}
}
else if (type.has<WrapperTrait>())
{
const auto& trait = type.get<WrapperTrait>();
CUBOS_ERROR("Try");
if (!this->read(trait.type(), trait.value(value))) {
CUBOS_ERROR("Could not deserialize wrapper of type {}", trait.type().name());
return false;
}
}
else if (type.has<StringConversionTrait>())
{
const auto& trait = type.get<StringConversionTrait>();
Expand Down
20 changes: 8 additions & 12 deletions core/src/data/des/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <cubos/core/reflection/traits/fields.hpp>
#include <cubos/core/reflection/traits/nullable.hpp>
#include <cubos/core/reflection/traits/string_conversion.hpp>
#include <cubos/core/reflection/traits/wrapper.hpp>
#include <cubos/core/reflection/type.hpp>
#include <cubos/core/tel/logging.hpp>

Expand All @@ -23,6 +24,7 @@ using cubos::core::reflection::FieldsTrait;
using cubos::core::reflection::NullableTrait;
using cubos::core::reflection::StringConversionTrait;
using cubos::core::reflection::Type;
using cubos::core::reflection::WrapperTrait;

// NOLINTBEGIN(bugprone-macro-parentheses)
#define AUTO_HOOK(T, fromString) \
Expand Down Expand Up @@ -120,6 +122,12 @@ bool JSONDeserializer::decompose(const Type& type, void* value)
return true;
}

if (type.has<WrapperTrait>())
{
const auto& wrapper = type.get<WrapperTrait>();
return this->read(wrapper.type(), wrapper.value(value));
}

if (type.has<NullableTrait>() && mIterator->is_null())
{
const auto& trait = type.get<NullableTrait>();
Expand Down Expand Up @@ -242,18 +250,6 @@ bool JSONDeserializer::decompose(const Type& type, void* value)
const auto& trait = type.get<FieldsTrait>();
auto view = trait.view(value);

if (trait.size() == 1)
{
// If there's a single field, read it directly.
if (!this->read(trait.begin()->type(), view.begin()->value))
{
CUBOS_WARN("Couldn't deserialize wrapped field {}", trait.begin()->name());
return false;
}

return true;
}

if (!mIterator->is_object())
{
CUBOS_WARN("Expected an object of type {}, found a {}", type.name(), mIterator->type_name());
Expand Down
12 changes: 12 additions & 0 deletions core/src/data/ser/binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <cubos/core/reflection/traits/fields.hpp>
#include <cubos/core/reflection/traits/mask.hpp>
#include <cubos/core/reflection/traits/string_conversion.hpp>
#include <cubos/core/reflection/traits/wrapper.hpp>
#include <cubos/core/reflection/type.hpp>
#include <cubos/core/tel/logging.hpp>

Expand All @@ -19,6 +20,7 @@ using cubos::core::reflection::FieldsTrait;
using cubos::core::reflection::MaskTrait;
using cubos::core::reflection::StringConversionTrait;
using cubos::core::reflection::Type;
using cubos::core::reflection::WrapperTrait;

#define AUTO_HOOK(casted, type) \
this->hook<type>([this](const type& value) { \
Expand Down Expand Up @@ -151,6 +153,16 @@ bool BinarySerializer::decompose(const Type& type, const void* value)
}
}
}
else if (type.has<WrapperTrait>())
{
const auto& trait = type.get<WrapperTrait>();
CUBOS_ERROR("Try");
if (!this->write(trait.type(), trait.value(value)))
{
CUBOS_ERROR("Could not serialize wrapper of type {}", trait.type().name());
return false;
}
}
else if (type.has<StringConversionTrait>())
{
const auto& trait = type.get<StringConversionTrait>();
Expand Down
11 changes: 11 additions & 0 deletions core/src/data/ser/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <cubos/core/reflection/traits/enum.hpp>
#include <cubos/core/reflection/traits/fields.hpp>
#include <cubos/core/reflection/traits/string_conversion.hpp>
#include <cubos/core/reflection/traits/wrapper.hpp>
#include <cubos/core/reflection/type.hpp>
#include <cubos/core/tel/logging.hpp>

Expand All @@ -16,6 +17,7 @@ using cubos::core::reflection::EnumTrait;
using cubos::core::reflection::FieldsTrait;
using cubos::core::reflection::StringConversionTrait;
using cubos::core::reflection::Type;
using cubos::core::reflection::WrapperTrait;

#define AUTO_HOOK(type, ...) \
this->hook<type>([this](const type& value) { \
Expand Down Expand Up @@ -169,6 +171,15 @@ bool DebugSerializer::decompose(const Type& type, const void* value)
this->separate(true);
mStream.put(')');
}
else if (type.has<WrapperTrait>())
{
const auto& trait = type.get<WrapperTrait>();
if (!this->write(trait.type(), trait.value(value)))
{
CUBOS_ERROR("Could not serialize wrapper of type {}", trait.type().name());
return false;
}
}
else if (type.has<StringConversionTrait>())
{
const auto& trait = type.get<StringConversionTrait>();
Expand Down
24 changes: 11 additions & 13 deletions core/src/data/ser/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <cubos/core/reflection/traits/fields.hpp>
#include <cubos/core/reflection/traits/nullable.hpp>
#include <cubos/core/reflection/traits/string_conversion.hpp>
#include <cubos/core/reflection/traits/wrapper.hpp>
#include <cubos/core/reflection/type.hpp>
#include <cubos/core/tel/logging.hpp>

Expand All @@ -19,6 +20,7 @@ using cubos::core::reflection::NullableTrait;
using cubos::core::reflection::reflect;
using cubos::core::reflection::StringConversionTrait;
using cubos::core::reflection::Type;
using cubos::core::reflection::WrapperTrait;

// Macro used to reduce code duplication for primitive type hooks.
#define AUTO_HOOK(type, ...) \
Expand Down Expand Up @@ -65,6 +67,12 @@ nlohmann::json JSONSerializer::output()

bool JSONSerializer::decompose(const Type& type, const void* value)
{
if (type.has<WrapperTrait>())
{
const auto& wrapper = type.get<WrapperTrait>();
return this->write(wrapper.type(), wrapper.value(value));
}

if (type.has<NullableTrait>())
{
const auto& trait = type.get<NullableTrait>();
Expand Down Expand Up @@ -112,21 +120,11 @@ bool JSONSerializer::decompose(const Type& type, const void* value)

if (type.has<FieldsTrait>())
{
if (type.get<FieldsTrait>().size() == 1)
{
// If there's a single field, write it directly.
if (!this->write(type.get<FieldsTrait>().begin()->type(),
type.get<FieldsTrait>().view(value).begin()->value))
{
CUBOS_WARN("Couldn't serialize wrapped field {}", type.get<FieldsTrait>().begin()->name());
return false;
}

return true;
}
const auto& trait = type.get<FieldsTrait>();
auto view = trait.view(value);
auto jsonObj = nlohmann::json::object();

for (const auto& [field, fieldValue] : type.get<FieldsTrait>().view(value))
for (const auto& [field, fieldValue] : view)
{
if (!this->write(field->type(), fieldValue))
{
Expand Down
1 change: 1 addition & 0 deletions core/src/data/ser/serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ bool Serializer::write(const reflection::Type& type, const void* value)
}
else if (!this->decompose(type, value))
{

CUBOS_WARN("Serialization decomposition for type {} failed", type.name());
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/ecs/cubos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ CUBOS_REFLECT_IMPL(DeltaTime)

CUBOS_REFLECT_IMPL(ShouldQuit)
{
return TypeBuilder<ShouldQuit>("cubos::core::ecs::ShouldQuit").withField("value", &ShouldQuit::value).build();
return TypeBuilder<ShouldQuit>("cubos::core::ecs::ShouldQuit").wrap(&ShouldQuit::value);
}

CUBOS_REFLECT_IMPL(Arguments)
{
return TypeBuilder<Arguments>("cubos::core::ecs::Arguments").withField("value", &Arguments::value).build();
return TypeBuilder<Arguments>("cubos::core::ecs::Arguments").wrap(&Arguments::value);
}

struct Cubos::State
Expand Down
4 changes: 2 additions & 2 deletions core/src/geom/box.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <cubos/core/geom/box.hpp>
#include <cubos/core/reflection/external/glm.hpp>
#include <cubos/core/reflection/traits/constructible.hpp>
#include <cubos/core/reflection/traits/fields.hpp>
#include <cubos/core/reflection/traits/wrapper.hpp>
#include <cubos/core/reflection/type.hpp>

CUBOS_REFLECT_IMPL(cubos::core::geom::Box)
Expand All @@ -10,5 +10,5 @@ CUBOS_REFLECT_IMPL(cubos::core::geom::Box)

return Type::create("cubos::core::geom::Box")
.with(ConstructibleTrait::typed<Box>().withBasicConstructors().build())
.with(FieldsTrait().withField("halfSize", &Box::halfSize));
.with(WrapperTrait(&Box::halfSize));
}
11 changes: 11 additions & 0 deletions core/src/reflection/traits/wrapper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <cubos/core/reflection/external/string.hpp>
#include <cubos/core/reflection/traits/wrapper.hpp>
#include <cubos/core/reflection/type.hpp>
#include <cubos/core/tel/logging.hpp>

using namespace cubos::core::reflection;

CUBOS_REFLECT_IMPL(WrapperTrait)
{
return Type::create("cubos::core::ecs::WrapperTrait");
}
4 changes: 3 additions & 1 deletion engine/samples/games/cubosurfers/assets/scenes/main.cubos
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@
}
},
"camera": {
"cubos::engine::PerspectiveCamera": 60.0,
"cubos::engine::PerspectiveCamera": {
"fovY": 60.0
},
"cubos::engine::DrawsTo@render-target": {},
"cubos::engine::Position": {
"x": 0,
Expand Down
8 changes: 6 additions & 2 deletions engine/samples/render/main/assets/main.cubos
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"cubos::engine::RenderTargetDefaults": {}
},
"camera": {
"cubos::engine::PerspectiveCamera": 60.0,
"cubos::engine::PerspectiveCamera": {
"fovY": 60.0
},
"cubos::engine::DrawsTo@render-target": {},
"cubos::engine::Position": {
"x": 0,
Expand All @@ -13,7 +15,9 @@
}
},
"camera2": {
"cubos::engine::PerspectiveCamera": 60.0,
"cubos::engine::PerspectiveCamera": {
"fovY": 60.0
},
"cubos::engine::DrawsTo@render-target": {},
"cubos::engine::Position": {
"x": 3,
Expand Down
Loading
Loading