mercredi 31 octobre 2018

Can I have an array of lambda pointers in order to hold functions with different types of arguments/return values?

Let's assume I have a class with two member functions:

#include <string>
#include <vector>

typedef string (*MyFunctionPointer)(string);

using namespace std;

class MyClass{
  MyClass(){
    MyCommandServer server;
    server.addToCollection(&[](string a)->string{return helloWorld();});
    server.addToCollection(&[](string a)->string{return to_string(add(stoi(a[0]),stoi(a[1])));});

    while(true){
      server.pollForCommand();
      doSomethingElse();
    }
  }

  string helloWorld(){
    return "Hello World!";
  }

  int add(int a, int b){
    return a+b;
  }
};

And another Class that is instantiated in the first one:

class MyCommandServer{
  MyCommandServer(){}

  void addToCollection(MyFunctionPointer ptr){
    functionCollection.push_back(ptr);
  }

  string callFunctionByIndex(char index, float arg1, int arg2){
    functionCollection[index](arg1,arg2);
  }

  void pollForCommand(){
    string newCommand = checkForCommand(&args);
    if(newCommand.size()&&isValidCommand(newCommand[0])){
      functionCollection[newCommand[0]](newCommand.substr(1));
    }
  }

  string checkForCommand();
  bool isValidCommand(char);
  vector<MyFunctionPointer> functionCollection;
};

How would I go about passing my first two methods to addToCollection(MyFunctionPointer)? I was wondering if something along the lines of the example was possible. My goal is to have a way to call the functions of MyClass with only their index in the container, and figuring out how to use the provided arguments, if at all, by means of the supplied lambda function. This is supposed to enable a Command Server class, where I get a char as the said index and the arguments, if any, as a string via UDP.

However, the compiler won't let me pass a reference to a lambda..

Making a switch-case statement in the MyServerClass, where I manually enter the code I put in the lambdas in the example, is not feasible since the Server class does not know about its parents methods.

Is something similar to the example given possible?

Sorry if the post is not up to standards, its my first on here.

Thanks for your help!

Aucun commentaire:

Enregistrer un commentaire