template<typename Service, const std::string & container, const std::string & service_name>
class weak_service_reference< Service, container, service_name >
A utility class to implement a delayed service reference.
This class allows a component to have a "weak" reference to a component service. When the weak reference is initialized the class checks if the service required already has implementations. If it does, the class takes a reference to the default one and calls the supplied function. If there's no implementation of the service the class registers a listener to the dynamic_loader_services_loaded_notification broadcast service by implementing a dynamic_loader_services_loaded_notification service that, when called by the dynamic loader, will take a refernece to the desired service and call the function supplied. And then it sets a flag preventing any further calls to the function.
At deinit time, deinit tries to acquire the foo service and, if successful, calls the supplied function and passes it as a parameter. Note that if the service implementation has been undefined in the meanwhile no call of the deinit supplied function is done.
Normal usage pattern is that the weak_service_reference::init() is called during component initialization.
And weak_service_reference::deinit() is called during the component deinitialization.
- Warning
- Please pass the _no_lock registry variants to the deinit() call! It's because component deinit function is called while the registry lock is held. So trying to take the lock again (which is what the normal registry functions do) is going to lead to a deadlock!
One can expect that the function argument is called either at init() time or asyncronously, possibly from anoher thread, when an implementation of a service is registered.
Typical usage:
...
#include "mysql/components/services/foo.h"
...
mysql_service_registration_no_lock);
weak_foo_service;
...
mysql_minimal_chassis_no_lock,
mysql_service_registration_no_lock),
mysql_service_registry_no_lock),
...
END_COMPONENT_REQUIRES();
bool component_init() {
...
return 0 != foo_svc->define(12);
}))
return 1;
...
}
bool component_deinit() {
...
mysql_service_registry_no_lock, mysql_service_registration_no_lock,
return 0 != foo_svc->undefine(12);
}))
return 1;
static mysql_service_status_t deinit()
Component deinitialization.
Definition: audit_api_message_emit.cc:580
static mysql_service_status_t init()
Component initialization.
Definition: audit_api_message_emit.cc:571
A utility class to implement a delayed service reference.
Definition: weak_service_reference.h:133
static const mysql_service_registry_t * registry
We need to store a reference to the registry since the init callback needs it.
Definition: weak_service_reference.h:145
#define BEGIN_COMPONENT_REQUIRES(name)
A macro to specify requirements of the component.
Definition: component_implementation.h:223
#define REQUIRES_SERVICE(service)
Adds a Service requirement with a pointer to placeholder to the list of components.
Definition: component_implementation.h:305
#define REQUIRES_SERVICE_PLACEHOLDER_AS(service, name)
Create a service placeholder, with an arbitrary name.
Definition: component_implementation.h:296
#define SERVICE_PLACEHOLDER(service)
Use this macro to reference the service placeholder as defined by the REQUIRES_SERVICE_PLACEHOLDER ma...
Definition: component_implementation.h:382
#define REQUIRES_SERVICE_IMPLEMENTATION_AS(service, implementation, name)
Adds a Service implementation requirement with a pointer to placeholder to the list of components.
Definition: component_implementation.h:354
static const std::string c_name("mysql_server")
static const std::string s_name("mysql_option_tracker_option")
const mysql_service_registry_registration_t * registry_registration
#define SERVICE_TYPE(name)
Generates the standard Service type name.
Definition: service.h:76
- Template Parameters
-
Service | This is the type of the service to be called. E.g. SERVICE_TYPE(foo) |
container | The name of the "container". Usually a component name. It has to be a rvalue ref since you would need a distinct set of the static members of the template class for every service/component combo. |
service_name | The name of the service to try to call. It has to be a rvalue ref since you would need a distinct set of the static members of the template class for every service/component combo. |