diff --git a/core/samples/CMakeLists.txt b/core/samples/CMakeLists.txt index c8b7dc32e2..837eec003e 100644 --- a/core/samples/CMakeLists.txt +++ b/core/samples/CMakeLists.txt @@ -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") diff --git a/core/samples/reflection/page.md b/core/samples/reflection/page.md index b362f80a10..a96184b7b0 100644 --- a/core/samples/reflection/page.md +++ b/core/samples/reflection/page.md @@ -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 diff --git a/core/samples/reflection/traits/array/main.cpp b/core/samples/reflection/traits/array/main.cpp new file mode 100644 index 0000000000..e232f9330d --- /dev/null +++ b/core/samples/reflection/traits/array/main.cpp @@ -0,0 +1,52 @@ +#include + +/// [Printing any array] +#include +#include + +using cubos::core::reflection::ArrayTrait; +using cubos::core::reflection::Type; + +void printArray(const Type& type, const void* instance) +{ + const auto& arrayTrait = type.get(); + /// [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()) + { + 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(arrayTrait.get(instance, i))); + } +} +/// [Getting array elements] + +/// [Typed wrapper] +template +void printArray(const T& array) +{ + using cubos::core::reflection::reflect; + + printArray(reflect(), &array); +} +/// [Typed wrapper] + +/// [Usage] +#include +#include + +int main() +{ + std::vector vec = {1, 1, 2, 3, 5, 8, 13}; + printArray(vec); +} +/// [Usage] diff --git a/core/samples/reflection/traits/array/page.md b/core/samples/reflection/traits/array/page.md new file mode 100644 index 0000000000..fe73217409 --- /dev/null +++ b/core/samples/reflection/traits/array/page.md @@ -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`, which also means reflecting `int`.