-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add constructible_utils.hpp
- Loading branch information
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
core/include/cubos/core/reflection/traits/constructible_utils.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/// @file | ||
/// @brief Utilities for @ref cubos::core::reflection::ConstructibleTrait. | ||
/// @note Avoid using this header as it includes `<type_traits>`. | ||
/// @ingroup core-reflection | ||
|
||
#pragma once | ||
|
||
#include <type_traits> | ||
|
||
#include <cubos/core/reflection/traits/constructible.hpp> | ||
|
||
namespace cubos::core::reflection | ||
{ | ||
/// @brief Returns a ConstructibleTrait with the default, copy and move constructors, set only | ||
/// if the type has them. | ||
/// @warning Avoid using this function as its header includes `<type_traits>`. | ||
/// @tparam T Type. | ||
/// @return ConstructibleTrait. | ||
/// @ingroup core-reflection | ||
template <typename T> | ||
ConstructibleTrait autoConstructibleTrait() | ||
{ | ||
auto builder = ConstructibleTrait::typed<T>(); | ||
|
||
if (std::is_default_constructible<T>::value) | ||
{ | ||
builder = static_cast<ConstructibleTrait::Builder<T>&&>(builder).withDefaultConstructor(); | ||
} | ||
|
||
if (std::is_copy_constructible<T>::value) | ||
{ | ||
builder = static_cast<ConstructibleTrait::Builder<T>&&>(builder).withCopyConstructor(); | ||
} | ||
|
||
if (std::is_move_constructible<T>::value) | ||
{ | ||
builder = static_cast<ConstructibleTrait::Builder<T>&&>(builder).withMoveConstructor(); | ||
} | ||
|
||
return static_cast<ConstructibleTrait::Builder<T>&&>(builder).build(); | ||
} | ||
} // namespace cubos::core::reflection |