Created
December 20, 2017 17:34
-
-
Save vczh/b26f5d50cc3de2ffd327d5536352f3a8 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 <iostream> | |
#include <functional> | |
#include <string> | |
#include <vector> | |
using namespace std; | |
class Interceptor | |
{ | |
using THandler = function<void(string, function<void(string)>)>; | |
vector<THandler> handlers; | |
void Next(string data, int index) | |
{ | |
if (index < handlers.size()) | |
{ | |
handlers[index](data, [=](auto newData) {Next(newData, index + 1); }); | |
} | |
} | |
public: | |
Interceptor& Child(THandler handler) | |
{ | |
handlers.push_back(handler); | |
return *this; | |
} | |
void Handle(string data) | |
{ | |
Next(data, 0); | |
} | |
}; | |
int main() | |
{ | |
Interceptor interceptor; | |
interceptor | |
.Child([](auto data, auto next) {next("[1]" + data); }) | |
.Child([](auto data, auto next) {next("[2]" + data); }) | |
.Child([](auto data, auto next) {cout << data << endl; }) | |
; | |
interceptor.Handle("text"); | |
return 0; | |
} |
IronsDu
commented
Jan 10, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment