mardi 29 septembre 2015

Executing a specific function based on a std::string

I am trying to write a program that will execute a function based on a string I fetch from a database. Basically what I do is:

// Create an enum
enum AFunc{
 invalidFunction,
 function2,
 function3
}

// have a class handling the functions
struct A
{
  static AFunc resolveStringToFunction(std::string) {...}

  template<int T>
  void execute(...)
  {
     // this may not be called = invalidFunction
  }

  template<>
  void execute<1> (...)
  { 
     // do stuff = function1
  }

  template<>
  void execute<2> (...)
  { 
     // do stuff = function2
  }
};

In my application i do this:

A a;
std::string funcString = getFromDatabase (...) // Not really, but this is abstract
const AFunc funcType   = A::resolveStringToFunction(funcString);

a.execute<funcType>(...);

The problem here is that the compiler does not accept the dynamic calling of a template function, because (as I understood it) it needs to know which function is called by compile time.

Is there any way around this?

Is there a better solution to this problem? Maybe a design pattern?





Aucun commentaire:

Enregistrer un commentaire