This is doable with the Visitor design pattern.
Visitor injects new functionality into a hierarchy of classes. This need not be virtual and can be expressed as a function template with no problems.
The usual downsides of Visitor apply (the cyclic dependency), as do the usual fixes/workarounds (the acyclic dynamic visitor technique).
Here's a (regular, cyclic) implementation example, quickly thrown together.
#include <iostream>
#include <typeinfo>
class Manager1;
class Manager2;
class Visitor
{
public:
virtual void visit (Manager1*) = 0;
virtual void visit (Manager2*) = 0;
};
class IManager
{
public:
template<class T> void Set(T* t);
template<class T> T *Get();
virtual void accept(Visitor* v) = 0;
};
class Manager1: public IManager
{
public:
template<class T> void Set(T*)
{ std::cout << "Manager1::Set " << typeid(T).name() << std::endl; }
template<class T> T *Get()
{ std::cout << "Manager1::Get " << typeid(T).name() << std::endl; return 0; }
virtual void accept(Visitor* v)
{ v->visit(this); }
};
class Manager2: public IManager
{
public:
template<class T> void Set(T* t)
{ std::cout << "Manager2::Set " << typeid(T).name() << std::endl; }
template<class T> T *Get()
{ std::cout << "Manager2::Get " << typeid(T).name() << std::endl; return 0; }
virtual void accept(Visitor* v)
{ v->visit(this); }
};
template <class T>
class GetVisitor : public Visitor
{
public:
T* GetFunc(IManager* m) { m->accept(this); return t; }
void visit(Manager1* m) { t = m->Get<T>(); }
void visit(Manager2* m) { t = m->Get<T>(); }
private:
T* t;
};
template <class T>
class SetVisitor : public Visitor
{
public:
void SetFunc(IManager* m, T* tt) { t = tt; m->accept(this); }
void visit(Manager1* m) { m->Set(t); }
void visit(Manager2* m) { m->Set(t); }
private:
T* t;
};
template<class T> void IManager::Set(T* t)
{ SetVisitor<T> v; v.SetFunc(this, t); }
template<class T> T *IManager::Get()
{ GetVisitor<T> v; return v.GetFunc(this); }
class Foo {};
int main ()
{
IManager* mgr1 = new Manager1;
IManager* mgr2 = new Manager2;
int a = 5;
const char* b = "abc";
double c = 1.0;
Foo d;
mgr1->Set(&a);
mgr1->Set(&b);
mgr1->Set(&c);
mgr1->Set(&d);
mgr1->Get<Foo>();
mgr2->Set(&a);
mgr2->Set(&b);
mgr2->Set(&c);
mgr2->Set(&d);
mgr2->Get<Foo>();
}
It is possible to break the cycle with some dynamic_cast
s, but each user of Set
and Get
will still depend on all Manager
classes. This is how templates work in C++. If this is not acceptable, then templates perhaps are not the right tool for this job.