Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

C/C++

Building Your Own Plugin Framework: Part 5


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);
};
Example 8

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();
}
Listing Five

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.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.