Last active
March 31, 2017 20:36
-
-
Save DJMcMayhem/dbe5fd0c395212e70450985601f51e96 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <iostream> | |
#include <vector> | |
#include <map> | |
#include <string> | |
#include <functional> | |
class Table | |
{ | |
public: | |
Table() : table_() {} | |
void registerFunction(std::string name, std::function<int()> foo) { | |
table_[name] = foo; | |
} | |
int callByString(std::string s) | |
{ | |
auto iter = table_.find(s); | |
if (iter != table_.end()) | |
return iter->second(); | |
return -1; | |
} | |
private: | |
std::map<std::string, std::function<int()>> table_; | |
}; | |
class Engine | |
{ | |
public: | |
Engine(Table *t) { | |
t->registerFunction("f1", std::bind(&Engine::f1, this)); | |
t->registerFunction("f2", &Engine::f2); | |
} | |
private: | |
int f1() { return 1; }; | |
static int f2() { return 2; }; | |
}; | |
int main() | |
{ | |
Table t; | |
Engine e(&t); | |
std::cout << t.callByString("f1"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not exactly my brace style.
(I use 1TBS)