Skip to main content

Add

Register a new service in the handler.

template <typename T, typename... Args>
void Add(Args&&... args);

This method takes a template parameter T which is the service to register.

Parameters​

  • T: The service to register.
  • args: The arguments to pass to the service constructor.

Return value​

  • None

Notes​

info

If you want to get access to the current DependenciesHandler instance in the constructor of the service, you can pass a DependenciesHandler::Ptr as the first argument of the constructor.

Examples​

class MyServiceWithNoDeps {
public:
void DoSomething() { std::cout << "Doing something" << std::endl; }
};

class MyServiceWithDeps {
public:
explicit MyServiceWithDeps(DependenciesHandler::Ptr handler, std::string identifier)
: handler_(handler), identifier_(identifier) {}

void DoSomething() {
auto service = handler_->Get<MyServiceWithNoDeps>();
service.DoSomething();
}

private:
DependenciesHandler::Ptr handler_;
std::string indentifer_;
};

int main() {
auto handler = DependenciesHandler::Create();

handler->Add<MyServiceWithNoDeps>();
handler->Add<MyServiceWithDeps>("MyServiceWithDeps");

return 0;
}