Skip to content

Instantly share code, notes, and snippets.

@Ratstail91
Last active June 27, 2017 00:37
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 Ratstail91/9fe1c25d30c94bc1efe12e987dfd09bf to your computer and use it in GitHub Desktop.
Save Ratstail91/9fe1c25d30c94bc1efe12e987dfd09bf to your computer and use it in GitHub Desktop.
//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