Last active
June 27, 2017 00:37
-
-
Save Ratstail91/9fe1c25d30c94bc1efe12e987dfd09bf 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
//This isn't really possible with templates, and even without templates it needs refactoring every time you want to add something new. | |
#include <functional> | |
#include <iostream> | |
#include <string> | |
class Switcher { | |
public: | |
Switcher(std::string s); | |
Switcher(int i); | |
Switcher& Case(std::string s, std::function<void()> fn); | |
Switcher& Case(int i, std::function<void()> fn); | |
Switcher& Break(); | |
Switcher& Default(std::function<void()>); | |
}; | |
int main() { | |
Switcher("Hello") | |
.Case("Foobar", [](){ | |
std::cout << "Incorrect" << std::endl; | |
}) | |
.Break() | |
.Case("Hello", [](){ | |
std::cout << "Correct" << std::endl; | |
}) | |
.Case(6, [](){ | |
std::cout << "Number" << std::endl; | |
}) | |
.Break() | |
.Default([](){ | |
std::cout << "Default" << std::endl; | |
}) | |
.Invoke(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment