Hi, Jem :)
Well actually creating an API is not really about programming as much as
documenting.
So it's really just about letting people know which functions they can
use and what they do... Or more elegantly, which functions they must
PROVIDE in their plugin architecture, and then you use the functions
(the callback approach).
Since you're using C++ there's a very elegant approach to this called
polymorphism. What you do is create an abstract base class, where you
implement the complete API as public members, and then let your plugin
developers know they can create their plugins as a sub-class of this
class (provide the header file). Here's how it looks:
*Untested code!*
/*
plugin.h
*/
#ifndef _PLUGIN_H
#define _PLUGIN_H
class Plugin {
public:
Plugin() {}
~Plugin() {} // Abstract class has no body file so constructors
// must be implemented here
virtual int getNotes()=0;
virtual void setNotes(int note)=0; // Here your API consists of
// these two virtual functions
};
#endif
/*
notemesmerizer.h
*/
#ifndef _NOTEMESMERIZER_H
#define _NOTEMESMERIZER_H
#include "plugin.h"
class NoteMesmerizer: Plugin {
public:
NoteMesmerizer();
~NoteMesmerizer();
int getNotes(); // Not implementing both functions in the
void setNotes(int note); // base class would cause a compile error
}
#endif
/*
notemesmerizer.cxx
*/
#include "notemesmerizer.h"
NoteMesmerizer::NoteMesmerizer(): Plugin() {
// Do something; construct
}
NoteMesmerizer::~NoteMesmerizer() {
// Do something; destruct
}
int NoteMesmerizer::getNotes() {
// Do interesting stuff here
return e=mc2
}
void NoteMesmerizer::setNotes(int note) {
// Do interesting stuff here
}
/*
main.cxx
*/
#include "plugin.h"
int main() {
// DLOPEN stuff here
// Polymorphism: Declare base class, implement
// derived class
Plugin* plugins[];
int notes[];
for (int i=0;i<maxplugins;i++) {
plugins[i]=dlopen_create(libraries[i]);
notes[i]=plugins[i]->getNotes();
}
}
Ask away if you wanna know more... Took me long enough to find this
stuff out so you should know too :)
Hope this is helpful!
Carlo