Developing Apama Applications > Writing Correlator Plug-ins > Writing a Plug-in in C and C++ > A simple C plug-in
A simple C plug-in
The C version of the above example, simple_plugin.c, is very similar. The first difference is the use of the C version of the API, which is correlator_plugin.h. This can be located in the include directory of the Apama installation.
As before, there is only one function, called test, which takes a string as its sole parameter, makes some alterations to it, prints it out, and passes back another string as the result.
The C method that implements this plug-in function must be defined as follows:
static void AP_PLUGIN_CALL simplePluginTest(
  const AP_PluginContext* ctx,
  const AP_PluginTypeList* args,
  AP_PluginType* rval,
  AP_TypeDiscriminator rtype)
The rest of the example is very similar to the C++ example. The complete code base is as follows, and may be found in the Apama installation’s samples\correlator_plugin\c\simple_plugin.c directory. A ‘makefile’ (for use with GNU Make) and batch file (for Microsoft’s Visual Studio .NET) are provided in this folder to assist with compiling plug-ins on Unix and Windows platforms respectively. A README.txt file in the directory describes how to build the plug-in.

/**
* simple_plugin.c
*
* A very simple example plugin.
*
* $Copyright(c) 2013 Software AG, Darmstadt, Germany and/or its licensors$
*
* $Revision: 188575 $ $Date: 2012-07-17 20:22:08 +0100 (Tue, 17 Jul 2012) $
*/

#include <correlator_plugin.h>
 
#include <stdio.h>
 
/* Used in the test function below */
#define TEST_STRING "Hello, World"
 
/**
* Plugin function. Takes a single string parameter and returns
* another string. The input string will also be modified (pass by
* reference for non-primitive types).
*
* @param ctx Execution context for this invocation of the function.
* @param args Function parameters, as declared below.
* @param rval Function return value, to be filled in by plugin.
*/
static void AP_PLUGIN_CALL simplePluginTest(
const AP_PluginContext* ctx, const AP_PluginTypeList* args,
AP_PluginType* rval, AP_TypeDiscriminator rtype)
{
AP_PluginType* arg;
printf("simple_plugin function test called\n");
arg = args->functions->getElement(args,0);
printf("args[0] = %s\n",arg->functions->getStringValue(arg));
rval->functions->setStringValue(rval,TEST_STRING);
printf("return value = %s\n",rval->functions->getStringValue(rval));
fflush(stdout);
}
 
/** Parameter types for the 'test' function */
static const AP_char8* testParamTypes[1] = { "string" };
 
/** Declare functions provided by this plugin */
static AP_PluginFunction Functions[1] = {
{ "test", &simplePluginTest, 1, &testParamTypes[0], "string" }
};
 
/**
* Initialisation and shutdown functions. Signatures must match the
* AP_PluginInitFunctionPtr and AP_PluginShutdownFunctionPtr typedefs,
* respectively. Likewise, the function names must use the
* AP_INIT_FUNCTION_NAME and AP_SHUTDOWN_FUNCTION_NAME macros.
*/
 
/**
* Plugin initialisation function. Called each time the plugin library is
* loaded, so must expect to be called more than once.
*
* @param ctx Execution context for the function call.
*
* @param version Active plugin API version. The plugin should verify it
* is compatible with this version (and return AP_VERSION_MISMATCH_ERROR
* if not) and update version to indicate the API version the plugin was
* compiled against.
*
* @param nFunctions The plugin should set this to the number of functions
* it exports.
*
* @param functions The plugin should set this to the address of an array
* of AP_Functions structures, of length nFunctions, describing the
* functions exported by the plugin. This data will be read and copied by
* the plugin API.
*
* @return An AP_ErrorCode indicating the success or otherwise of the
* library initialisation.
*/
AP_PLUGIN_DLL_SYM AP_ErrorCode AP_PLUGIN_CALL AP_INIT_FUNCTION_NAME(
const AP_PluginContext* ctx, AP_uint32* version,
AP_uint32* nFunctions, AP_PluginFunction** functions)
{
/* Export the plugin's version */
*version = AP_CORRELATOR_PLUGIN_VERSION;
/* Export function declarations */
*nFunctions = 1;
*functions = &Functions[0];
return AP_NO_ERROR;
}
 
/**
* Plugin shutdown function. Called each time the plugin library is
* unloaded, so must expect to be called more than once.
*
* @param ctx Execution context for the function call.
*
* @return An AP_ErrorCode indicating the success or otherwise of the
* library initialisation.
*/
AP_PLUGIN_DLL_SYM AP_ErrorCode AP_PLUGIN_CALL AP_SHUTDOWN_FUNCTION_NAME(
const AP_PluginContext* ctx) {
/* Nothing to do -- just indicate success */
return AP_NO_ERROR;
}
 
/**
* Plugin library version function.
*
* @param ctx Execution context for the function call.
*
* @param version The plugin should fill this in with the version of the
* API it was compiled against.
*
* @return An AP_ErrorCode indicating the success or otherwise of the
* function call.
*/
AP_PLUGIN_DLL_SYM AP_ErrorCode AP_PLUGIN_CALL AP_PLUGIN_VERSION_FUNCTION_NAME(
const AP_PluginContext* ctx, AP_uint32* version) {
/* Just return the version number */
*version = AP_CORRELATOR_PLUGIN_VERSION;
return AP_NO_ERROR;
}
 
/**
* Plugin capabilities function.
*
* @param ctx Execution context for the function call.
*
* @return An AP_Capabilities bitset describing the capabilities of this
* plugin.
*/
AP_PLUGIN_DLL_SYM AP_Capabilities AP_PLUGIN_CALL AP_PLUGIN_GET_CAPABILITIES_FUNCTION_NAME(
const AP_PluginContext* ctx) {
// This plugin promises no special capabilities. But if you wanted to
// declare any, you could return them, or-ed together.
return AP_CAPABILITIES_NONE;
}
Copyright © 2013 Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, and/or Terracotta Inc., San Francisco, CA, USA, and/or Software AG (Canada) Inc., Cambridge, Ontario, Canada, and/or, Software AG (UK) Ltd., Derby, United Kingdom, and/or Software A.G. (Israel) Ltd., Or-Yehuda, Israel and/or their licensors.