M
M
Mikhail Mironov2016-01-19 13:40:45
C++ / C#
Mikhail Mironov, 2016-01-19 13:40:45

How to save a list of class types, which then (at runtime) should be called in a template parameter?

There is some hierarchy of abstract base classes with a set of concrete classes;

Foo1Base: 
Foo1Concrete1; Foo1Concrete2; ...
Foo2Base:
Foo2Concrete1; Foo2Concrete2; ...

A template factory is used to create them.
template <class T> FooFactory {
void Register(int id);
T* Create(int), 
vector<int> GetAvaliable();
}

Accordingly, usage:
Factory<Foo1Base>.Create(...)
Now there are some other concrete classes
BarBase:
BarConcrete1;BarConcrete2;...

A normal factory is used to create.
Each of the concrete classes uses a specific set of Foo#Base base classes known at compile time.
Even before creating a specific Bar, you need to know which of the Foo base classes will be used in it. Accordingly, in my understanding, the Bar factory has a method that should return a list with the types of parameters used, in order to call the Foo template factory with each of these parameters, i.e.:
Pseudocode:
for( foo_type : vector<FooTypeHolder>) {
auto foos_ = FooFactory <foo_type >.GetAvaliable()
}

How to implement this? Where to dig?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
T
tsarevfs, 2016-01-19
@tsarevfs

www.boost.org/doc/libs/1_59_0/libs/mpl/doc/refmanu... perhaps something from boost mpl will help you. But it is highly discouraged to do this in pruduction code. Such code is usually very expensive to maintain.

M
maaGames, 2016-01-19
@maaGames

Templates are instantiated at compilation. Those. you cannot do anything with templates in runtime, because there are no templates in runtime.

A
AtomKrieg, 2016-01-19
@AtomKrieg

Dig towards variadic template + loop
stackoverflow.com/questions/7230621/how-can-i-iter...

S
Sergey Nizovtsev, 2016-02-16
@snizovtsev

It smacks of dependency injection. Look at https://github.com/boost-experimental/di or https://github.com/google/fruit

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question