Skip to content

Commit

Permalink
docs(core): add ArrayTrait sample
Browse files Browse the repository at this point in the history
  • Loading branch information
RiscadoA committed Sep 19, 2023
1 parent 49ca5b5 commit fd352f5
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ endmacro()
make_sample(DIR "logging")
make_sample(DIR "reflection/basic")
make_sample(DIR "reflection/traits/constructible")
make_sample(DIR "reflection/traits/array")
make_sample(DIR "data/fs/embedded_archive" SOURCES "embed.cpp")
make_sample(DIR "data/fs/standard_archive")
make_sample(DIR "data/serialization")
Expand Down
1 change: 1 addition & 0 deletions core/samples/reflection/page.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

- @subpage examples-core-reflection-basic - @copybrief examples-core-reflection-basic
- @subpage examples-core-reflection-traits-constructible - @copybrief examples-core-reflection-traits-constructible
- @subpage examples-core-reflection-traits-array - @copybrief examples-core-reflection-traits-array
52 changes: 52 additions & 0 deletions core/samples/reflection/traits/array/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <cubos/core/log.hpp>

/// [Printing any array]
#include <cubos/core/reflection/traits/array.hpp>
#include <cubos/core/reflection/type.hpp>

using cubos::core::reflection::ArrayTrait;
using cubos::core::reflection::Type;

void printArray(const Type& type, const void* instance)
{
const auto& arrayTrait = type.get<ArrayTrait>();
/// [Printing any array]

/// [Getting array length and type]
CUBOS_INFO("Array with {} elements of type {}", arrayTrait.length(instance), arrayTrait.elementType().name());
/// [Getting array length and type]

/// [Getting array elements]
if (!arrayTrait.elementType().is<int>())
{
CUBOS_INFO("This function does not support printing arrays of types other than int");
return;
}

for (std::size_t i = 0; i < arrayTrait.length(instance); ++i)
{
CUBOS_INFO("Element {}: {}", i, *static_cast<const int*>(arrayTrait.get(instance, i)));
}
}
/// [Getting array elements]

/// [Typed wrapper]
template <typename T>
void printArray(const T& array)
{
using cubos::core::reflection::reflect;

printArray(reflect<T>(), &array);
}
/// [Typed wrapper]

/// [Usage]
#include <cubos/core/reflection/external/primitives.hpp>
#include <cubos/core/reflection/external/vector.hpp>

int main()
{
std::vector<int> vec = {1, 1, 2, 3, 5, 8, 13};
printArray(vec);
}
/// [Usage]
32 changes: 32 additions & 0 deletions core/samples/reflection/traits/array/page.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Array Trait {#examples-core-reflection-traits-array}

@brief Exposing and using array functionality of a type.

The @ref cubos::core::reflection::ArrayTrait "ArrayTrait" trait is used to
expose the array functionality of a type. In this example, we will write a
function which takes a type and an instance of that type, and prints its
elements:

@snippet reflection/traits/array/main.cpp Printing any array

Through the trait, we can access the size of the array and its element type:

@snippet reflection/traits/array/main.cpp Getting array length and type

We can also get pointers to the elements of the array and iterate over them:

@snippet reflection/traits/array/main.cpp Getting array elements

In this example, we're only supporting arrays of `int`s, but we could for
example implement a printing function which supports all primitive types.

To make calling our function easier, we can add a convenience typed wrapper:

@snippet reflection/traits/array/main.cpp Typed wrapper

Using this function is now as simple as:

@snippet reflection/traits/array/main.cpp Usage

Its important to note that both the includes above are necessary, as we're
reflecting the type `std::vector<int>`, which also means reflecting `int`.

0 comments on commit fd352f5

Please sign in to comment.