Base: Add asserts to check that Type has been initialized

This commit is contained in:
Benjamin Nauck
2025-02-18 08:16:13 +01:00
parent 667b2d27b7
commit 01ad16e2e8
2 changed files with 18 additions and 2 deletions

View File

@@ -72,15 +72,25 @@ std::set<std::string> Type::loadModuleSet;
const Type Type::BadType;
Type::instantiationMethod Type::getInstantiationMethod() const
{
assert(typedata.size() >= 1 && "Type::init() must be called before creating instances");
assert(typedata.size() > index && "Type index out of bounds");
if (isBad() || typedata.size() <= index) {
return nullptr;
}
return typedata[index]->instMethod;
}
void* Type::createInstance() const
{
instantiationMethod method = typedata[index]->instMethod;
const auto method = getInstantiationMethod();
return method ? (*method)() : nullptr;
}
bool Type::canInstantiate() const
{
instantiationMethod method = typedata[index]->instMethod;
const auto method = getInstantiationMethod();
return method != nullptr;
}
@@ -183,11 +193,15 @@ const Type Type::fromKey(TypeId key)
const char* Type::getName() const
{
assert(typedata.size() >= 1
&& "Type::init() must be called before fetching names, even for bad types");
return typedata[index]->name.c_str();
}
const Type Type::getParent() const
{
assert(typedata.size() >= 1
&& "Type::init() must be called before fetching parents, even for bad types");
return typedata[index]->parent;
}

View File

@@ -134,6 +134,8 @@ public:
static const std::string getModuleName(const char* className);
private:
[[nodiscard]] instantiationMethod getInstantiationMethod() const;
TypeId index {BadTypeIndex};
static std::map<std::string, TypeId> typemap;