Last active
April 15, 2019 02:21
-
-
Save yxw/24297edc2fb022379eef to your computer and use it in GitHub Desktop.
C++11 - bind server and services
This file contains hidden or 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 <string> | |
| #include <iostream> | |
| #include <functional> | |
| class Service | |
| { | |
| public: | |
| void listen( std::function< void ( const std::string&, | |
| const std::string&, | |
| const std::string&, | |
| const std::string& ) > const& login ) | |
| { | |
| std::cout << "in listen\n"; | |
| login("w", "x", "y", "z"); | |
| } | |
| }; | |
| class Threadpool | |
| { | |
| public: | |
| void schedule( const std::function< void ( void ) > const& task ) | |
| { | |
| std::cout << "in schedule\n"; | |
| task(); | |
| } | |
| }; | |
| class Server | |
| { | |
| public: | |
| Server() | |
| { | |
| Threadpool* threadpool = new Threadpool( ); | |
| Service* service = new Service( ); | |
| using namespace std::placeholders; | |
| threadpool->schedule( | |
| bind( &Service::listen, service, | |
| std::function< void ( const std::string&, const std::string&, | |
| const std::string&, const std::string& ) >( | |
| std::bind( &Server::subscribe, this, _1, _2, _3, _4 ) ) ) ); | |
| } | |
| void subscribe( const std::string& username, const std::string& password, | |
| const std::string& hostname, const std::string& data ) | |
| { | |
| // funny stuff | |
| } | |
| }; | |
| int main () | |
| { | |
| Server server; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment