0

I'm trying to avoid switch as much as possible and I've come up with this (not) solution. do you know if there is a way to do something like:

enum MyEnum { A, B, C, D, E, F, SIZE=F};

typedef void (*GenericFunction)(int);

template<class EnumType>
struct MyEnumTraits
{
  template<EnumType code, Args... args>
  void doStuff(args);
};

template<class EnumType, int size, MyEnumTrait>
struct Switcher
{
  Switcher()
  {
     //Do compile time initialization by populating functions_
     //doing something like: (pseudo c++ code following)

      // template metaprogramming iteration of the EnumType values: 
      //from the first to the last one (size)

     for(int i; i<size;i++)
       functions_[i] = (GenericFunction) MyEnumTrait::MyFunctionType; //
  }

  GenericFunction functions_[size];
};

template<>
struct MyEnumTraits<MyEnum>
{
  template<>
  void doStuff<A>(int a)
  {
     // do something
  }
};

template<>
struct MyEnumTraits<MyEnum>
{
  void doStuff<B,int,int>(int a, int b)
  {
    // do something else
  }
};

template<>
struct MyEnumTraits<MyEnum>
{
  template<>
  void doStuff<C, std::string>(const std::string& myString)
  {
    // do something else..
  }
};

int main()
{

  Switcher<MyEnum, SIZE> switcher;
  // Next calls will not work because I need a cast.. 
  //is there any magic I can do to avoid the cast?

  // these may work
  switcher.functions_[A](3);
  switcher.functions_[B](1,2);
  switcher.functions_[C]("bla bla");

  // this is probably impossible to do... isn't it?
  MyEnum en = /* get it from cin */;
  switcher.functions_[en](1,2,3);
}

I apologise for the bad indentation, I will try to refactor the code ASAP

[EDIT] Changed the last 3 rows of the code

user3770392
  • 453
  • 5
  • 12
  • 8
    Why do you apologize for the indentation? Why don't you just indent your properly before you hit "submit"? – Kerrek SB Jun 09 '16 at 16:42
  • you're right, I just had to go out in 5 minutes – user3770392 Jun 10 '16 at 07:09
  • I think it is possible. I once asked a question that looks like yours, and had an answer http://stackoverflow.com/questions/37415812/simulate-the-switch-comportment-in-template-specialization – Boiethios Jun 10 '16 at 11:00
  • I think the problem is a bit different because I would like to use functions with a variable number of arguments. I think that if I had to use only functions with the same signature it could have been much easier.. another thing I haven't mentioned before is that this switcher should be efficient like a switch.. too much perhaps.. – user3770392 Jun 10 '16 at 11:10
  • I would like to avoid macros as well, too easy with them ;) I've a different idea every 5 minutes but nothing seems working.. – user3770392 Jun 10 '16 at 11:18
  • I've successfully implemented some code that works using virtual methods.. the problem with this approach is that I have to manually add all the elements, so I can't use compile time initialization.. I can statically initialize it but in this way it's not reusable – user3770392 Jun 10 '16 at 15:48

0 Answers0