Dealing with deprecation of CreateInstance methods
I have been using KPluginFactory and associated loaders to get object instances from plugins. These objects have constructors that do not match the four specific patterns that InheritanceChecker
supports. My existing code subclasses KPluginFactory
and provides a createInstance()
method and an overload of registerPlugin()
, like so:
template < class impl, class ParentType >
static QObject* createInstance( QWidget* parentWidget, QObject* parent, const QVariantList& args )
{
return new impl( parent );
}
template < class T >
void registerPlugin()
{
KPluginFactory::registerPlugin< T >( QString(), &createInstance< T, QObject > );
}
This is because the classes / instances have only a constructor that takes a single QObject* parent
. That API (constructor pattern) is not-changeable. The deprecation warning points to "just use registerPlugin()
", but how? Following the instructions naively yields the following compilation error (here with a specific class T
):
/usr/local/include/KF5/KCoreAddons/kpluginfactory.h:745:10: note: candidate template ignored: requirement 'InheritanceChecker<ContextualProcessJob>::enabled' was not satisfied [with T = ContextualProcessJob]
void registerPlugin()
The code-comments suggest "you can inherit it" (referring to InheritanceChecker
), but how?
Adding a wrapper-class that satisfies the constructor patterns that
InheritanceChecker
does support, works; then I just end up with one useless QObject-creation per plugin-loading, and some extra cleanup-work to do. It would be nice, though, to have documentation that explains in more detail how to useKPluginFactory
outside of the existing narrow-scope (or, if that is not intended to be supported, point users of customcreateInstance
methods to lower-level non-frameworks API).