Hybrid C/C++ Plugins
So C is cumbersome and you would like to use the C++ API, but you must provide total binary compatibility. What a conundrum. Fortunately, the Hybrid C/C++ plugin is just the ticket. It provides C compatibility with a C++ API via ActorBaseTemplate. It also cuts down significantly on the plugin initialization code by using PluginHelper. The plugin registers two monsters: GnarlyGolem and PsychicPiranea. Both derive from ActorBaseTemplate and both implement the C++ IActor interface, but GnarlyGolem is actually talking in C to the PluginManager while PsychicPiranea is talking C++.
Example 8 contains the definitions of both classes. You can see how compact and clean they look. There is no need for the create() or destroy() static methods anymore (ActorBaseTemplate takes care of it). The only difference between the two is that PsychicPiranea specifies the IActor as the second template parameter for ActorBaseTemplate. This is the infamous Interface parameter, which is C_Actor by default.
class GnarlyGolem : public ActorBaseTemplate<GnarlyGolem> { public: GnarlyGolem(PF_ObjectParams *); // IActor methods virtual void getInitialInfo(ActorInfo * info); virtual void play(ITurn * turnInfo); }; class PsychicPiranea : public ActorBaseTemplate<PsychicPiranea, IActor> { public: PsychicPiranea(PF_ObjectParams *); // IActor methods virtual void getInitialInfo(ActorInfo * info); virtual void play(ITurn * turnInfo); };
PsychicPiranea could have been been derived from IActor directly, just like KillerBunny and StationarySatan in the C++ plugin. The reason it derives from ActorBaseTemplate is threefold:
- It saves you from write create()/destroy() static methods
- It lets you switch quickly between C and C++ if you deploy the same plugins in different situations
- So I can demonstrate this cool capability
This is really cool because between the automatic adaptation of C objects by the PluginManager and the nice C++ wrapper that ActorBaseTemplate provides, application developers and plugin developers can be blissfully ignorant of the C that flows between them. The only developers who should be concerned with the C/C++ dualism are the object model developers. If your system is a serious platform, then the object model will congeal sooner or later. Then everyone can forget about C and just extend the application that's built on top of the object model and write lots of plugins -- all in C++.
The implementation of GnarlyGolem and PsychicPiranea is of typical C++ plugin implementations. GnarlyGolem is C under the covers, but it doesn't care.
Listing Five is the initialization code of the hybrid plugin. There are more #include statements than code. (I kid you not. Count them. This is as close to a domain-specific language as it gets -- without macros, anyway.) You define a PluginHelper object and call registerObject() for each object. No need for annoying structs with lots of function pointers, no need for error checking. Pure simplicity. In the end, return the result.
#include "wrapper_plugin.h" #include "plugin_framework/plugin.h" #include "plugin_framework/PluginHelper.h" #include "GnarlyGolem.h" #include "PsychicPiranea.h" extern "C" PLUGIN_API PF_ExitFunc PF_initPlugin(const PF_PlatformServices * params) { PluginHelper p(params); p.registerObject<GnarlyGolem>((const apr_byte_t *)"GnarlyGolem"); p.registerObject<PsychicPiranea>((const apr_byte_t *)"PsychicPiranea", PF_ProgrammingLanguage_CPP); return p.getResult(); }
There is one nit to pick. When registering the PsychicPiranea, you need to specify PF_ProgrammingLanguage_CPP (the default is PF_ProgrammingLanguage_C). The programming language can be gleaned automatically from the PsychicPiranea class itself because it passed the programming language as the Interface parameter to the ActorBaseTemplate. However, this requires some template meta-programming tricks (type detection) and I'm running out of scope quickly. Here is a quick link if you're curious: www.ddj.com/cpp/184402050.