diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index f37f5b925..28167778d 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -39,6 +39,7 @@ set(CUBOS_CORE_SOURCE "src/cubos/core/reflection/traits/array.cpp" "src/cubos/core/reflection/traits/dictionary.cpp" "src/cubos/core/reflection/traits/string_conversion.cpp" + "src/cubos/core/reflection/traits/nullable.cpp" "src/cubos/core/reflection/external/primitives.cpp" "src/cubos/core/reflection/external/string.cpp" "src/cubos/core/reflection/external/uuid.cpp" diff --git a/core/include/cubos/core/reflection/traits/nullable.hpp b/core/include/cubos/core/reflection/traits/nullable.hpp new file mode 100644 index 000000000..e47f5b504 --- /dev/null +++ b/core/include/cubos/core/reflection/traits/nullable.hpp @@ -0,0 +1,41 @@ +/// @file +/// @brief Class @ref cubos::core::reflection::NullableTrait. +/// @ingroup core-reflection + +#pragma once + +#include + +namespace cubos::core::reflection +{ + /// @brief Used to manipulate values of null-representable types. + /// @see See @ref examples-core-reflection-traits-nullable for an example of using this trait. + /// @ingroup core-reflection + class NullableTrait + { + public: + /// @brief Function pointer to check if a value represents null. + using IsNull = bool (*)(const void* instance); + + /// @brief Function pointer to set a value to its null representation. + using SetToNull = void (*)(void* instance); + + /// @brief Constructs. + /// @param into IsNull. + /// @param from SetToNull. + NullableTrait(IsNull isNull, SetToNull setToNull); + + /// @brief Checks if the given value represents null. + /// @param instance Instance. + /// @return Whether the value represents null. + bool isNull(const void* instance) const; + + /// @brief Sets a value to its null representation. + /// @param instance Instance. + void setToNull(void* instance) const; + + private: + IsNull mIsNull; + SetToNull mSetToNull; + }; +} // namespace cubos::core::reflection diff --git a/core/src/cubos/core/reflection/traits/nullable.cpp b/core/src/cubos/core/reflection/traits/nullable.cpp new file mode 100644 index 000000000..9536451ce --- /dev/null +++ b/core/src/cubos/core/reflection/traits/nullable.cpp @@ -0,0 +1,19 @@ +#include + +using cubos::core::reflection::NullableTrait; + +NullableTrait::NullableTrait(IsNull isNull, SetToNull setToNull) + : mIsNull(isNull) + , mSetToNull(setToNull) +{ +} + +bool NullableTrait::isNull(const void* instance) const +{ + return mIsNull(instance); +} + +void NullableTrait::setToNull(void* instance) const +{ + mSetToNull(instance); +}