Skip to content

Commit

Permalink
refactor(core): move this in ConstructibleTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
RiscadoA committed Sep 12, 2023
1 parent bbfe846 commit 6362934
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
23 changes: 12 additions & 11 deletions core/include/cubos/core/reflection/traits/constructible.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,31 +47,31 @@ namespace cubos::core::reflection
/// @tparam T Type to build a trait for.
/// @return Trait builder.
template <typename T>
static Builder<T> builder();
static Builder<T> typed();

/// @brief Sets the default constructor of the type.
///
/// Aborts if the default constructor has already been set.
///
/// @param defaultConstructor Function pointer to the default constructor of the type.
/// @return Reference to this object.
ConstructibleTrait& withDefaultConstructor(DefaultConstructor defaultConstructor);
/// @return Trait.
ConstructibleTrait&& withDefaultConstructor(DefaultConstructor defaultConstructor) &&;

/// @brief Sets the copy constructor of the type.
///
/// Aborts if the copy constructor has already been set.
///
/// @param copyConstructor Function pointer to the copy constructor of the type.
/// @return Reference to this object.
ConstructibleTrait& withCopyConstructor(CopyConstructor copyConstructor);
/// @return Trait.
ConstructibleTrait&& withCopyConstructor(CopyConstructor copyConstructor) &&;

/// @brief Sets the move constructor of the type.
///
/// Aborts if the copy constructor has already been set.
///
/// @param moveConstructor Function pointer to the move constructor of the type.
/// @return Reference to this object.
ConstructibleTrait& withMoveConstructor(MoveConstructor moveConstructor);
/// @return Trait.
ConstructibleTrait&& withMoveConstructor(MoveConstructor moveConstructor) &&;

/// @brief Returns the size of the type in bytes.
/// @return Size of the type in bytes.
Expand Down Expand Up @@ -132,15 +132,16 @@ namespace cubos::core::reflection
/// @return Builder.
Builder&& withDefaultConstructor() &&
{
mTrait.withDefaultConstructor([](void* instance) { new (instance) T(); });
mTrait = static_cast<ConstructibleTrait&&>(mTrait).withDefaultConstructor(
[](void* instance) { new (instance) T(); });
return static_cast<Builder&&>(*this);
}

/// @brief Sets the copy constructor of the type.
/// @return Builder.
Builder&& withCopyConstructor() &&
{
mTrait.withCopyConstructor(
mTrait = static_cast<ConstructibleTrait&&>(mTrait).withCopyConstructor(
[](void* instance, const void* other) { new (instance) T(*static_cast<const T*>(other)); });
return static_cast<Builder&&>(*this);
}
Expand All @@ -149,7 +150,7 @@ namespace cubos::core::reflection
/// @return Builder.
Builder&& withMoveConstructor() &&
{
mTrait.withMoveConstructor(
mTrait = static_cast<ConstructibleTrait&&>(mTrait).withMoveConstructor(
[](void* instance, void* other) { new (instance) T(static_cast<T&&>(*static_cast<T*>(other))); });
return static_cast<Builder&&>(*this);
}
Expand All @@ -159,7 +160,7 @@ namespace cubos::core::reflection
};

template <typename T>
ConstructibleTrait::Builder<T> ConstructibleTrait::builder()
ConstructibleTrait::Builder<T> ConstructibleTrait::typed()
{
return Builder<T>();
}
Expand Down
2 changes: 1 addition & 1 deletion core/samples/reflection/traits/constructible/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ using cubos::core::reflection::Type;

CUBOS_REFLECT_IMPL(Scale)
{
return Type::create("Scale").with(ConstructibleTrait::builder<Scale>().withDefaultConstructor().build());
return Type::create("Scale").with(ConstructibleTrait::typed<Scale>().withDefaultConstructor().build());
}
/// [Scale definition]

Expand Down
12 changes: 6 additions & 6 deletions core/src/cubos/core/reflection/traits/constructible.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@ ConstructibleTrait::ConstructibleTrait(std::size_t size, std::size_t alignment,
CUBOS_ASSERT(mDestructor, "Destructor must be non-null");
}

ConstructibleTrait& ConstructibleTrait::withDefaultConstructor(DefaultConstructor defaultConstructor)
ConstructibleTrait&& ConstructibleTrait::withDefaultConstructor(DefaultConstructor defaultConstructor) &&
{
CUBOS_ASSERT(!mDefaultConstructor, "Default constructor already set");
mDefaultConstructor = defaultConstructor;
return *this;
return static_cast<ConstructibleTrait&&>(*this);
}

ConstructibleTrait& ConstructibleTrait::withCopyConstructor(CopyConstructor copyConstructor)
ConstructibleTrait&& ConstructibleTrait::withCopyConstructor(CopyConstructor copyConstructor) &&
{
CUBOS_ASSERT(!mCopyConstructor, "Copy constructor already set");
mCopyConstructor = copyConstructor;
return *this;
return static_cast<ConstructibleTrait&&>(*this);
}

ConstructibleTrait& ConstructibleTrait::withMoveConstructor(MoveConstructor moveConstructor)
ConstructibleTrait&& ConstructibleTrait::withMoveConstructor(MoveConstructor moveConstructor) &&
{
CUBOS_ASSERT(!mMoveConstructor, "Move constructor already set");
mMoveConstructor = moveConstructor;
return *this;
return static_cast<ConstructibleTrait&&>(*this);
}

std::size_t ConstructibleTrait::size() const
Expand Down
14 changes: 8 additions & 6 deletions core/tests/reflection/traits/constructible.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ TEST_CASE("reflection::ConstructibleTrait")
{
SUBCASE("size and alignment are correct")
{
auto trait = ConstructibleTrait::builder<DetectDestructor>().build();
auto trait = ConstructibleTrait::typed<DetectDestructor>().build();
CHECK(trait.size() == sizeof(DetectDestructor));
CHECK(trait.alignment() == alignof(DetectDestructor));
}
Expand All @@ -31,20 +31,22 @@ TEST_CASE("reflection::ConstructibleTrait")
{
auto ptr = operator new(sizeof(DetectDestructor));

auto trait = ConstructibleTrait::builder<DetectDestructor>().build();
auto trait = ConstructibleTrait::typed<DetectDestructor>().build();
bool destroyed = false;
auto detector = new (ptr) DetectDestructor(&destroyed);

REQUIRE_FALSE(destroyed);
trait.destruct(detector);
CHECK(destroyed);

operator delete(ptr);
}

SUBCASE("non-assigned constructors work as expected")
{
auto ptr = operator new(sizeof(DetectDestructor));

auto trait = ConstructibleTrait::builder<DetectDestructor>().build();
auto trait = ConstructibleTrait::typed<DetectDestructor>().build();
DetectDestructor detector{};

CHECK_FALSE(trait.defaultConstruct(ptr));
Expand All @@ -58,7 +60,7 @@ TEST_CASE("reflection::ConstructibleTrait")
{
auto ptr = operator new(sizeof(SimpleConstructible));

auto trait = ConstructibleTrait::builder<SimpleConstructible>().withDefaultConstructor().build();
auto trait = ConstructibleTrait::typed<SimpleConstructible>().withDefaultConstructor().build();
REQUIRE(trait.defaultConstruct(ptr));

auto simple = static_cast<SimpleConstructible*>(ptr);
Expand All @@ -72,7 +74,7 @@ TEST_CASE("reflection::ConstructibleTrait")
{
auto ptr = operator new(sizeof(SimpleConstructible));

auto trait = ConstructibleTrait::builder<SimpleConstructible>().withCopyConstructor().build();
auto trait = ConstructibleTrait::typed<SimpleConstructible>().withCopyConstructor().build();
SimpleConstructible copied{};
copied.value = 1;
REQUIRE(trait.copyConstruct(ptr, &copied));
Expand All @@ -88,7 +90,7 @@ TEST_CASE("reflection::ConstructibleTrait")
{
auto ptr = operator new(sizeof(DetectDestructor));

auto trait = ConstructibleTrait::builder<DetectDestructor>().withMoveConstructor().build();
auto trait = ConstructibleTrait::typed<DetectDestructor>().withMoveConstructor().build();
bool destroyed = false;

// The destroyed flag is moved from the 'moved' detector to the detector at 'ptr', and thus
Expand Down

0 comments on commit 6362934

Please sign in to comment.