Zheng said:
I don't know where should I ask the question, so send the email to this
group.
I choose this group, because I want to write the program with c++

I want to write a program which support multiprotocol, but do not want
to write code for all protocols which I want to support. I plan I give
a interface and others give a module which implements a protocol, and
then the module can be inserted into my program without recompiling my
program.
How should I write the program to support this function?
Thank you.
There are two halves to your question. First, how do you support that
from a language standpoint? Second, how do you support that from a
platform standpoint?
From a language standpoint, the best way (in my opinion) to support
that sort of behavior is to define your interface as an abstract base
class (or if appropriate, a set of abstract base classes). An /abstract
class/ is one that contains at least one pure virtual function. It is
my recommendation that your interface contain only pure virtual
functions. Example:
class protocol_interface
{
public :
virtual void do_something_neat() = 0 ;
virtual int get_some_information() = 0 ;
virtual bool is_whatever() = 0 ;
} ;
Then, when people go to write a specific protocol, they will derive from
your interface, and implement the functions. For example:
class my_boring_protocol : public protocol_interface
{
public :
void do_something_neat() { std::cout << "This is neat!" << std::endl ; }
int get_some_information() { return 42 ; }
bool is_whatever() { return false ; }
} ;
Somewhere in your application you will have a protocol_interface pointer
that you use to control the protocol. Assume for a minute that you
managed to get it to point at an instance of my_boring_protocol, then it
would be my_boring_protocol's implementation that was being used.
So, how do you get your application's protocol_interface pointer to
point at an instance of my_boring_protocol, since it did not know know
about the existence of my_boring_protocol when it was compiled? That is
where the platform specific part of your question kicks in.
Unfortunately, I can't fully answer that for you because I don't know
what platform you are using, nor is that on topic for this newsgroup.
However, whatever platform you are using probably has some concept of
shared objects or shared libraries. On Windows these are called DLLs,
and on most Unix variants these are .so files.
Whatever your platform, it probably has some function that let's you
load an arbitrary shared object, and get the address of an arbitrary
function inside it. What you'll likely want to do is have a function
that returns a protocol_interface pointer that points to an instance of
the type of protocol that that shared object manages. For example:
my_boring_protocol mbp_inst ;
extern "C"
protocol_interface *get_instance() { return &mbp_inst ; }
If the instance returned in dynamically allocated, you probably need to
also have a function to destroy the instance when you are done with it.
Finally, if you are are using Windows, I've written a working example
you are free to copy and mess around with. The following package
contains all the code and a Visual Studio project to build it:
http://www.stanford.edu/~alanwj/examples/protocol_plugins_example.zip
-Alan