From: Christophe Meessen on
Hello,

I'm implementing a remote method invocation system. Each class has a
dispatch method whose role is to call the appropriate instance method.

The problem is that I need a generic member function pointer container
and a generic way to call the method by the dispatch method.

In my case all member functions involved have the same signature (same
arguments and same result value types) but the classes they belong to
may differ.

Apparently member function's type is bound to the class name. Is there a
way to get around this constrain ?

The only solution I found so far is to encapsulate the method table into
a polymorphic dispatcher class with a virtual dispatch method. It is a
singleton and encapsulates all the type specific stuff. The class is
templated so that it is automatically generated for the appropriate type.

The instance has then a pointer on the dispatcher class that can be
changed to change the behavior of the object (i.e. link it with access
rights policy).

Isn't there a simpler and more efficient way to do this in C++ ? Did I
miss it ?

As far as I understand it, the problem boils down to the fact that
member function pointer types are bound to a specific class type.


Some comments or advises are warmly welcomed.

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Thomas Lehmann on
> The problem is that I need a generic member function pointer container
> and a generic way to call the method by the dispatch method.

When somebody is calling a concrete method of a concrete class
at client side wouldn't you do the same at server side?
If so, can't you manage to store instances and methods in
containers per class type?

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Ulrich Eckhardt on
Christophe Meessen wrote:
> I'm implementing a remote method invocation system. Each class has a
> dispatch method whose role is to call the appropriate instance method.
>
> The problem is that I need a generic member function pointer container
> and a generic way to call the method by the dispatch method.
>
> In my case all member functions involved have the same signature (same
> arguments and same result value types) but the classes they belong to
> may differ.
>
> Apparently member function's type is bound to the class name. Is there a
> way to get around this constrain ?

Yes, there are even libraries that help you do exactly that, e.g.
Boost.Function. If you have e.g. two classes Fou and Barre and their
according methods, you would have a common function signature:

typedef boost::function<int, double> function_type;

and you would then generate these functors using bind():

Fou object1;
function_type f1 = boost::bind( &Fou::method1, &object1, _1);
Barre object2;
function_type f2 = boost::bind( &Barre::method2 &object2, _1);

and store them in e.g. a map or vector.


> The only solution I found so far is to encapsulate the method table into
> a polymorphic dispatcher class with a virtual dispatch method. It is a
> singleton and encapsulates all the type specific stuff. The class is
> templated so that it is automatically generated for the appropriate type.

Nah, there is an easier way around:

You can wrap any memberfunction into a normal function:

// wrapper for Fou::method1
int fou_method1( Fou* o, double p1) {
return o->method1(p1);
}

Similarly, you can use a typeless pointer:

// wrapper for Fou::method1
int fou_method1( void* o, double p1) {
return static_cast<Fou*>(o)->method1(p1);
}

and then you have a function which has the same signature for any class,
provided the arguments match. Of course, this isn't type-safe due to the
typeless pointer, so you have to take care that this isn't used on the
wrong types.

Uli

--
Sator Laser GmbH
Geschäftsführer: Michael Wöhrmann, Amtsgericht Hamburg HR B62 932


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Thomas Richter on
Christophe Meessen schrieb:
> Hello,
>
> I'm implementing a remote method invocation system. Each class has a
> dispatch method whose role is to call the appropriate instance method.
>
> The problem is that I need a generic member function pointer container
> and a generic way to call the method by the dispatch method.
>
> In my case all member functions involved have the same signature (same
> arguments and same result value types) but the classes they belong to
> may differ.
>
> Apparently member function's type is bound to the class name. Is there a
> way to get around this constrain ?

Sorry, I don't quite understand. A method is bound to the class it is
part of, which is the OO design paradigm. C++ also offers freestanding
functions that are not bound to specific classes.

> The only solution I found so far is to encapsulate the method table into
> a polymorphic dispatcher class with a virtual dispatch method. It is a
> singleton and encapsulates all the type specific stuff. The class is
> templated so that it is automatically generated for the appropriate type.
>
> The instance has then a pointer on the dispatcher class that can be
> changed to change the behavior of the object (i.e. link it with access
> rights policy).
>
> Isn't there a simpler and more efficient way to do this in C++ ? Did I
> miss it ?

I would define an abstract base class for all classes that can be
dispatched/called from remote. This abstract base contains a method that
performs the invocation. Then, derive each class you need to make
accessible from remote from this base. Since the signature of all your
classes is identical, this should not impose problems.

> As far as I understand it, the problem boils down to the fact that
> member function pointer types are bound to a specific class type.

Right, which is the OO design fundamental. But there are freestanding
functions as well.

> Some comments or advises are warmly welcomed.

An unrelated comment, maybe: It seems to me that you try to re-invent
the wheel. There are a couple of remote-function call environments you
might want to look into, CORBA being one (but having a couple of
problems of its own, though).

So long,
Thomas

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

 | 
Pages: 1
Prev: C++ books
Next: C++ side effects