Skip to content

Instantiation Control

Rodrigo Fernandes edited this page Nov 6, 2013 · 1 revision

Instantiation control allows to specify how the classes which contain the registered operation in answer will be used.

By default, each request will use a single instance, which is destructed after the response is sent. If the contructor takes a long time, because it has to initialize some data, or if one needs to keep information between calls, one should inherit from answer::initialization::Singleton. This will construct the class on load (adapter startup) and will reuse the instance for multiple requests.

class MemoryClass: public answer::instantiation::Singleton{
  std::string _lastCaller;
  public:
    void call(const std::string& name){
      std::cerr << "Hello :" name << std::endl;
      std::cerr << "Previous caller was :" << _lastCaller << std::endl;
      _lastCaller = name;
    }
};

ANSWER_REGISTER_OPERATION(MemoryClass::call);

Another option is answer::initialization::LazySingleton, which will delay calling the contructor until the first request to any of it's member function is required. After initialization the same instance will be used in subsequent requests.

Clone this wiki locally