Re: is it possible to get a unique key for a (instance, method) pair?
t.lehmann@rtsgroup.net wrote:
Well, you example is working fine - still one question ... How do I've
to implement this class for wrapping any method instance? See my code
(the compiler is yelling about the line inside of 'make_dummy_instance'
- can you help?
class DummyInstanceBase
{
public:
DummyInstanceBase(){}
virtual ~DummyInstanceBase(){}
};
template <class T>
class DummyInstance : public DummyInstanceBase
{
public:
template <T RealInstance>
static DummyInstanceBase* get()
{
static DummyInstance instance;
return &instance;
}
};
A more general wrapper class template - one that could wrap any kind of
class method - would require a great deal more "scaffolding" to
accomodate all of the possible member pointer types that may need to be
wrapped. Since I am too lazy to write an entire implementation, I have
just started on one below and which hopefully is enough to suggest a
complete solution.
A few notes on the code below: the major change to the Wrapper class
template is the addition of a second type parameter argument (in
addition to first type parameter which remains the class type of the
wrapped method). The second type parameter is a std::tr1::tuple that
describes the "signature" of the wrapped routine. Specifically: the
first type element in the tuple corresponds to the method's return
type, while the following zero to nine types correspond to the types of
parameters the method accepts.(Note that if the tr1 classes are not
available for your compiler, the Boost versions of the same should work
just as well as replacements). Invoking the member function through the
Wrapper object is also different - since the number of parameters
needed for the call varies. Therefore the method is called indirectly -
through a std::tr1::function object obtained from the Wrapper class
object for this purpose. An example is also included below.
Otherwise the Get() method works just as it did before: matching a
unique Wrapper object for each member function pointer (that can come
in a greater variety than ever before). And, with all of the code below
it's easy to lose sight of the fact that implementing Wrapper's Get()
method is the only reason why this code exists in the first place.
#include <iostream>
#include <tr1/tuple>
#include <tr1/functional>
using std::tr1::tuple;
using std::tr1::tuple_size;
using std::tr1::tuple_element;
using std::tr1::function;
// Helper templates to convert a class (T) and
// a tuple (S) into a member function pointer type
template <class T, class S,
int N = tuple_size<S>::value >
struct MemFunHelper { };
template <class T, class S>
struct MemFunHelper<T, S, 0> {
typedef void R;
typedef R (T::*type)();
typedef function< R (T)> FunctionT;
};
template <class T, class S>
struct MemFunHelper<T, S, 1> {
typedef typename tuple_element<0, S>::type R;
typedef R (T::*type)();
typedef function< R (T)> FunctionT;
};
template <class T, class S>
struct MemFunHelper<T, S, 2> {
typedef typename tuple_element<0, S>::type R;
typedef typename tuple_element<1, S>::type P1;
typedef R (T::*type)(P1);
typedef function<R (T, P1)> FunctionT;
};
/* MethodWrapper is parameterized with the class type and
a tuple describing the method's "signature". The tuple
contains the return type first followed by the types
of parameters (if any):
tuple< ReturnType, ParamOneType, ParamTwoType...
*/
template <class T, class Signature>
struct MethodWrapper
{
typedef MemFunHelper<T, Signature> MFH;
typedef typename MFH::type MemberPointerT;
typedef typename MFH::FunctionT FunctionT;
MethodWrapper( MemberPointerT inMF)
: mf_(inMF)
{}
template <MemberPointerT mf>
static MethodWrapper * Get()
{
static MethodWrapper sDummy(mf);
return &sDummy;
}
FunctionT Invoke()
{
return FunctionT(mf_);
}
private:
MemberPointerT mf_;
};
class Test
{
public:
void test() {}
long test2(char *) { return 2; }
};
typedef MethodWrapper< Test, tuple<void> >
MethodWrapperOne, * MethodOnePtr;
typedef MethodWrapper< Test, tuple<long, char*> >
MethodWrapperTwo, * MethodTwoPtr;
int main()
{
MethodOnePtr mf1 = MethodWrapperOne::Get<&Test::test>();
MethodTwoPtr mf2 = MethodWrapperTwo::Get<&Test::test2>();
Test t;
long i = mf2->Invoke()(t, "test");
}
Greg
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]