-
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.
- Loading branch information
Showing
4 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
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
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
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,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] |
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,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`. |