Created
November 7, 2016 01:57
-
-
Save Chaosed0/400abacab2baac6eb0593ab1326f9d6b to your computer and use it in GitHub Desktop.
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 "Callbacks.h" | |
void CallbackMap::setCallback(const std::string& callbackName, Callback callback) | |
{ | |
map[callbackName] = callback; | |
} | |
CallbackMap::Error CallbackMap::call(const std::string& callbackName, const std::string& args) | |
{ | |
auto iter = map.find(callbackName); | |
if (iter == map.end()) { | |
return CALLBACK_NOT_FOUND; | |
} | |
bool success = iter->second(args); | |
if (!success) { | |
return CALLBACK_BAD_ARGS; | |
} | |
return CALLBACK_NO_ERROR; | |
} | |
Callback CallbackMap::defineCallback(std::function<void(void)> func) | |
{ | |
auto f = [func](const std::string& args) { | |
func(); | |
return true; | |
}; | |
return f; | |
} |
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
#pragma once | |
#include <unordered_map> | |
#include <vector> | |
#include <functional> | |
#include <sstream> | |
typedef std::function<bool(const std::string& args)> Callback; | |
class CallbackMap | |
{ | |
public: | |
enum Error { | |
CALLBACK_NOT_FOUND, | |
CALLBACK_BAD_ARGS, | |
CALLBACK_NO_ERROR | |
}; | |
void setCallback(const std::string& callbackName, Callback callback); | |
Error call(const std::string& callbackName, const std::string& args); | |
static Callback defineCallback(std::function<void(void)> func); | |
template <class T1> | |
static Callback defineCallback(std::function<void(T1)> func); | |
template <class T1, class T2> | |
static Callback defineCallback(std::function<void(T1,T2)> func); | |
template <class T1, class T2, class T3> | |
static Callback defineCallback(std::function<void(T1,T2,T3)> func); | |
private: | |
std::unordered_map<std::string, Callback> map; | |
}; | |
template <class T1> | |
Callback CallbackMap::defineCallback(std::function<void(T1)> func) | |
{ | |
auto f = [func](const std::string& args) { | |
T1 v1; | |
std::stringstream sstream; | |
sstream << args; | |
sstream >> v1; | |
if (sstream.fail()) { | |
return false; | |
} | |
func(v1); | |
return true; | |
}; | |
return f; | |
} | |
template <class T1, class T2> | |
Callback CallbackMap::defineCallback(std::function<void(T1,T2)> func) | |
{ | |
auto f = [func](const std::string& args) { | |
T1 v1; | |
T2 v2; | |
std::stringstream sstream; | |
sstream << args; | |
sstream >> v1 >> v2; | |
if (sstream.fail()) { | |
return false; | |
} | |
func(v1,v2); | |
return true; | |
}; | |
return f; | |
} | |
template <class T1, class T2, class T3> | |
Callback CallbackMap::defineCallback(std::function<void(T1,T2,T3)> func) | |
{ | |
auto f = [func](const std::string& args) { | |
T1 v1; | |
T2 v2; | |
T3 v3; | |
std::stringstream sstream; | |
sstream << args; | |
sstream >> v1 >> v2 >> v3; | |
if (sstream.fail()) { | |
return false; | |
} | |
func(v1,v2,v3); | |
return true; | |
}; | |
return f; | |
} |
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
map->setCallback("exit", CallbackMap::defineCallback(std::bind(&Game::exit, this))); | |
map->setCallback("wireframe", CallbackMap::defineCallback<bool>(std::bind(&Game::setWireframe, this, std::placeholders::_1))); | |
map->setCallback("noclip", CallbackMap::defineCallback<bool>(std::bind(&Game::setNoclip, this, std::placeholders::_1))); | |
map->setCallback("enableBulletDebugDraw", CallbackMap::defineCallback<bool>(std::bind(&Game::setBulletDebugDraw, this, std::placeholders::_1))); | |
map->setCallback("refreshBulletDebugDraw", CallbackMap::defineCallback(std::bind(&Game::refreshBulletDebugDraw, this))); | |
map->setCallback("restart", CallbackMap::defineCallback(std::bind(&Game::restartGame, this))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment