Created
November 9, 2017 02:14
-
-
Save floatflower/89f5e55044df84b6ecf220e6c7cc6be0 to your computer and use it in GitHub Desktop.
FunctionDispatcher in C++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef FUNCTIONDISPATCHER_H | |
#define FUNCTIONDISPATCHER_H | |
#include <QObject> | |
#include <QHash> | |
#include <functional> | |
class FunctionDispatcher : public QObject | |
{ | |
Q_OBJECT | |
public: | |
FunctionDispatcher(QObject* parent = 0); | |
int functionDispatcher(QString functionName, int dataA, int dataB); | |
int function1(int a, int b); | |
int function2(int a, int b); | |
int function3(int a, int b); | |
int function4(int a, int b); | |
int function5(int a, int b); | |
int function6(int a, int b); | |
int function7(int a, int b); | |
int function8(int a, int b); | |
int function9(int a, int b); | |
int function10(int a, int b); | |
void functionTable(); | |
private: | |
QHash<QString, std::function<int(int, int)>> m_functionsTable; | |
}; | |
#endif // FUNCTIONDISPATCHER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "functiondispatcher.h" | |
#include <QDebug> | |
#include <functional> | |
#define ADD_FUNCTION(_functionName, _function) m_functionsTable.insert(_functionName, std::bind(_function, this, std::placeholders::_1, std::placeholders::_2)); | |
FunctionDispatcher::FunctionDispatcher(QObject *parent) | |
: QObject(parent) | |
{ | |
functionTable(); | |
} | |
int FunctionDispatcher::functionDispatcher(QString functionName, int dataA, int dataB) | |
{ | |
std::function<int(int, int)> func = m_functionsTable.value(functionName); | |
int result = 0; | |
try { | |
result = func(dataA, dataB); | |
} | |
catch(const std::bad_function_call& e) { | |
qDebug() << e.what(); | |
} | |
return result; | |
} | |
int FunctionDispatcher::function1(int a, int b) | |
{ | |
return a + b; | |
} | |
int FunctionDispatcher::function2(int a, int b) | |
{ | |
return a * b; | |
} | |
int FunctionDispatcher::function3(int a, int b) | |
{ | |
return a / b; | |
} | |
int FunctionDispatcher::function4(int a, int b) | |
{ | |
return (a+12) * b; | |
} | |
int FunctionDispatcher::function5(int a, int b) | |
{ | |
return (a+1) * b; | |
} | |
int FunctionDispatcher::function6(int a, int b) | |
{ | |
return (a+2221) * b; | |
} | |
int FunctionDispatcher::function7(int a, int b) | |
{ | |
return (a+31) * b; | |
} | |
int FunctionDispatcher::function8(int a, int b) | |
{ | |
return (a+11) * b; | |
} | |
int FunctionDispatcher::function9(int a, int b) | |
{ | |
return (a+141) * b; | |
} | |
int FunctionDispatcher::function10(int a, int b) | |
{ | |
return (a+12) * b; | |
} | |
void FunctionDispatcher::functionTable() | |
{ | |
ADD_FUNCTION("function1", &FunctionDispatcher::function1); | |
ADD_FUNCTION("function2", &FunctionDispatcher::function2); | |
ADD_FUNCTION("function3", &FunctionDispatcher::function3); | |
ADD_FUNCTION("function4", &FunctionDispatcher::function4); | |
ADD_FUNCTION("function5", &FunctionDispatcher::function5); | |
ADD_FUNCTION("function6", &FunctionDispatcher::function6); | |
ADD_FUNCTION("function7", &FunctionDispatcher::function7); | |
ADD_FUNCTION("function8", &FunctionDispatcher::function8); | |
ADD_FUNCTION("function9", &FunctionDispatcher::function9); | |
ADD_FUNCTION("function10", &FunctionDispatcher::function10); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QCoreApplication> | |
#include "functiondispatcher.h" | |
#include <ctime> | |
#include <QDebug> | |
#include <iostream> | |
int main(int argc, char *argv[]) | |
{ | |
time_t t1 = time(NULL); | |
for (int i = 0; i < 10000000; i ++) { | |
FunctionDispatcher *f = new FunctionDispatcher(); | |
f->functionDispatcher("function1", i, i + 1); | |
f->functionDispatcher("function2", i, i + 1); | |
f->functionDispatcher("function3", i, i + 1); | |
f->functionDispatcher("function4", i, i + 1); | |
f->functionDispatcher("function5", i, i + 1); | |
f->functionDispatcher("function6", i, i + 1); | |
f->functionDispatcher("function7", i, i + 1); | |
f->functionDispatcher("function8", i, i + 1); | |
f->functionDispatcher("function9", i, i + 1); | |
f->functionDispatcher("function10", i, i + 1); | |
delete f; | |
} | |
time_t t2 = time(NULL); | |
qDebug() << t2 - t1; | |
// return a.exec(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment