Choose at runtime which CRTP implementation to use

Viewed 494

I would like to use static polymorphism using the CRTP idiom while being able to choose at runtime which implementation to use. Let me take an example:

I have some classes responsible for computing stuff:

template<typename Implementation>
class FooInterface {
public:
  void compute(){
    (static_cast<Implementation*>(this))->compute();
  }
};

class FooForward : public FooInterface<FooForward> {
public:
  void compute(){
    //do stuff
  }
};

class FooBackward : public FooInterface<FooBackward> {
public:
  void compute(){
    //do other stuff
  }
};

and

template<typename Implementation>
class BarInterface {
public:
  void eval(){
    (static_cast<Implementation*>(this))->eval();
  }
};

class BarForward : public BarInterface<BarForward> {
public:
  void eval(){
    //do something
  }
};

class BarBackward : public BarInterface<BarBackward> {
public:
  void eval(){
    //do something else
  }
};

Now I want to use these objects as members of another class, let's call it Model, and use them in a loop:

template<typename Foo, typename Bar>
class Model {
private:
  Foo* foo_;
  Bar* bar_;
  int max_iter_;

public:
  Model<Foo, Bar>(int max_iter) : max_iter_(max_iter){
    foo_ = new Foo();
    bar_ = new Bar();
  }

  void solve(){
    for(int i = 0; i < max_iter_; ++i){
      foo_->compute();
      bar_->eval();
    }
  }
};

Note that the function Model::solve() performs a high number of iterations and performance in my application is critical, hence the use of CRTP instead of dynamic polymorphism to avoid virtual function calls and enable inlining of the functions by the compiler.

Now my problem arises when I want to let the user decide which implementation of FooInterface and BarInterface to use at runtime. In my main.cpp I have:

int main(int argc, char** argv){
  /*
   * Here an input file is read into a map which looks like this
   * std::map<std::string, std::string> settings
   */
  // Here I need a way to choose, based on settings, what will Foo and Bar be
  Model<Foo, Bar> model;
  model.solve();
}

I have thought about a kind of factory that could return the correct Model but I don't know what the return type could be, and the way I imagine it is not convenient because in my application I have more than 2 template parameters and then the number of combinations becomes very large

class Factory{
  /*type?*/ createModel(std::map<std::string, std::string> settings){
    if ((settings["foo"] == "fwd") && (settings["bar"] == "fwd")){
      Model<FooForward, BarForward>* model = new Model<FooForward, BarForward>();
      return model;
    }
    else if ((settings["foo"] == "fwd") && (settings["bar"] == "bwd")){
      Model<FooForward, BarBackward>* model = new Model<FooForward, BarBackward>();
      return model;
    }
    else if ((settings["foo"] == "bwd") && (settings["bar"] == "fwd")){
      Model<FooBackward, BarForward>* model = new Model<FooBackward, BarForward>();
      return model;
    }
    else {
      Model<FooBackward, BarBackward>* model = new Model<FooBackward, BarBackward>();
      return model;
    }
  }
};

The way I imagine things, all the template combinations would be compiled and the user could choose at runtime which one to use. Is there a way to accomplish this using CRTP ?

1 Answers
Related