-
Notifications
You must be signed in to change notification settings - Fork 21
Enum manual reflection
Julien SOYSOUVANH edited this page Oct 26, 2021
·
2 revisions
For the example, we will manually reflect the following enum:
//ThirdPartyEnum.h
enum class EThirdPartyEnum : uint8_t
{
Value1 = 1 << 0,
Value2 = 1 << 1
};
First, create a header file that includes the third party enum and specializes the rfk::getEnum function:
//ManualEnumReflection.h
#include "ThirdPartyEnum.h"
//Contains base rfk::getEnum definition
#include <Refureku/TypeInfo/Archetypes/Enum.h>
namespace rfk
{
template <>
rfk::Enum const* getEnum<EThirdPartyEnum>() noexcept;
}
Then, we define the body in a c++ file:
//ManualEnumReflection.cpp
//std::hash<std::string_view>
#include <string_view>
#include <Refureku/TypeInfo/Archetypes/GetArchetype.h>
#include "ManualEnumReflection.h"
template <>
rfk::Enum const* rfk::getEnum<EThirdPartyEnum>() noexcept
{
static bool initialized = false;
static rfk::Enum enumArchetype("EThirdPartyEnum",
std::hash<std::string_view>()("EThirdPartyEnum"),
rfk::getArchetype<uint8_t>(),
nullptr);
if (!initialized)
{
initialized = true;
enumArchetype.addEnumValue("Value1", std::hash<std::string_view>()("Value1"), 1 << 0);
enumArchetype.addEnumValue("Value2", std::hash<std::string_view>()("Value2"), 1 << 1);
}
return &enumArchetype;
}
You should now be able to retrieve the enum metadata with a rfk::getEnum or rfk::getArchetype call.
We need one extra step to register the enum to the database. In the same source file, add the following:
//ManualEnumReflection.cpp
#include <Refureku/TypeInfo/Archetypes/ArchetypeRegisterer.h>
rfk::ArchetypeRegisterer registerer = *rfk::getEnum<EThirdPartyEnum>();