![]() |
Home | Libraries | People | FAQ | More |
Some applications should only have a single instance running on a given operating system, Boost.Application provides a means to easily achieve this, as:
User need define a unique identity to application. To do this the user need generate a Boost.Uuid.
Optionally, the user can define a callback that will handle if a new instance of application should continue or exit.
If this callback this is not set the default behavior is terminate.
class myapp { public: myapp(application::context& context) : context_(context) { } // param int operator()() { boost::shared_ptr<application::args> args = context_.find<application::args>(); if(args) { std::vector<std::string> &arg_vector = args->arg_vector(); // only print args on screen for(std::vector<std::string>::iterator it = arg_vector.begin(); it != arg_vector.end(); ++it) { std::cout << *it << std::endl; } } context_.find<application::wait_for_termination_request>()->wait(); return 0; }bool instace_aready_running() { char type; do { std::cout << "An instance of this application is already running on " "this operating system!" << std::endl; std::cout << "Do you want to start another? [y/n]" << std::endl; std::cin >> type; } while( !std::cin.fail() && type!='y' && type!='n' ); if(type == 'y') // tell to app to continue. return true; // tell to app to exit. return false; } private: application::context& context_; }; // main int main(int argc, char *argv[]) { application::context app_context; myapp app(app_context); boost::uuids::string_generator gen;
boost::uuids::uuid appuuid = gen("{9F66E4AD-ECA5-475D-8784-4BAA329EF9F2}"); // way 1 /* application::handler<bool>::callback cb = boost::bind<bool>(&myapp::instace_aready_running, &app); app_context.insert<application::limit_single_instance>( boost::make_shared<application::limit_single_instance_default_behaviour>(appuuid, cb)); */ // way 2 app_context.insert<application::limit_single_instance>( boost::make_shared<application::limit_single_instance_default_behaviour>(appuuid, application::handler<bool>::make_callback(app, &myapp::instace_aready_running))); return application::launch<application::common>(app, app_context); }