Cogs.Core
Type.cpp
1#include "Type.h"
2
3#include "TypeDatabase.h"
4
6{
7 assert(enumerators.empty() && "Enumeration cannot contain methods.");
8 assert(this->methods.empty() && "set only.");
9 this->methods.reserve(this->methods.size() + count);
10
11 for (size_t i = 0; i < count; ++i) {
12 this->methods.emplace_back(std::move(functions[i]));
13 }
14
15 return *this;
16}
17
19{
20 assert(fields.empty() && "Enumeration cannot contain fields.");
21 assert(methods.empty() && "Enumeration cannot contain methods.");
22 assert(this->enumerators.empty() && "set only.");
23
24 this->enumerators.reserve(this->enumerators.size() + count);
25 for (size_t i = 0; i < count; ++i) {
26 this->enumerators.emplace_back(enumerators[i].getName(), enumerators[i].getValue());
27 }
28
29 return *this;
30}
31
33{
34 if (base == NoType) return nullptr;
35
36 return &TypeDatabase::getType(base);
37}
38
39bool Cogs::Reflection::Type::canCastTo(const TypeId& destinationId) const
40{
41 if (this->getTypeId() == destinationId) {
42 return true;
43 }
44
45 const Type* typeBase = getBase();
46 while (typeBase && (typeBase->getTypeId() != destinationId)) {
47 typeBase = typeBase->getBase();
48 }
49
50 return (typeBase != nullptr);
51}
52
54{
55 for (const Field & item : fields) {
56 if (item.getName() == name) {
57 return &item;
58 }
59 }
60
61 const Cogs::Reflection::Type* baseType = getBase();
62 return baseType ? baseType->getField(name) : nullptr;
63}
64
66{
67 if (field < fields.data() || field >(fields.data() + fields.size())) {
68 const Cogs::Reflection::Type* baseType = getBase();
69 if (baseType) {
70 FieldId baseFieldId = baseType->getFieldId(field);
71
72 return baseFieldId != NoField ? baseFieldId + static_cast<FieldId>(fields.size()) : NoField;
73 } else {
74 return NoField;
75 }
76 }
77
78 return static_cast<FieldId>(field - fields.data());
79}
80
82{
83 assert(componentTypeId != NoType);
84 const Type& componentType = TypeDatabase::getType(componentTypeId);
85
86 const Field* field = componentType.getField(name);
87 if (field) {
88 FieldId fieldId = componentType.getFieldId(field);
89 return fieldId;
90 }
91 else {
93 }
94}
95
97{
98 const Cogs::Reflection::Type* baseType = getBase();
99 if (baseType) {
100 return baseType->getNumHierarchyFields() + getNumFields();
101 }
102 else {
103 return getNumFields();
104 }
105}
106
107
109{
110 const FieldId myFieldCount = static_cast<FieldId>(fields.size());
111 if (id == NoField) {
112 return nullptr;
113 }
114 else if (id >= myFieldCount) {
115 const Cogs::Reflection::Type* baseType = getBase();
116 return baseType ? baseType->getField(id - myFieldCount) : nullptr;
117 }
118
119 return &fields[id];
120}
121
123{
124 for (size_t i = 0; i < fields.size(); ++i) {
125 if (fields[i].getOffset() == offset) {
126 return static_cast<FieldId>(i);
127 }
128 }
129
130 return NoField;
131}
132
134{
135 for (const Method& item : methods) {
136 if (item.getName() == name) {
137 return &item;
138 }
139 }
140
141 const Cogs::Reflection::Type* baseType = getBase();
142 return baseType ? baseType->getMethod(name) : nullptr;
143}
144
146{
147 for (const Enumerator& item : enumerators) {
148 if (item.getName() == name) {
149 return &item;
150 }
151 }
152
153 const Cogs::Reflection::Type* baseType = getBase();
154 return baseType ? baseType->getEnumerator(name) : nullptr;
155}
156
158{
159 assert(enumerators.empty() && "Enumeration cannot contain fields.");
160 assert(this->fields.empty() && "set only.");
161
162 this->fields.reserve(count);
163 for (size_t i = 0; i < count; ++i) {
164 this->fields.emplace_back(std::move(fields[i]));
165 }
166
167 return *this;
168}
Field definition describing a single data member of a data structure.
Definition: Field.h:68
Simple method definition.
Definition: Method.h:72
static const Type & getType()
Get the Type of the given template argument.
Definition: TypeDatabase.h:168
Represents a discrete type definition, describing a native type class.
Definition: Type.h:89
FieldId getFieldId(const Field *field) const
Get the Reflection::FieldId of the given field.
Definition: Type.cpp:65
const Type * getBase() const
Get the base type.
Definition: Type.cpp:32
const Enumerator * getEnumerator(const Name &name) const
Get a pointer to the enumerator with the given name.
Definition: Type.cpp:145
std::vector< Enumerator > enumerators
Collection of enumerators if the type is an enum type.
Definition: Type.h:365
const Method * getMethod(const Name &name) const
Get a pointer to the method with the given name.
Definition: Type.cpp:133
size_t getNumHierarchyFields() const
Get the number of fields in the type + types in all base types.
Definition: Type.cpp:96
std::vector< Method > methods
Collection of methods available on the type.
Definition: Type.h:362
const Field * getField(const Name &name) const
Get a pointer to the field info of the field with the given name.
Definition: Type.cpp:53
Type & setEnumerators(const EnumeratorDef(&enumerators)[count])
Set the enumerators of the type to the given array of enumerator instances.
Definition: Type.h:127
bool canCastTo() const
Check if this type can be cast to the destination type.
Definition: Type.inl:12
Type & setMethods(Method(&functions)[count])
Set the methods of the type to the given array of method instances.
Definition: Type.h:394
constexpr TypeId getTypeId() const
Get the unique Reflection::TypeId of this instance.
Definition: Type.h:325
Type & setFields(Field(&fields)[count])
Set the fields of the type to the given array of field instances.
Definition: Type.h:388
uint16_t TypeId
Built in type used to uniquely identify a single type instance.
Definition: Name.h:48
uint16_t FieldId
Type used to index fields.
Definition: Name.h:54
constexpr FieldId NoField
No field id.
Definition: Name.h:60
constexpr TypeId NoType
Definition of no type.
Definition: Name.h:51
Single integral constant enumerator.
Definition: Type.h:58
Represents an unique name.
Definition: Name.h:70