ILLIXR: Illinois Extended Reality Testbed
Classes | Public Member Functions | List of all members
ILLIXR::phonebook Class Reference

A service locator for ILLIXR. More...

#include <phonebook.hpp>

Classes

class  service
 A 'service' that can be registered in the phonebook. More...
 

Public Member Functions

template<typename specific_service >
void register_impl (std::shared_ptr< specific_service > impl)
 Registers an implementation of baseclass for future calls to lookup. More...
 
template<typename specific_service >
std::shared_ptr< specific_service > lookup_impl () const
 Look up an implementation of specific_service, which should be registered first. More...
 

Detailed Description

A service locator for ILLIXR.

This will be explained through an exmaple: 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 caseB_serviceshould be incommon`, 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.

Member Function Documentation

◆ lookup_impl()

template<typename specific_service >
std::shared_ptr<specific_service> ILLIXR::phonebook::lookup_impl ( ) const
inline

Look up an implementation of specific_service, which should be registered first.

Safe to be called from any thread.

Do not call delete on the returned object; it is still managed by phonebook.

Exceptions
ifan implementation is not already registered.

◆ register_impl()

template<typename specific_service >
void ILLIXR::phonebook::register_impl ( std::shared_ptr< specific_service >  impl)
inline

Registers an implementation of baseclass for future calls to lookup.

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: