1#include "TypeDatabase.h"
5#include "../ComponentModel/Attributes.h"
7#include <unordered_map>
30Cogs::Reflection::TypeDatabase::~TypeDatabase()
38 typeStore->types.emplace_back();
40 TypeDatabase::createType<bool>();
41 TypeDatabase::createType<std::string>();
43 TypeDatabase::createType<int8_t>();
44 TypeDatabase::createType<int16_t>();
45 TypeDatabase::createType<int32_t>();
46 TypeDatabase::createType<int64_t>();
48 TypeDatabase::createType<uint8_t>();
49 TypeDatabase::createType<uint16_t>();
50 TypeDatabase::createType<uint32_t>();
51 TypeDatabase::createType<uint64_t>();
53#if defined(EMSCRIPTEN) || defined(__APPLE__)
54 TypeDatabase::createType<size_t>();
57 TypeDatabase::createType<float>();
58 TypeDatabase::createType<double>();
60 TypeDatabase::createType<std::vector<std::string>>();
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>>();
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>>();
72 TypeDatabase::createType<std::vector<float>>();
73 TypeDatabase::createType<std::vector<double>>();
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>();
85 typeStore->types.clear();
86 typeStore->typesByName.clear();
91 debug_assert(typeId !=
NoType && typeId < typeStore->types.size() &&
"Type with the given name is not registered in database.");
94 const auto index = typeId < typeStore->types.size() ? typeId : 0;
96 return typeStore->types[index];
101 auto foundIt = typeStore->typesByName.find(name.
getId());
103 if (foundIt != typeStore->typesByName.end()) {
104 return getType(foundIt->second);
123 assert(type.
isValid() &&
"Invalid type cannot be used to construct instance.");
124 assert(type.
getSize() <= dataSize &&
"Placement target too small to construct in.");
150 count = typeStore->types.
size();
152 return typeStore->types.data();
163 typeStore->types.emplace_back();
164 auto & t = typeStore->types.back();
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");
169 t.typeId =
static_cast<TypeId>(typeStore->types.size() - 1);
174 t.createInstance = createInstance;
175 t.destroyInstance = destroyInstance;
176 t.constructInstance = constructInstance;
177 t.destructInstance = destructInstance;
180 debug_assert(typeStore->typesByName.find(t.name.getId()) == typeStore->typesByName.end() &&
"Type name already in use.");
182 typeStore->typesByName[t.name.getId()] = t.typeId;
static T * createInstance(const StringView &name)
Create an instance of the type with the given name.
static const Type & getType()
Get the Type of the given template argument.
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.
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.
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.
DestructInstance destructInstance
CreateInstance createInstance
Method used to create a new instance using operator new.
size_t size
Size of an instance of a class with this type in bytes.
constexpr bool isValid() const
Gets if the type is considered a valid, registered type.
ConstructInstance constructInstance
Method used to construct an instance using placement new.
constexpr size_t getSize() const
Get the size of an instance of the reflected type, in bytes.
Provides a weakly referenced view over the contents of a string.
constexpr bool empty() const noexcept
Check if the string is empty.
uint16_t TypeId
Built in type used to uniquely identify a single type instance.
void(*)(void *) DestroyInstance
Object destroy function type. Comparable to regular delete.
void *(*)(void *) ConstructInstance
Object construction function type. Comparable to placement new.
constexpr TypeId NoType
Definition of no type.
void *(*)() CreateInstance
Object creation function type. Comparable to regular new.
void(*)(void *) DestructInstance
Object destruction function type. Comparable to calling the destructor.
Contains all Cogs related functionality.
Represents an unique name.
size_t getId() const
Get the unique identifier of this name.