, .
class Manager {
public:
typedef int ID;
template <typename Function>
static void save (Function * f, ID id) {
functions <Function> () .add (id, f);
}
template <typename Function>
static Function * get (ID id) {
return functions <Function> () .get (id);
}
private:
template <typename Function>
class FunctionStore {
public:
void add (Function *, ID);
Function * get (ID);
private:
std :: map <ID, Function *> m_functions;
};
typedef std :: map <std :: type_index, void *> Store;
static Store m_store;
template <typename Function>
FunctionStore <Function> & functions () {
FunctionStore <Function> * fs;
Store :: iterator i = m_store .find (typeid Function);
if (m_store .end () == i) {
fs = new FunctionStore <Function> ();
m_store [typeid Function] = fs;
}
else {
fs = static_cast <FunctionStore<Function>*> (i -> second);
}
return *fs;
}
};
void foo1 (Foo *);
void bar1 (Bar *);
void foo2 (Foo *);
void bar2 (Bar *);
void init () {
Manager :: save (foo1, 1);
Manager :: save (foo2, 2);
Manager :: save (bar1, 1);
Manager :: save (bar2, 2);
Manager :: get <void(Foo*)> (1) (new Foo ());
Manager :: get <void(Foo*)> (1) (new Bar ());
Manager :: get <void(Bar*)> (2) (new Bar ());
}
m_store (/ void Manager::Store), Manager , - m_store. , , Function.
void init () {
Manager <void(Foo*)> :: save (foo1, 1);
Manager <void(Foo*)> :: save (foo2, 2);
Manager <void(Foo*)> :: save (bar1, 1);
Manager <void(Bar*)> :: save (bar1, 1);
Manager <void(Bar*)> :: save (bar2, 2);
Manager <void(Foo*)> :: get (1) (new Foo ());
Manager <void(Foo*)> :: get (1) (new Bar ());
Manager <void(Bar*)> :: get (2) (new Bar ());
}
init , : , , , . , , .