Skip to content

Commit

Permalink
feat(reflection): impl nullable trait
Browse files Browse the repository at this point in the history
  • Loading branch information
roby2014 committed Nov 18, 2023
1 parent 14e0b8c commit 836e4f1
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
41 changes: 41 additions & 0 deletions core/include/cubos/core/reflection/traits/nullable.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/// @file
/// @brief Class @ref cubos::core::reflection::NullableTrait.
/// @ingroup core-reflection

#pragma once

#include <cubos/core/reflection/reflect.hpp>

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
19 changes: 19 additions & 0 deletions core/src/cubos/core/reflection/traits/nullable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <cubos/core/reflection/traits/nullable.hpp>

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);
}

0 comments on commit 836e4f1

Please sign in to comment.