Question

I need some trick to implement c++ unsupportable: i need to mix dynamic and static polymorhism (virtual and template). What i need (some schematic code):

class IManager
{
public:
    template<class T>
    void Set();
    template<class T>
    T *Get();

protected:
    IManager *_parent;
};

class Manager1: public IManager{};
class Manager2: public IManager{};

// the main goal is a semantic:
IManager manager = Manager1;
// IManager manager = Manager2;
manager.Set<MyClass>();
MyClass *myClass = manager.Get<MyClass>();

This is a little bit looks like service locator. but it has some differences and i need a such interface declaration. i already have a such implementation, but without interface (it just configuratable by options in constructor, and i want to separate implementations of each interface)

Update: For now i have 2 implementations: 1. I use interfaces, but it methods are cover all states of template paramteres via arguments. And also i have static function helpers, which convert template parameters and pass them to interfaces as arguments

class Manager: IManager{};
Manager manager;
Helper::Set<T>( manager );
Helper::Get<T>( manager );
  1. i don't use interfaces, but inject all different implementations in one instance and configurate it by constructor arguments. Both solutions are ugly.

    class Manager{ public: Manager( options ); };

Was it helpful?

Solution

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_casts, 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top