Cogs.Core
TypeDatabase.cpp
1#include "TypeDatabase.h"
2
3#include "Type.h"
4
5#include "../ComponentModel/Attributes.h"
6
7#include <unordered_map>
8
9namespace Cogs
10{
11 namespace Reflection
12 {
17 {
18 public:
20 std::vector<Type> types;
21
23 std::unordered_map<size_t, TypeId> typesByName;
24 };
25
26 std::unique_ptr<TypeStore> TypeDatabase::typeStore = std::make_unique<TypeStore>();
27 }
28}
29
30Cogs::Reflection::TypeDatabase::~TypeDatabase()
31{
32
33}
34
36{
37 // Insert invalid type at index zero.
38 typeStore->types.emplace_back();
39
40 TypeDatabase::createType<bool>();
41 TypeDatabase::createType<std::string>();
42
43 TypeDatabase::createType<int8_t>();
44 TypeDatabase::createType<int16_t>();
45 TypeDatabase::createType<int32_t>();
46 TypeDatabase::createType<int64_t>();
47
48 TypeDatabase::createType<uint8_t>();
49 TypeDatabase::createType<uint16_t>();
50 TypeDatabase::createType<uint32_t>();
51 TypeDatabase::createType<uint64_t>();
52
53#if defined(EMSCRIPTEN) || defined(__APPLE__)
54 TypeDatabase::createType<size_t>();
55#endif
56
57 TypeDatabase::createType<float>();
58 TypeDatabase::createType<double>();
59
60 TypeDatabase::createType<std::vector<std::string>>();
61
62 TypeDatabase::createType<std::vector<int8_t>>();
63 TypeDatabase::createType<std::vector<int16_t>>();
64 TypeDatabase::createType<std::vector<int32_t>>();
65 TypeDatabase::createType<std::vector<int64_t>>();
66
67 TypeDatabase::createType<std::vector<uint8_t>>();
68 TypeDatabase::createType<std::vector<uint16_t>>();
69 TypeDatabase::createType<std::vector<uint32_t>>();
70 TypeDatabase::createType<std::vector<uint64_t>>();
71
72 TypeDatabase::createType<std::vector<float>>();
73 TypeDatabase::createType<std::vector<double>>();
74
75 TypeDatabase::createType<ComponentModel::DescriptionAttribute>();
76 TypeDatabase::createType<ComponentModel::RangeAttribute<float>>();
77 TypeDatabase::createType<ComponentModel::DefaultValueAttribute<float>>();
78 TypeDatabase::createType<ComponentModel::StepSizeAttribute<float>>();
79 TypeDatabase::createType<ComponentModel::SerializableAttribute>();
80 TypeDatabase::createType<ComponentModel::ColorAttribute>();
81}
82
84{
85 typeStore->types.clear();
86 typeStore->typesByName.clear();
87}
88
90{
91 debug_assert(typeId != NoType && typeId < typeStore->types.size() && "Type with the given name is not registered in database.");
92
93 // Fall back to invalid type if index is out of bounds.
94 const auto index = typeId < typeStore->types.size() ? typeId : 0;
95
96 return typeStore->types[index];
97}
98
100{
101 auto foundIt = typeStore->typesByName.find(name.getId());
102
103 if (foundIt != typeStore->typesByName.end()) {
104 return getType(foundIt->second);
105 } else {
106 return getType(0);
107 }
108}
109
121void Cogs::Reflection::TypeDatabase::constructInstance(const Type & type, void * data, const size_t dataSize)
122{
123 assert(type.isValid() && "Invalid type cannot be used to construct instance.");
124 assert(type.getSize() <= dataSize && "Placement target too small to construct in.");
125
126 type.constructInstance(data);
127}
128
139{
140 type.destructInstance(data);
141}
142
144{
145 return type.createInstance();
146}
147
149{
150 count = typeStore->types.size();
151
152 return typeStore->types.data();
153}
154
156 size_t size,
157 CreateInstance createInstance,
158 DestroyInstance destroyInstance,
159 ConstructInstance constructInstance,
160 DestructInstance destructInstance,
161 bool isAbstract)
162{
163 typeStore->types.emplace_back();
164 auto & t = typeStore->types.back();
165
166 debug_assert(typeStore->types.size() < std::numeric_limits<TypeId>::max() && "Type store over size limit.");
167 debug_assert(!name.empty() && "Missing name registration getName<> specialization");
168
169 t.typeId = static_cast<TypeId>(typeStore->types.size() - 1);
170 t.name = Name(name);
171 t.size = size;
172
173 if (!isAbstract) {
174 t.createInstance = createInstance;
175 t.destroyInstance = destroyInstance;
176 t.constructInstance = constructInstance;
177 t.destructInstance = destructInstance;
178 }
179
180 debug_assert(typeStore->typesByName.find(t.name.getId()) == typeStore->typesByName.end() && "Type name already in use.");
181
182 typeStore->typesByName[t.name.getId()] = t.typeId;
183
184 return t;
185}
static T * createInstance(const StringView &name)
Create an instance of the type with the given name.
Definition: TypeDatabase.h:206
static const Type & getType()
Get the Type of the given template argument.
Definition: TypeDatabase.h:168
static void initializeBaseTypes()
Initialize the set of types describing built in C++ types like int, float etc.
static std::unique_ptr< class TypeStore > typeStore
Stores registered types.
Definition: TypeDatabase.h:243
static void clear()
Clear the database, removing all existing types. Invalidates all existing Reflection::TypeId instance...
static Type & createType(bool isAbstract=false)
Create a new Type from the provided template type.
Definition: TypeDatabase.h:123
static const Type * getTypes(size_t &count)
Get a pointer to the array of types currently in the type database.
static void destructInstance(const Type &type, void *data)
Destruct an instance of the given type located in data.
static void constructInstance(const Type &type, void *data, const size_t dataSize)
Construct an instance of the given type into the given data.
Storage class for registered types.
std::unordered_map< size_t, TypeId > typesByName
Map of name hashes to type ids for name based lookup.
std::vector< Type > types
Type instances registered in the global database.
Represents a discrete type definition, describing a native type class.
Definition: Type.h:89
DestructInstance destructInstance
Definition: Type.h:378
CreateInstance createInstance
Method used to create a new instance using operator new.
Definition: Type.h:368
size_t size
Size of an instance of a class with this type in bytes.
Definition: Type.h:347
constexpr bool isValid() const
Gets if the type is considered a valid, registered type.
Definition: Type.h:328
ConstructInstance constructInstance
Method used to construct an instance using placement new.
Definition: Type.h:374
constexpr size_t getSize() const
Get the size of an instance of the reflected type, in bytes.
Definition: Type.h:201
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
constexpr bool empty() const noexcept
Check if the string is empty.
Definition: StringView.h:122
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
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
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
Represents an unique name.
Definition: Name.h:70
size_t getId() const
Get the unique identifier of this name.
Definition: Name.h:120