Skip to content

Commit

Permalink
refactor(core): use memory::move instead of static_cast
Browse files Browse the repository at this point in the history
  • Loading branch information
RiscadoA committed Sep 19, 2023
1 parent 05efbd2 commit 456ae3b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
17 changes: 9 additions & 8 deletions core/include/cubos/core/reflection/traits/constructible.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

#include <cstddef>

#include <cubos/core/memory/move.hpp>

namespace cubos::core::reflection
{
/// @brief Describes how a reflected type may be constructed and destructed.
Expand Down Expand Up @@ -132,27 +134,26 @@ namespace cubos::core::reflection
/// @return Builder.
Builder&& withDefaultConstructor() &&
{
mTrait = static_cast<ConstructibleTrait&&>(mTrait).withDefaultConstructor(
[](void* instance) { new (instance) T(); });
return static_cast<Builder&&>(*this);
mTrait = memory::move(mTrait).withDefaultConstructor([](void* instance) { new (instance) T(); });
return memory::move(*this);
}

/// @brief Sets the copy constructor of the type.
/// @return Builder.
Builder&& withCopyConstructor() &&
{
mTrait = static_cast<ConstructibleTrait&&>(mTrait).withCopyConstructor(
mTrait = memory::move(mTrait).withCopyConstructor(
[](void* instance, const void* other) { new (instance) T(*static_cast<const T*>(other)); });
return static_cast<Builder&&>(*this);
return memory::move(*this);
}

/// @brief Sets the move constructor of the type.
/// @return Builder.
Builder&& 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);
mTrait = memory::move(mTrait).withMoveConstructor(
[](void* instance, void* other) { new (instance) T(memory::move(*static_cast<T*>(other))); });
return memory::move(*this);
}

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ namespace cubos::core::reflection

if constexpr (std::is_default_constructible<T>::value)
{
builder = static_cast<ConstructibleTrait::Builder<T>&&>(builder).withDefaultConstructor();
builder = memory::move(builder).withDefaultConstructor();
}

if constexpr (std::is_copy_constructible<T>::value)
{
builder = static_cast<ConstructibleTrait::Builder<T>&&>(builder).withCopyConstructor();
builder = memory::move(builder).withCopyConstructor();
}

if constexpr (std::is_move_constructible<T>::value)
{
builder = static_cast<ConstructibleTrait::Builder<T>&&>(builder).withMoveConstructor();
builder = memory::move(builder).withMoveConstructor();
}

return static_cast<ConstructibleTrait::Builder<T>&&>(builder).build();
return memory::move(builder).build();
}
} // namespace cubos::core::reflection
6 changes: 3 additions & 3 deletions core/src/cubos/core/reflection/traits/constructible.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ ConstructibleTrait&& ConstructibleTrait::withDefaultConstructor(DefaultConstruct
{
CUBOS_ASSERT(!mDefaultConstructor, "Default constructor already set");
mDefaultConstructor = defaultConstructor;
return static_cast<ConstructibleTrait&&>(*this);
return memory::move(*this);
}

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

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

std::size_t ConstructibleTrait::size() const
Expand Down

0 comments on commit 456ae3b

Please sign in to comment.