I'm wondering if there's a good, generic way to write an object wrapper such that you could call all the methods defined by the class it wraps on the wrapper class itself (without having to explicitly redefine the class interface in the wrapper class). For example, two different, arbitrary classes:
class InnerExample1 {
public:
InnerExample1(int a, bool b) {...}
void DoSomething1(int a) {...}
bool DoSomething2(bool b) {...}
etc...
};
class InnerExample2 {
InnerExample2(float a, int b) {...}
void DoWork(int a) {...}
bool GetResult(bool b) {...}
etc...
};
I'd like a generically defined wrapper class such that I could do:
Wrapper<InnerExample1> inner1;
inner1.DoSomething(42);
bool result = inner1.DoSomething2(false);
and
Wrapper<InnerExample2> inner2;
inner2.DoWork(21);
bool result = inner2.GetResult(true);
With 'Wrapper' being generic enough to be able to handle wrapper either class and forward all calls made on it to its inner delegate.
I've seen code for things like a factory pattern where, with variadic args, a generic factory could instantiate any class, as well as a generalizing a binding to a single member method, but nothing that's done it for the entire class. It seems likely to me that it isn't possible...but people on SO come up with all sorts of stuff I never would have :)
EDIT: I was trying to keep the question simple by leaving out unnecessary details, but I left out too many. I need the wrapper to insert some logic around each call. Say, for example, I want this wrapper to be able to count how many times each method has been called in addition to forwarding the call to the inner object.
EDIT2: To be specific, I'm trying to write a generic wrapper to implement ActiveObject pattern. I ended up doing an 'Active' base class that handles the common bits (managing the internal thread, queuing up function calls) and writing subclasses for each internal type (with their interface mimicked).
Aucun commentaire:
Enregistrer un commentaire