From 01ad16e2e8b445dfd5d19ba41e1e4becd1b31fb1 Mon Sep 17 00:00:00 2001 From: Benjamin Nauck Date: Tue, 18 Feb 2025 08:16:13 +0100 Subject: [PATCH] Base: Add asserts to check that Type has been initialized --- src/Base/Type.cpp | 18 ++++++++++++++++-- src/Base/Type.h | 2 ++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Base/Type.cpp b/src/Base/Type.cpp index cf5d4d5bb1..63eb34245a 100644 --- a/src/Base/Type.cpp +++ b/src/Base/Type.cpp @@ -72,15 +72,25 @@ std::set 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; } diff --git a/src/Base/Type.h b/src/Base/Type.h index 178912b3f8..d96f46c22a 100644 --- a/src/Base/Type.h +++ b/src/Base/Type.h @@ -134,6 +134,8 @@ public: static const std::string getModuleName(const char* className); private: + [[nodiscard]] instantiationMethod getInstantiationMethod() const; + TypeId index {BadTypeIndex}; static std::map typemap;