Originally Posted by
zuck
if I need to share some concrete methods (which the pkServer instance needs to know too) with all applications derived from pkApplication interface? How can I do that?
it is recommended to avoid that as much as possible. Your plugin system should be designed (mostly) one-way : the application call methods from the plugin and the plugin acts on its own.
For the cases where it is really needed, there are two main ways :
- passing pointers to "controllers" object (recommened method in most cases, more on this below)
- putting all the common code in a shared lib. both the app and the plugins would link against it
A controller object is basically a class that allows the plugin to obtain data or to perform actions in a way that is not "predictable" by the app.
Just create another abstract class exposing methods of interest (which must be pure virtual). Then subclass that class in your app to provide actual implementation of these methods and pass a pointer to an object of your controller class to the plugins. e.g :
class pkApplicationController
{
public:
virtual ~pkApplicationController() {}
virtual QString statusMessage
() const = 0;
virtual void setStatusMessage(const QString& s) = 0;
};
class pkApplicationController
{
public:
virtual ~pkApplicationController() {}
virtual QString statusMessage() const = 0;
virtual void setStatusMessage(const QString& s) = 0;
};
To copy to clipboard, switch view to plain text mode
class pkConcreteAppController : public pkApplicationController
{
public:
QString statusMessage
() const { return m_status;
} void setStatusMessage(const QString& s) { m_status = s; }
};
class pkConcreteAppController : public pkApplicationController
{
public:
QString statusMessage() const { return m_status; }
void setStatusMessage(const QString& s) { m_status = s; }
};
To copy to clipboard, switch view to plain text mode
// somewhere in your app : (pointerToController is a pkConcreteAppController*)
plugin->someMethod(pointerToController);
// somewhere in your app : (pointerToController is a pkConcreteAppController*)
plugin->someMethod(pointerToController);
To copy to clipboard, switch view to plain text mode
// in a plugin
void SomePlugin::someMethod(pkApplicationController *c)
{
if ( !happy )
c->setStatusMessage("Always look at the bright side of life.");
}
// in a plugin
void SomePlugin::someMethod(pkApplicationController *c)
{
if ( !happy )
c->setStatusMessage("Always look at the bright side of life.");
}
To copy to clipboard, switch view to plain text mode
I hope this helps.
Bookmarks