-
Notifications
You must be signed in to change notification settings - Fork 23
Namespace manual reflection
Julien SOYSOUVANH edited this page Oct 27, 2021
·
3 revisions
For the example, we will manually reflect the following namespace:
//ThirdPartyNamespace.h
namespace third_party_namespace
{
namespace nested_third_party_namespace
{
void thirdPartyFunc();
}
enum class ThirdPartyEnum {};
}
Nested entities are very simple to keep everything as readable as possible.
We start by the most inner entity and will end with the most outer one. Refer to the manual function reflection and manual enum reflection to grasp the following code:
//ThirdPartyNamespaceProxy.h
#include "ThirdPartyNamespace.h"
#include <Refureku/TypeInfo/Archetypes/Enum.h>
namespace rfk
{
template <>
rfk::Enum const* getEnum<third_party_namespace::ThirdPartyEnum>() noexcept;
}
//ThirdPartyNamespaceProxy.cpp
#include "ThirdPartyNamespaceProxy.h"
#include <string_view> //std::hash<std::string_view>
#include <Refureku/TypeInfo/Archetypes/GetArchetype.h>
#include <Refureku/TypeInfo/Functions/Function.h>
//Function manual reflection
static rfk::Function const& getFunction_thirdPartyFunc() noexcept
{
static rfk::Function func("thirdPartyFunc",
std::hash<std::string_view>()("third_party_namespace::nested_third_party_namespace::thirdPartyFunc"),
rfk::getType<void>(),
new rfk::NonMemberFunction<void()>(&third_party_namespace::nested_third_party_namespace::thirdPartyFunc),
rfk::EFunctionFlags::Default);
return func;
}
//Enum manual reflection
template <>
rfk::Enum const* rfk::getEnum<third_party_namespace::ThirdPartyEnum>() noexcept
{
static rfk::Enum enumMetadata("ThirdPartyEnum",
std::hash<std::string_view>()("third_party_namespace::ThirdPartyEnum"),
rfk::getArchetype<int>(),
nullptr);
return &enumMetadata;
}
Note that namespace nested entities are not registered to the database. The namespace registerer registers its nested entities to the database itself, so we do not need to (and must not) register them ourselves.
Now, let's write the actual code for namespace reflection:
#include <Refureku/TypeInfo/Namespace/Namespace.h>
#include <Refureku/TypeInfo/Namespace/NamespaceFragment.h>
#include <Refureku/TypeInfo/Namespace/NamespaceFragmentRegisterer.h>
static rfk::NamespaceFragment const& getNamespaceFragment_nested_third_party_namespace() noexcept
{
static bool initialized = false;
static rfk::NamespaceFragment fragment("nested_third_party_namespace",
std::hash<std::string_view>()("third_party_namespace::nested_third_party_namespace"));
if (!initialized)
{
initialized = true;
fragment.setNestedEntitiesCapacity(1u);
fragment.addNestedEntity(getFunction_thirdPartyFunc());
}
return fragment;
}
And finally, the most outer namespace:
static rfk::NamespaceFragment const& getNamespaceFragment_third_party_namespace() noexcept
{
static bool initialized = false;
static rfk::NamespaceFragment fragment("third_party_namespace",
std::hash<std::string_view>()("third_party_namespace"));
if (!initialized)
{
initialized = true;
fragment.setNestedEntitiesCapacity(2u);
fragment.addNestedEntity(getNamespaceFragment_nested_third_party_namespace());
fragment.addNestedEntity(*rfk::getEnum<third_party_namespace::ThirdPartyEnum>());
}
return fragment;
}
rfk::NamespaceFragmentRegisterer third_party_namespace_registerer(getNamespaceFragment_third_party_namespace());