Skip to content

Instantly share code, notes, and snippets.

@DJMcMayhem
Last active March 31, 2017 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DJMcMayhem/dbe5fd0c395212e70450985601f51e96 to your computer and use it in GitHub Desktop.
Save DJMcMayhem/dbe5fd0c395212e70450985601f51e96 to your computer and use it in GitHub Desktop.
#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;
}
@ckjbgames
Copy link

Not exactly my brace style.
(I use 1TBS)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment