Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
RiscadoA committed Sep 12, 2023
1 parent 6c01c99 commit 6093bbb
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
6 changes: 6 additions & 0 deletions core/samples/reflection/page.md
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
48 changes: 48 additions & 0 deletions core/samples/reflection/traits/fields/main.cpp
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]
42 changes: 42 additions & 0 deletions core/samples/reflection/traits/fields/page.md
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


0 comments on commit 6093bbb

Please sign in to comment.