Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Application Context

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 access

1inline application::global_context_ptr this_application() {
   return application::global_context::get();
}

// my functor application

class myapp
{
public:

   2int 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;
         }
      }


      3// code your application

      return 0;
   }

};

// main

int main(int argc, char *argv[])
{
   4application::global_context_ptr ctx = application::global_context::create();

   5myapp app;

   6this_application()->insert<application::args>(
      boost::make_shared<application::args>(argc, argv));

   7int ret = application::launch<application::common>(app, this_application());

   8application::global_context::destroy();

   return ret;
}

1

Optionally, create a function to give us easy access to global context

2

Define the application operator (singleton, no param)

3

Add other application logic

4

Create a global context application aspect pool

5

Instantiate your application

6

Add an aspect for future use. An 'aspect' can be customized, or new aspects can be created

7

Start the application on the desired mode (common, server)

8

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;

1class myapp
{
public:

   2myapp(application::context& context)
      : context_(context)
   {
   }

   3int operator()()
   {

      4application::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;
         }
      }

      5// code your application

      return 0;
   }

private:

   6application::context& context_;

};

// main

int main(int argc, char *argv[])
{
   7application::context app_context;

   8myapp app(app_context);

   9app_context.insert<application::args>(
      application::csbl::make_shared<application::args>(argc, argv));

   10return application::launch<application::common>(app, app_context);
}

1

Define application functor class

2

Define the constructor that will receive a application context

3

Define the application operator

4

Make use of an'aspect'

5

Add other application logic

6

Application context to hold aspects

7

Create a context application aspect pool

8

Instantiate your application

9

Add an aspect for future use. An 'aspect' can be customized, or new aspects can be created

10

Start the application on the desired mode (common, server)


PrevUpHomeNext