void App::setup() {
bt_rest.function("startManufacturing", startManufacturing);
}
int App::startManufacturing(String command) {
...
return 1;
}
// Functions array
uint8_t functions_index;
int (*functions[NUMBER_FUNCTIONS])(String);
char * functions_names[NUMBER_FUNCTIONS];
void function(char * function_name, int (*f)(String)){
functions_names[functions_index] = function_name;
functions[functions_index] = f;
functions_index++;
}
#include<iostream>
#include<string>
/*
* Будем считать, что std::string это String
*/
#define NUMBER_FUNCTIONS 10
struct aRest
{
uint8_t functions_index;
int (*functions[NUMBER_FUNCTIONS])(std::string);
char* functions_names[NUMBER_FUNCTIONS];
//...
aRest() : functions_index(0){};
void function(char * function_name, int (*f)(std::string))
{
functions_names[functions_index] = function_name;
functions[functions_index] = f;
functions_index++;
}
};
class App
{
aRest bt_rest;
public:
explicit App() : bt_rest(){};
//...
int startManufacturing(std::string command);
void setup();
void call(std::string s);// Для теста. Там то же по другому...
//...
};
int App::startManufacturing(std::string command)
{
std::cout << "startManufacturing\n"
<< "with command " << command
<< " " << this << std::endl;
return 1;
}
void App::setup()
{
bt_rest.function("startManufacturing", (int(*)(std::string))&startManufacturing);
//reinterpret_cast<int(*)(std::string)>(&startManufacturing)
}
void App::call(std::string s)
{
bt_rest.functions[0](s);
}
int main()
{
App ap;
ap.setup();
ap.call("COMMAND");
}
#include<iostream>
#include<string>
#include<functional>
#define NUMBER_FUNCTIONS 10
template<typename T>
struct aRest
{
uint8_t functions_index;
int (T::*functions[NUMBER_FUNCTIONS])(std::string);
char* functions_names[NUMBER_FUNCTIONS];
//...
aRest() : functions_index(0){};
void function(char * function_name, int(T::*f)(std::string))
{
functions_names[functions_index] = function_name;
functions[functions_index] = f;
functions_index++;
}
};
class App
{
aRest<App> bt_rest;
public:
App() : bt_rest(){};
//...
int startManufacturing(std::string command);
void setup();
void call(std::string fn);
//...
};
int App::startManufacturing(std::string command)
{
std::cout << "startManufacturing\n"
<< "with command " << command
<< " " << this << " " << std::endl;
return 1;
}
void App::setup()
{
bt_rest.function("startManufacturing", &startManufacturing);
}
void App::call(std::string s)
{
std::mem_fn(bt_rest.functions[0])(this, s);
}
int main()
{
App ap;
ap.setup();
ap.call("COMMAND");
}
Rest::function(char * function_name, int (App::* f)(String), App& object)
// вместо App можно какой-то интерфейс, который App реализует
…
object.*f("string");
...
bt_rest.function("", &App::startManufacturing, *this);
class App {
static int startManufacturing(String command)
};
Rest::function(char * function_name, int (*f)(String, void*), void*);
void doStartManufacturing(String command, void* closure) {
reinterpret_cast<App*>(closure)->startManufacturing(command);
}
...
bt_rest.function("startManufacturing", doStartManufacturing, this);
App app;
int doStartManufacturing(String command) { return app.startManufacturing(command); }
...
bt_rest.function("startManufacturing", doStartManufacturing);