-
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
3 changed files
with
96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Reflection {#examples-core-reflection} | ||
|
||
@brief How to use the reflection system. | ||
|
||
- @subpage examples-core-reflection-basic - @copybrief examples-core-reflection-basic | ||
- @subpage examples-core-reflection-traits-fields - @copybrief examples-core-reflection-traits-fields |
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,48 @@ | ||
#include <cubos/core/log.hpp> | ||
|
||
/// [Scale declaration] | ||
#include <cubos/core/reflection/reflect.hpp> | ||
|
||
struct Scale | ||
{ | ||
CUBOS_REFLECT; | ||
float value = 1.0F; | ||
}; | ||
/// [Scale declaration] | ||
|
||
/// [Scale definition] | ||
#include <cubos/core/reflection/traits/constructible.hpp> | ||
#include <cubos/core/reflection/type.hpp> | ||
|
||
using cubos::core::reflection::ConstructibleTrait; | ||
using cubos::core::reflection::Type; | ||
|
||
CUBOS_REFLECT_IMPL(Scale) | ||
{ | ||
return Type::create("Scale").with(ConstructibleTrait::builder<Scale>().withDefaultConstructor().build()); | ||
} | ||
/// [Scale definition] | ||
|
||
/// [Accessing the trait] | ||
int main() | ||
{ | ||
using cubos::core::reflection::reflect; | ||
|
||
const auto& scaleType = reflect<Scale>(); | ||
CUBOS_ASSERT(scaleType.has<ConstructibleTrait>()); | ||
const auto& constructible = scaleType.get<ConstructibleTrait>(); | ||
/// [Accessing the trait] | ||
|
||
/// [Creating a default instance] | ||
// Allocate memory for the instance and default-construct it. | ||
void* instance = operator new(constructible.size()); | ||
CUBOS_ASSERT(constructible.defaultConstruct(instance)); | ||
CUBOS_ASSERT(static_cast<Scale*>(instance)->value == 1.0F); | ||
/// [Creating a default instance] | ||
|
||
/// [Destroying the instance] | ||
// Destroy the instance and deallocate its memory. | ||
constructible.destruct(instance); | ||
operator delete(instance); | ||
} | ||
/// [Destroying the instance] |
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,42 @@ | ||
# Fields Trait {#examples-core-reflection-traits-fields} | ||
|
||
@brief Exposing the fields of a type. | ||
|
||
For structured types, like classes and structs, you might want to expose its | ||
public fields using the @ref cubos::core::reflection::FieldsTrait "FieldsTrait" | ||
trait. | ||
|
||
|
||
|
||
@snippet reflection/traits/constructible/main.cpp Scale declaration | ||
|
||
We're going to add the @ref cubos::core::reflection::ConstructibleTrait | ||
"ConstructibleTrait" trait to it, so that we can create instances of it at | ||
runtime: | ||
|
||
@snippet reflection/traits/constructible/main.cpp Scale definition | ||
|
||
Now, we can access the trait from the reflected type: | ||
|
||
@snippet reflection/traits/constructible/main.cpp Accessing the trait | ||
|
||
Imagine for a moment that you don't know the type of the data you're working, | ||
and you only have access to its reflection data through `scaleType`. If you | ||
want to create a default instance of the type, you can call the default | ||
constructor stored in the trait: | ||
|
||
@snippet reflection/traits/constructible/main.cpp Creating a default instance | ||
|
||
The @ref cubos::core::reflection::ConstructibleTrait::defaultConstruct | ||
"defaultConstruct" method returns a boolean which indicates if the type has a | ||
default constructor or not. In this case, since we added the default | ||
constructor to the trait, it will return `true`. | ||
|
||
This could be useful, for example, to fallback from using `moveConstruct` to | ||
`copyConstruct`, if the first isn't available. | ||
|
||
Don't forget to destroy the instance manually when you're done with it: | ||
|
||
@snippet reflection/traits/constructible/main.cpp Destroying the instance | ||
|
||
|