Cogs.Core
Type.h
1#pragma once
2
3
4#include "Field.h"
5#include "Method.h"
6
7namespace Cogs
8{
9 namespace Reflection
10 {
12 struct TypeFlags
13 {
16 {
18 None = 0,
21 };
22 };
23
28 template<typename T>
30 constexpr EnumeratorDef(Cogs::StringView name, T value) : name(name), value(static_cast<int>(value))
31 {
32 // See also Field.inl
33 static_assert(sizeof(T) <= 4, "64bit enums not supported.");
34 }
35
37 constexpr Cogs::StringView getName() const { return name; }
38
40 constexpr int getValue() const { return value; }
41
42 private:
44
47
49 int value = 0;
50
52 };
53
57 struct COGSFOUNDATION_API Enumerator
58 {
59 template<typename T>
61 Enumerator(const Name& name, T value) : name(name), value(static_cast<int>(value))
62 {
63 // See also Field.inl
64 static_assert(sizeof(T) <= 4, "64bit enums not supported.");
65 }
66
68 const Name& getName() const { return name; }
69
71 int getValue() const { return value; }
72
73 private:
75
77 Name name;
78
80 int value = 0;
81
83 };
84
88 class COGSFOUNDATION_API Type
89 {
90 public:
92 Type() = default;
93
95 Type(const Type & other) = delete;
96
98 Type(Type && other) noexcept = default;
99
101 Type & operator=(const Type & other) = delete;
102
104 Type & operator=(Type && other) noexcept = delete;
105
109 template<size_t count>
110 Type & setFields(Field(&fields)[count]);
111
115 template<size_t count>
116 Type & setMethods(Method(&functions)[count]);
117
121 Type & setMethods(Method * functions, size_t count);
122
126 template<size_t count>
127 Type& setEnumerators(const EnumeratorDef(&enumerators)[count])
128 {
129 return setEnumerators(enumerators, count);
130 }
131
135 Type& setEnumerators(const EnumeratorDef* enumerators, size_t count);
136
138 Type & setBase(const Type * base)
139 {
140 this->base = base ? base->typeId : NoType;
141
142 return *this;
143 }
144
146 template<typename BaseType>
148
150 [[nodiscard]] const Type * getBase() const;
151
159 template<typename DestinationType>
160 [[nodiscard]] bool canCastTo() const;
161
169 [[nodiscard]] bool canCastTo(const Type& destination) const
170 {
171 return canCastTo(destination.getTypeId());
172 }
173
184 [[nodiscard]] bool canCastTo(const TypeId& destinationId) const;
185
192 [[nodiscard]] constexpr bool operator==(const Type & other) const
193 {
194 return this->typeId == other.typeId;
195 }
196
198 [[nodiscard]] constexpr const Name & getName() const { return name; }
199
201 [[nodiscard]] constexpr size_t getSize() const { return size; }
202
208 [[nodiscard]] const Field * getField(const Name & name) const;
209
215 [[nodiscard]] FieldId getFieldId(const Field * field) const;
216
222 [[nodiscard]] static FieldId getFieldId(TypeId componentTypeId, const Name& name);
223
230 [[nodiscard]] size_t getNumFields() const { return fields.size(); }
231
239 [[nodiscard]] size_t getNumHierarchyFields() const;
240
246 [[nodiscard]] const Field * getField(const FieldId id) const;
247
255 [[nodiscard]] FieldId getFieldId(size_t offset) const;
256
264 template<typename ClassType, typename FieldType>
265 [[nodiscard]] FieldId getFieldId(FieldType ClassType::* field) const
266 {
267 const size_t offset = memberOffset(field);
268
269 return getFieldId(offset);
270 }
271
277 [[nodiscard]] const Method * getMethod(const Name & name) const;
278
284 [[nodiscard]] MethodId getMethodId(const Method * method) const
285 {
286 return static_cast<MethodId>(method - methods.data());
287 }
288
290 [[nodiscard]] const Method * getMethod(const MethodId id) const
291 {
292 return &methods[id];
293 }
294
300 [[nodiscard]] const Enumerator * getEnumerator(const Name & name) const;
301
307 [[nodiscard]] const Enumerator * getEnumerator(const size_t index) const
308 {
309 if (index >= enumerators.size()) return nullptr;
310
311 return &enumerators[index];
312 }
313
315 [[nodiscard]] bool isEnum() const { return !enumerators.empty(); }
316
322 [[nodiscard]] size_t getNumEnumerators() const { return enumerators.size(); }
323
325 [[nodiscard]] constexpr TypeId getTypeId() const { return typeId; }
326
328 [[nodiscard]] constexpr bool isValid() const { return typeId != NoType; }
329
331 constexpr void setFlags(uint32_t flags) { this->flags = flags; }
332
334 [[nodiscard]] bool isEnumFlags() const { return isEnum() && (flags & TypeFlags::EnumFlags) != 0; }
335
337 constexpr Type & setEnumFlags() { setFlags(flags | TypeFlags::EnumFlags); return *this; }
338
339 private:
340 Type& setFields(Field* fields, size_t count);
341
342 private:
345
347 size_t size = 0;
348
350 uint32_t flags = TypeFlags::None;
351
353 TypeId typeId = NoType;
354
357
359 std::vector<Field> fields;
360
362 std::vector<Method> methods;
363
365 std::vector<Enumerator> enumerators;
366
368 CreateInstance createInstance = nullptr;
369
371 DestroyInstance destroyInstance = nullptr;
372
374 ConstructInstance constructInstance = nullptr;
375
378 DestructInstance destructInstance = nullptr;
379
382
383 private:
384 friend class TypeDatabase;
385 };
386
387 template<size_t count>
389 {
390 return setFields(fields, count);
391 }
392
393 template<size_t count>
395 {
396 return setMethods(functions, count);
397 }
398 }
399}
400
401namespace std
402{
404 template<>
405 struct hash<Cogs::Reflection::Type>
406 {
408 size_t operator()(const Cogs::Reflection::Type & type) const noexcept
409 {
410 return type.getName().getId();
411 }
412 };
413}
414
415#include "Type.inl"
416#include "Attributes.inl"
Field definition describing a single data member of a data structure.
Definition: Field.h:68
Simple method definition.
Definition: Method.h:72
Manages all Type instances currently created in the system.
Definition: TypeDatabase.h:105
Represents a discrete type definition, describing a native type class.
Definition: Type.h:89
Type & operator=(const Type &other)=delete
Disallow copy assignment.
size_t getNumFields() const
Get the number of fields in the type.
Definition: Type.h:230
std::vector< Enumerator > enumerators
Collection of enumerators if the type is an enum type.
Definition: Type.h:365
bool isEnum() const
Get if the type is an enumeration type.
Definition: Type.h:315
constexpr const Name & getName() const
Get the unique name of the type.
Definition: Type.h:198
bool isEnumFlags() const
Get if the type is a flag enumeration type.
Definition: Type.h:334
std::vector< Method > methods
Collection of methods available on the type.
Definition: Type.h:362
Name name
Unique name of the type.
Definition: Type.h:344
Type & setBase()
Set the base of this type to the given type.
Type(const Type &other)=delete
Copying type instances is disallowed.
bool canCastTo(const Type &destination) const
Check if this type can be cast to the destination type.
Definition: Type.h:169
size_t getNumEnumerators() const
Get the number of enumerators in the type.
Definition: Type.h:322
TypeId typeId
Unique type id for this type.
Definition: Type.h:353
StringView description
Optional description of the type.
Definition: Type.h:381
Type()=default
Default constructor.
Type & operator=(Type &&other) noexcept=delete
Default move assignment.
Type(Type &&other) noexcept=default
Default move constructor.
constexpr bool isValid() const
Gets if the type is considered a valid, registered type.
Definition: Type.h:328
const Method * getMethod(const MethodId id) const
Get a pointer to the method with the given id.
Definition: Type.h:290
constexpr void setFlags(uint32_t flags)
Set type flags.
Definition: Type.h:331
constexpr bool operator==(const Type &other) const
Check if this type is equal to other.
Definition: Type.h:192
FieldId getFieldId(FieldType ClassType::*field) const
Get the id of the field given by pointer to member.
Definition: Type.h:265
Type & setEnumerators(const EnumeratorDef(&enumerators)[count])
Set the enumerators of the type to the given array of enumerator instances.
Definition: Type.h:127
constexpr Type & setEnumFlags()
Indicate the type is a flag enum.
Definition: Type.h:337
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
const Enumerator * getEnumerator(const size_t index) const
Get the enumerator with the given index.
Definition: Type.h:307
Type & setFields(Field(&fields)[count])
Set the fields of the type to the given array of field instances.
Definition: Type.h:388
Type & setBase(const Type *base)
Set the base type of this base. If base is nullptr, there will be no base type.
Definition: Type.h:138
MethodId getMethodId(const Method *method) const
Get the Reflection::MethodId of the given method.
Definition: Type.h:284
constexpr size_t getSize() const
Get the size of an instance of the reflected type, in bytes.
Definition: Type.h:201
std::vector< Field > fields
Collection of fields available on the type.
Definition: Type.h:359
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
uint16_t TypeId
Built in type used to uniquely identify a single type instance.
Definition: Name.h:48
void(*)(void *) DestroyInstance
Object destroy function type. Comparable to regular delete.
Definition: Name.h:42
void *(*)(void *) ConstructInstance
Object construction function type. Comparable to placement new.
Definition: Name.h:39
uint16_t FieldId
Type used to index fields.
Definition: Name.h:54
constexpr TypeId NoType
Definition of no type.
Definition: Name.h:51
void *(*)() CreateInstance
Object creation function type. Comparable to regular new.
Definition: Name.h:36
void(*)(void *) DestructInstance
Object destruction function type. Comparable to calling the destructor.
Definition: Name.h:45
uint16_t MethodId
Type used to index methods.
Definition: Name.h:57
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
size_t memberOffset(FieldType ClassType::*ptr)
Find the offset of a pointer to member in a class or struct.
STL namespace.
constexpr EnumeratorDef(Cogs::StringView name, T value)
Construct a new enumerator with the given name and value.
Definition: Type.h:30
constexpr Cogs::StringView getName() const
Get the name of the enumerator.
Definition: Type.h:37
constexpr int getValue() const
Get the value of the enumerator.
Definition: Type.h:40
Single integral constant enumerator.
Definition: Type.h:58
Enumerator(const Name &name, T value)
Construct a new enumerator with the given name and value.
Definition: Type.h:61
const Name & getName() const
Get the name of the enumerator.
Definition: Type.h:68
int getValue() const
Get the value of the enumerator.
Definition: Type.h:71
Represents an unique name.
Definition: Name.h:70
Type flags controlling type behavior.
Definition: Type.h:13
ETypeFlags
Type flag enumerators.
Definition: Type.h:16
@ EnumFlags
Enum type is bit flag type.
Definition: Type.h:20
size_t operator()(const Cogs::Reflection::Type &type) const noexcept
Function call operator for hash object.
Definition: Type.h:408