[Qt-interest] Creating a macro to simplify plugin interface implementation

Alex Wade ARA/CFD awade at ara.com
Fri Oct 30 18:02:04 CET 2009


I'm creating an API and would like to prevent users from having to learn the Qt Plugin API in order to use my API.  To do this, I'm using code similar to what's below.  Basically an API user should just have to create their model by inheriting from IModel and calling DECLARE_DYNAMICALLY_LOADABLE() on it.

I have two questions:

1)      Can anybody think of a better way to accomplish this?

2)      This will not compile once I add the Q_EXPORT_PLUGIN2 call to my macro. Without it, it compiles but I don't have a valid plugin. I get the following errors:

error C2065: 'MyModel' : undeclared identifier

error C2275: 'MyModelLoader' : illegal use of this type as an expression

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

error C2078: too many initializers

error C2275: 'TestModelLoader' : illegal use of this type as an expression

Worth noting: I assume that the problem is due to the MOC preprocessor is being run before the C preprocessor. I use CMake to build and I'm calling QT4_WRAP_CPP on MyModel.h. Right now, I'm playing with a file generated when I ran the C preprocessor on MyModel.h and am trying to build starting with the preprocessed output for now. If that works, I'll set my build process to do that automatically.

// File: "DynamicallyLoadableModel.h"
// Model interface.
class DynamicallyLoadableModel
{
    public:
        virtual void start() = 0;
        virtual void stop() = 0;
};
Q_DECLARE_INTERFACE(DynamicallyLoadableModel, "DynamicallyLoadableModel/1.0");

// Macro to create plugin implementation.
#define DECLARE_DYNAMICALLY_LOADABLE(MODEL_NAME)\
      class MODEL_NAME##Loader : public QObject, public DynamicallyLoadableModel\
      {\
            Q_OBJECT\
            Q_INTERFACES(DynamicallyLoadableModel);\
            private:\
                  IModel* mInstance;\
            public:\
                  virtual void start(shade::messaging::Service& service)\
                  {\
                        mInstance = new MODEL_NAME();\
                        mInstance->register();\
                  }\
                  virtual void stop()\
                  {\
                        mInstance->unregister();\
                        delete mInstance;\
                  }\
      };\
      Q_EXPORT_PLUGIN2(MODEL_NAME, MODEL_NAME##Loader);

// File: "IModel.h"
class IModel : public QObject
{
    // Declarations...
}

// File: "MyModel.h"
#include "IModel.h"
#include "DynamicallyLoadableModel.h"

class MyModel : public IModel
{
    // Declarations...
}
DECLARE_DYNAMICALLY_LOADABLE(MyModel)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20091030/f9b2c005/attachment.html 


More information about the Qt-interest-old mailing list