App: add overloaded template method getExtension()

This allows it to write more elegant code like:
auto ext = obj->getExtension<>(GroupExtension);
instead of
auto ext = dynamic_cast<GroupExtension*>(obj->getExtension(GroupExtension::getExtensionClassTypeId(), true, true));
This commit is contained in:
wmayer
2024-03-15 15:08:50 +01:00
committed by wwmayer
parent d57640005e
commit 75d30d2b04

View File

@@ -125,6 +125,11 @@ public:
bool hasExtensions() const;
App::Extension* getExtension(Base::Type, bool derived = true, bool no_except=false) const;
App::Extension* getExtension(const std::string& name) const; //this version does not check derived classes
// this version checks for derived types and doesn't throw
template<typename ExtensionT>
ExtensionT* getExtension() const {
return static_cast<ExtensionT*>(getExtension(ExtensionT::getExtensionClassTypeId(), true, true));
}
//returns first of type (or derived from) and throws otherwise
template<typename ExtensionT>