Skip to content

Instantly share code, notes, and snippets.

@yxw
Last active April 15, 2019 02:21
Show Gist options
  • Select an option

  • Save yxw/24297edc2fb022379eef to your computer and use it in GitHub Desktop.

Select an option

Save yxw/24297edc2fb022379eef to your computer and use it in GitHub Desktop.
C++11 - bind server and services
#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