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

Implement reflection for all components #694

Merged
merged 18 commits into from
Oct 8, 2023
Merged
Changes from 1 commit
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
60 changes: 60 additions & 0 deletions core/include/cubos/core/ecs/component/reflection.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/// @file
/// @brief Class @ref cubos::core::ecs::ComponentTypeBuilder.
/// @ingroup core-ecs-component

#pragma once

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

namespace cubos::core::ecs
{
/// @brief Builder for @ref reflection::Type objects which represent component types.
///
/// Used to reduce the amount of boilerplate code required to define a component type.
/// Automatically adds the @ref reflection::ConstructibleTrait and @ref reflection::FieldsTrait.
/// The type @p T must be default-constructible, copy-constructible and move-constructible.
///
/// @tparam T Component type.
template <typename T>
class ComponentTypeBuilder
{
public:
/// @brief Constructs.
/// @param name Component type name, including namespace.
ComponentTypeBuilder(std::string name)
: mType(reflection::Type::create(std::move(name)))
{
mType.with(reflection::ConstructibleTrait::typed<T>()
.withDefaultConstructor()
.withCopyConstructor()
.withMoveConstructor()
RiscadoA marked this conversation as resolved.
Show resolved Hide resolved
.build());
}

/// @brief Adds a field to the component type.
/// @tparam F Field type.
/// @param name Field name.
/// @param pointer Field pointer.
/// @return Builder.
template <typename F>
ComponentTypeBuilder&& withField(std::string name, F T::*pointer) &&
{
mFields.addField(std::move(name), pointer);
return std::move(*this);
}

/// @brief Builds the component type.
/// @return Component type.
reflection::Type& build() &&
{
mType.with(std::move(mFields));
return mType;
}

private:
reflection::Type& mType;
reflection::FieldsTrait mFields;
};
} // namespace cubos::core::ecs
RiscadoA marked this conversation as resolved.
Show resolved Hide resolved