Cogs.Core
ReflectionFunctions.cpp
1#include "ReflectionFunctions.h"
2
3#include "Foundation/Logging/Logger.h"
4#include "Foundation/Reflection/TypeDatabase.h"
5
6using namespace Cogs::Reflection;
7
8namespace
9{
10 Cogs::Logging::Log logger = Cogs::Logging::getLogger("ReflectionFunctions");
11}
12
13int getTypeId(const char * name)
14{
15 auto & type = TypeDatabase::getType(Cogs::StringView(name));
16
17 if (!type.isValid()) {
18 LOG_WARNING(logger, "Type %s does not exist.", name);
19 }
20
21 return static_cast<int>(type.getTypeId());
22}
23
24int getBaseTypeId(const int typeId)
25{
26 assert(typeId != NoType);
27 auto & type = TypeDatabase::getType(static_cast<TypeId>(typeId));
28
29 auto base = type.getBase();
30
31 return static_cast<int>(base ? base->getTypeId() : NoType);
32}
33
34const char * getTypeName(const int typeId)
35{
36 assert(typeId != NoType);
37 return TypeDatabase::getType((TypeId)typeId).getName().c_str();
38}
39
40const char * getFieldName(const int componentTypeId, const int fieldId)
41{
42 assert(componentTypeId != NoType);
43 assert(fieldId != NoField);
44 auto & type = TypeDatabase::getType(static_cast<TypeId>(componentTypeId));
45
46 return type.getField(static_cast<FieldId>(fieldId))->getName().c_str();
47}
48
49int getNumFields(const int componentTypeId)
50{
51 assert(componentTypeId != NoType);
52 auto & type = TypeDatabase::getType(static_cast<TypeId>(componentTypeId));
53
54 return static_cast<int>(type.getNumFields());
55}
56
57int getNumHierarchyFields(const int componentTypeId)
58{
59 assert(componentTypeId != NoType);
60 auto& type = TypeDatabase::getType(static_cast<TypeId>(componentTypeId));
61
62 return static_cast<int>(type.getNumHierarchyFields());
63}
64
65int getFieldType(const int componentTypeId, const int fieldId)
66{
67 assert(componentTypeId != NoType);
68 assert(fieldId != NoField);
69 auto & type = TypeDatabase::getType(static_cast<TypeId>(componentTypeId));
70
71 return static_cast<int>(type.getField(static_cast<FieldId>(fieldId))->getTypeId());
72}
73
74int getFieldId(const int componentTypeId, const char * name)
75{
76 assert(componentTypeId != NoType);
77 auto & componentType = TypeDatabase::getType(static_cast<TypeId>(componentTypeId));
78
79 auto field = componentType.getField(name);
80
81 if (!field) {
82 LOG_WARNING(logger, "Field \"%s\" does not exist in %s.", name, componentType.getName().c_str());
83
84 return static_cast<int>(Cogs::Reflection::NoField);
85 }
86
87 FieldId fieldId = componentType.getFieldId(field);
88
89 return static_cast<int>(fieldId);
90}
Log implementation class.
Definition: LogManager.h:139
static const Type & getType()
Get the Type of the given template argument.
Definition: TypeDatabase.h:168
constexpr const Name & getName() const
Get the unique name of the type.
Definition: Type.h:198
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Definition: LogManager.h:180
Contains reflection support.
Definition: Component.h:11
uint16_t TypeId
Built in type used to uniquely identify a single type instance.
Definition: Name.h:48
constexpr FieldId NoField
No field id.
Definition: Name.h:60
constexpr TypeId NoType
Definition of no type.
Definition: Name.h:51
const char * c_str() const
Gets the name as a null-terminated string.
Definition: Name.h:116