Class ILLIXR::phonebook
ClassList > ILLIXR > phonebook
A service locator forILLIXR .More...
#include <phonebook.hpp>
Classes
Type | Name |
---|---|
class | service A 'service' that can be registered in the phonebook. |
Public Functions
Type | Name |
---|---|
bool | has_impl () const |
std::shared_ptr< Specific_service > | lookup_impl () const Look up an implementation of Specific_service , which should be registered first. |
void | register_impl (std::shared_ptr< Specific_service > impl) Registers an implementation of baseclass for future calls to lookup. |
Detailed Description
This will be explained through an example: Suppose one dynamically-loaded plugin, A_plugin
, needs a service, B_service
, provided by another, B_plugin
. A_plugin
cannot statically construct a B_service
, because the implementation B_plugin
is dynamically loaded. However, B_plugin
can register an implementation of B_service
when it is loaded, and A_plugin
can lookup that implementation without knowing it.
B_service.hpp
in common
:
class B_service {
public:
virtual void frobnicate(foo data) = 0;
};
B_plugin.hpp
:
class B_impl : public B_service {
public:
virtual void frobnicate(foo data) {
// ...
}
};
void blah_blah(phonebook* pb) {
// Expose `this` as the "official" implementation of `B_service` for this run.
pb->register_impl<B_service>(std::make_shared<B_impl>());
}
A_plugin.cpp
:
#include "B_service.hpp"
void blah_blah(phonebook* pb) {
B_service* b = pb->lookup_impl<B_service>();
b->frobnicate(data);
}
If the implementation of B_service
is not known to A_plugin
(the usual case), B_service should be an [abstract class][2]. In either case
B_serviceshould be in
common`, so both plugins can refer to it.
One could even selectively return a different implementation of B_service
depending on the caller (through the parameters), but we have not encountered the need for that yet.
Public Functions Documentation
function has_impl
template<typename specific_service>
inline bool ILLIXR::phonebook::has_impl () const
function lookup_impl
Look up an implementation of Specific_service
, which should be registered first.
template<typename Specific_service>
inline std::shared_ptr< Specific_service > ILLIXR::phonebook::lookup_impl () const
Safe to be called from any thread.
Do not call delete
on the returned object; it is still managed by phonebook.
Exception:
if
an implementation is not already registered.
function register_impl
Registers an implementation of baseclass
for future calls to lookup.
template<typename Specific_service>
inline void ILLIXR::phonebook::register_impl (
std::shared_ptr< Specific_service > impl
)
Safe to be called from any thread.
The implementation will be owned by phonebook (phonebook calls delete
).
The documentation for this class was generated from the following file /home/friedel/devel/ILLIXR/include/illixr/phonebook.hpp