Uhm... you are right...
Reading another time your replies (and your link) I figure that I am missing something about how "exemplar" works in the code you are suggesting. The principle of Chain of responsability is clear (we loop on the Base Class pointers, any pointer will run the subclass(es) functions thank of the polymorphism, pointers are initialized) But it is not clear where and how you "create new instance" for the subclass to add into chain (with = new Subclass) as it is not clear that
binary_io_helper_v_1_0 exemplar { /* ... */ } ; // instantiate the prototype
The prototype pattern is not completely clear to me
--------------------------
> > Finally... the heart of my "plugin system" (I call it plugin even if it is all static) is that
> > I use a file with a list of undefined ADD_PLUGIN(ClassName, "description")
> > that is defined only inside specific functions (plugin reader check and plugin writer check
> > and exec_plugin_wtext) and also in another point (extends a drop-down menu of
> > "formats available for saving", in another part of the program) and discarded afterwards.
> I haven't understood this part; could you explain it, perhaps with an example?
to explain what I did I must explain the "old code" I mentioned in the firt topic - entirely public-access classes)
we have BinaryInterface class that comunicates to program
BynaryHandler class is pure virtual class to interact with the correct method to read/write/wText throughout BynaryHandler subclasses (using polymorphism).
In order to do this program uses 3 functions
BynaryHandler * select_plugin_reader() //checks for the correct reader and return pointer of the new subclass
BynaryHandler * select_plugin_writer() //same for writer
void exec_plugin_wtext() //exec wText of subclasses
aside from those functions I have 2 special "include files" called "plugin_list.h" and "plugin_list__headers.h"
1 2 3 4 5 6 7 8
|
// Plugin_list__headers.h
#include "Format1.h"
#include "Format2.h"
#include "Format3.h"
//here I semplified... however this file will contain where the "header of every subclass" is
//Assume we have Class Format1 : public BinaryHandler and so on
|
1 2 3 4
|
//Plugin_list.h - here where I declare active plugins
ADD_PLUGIN(Format1, "v.1.0 - the old version")
ADD_PLUGIN(Format2, "v.2.0 - the newest version")
ADD_PLUGIN(Format3, "Unsupported format - only used by my program")
|
at this point macro ADD_PLUGIN is not defined.... becouse it will be used only in 2 cpp files
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
//PluginChecker.cpp - checks the right plugin...
//........
BinaryHandler * select_plugin_reader(filename) {
#ifdef ADD_PLUGIN
#undef ADD_PLUGIN
#endif
BynaryHandler * pointer = NULL;
#define ADD_PLUGIN(ClassName, ClassDescription) \
{ pointer = new ClassName; \
bool k = pointer->read(filename); \
if(k == true) return pointer; \
else delete pointer; \
}
#include "Plugin_list.h"
#undef ADD_PLUGIN
return NULL;
}
// I know it is a bit trivial... it will return false both if format is wrong or if reading was impossible,
// but it is an example how the current system is
|
It is more or less used in this way (the code is not exactly this one, but it is more or less the same... now I don't remember the exact code for plugin write and plugin wtext but it works in a similar manner).
and SFF_PLUGIN is used also in another part of the code. Here it would be for me complex to report c++ so I will explain a sort of meta-code.
There menu in my program that allow user to "Select Binary format to use for save files". When you click it will be showed a drop-down of the list with available formats
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
//METACODE
StringList list; //list of plugins.
#ifdef ADD_PLUGIN
#undef ADD_PLUGIN
#endif
#define ADD_PLUGIN(ClassName, ClassDescription) list.add(ClassDescription)
#include "Plugin_list.h"
#undef ADD_PLUGIN
|
(the programs will display a brief description for all formats available... the user will choose from "v.1.0 - the old version" and the other descriptions
In this way you have only to update plugin_list.h and plugin_list__headers.h to allow the program to see all plugins available (it will automaticly update select_plugin_reader() select_plugin_writer(), exec_plugin_wtext() and "user dropdown list of available formats for writing binary") [reading instead is automated checking for actual format used]