![]() |
Home | Libraries | People | FAQ | More |
A central concept on Boost.Application is the 'aspects' that can be added to application context.
User is free to add any aspect to Application Context, and many operations are available (thread-safe) to manipulate the 'aspect pool' of application, refer to 'Application Context (aspect_map class)' reference to know more detail about provided methods.
The application context can be a global (singleton) or local, which passes the 'context' as parameter to constructor of functor.
The user is free to choose the best option to your application design.
Here's a simple example of how to construct a application using global context:
#include <iostream> #include <boost/application.hpp> using namespace boost; // singleton accessinline application::global_context_ptr this_application() { return application::global_context::get(); } // my functor application class myapp { public:
int operator()() { BOOST_APPLICATION_FEATURE_SELECT std::cout << "Test" << std::endl; shared_ptr<application::args> myargs = this_application()->find<application::args>(); if (myargs) { const std::vector<std::string> &arg_vector = myargs->arg_vector(); // only print args on screen for(std::vector<std::string>::const_iterator it = arg_vector.begin(); it != arg_vector.end(); ++it) { std::cout << *it << std::endl; } }
// code your application return 0; } }; // main int main(int argc, char *argv[]) {
application::global_context_ptr ctx = application::global_context::create();
myapp app;
this_application()->insert<application::args>( boost::make_shared<application::args>(argc, argv));
int ret = application::launch<application::common>(app, this_application());
application::global_context::destroy(); return ret; }
Optionally, create a function to give us easy access to global context |
|
Define the application operator (singleton, no param) |
|
Add other application logic |
|
Create a global context application aspect pool |
|
Instantiate your application |
|
Add an aspect for future use. An 'aspect' can be customized, or new aspects can be created |
|
Start the application on the desired mode (common, server) |
|
Destroy the application global context |
Here's a simple example of how to construct a application using local context:
#include <iostream> #include <boost/application.hpp> using namespace boost;class myapp { public:
myapp(application::context& context) : context_(context) { }
int operator()() {
application::csbl::shared_ptr<application::args> myargs = context_.find<application::args>(); if (myargs) { const std::vector<std::string> &arg_vector = myargs->arg_vector(); // only print args on screen for(std::vector<std::string>::const_iterator it = arg_vector.begin(); it != arg_vector.end(); ++it) { std::cout << *it << std::endl; } }
// code your application return 0; } private:
application::context& context_; }; // main int main(int argc, char *argv[]) {
application::context app_context;
myapp app(app_context);
app_context.insert<application::args>( application::csbl::make_shared<application::args>(argc, argv));
return application::launch<application::common>(app, app_context); }
Define application functor class |
|
Define the constructor that will receive a application context |
|
Define the application operator |
|
Make use of an'aspect' |
|
Add other application logic |
|
Application context to hold aspects |
|
Create a context application aspect pool |
|
Instantiate your application |
|
Add an aspect for future use. An 'aspect' can be customized, or new aspects can be created |
|
Start the application on the desired mode (common, server) |