Cogs.Core
TypeDatabase.h
1#pragma once
2
3#include "Name.h"
4
5#include <memory>
6
7template<> inline Cogs::StringView getNameImpl(bool *) { return "bool"; }
8template<> inline Cogs::StringView getNameImpl(std::string *) { return "string"; }
9
10template<> inline Cogs::StringView getNameImpl(int8_t *) { return "int8_t"; }
11template<> inline Cogs::StringView getNameImpl(int16_t *) { return "int16_t"; }
12template<> inline Cogs::StringView getNameImpl(int32_t *) { return "int32_t"; }
13template<> inline Cogs::StringView getNameImpl(int64_t *) { return "int64_t"; }
14
15template<> inline Cogs::StringView getNameImpl(uint8_t *) { return "uint8_t"; }
16template<> inline Cogs::StringView getNameImpl(uint16_t *) { return "uint16_t"; }
17template<> inline Cogs::StringView getNameImpl(uint32_t *) { return "uint32_t"; }
18template<> inline Cogs::StringView getNameImpl(uint64_t *) { return "uint64_t"; }
19
20#if defined(__EMSCRIPTEN__) or defined(__APPLE__)
21// __APPLE__: size_t is unsigned long, while uint64_t is unsigned long long
22template<> inline Cogs::StringView getNameImpl(size_t *) { return "size_t"; }
23#endif
24
25template<> inline Cogs::StringView getNameImpl(float *) { return "float"; }
26template<> inline Cogs::StringView getNameImpl(double *) { return "double"; }
27
28template<> inline Cogs::StringView getNameImpl(std::vector<std::string> *) { return "vector<string>"; }
29
30template<> inline Cogs::StringView getNameImpl(std::vector<int8_t> *) { return "vector<int8_t>"; }
31template<> inline Cogs::StringView getNameImpl(std::vector<int16_t> *) { return "vector<int16_t>"; }
32template<> inline Cogs::StringView getNameImpl(std::vector<int32_t> *) { return "vector<int32_t>"; }
33template<> inline Cogs::StringView getNameImpl(std::vector<int64_t> *) { return "vector<int64_t>"; }
34
35template<> inline Cogs::StringView getNameImpl(std::vector<uint8_t> *) { return "vector<uint8_t>"; }
36template<> inline Cogs::StringView getNameImpl(std::vector<uint16_t> *) { return "vector<uint16_t>"; }
37template<> inline Cogs::StringView getNameImpl(std::vector<uint32_t> *) { return "vector<uint32_t>"; }
38template<> inline Cogs::StringView getNameImpl(std::vector<uint64_t> *) { return "vector<uint64_t>"; }
39
40template<> inline Cogs::StringView getNameImpl(std::vector<float> *) { return "vector<float>"; }
41template<> inline Cogs::StringView getNameImpl(std::vector<double> *) { return "vector<double>"; }
42
43namespace Cogs
44{
45 namespace Reflection
46 {
47 class Type;
48
49 namespace Construction
50 {
51 template<typename T>
52 void * createInstanceImpl(std::false_type)
53 {
54 return nullptr;
55 }
56
57 template<typename T>
58 void * createInstanceImpl(std::true_type)
59 {
60 return new T();
61 }
62
63 template<typename T>
64 void * createInstance()
65 {
66 return createInstanceImpl<T>(std::is_constructible<T>());
67 }
68
69 template<typename T>
70 void destroyInstance(void * t)
71 {
72 delete reinterpret_cast<T *>(t);
73 }
74
75 template<typename T>
76 void * constructInstanceImpl(void * memory, std::true_type)
77 {
78 return new (memory) T();
79 }
80
81 template<typename T>
82 void * constructInstanceImpl(void *, std::false_type)
83 {
84 return nullptr;
85 }
86
87 template<typename T>
88 void * constructInstance(void * memory)
89 {
90 return constructInstanceImpl<T>(memory, std::is_constructible<T>());
91 }
92
93 template<typename T>
94 void destructInstance(void * t)
95 {
96 (void)t;
97 reinterpret_cast<T *>(t)->~T();
98 }
99 }
100
104 class COGSFOUNDATION_API TypeDatabase
105 {
106 public:
108
110 static void initializeBaseTypes();
111
113 static void clear();
114
122 template<typename T>
123 static Type & createType(bool isAbstract = false)
124 {
125 return createType(getName<T>(),
126 sizeof(T),
127 &Construction::createInstance<T>,
128 &Construction::destroyInstance<T>,
129 &Construction::constructInstance<T>,
130 &Construction::destructInstance<T>,
131 isAbstract || std::is_enum<T>::value);
132 }
133
139 template<typename T>
141 {
142 return createType(getName<T>(),
143 sizeof(T),
144 nullptr,
145 nullptr,
146 nullptr,
147 nullptr,
148 true);
149 }
150
154 static const Type & getType(const TypeId typeId);
155
159 static const Type & getType(const Name & name);
160
167 template<typename T>
168 static const Type & getType()
169 {
170 static auto typeId = getType(getName<T>()).typeId;
171
172 auto & type = getType(typeId);
173
174 return type;
175 }
176
187 static void constructInstance(const Type & type, void * data, const size_t dataSize);
188
197 static void destructInstance(const Type & type, void * data);
198
205 template<typename T>
206 static T * createInstance(const StringView & name)
207 {
208 auto & type = getType(name);
209
210 auto object = reinterpret_cast<T *>(createInstance(type));
211
212 return object;
213 }
214
220 static void * createInstance(const Type & type);
221
230 static const Type * getTypes(size_t & count);
231
233 static Type & createType(const StringView & name,
234 size_t size,
235 CreateInstance createInstance,
236 DestroyInstance destroyInstance,
237 ConstructInstance constructInstance,
238 DestructInstance destructInstance,
239 bool isAbstract = false);
240
241 private:
243 static std::unique_ptr<class TypeStore> typeStore;
244 };
245
246 }
247}
248
249#include "Type.h"
Manages all Type instances currently created in the system.
Definition: TypeDatabase.h:105
static Type & createAbstractType()
Create a new abstract Type from the provided template type.
Definition: TypeDatabase.h:140
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 std::unique_ptr< class TypeStore > typeStore
Stores registered types.
Definition: TypeDatabase.h:243
static Type & createType(bool isAbstract=false)
Create a new Type from the provided template type.
Definition: TypeDatabase.h:123
Represents a discrete type definition, describing a native type class.
Definition: Type.h:89
TypeId typeId
Unique type id for this type.
Definition: Type.h:353
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
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