LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
testing::ProviderList Class Reference

Container of service providers accessed by type and optional label. More...

#include "ProviderList.h"

Classes

struct  exception
 base exception class for ProviderList More...
 
struct  provider_deleted
 Exception thrown on when object is not available any more. More...
 
struct  provider_not_available
 Exception thrown on a request about an unregistered type. More...
 
struct  provider_wrong
 Exception thrown on a invalid type request. More...
 

Public Member Functions

template<typename T , typename SetupProc , typename... Args>
bool custom_setup_instance (std::string label, SetupProc &&provSetup, Args &&...args)
 Construct and register an object of type T. More...
 
template<typename T , typename SetupProc , typename... Args>
bool custom_setup (SetupProc &&provSetup, Args &&...args)
 Construct and register an object of type T with specified arguments. More...
 
template<typename T , typename... Args>
bool setup_instance (std::string label, Args &&...args)
 
template<typename T , typename... Args>
bool setup (Args &&...args)
 Construct and register an object of type T with specified arguments. More...
 
template<typename T >
bool acquire (std::unique_ptr< T > &&obj_ptr, std::string label="")
 Registers and gets ownership of the specified object. More...
 
template<typename T >
bool erase (std::string label="")
 Drops the object with the specified type and label. More...
 
template<typename Prov , typename Alias >
bool set_alias (std::string alias_label="", std::string prov_label="")
 Sets the Alias type as an alias of the Prov provider (with labels) More...
 
template<typename T >
bool known (std::string label="") const
 Returns whether we have a slot for this object. More...
 
template<typename T >
bool valid (std::string label="") const
 Returns whether the specified object is available. More...
 
template<typename T >
T const & get (std::string label="") const
 Retrieve the object of type T stored with the specified label. More...
 
template<typename T >
T & get (std::string label="")
 Retrieve the object of type T stored with the specified label. More...
 
template<typename T >
T const * getPointer (std::string label="") const
 Retrieve the object of type T stored with the specified label. More...
 
template<typename T >
T * getPointer (std::string label="")
 Retrieve the object of type T stored with the specified label. More...
 

Private Types

template<typename T >
using smart_pointer_t = std::unique_ptr< T >
 type of smart pointer we use to store elements More...
 
using pointer_t = smart_pointer_t< details::MovableClassWrapperBase >
 Type of objects contained in the list. More...
 
template<typename T >
using concrete_type_t = details::MovableClassWrapper< std::decay_t< T >>
 Type of list element with explicit element type memory. More...
 
template<typename T >
using concrete_pointer_t = smart_pointer_t< concrete_type_t< T >>
 Type of smart pointer to typed list element. More...
 
using key_type = size_t
 type used for key in the internal registry More...
 

Private Member Functions

template<typename T >
concrete_type_t< T > const & get_elem (std::string label="") const
 
template<typename T >
concrete_type_t< T > & get_elem (std::string label="")
 
template<typename T >
auto find (std::string label="") const
 
template<typename T >
auto find (std::string label="")
 

Static Private Member Functions

template<typename T >
static std::string type_name ()
 Convert a type into a (ugly) type name. More...
 
template<typename T >
static std::string type_name (T const *ptr)
 Convert a pointer to object into a (ugly) type name. More...
 
template<typename T >
static key_type key (std::string label="")
 Extracts and returns the key out of a type and label. More...
 

Private Attributes

std::unordered_map< key_type, pointer_tdata
 all our singletons More...
 

Detailed Description

Container of service providers accessed by type and optional label.


This container is expected to contain elements that are service providers of different types. Each provider is accessed by its class type and an optional instance label to discriminate between providers of the same type.

The list owns the providers. A provider is created with setup() (or setup_instance() if a instance label is needed). This method relies on a class testing::ProvideSetupClass to create and correctly set up the provider. For example, to set up the provider LArPropertiesStandard:

provList.setup<LArPropertiesStandard>(pset);

assuming that LArPropertiesStandard provider has a constructor with as only argument pset (supposedly, a fhicl::ParameterSet). If a custom setup is needed, the methods custom_setup_instance() and custom_setup() take as argument a setup function, which can do whatever it takes to perform the set up.

After a provider is set up, a reference to it can be obtained by get():

auto& larp = provList.get<LArPropertiesStandard>();

If no such class is available, an exception will be thrown. The presence of a provider can be checked beforehand with has().

Note
The presence of multiple providers of the same type, supported via instance names, is not useful in the current art/LArSoft.

How can a provider support the setup() construction method?

A provider can specify its own setup by specialising the class testing::ProvideSetupClass. A default implementation is provided, that constructs the provider with a parameter set.

Definition at line 159 of file ProviderList.h.

Member Typedef Documentation

Type of smart pointer to typed list element.

Definition at line 176 of file ProviderList.h.

template<typename T >
using testing::ProviderList::concrete_type_t = details::MovableClassWrapper<std::decay_t<T>>
private

Type of list element with explicit element type memory.

Definition at line 173 of file ProviderList.h.

using testing::ProviderList::key_type = size_t
private

type used for key in the internal registry

Definition at line 385 of file ProviderList.h.

Type of objects contained in the list.

Definition at line 169 of file ProviderList.h.

template<typename T >
using testing::ProviderList::smart_pointer_t = std::unique_ptr<T>
private

type of smart pointer we use to store elements

Definition at line 166 of file ProviderList.h.

Member Function Documentation

template<typename T >
bool testing::ProviderList::acquire ( std::unique_ptr< T > &&  obj_ptr,
std::string  label = "" 
)
inline

Registers and gets ownership of the specified object.

Template Parameters
Ttype of object being acquired
Parameters
obj_ptrpointer to the object to be acquired
labelname of the object instance
Returns
whether the object was acquired or not

The ProviderList takes ownership of the specified provider. If an object of type T is already registered, the pointer is left untouched and false is returned.

Definition at line 265 of file ProviderList.h.

266  {
267  auto k = key<T>(label); // key
268  auto it = data.find(k);
269  if (it != data.end()) return false;
270 
271  pointer_t ptr = std::make_unique<concrete_type_t<T>>(std::move(obj_ptr));
272  data.emplace_hint(it, std::move(k), std::move(ptr));
273  return true;
274  } // acquire()
smart_pointer_t< details::MovableClassWrapperBase > pointer_t
Type of objects contained in the list.
Definition: ProviderList.h:169
std::unordered_map< key_type, pointer_t > data
all our singletons
Definition: ProviderList.h:387
template<typename T , typename SetupProc , typename... Args>
bool testing::ProviderList::custom_setup ( SetupProc &&  provSetup,
Args &&...  args 
)
inline

Construct and register an object of type T with specified arguments.

Definition at line 225 of file ProviderList.h.

226  {
227  return custom_setup_instance<T, SetupProc, Args...>(
228  "", std::forward<SetupProc>(provSetup), std::forward<Args>(args)...);
229  } // custom_setup()
bool custom_setup_instance(std::string label, SetupProc &&provSetup, Args &&...args)
Construct and register an object of type T.
Definition: ProviderList.h:212
template<typename T , typename SetupProc , typename... Args>
bool testing::ProviderList::custom_setup_instance ( std::string  label,
SetupProc &&  provSetup,
Args &&...  args 
)
inline

Construct and register an object of type T.

Template Parameters
Ttype of the object to be constructed (caller specifies it)
SetupProctype of functor performing the actual setup
Argstype of constructor arguments (compiler fills them in)
Parameters
labelname of this instance of object type T (can be empty)
provSetupfunctor performing the setup
argsarguments to provSetup for the construction of T

An object is instantiated and associates it with the specified instance label. It can then be accessed with a get<T>(label) call.

The functor provSetup is expected to return a unique pointer to the newly created provider, std::unique_ptr<T>.

Definition at line 212 of file ProviderList.h.

213  {
214  auto k = key<T>(label); // key
215  auto it = data.find(k);
216  if (it != data.end()) return false;
217 
218  pointer_t ptr = std::make_unique<concrete_type_t<T>>(provSetup(std::forward<Args>(args)...));
219  data.emplace_hint(it, std::move(k), std::move(ptr));
220  return true;
221  } // custom_setup_instance()
smart_pointer_t< details::MovableClassWrapperBase > pointer_t
Type of objects contained in the list.
Definition: ProviderList.h:169
std::unordered_map< key_type, pointer_t > data
all our singletons
Definition: ProviderList.h:387
template<typename T >
bool testing::ProviderList::erase ( std::string  label = "")
inline

Drops the object with the specified type and label.

Template Parameters
Ttype of object being acquired
Parameters
labelname of the object instance
Returns
whether the object was present or not

If present, the object is destroyed

Definition at line 285 of file ProviderList.h.

286  {
287  auto k = key<T>(label); // key
288  auto target_it = data.find(k);
289  if (target_it == data.end()) return false;
290 
291  // erase this and all the aliases pointing to it
292  auto const* target_ptr = target_it->second.get();
293  auto it = data.begin();
294  while (it != data.end()) {
295  if (it->second.get() == target_ptr)
296  it = data.erase(it);
297  else
298  ++it;
299  } // while
300  return true;
301  } // erase()
std::unordered_map< key_type, pointer_t > data
all our singletons
Definition: ProviderList.h:387
template<typename T >
auto testing::ProviderList::find ( std::string  label = "") const
inlineprivate

Returns an iterator pointing to the requested key, or data.end()

Definition at line 406 of file ProviderList.h.

407  {
408  return data.find(key<T>(label));
409  }
std::unordered_map< key_type, pointer_t > data
all our singletons
Definition: ProviderList.h:387
template<typename T >
auto testing::ProviderList::find ( std::string  label = "")
inlineprivate

Returns an iterator pointing to the requested key, or data.end()

Definition at line 412 of file ProviderList.h.

413  {
414  return data.find(key<T>(label));
415  }
std::unordered_map< key_type, pointer_t > data
all our singletons
Definition: ProviderList.h:387
template<typename T >
T const& testing::ProviderList::get ( std::string  label = "") const
inline

Retrieve the object of type T stored with the specified label.

Template Parameters
Ttype of the object to be retrieved
Parameters
labeloptional label used when the object was inserted
Returns
the specified object as a reference to type T
Exceptions
provider_not_availableno type T class was stored with label
provider_deletedthe object that was stored is not present
provider_wrongthe object is not compatible with the type T

Definition at line 334 of file ProviderList.h.

335  {
336  return get_elem<T>(label).ref();
337  }
template<typename T >
T& testing::ProviderList::get ( std::string  label = "")
inline

Retrieve the object of type T stored with the specified label.

Template Parameters
Ttype of the object to be retrieved
Parameters
labeloptional label used when the object was inserted
Returns
the specified object as a reference to type T
Exceptions
provider_not_availableno type T class was stored with label
provider_deletedthe object that was stored is not present
provider_wrongthe object is not compatible with the type T

Definition at line 340 of file ProviderList.h.

341  {
342  return get_elem<T>(label).ref();
343  }
template<typename T >
concrete_type_t<T> const& testing::ProviderList::get_elem ( std::string  label = "") const
inlineprivate

Definition at line 419 of file ProviderList.h.

420  {
421  auto it = find<T>(label);
422  if (it == data.end()) throw provider_not_available("Not available: " + type_name<T>());
423  if (!(it->second)) throw provider_deleted("Deleted: " + type_name<T>());
424  auto* ptr = dynamic_cast<details::MovableClassWrapper<T>*>(it->second.get());
425  if (!ptr) {
426  throw provider_wrong("Wrong: " + type_name(it->second.get()) +
427  " [requested: " + type_name<T>() + "]");
428  }
429  return *ptr;
430  } // get_elem()
std::unordered_map< key_type, pointer_t > data
all our singletons
Definition: ProviderList.h:387
static std::string type_name()
Convert a type into a (ugly) type name.
Definition: ProviderList.h:391
template<typename T >
concrete_type_t<T>& testing::ProviderList::get_elem ( std::string  label = "")
inlineprivate

Definition at line 433 of file ProviderList.h.

434  {
435  auto it = find<T>(label);
436  if (it == data.end()) throw provider_not_available("Not available: " + type_name<T>());
437  if (!(it->second)) throw provider_deleted("Deleted: " + type_name<T>());
438  auto* ptr = dynamic_cast<details::MovableClassWrapper<T>*>(it->second.get());
439  if (!ptr) {
440  throw provider_wrong("Wrong: " + type_name(it->second.get()) +
441  " [requested: " + type_name<T>() + "]");
442  }
443  return *ptr;
444  } // get_elem()
std::unordered_map< key_type, pointer_t > data
all our singletons
Definition: ProviderList.h:387
static std::string type_name()
Convert a type into a (ugly) type name.
Definition: ProviderList.h:391
template<typename T >
T const* testing::ProviderList::getPointer ( std::string  label = "") const
inline

Retrieve the object of type T stored with the specified label.

Template Parameters
Ttype of the object to be retrieved
Parameters
labeloptional label used when the object was inserted
Returns
the specified object as a pointer to type T
Exceptions
provider_not_availableno type T class was stored with label
provider_deletedthe object that was stored is not present
provider_wrongthe object is not compatible with the type T

Definition at line 357 of file ProviderList.h.

358  {
359  return get_elem<T>(label).get();
360  }
template<typename T >
T* testing::ProviderList::getPointer ( std::string  label = "")
inline

Retrieve the object of type T stored with the specified label.

Template Parameters
Ttype of the object to be retrieved
Parameters
labeloptional label used when the object was inserted
Returns
the specified object as a pointer to type T
Exceptions
provider_not_availableno type T class was stored with label
provider_deletedthe object that was stored is not present
provider_wrongthe object is not compatible with the type T

Definition at line 363 of file ProviderList.h.

364  {
365  return get_elem<T>(label).get();
366  }
template<typename T >
static key_type testing::ProviderList::key ( std::string  label = "")
inlinestaticprivate

Extracts and returns the key out of a type and label.

Definition at line 448 of file ProviderList.h.

449  {
450  return typeid(std::decay_t<T>).hash_code() ^ std::hash<std::string>()(label);
451  }
template<typename T >
bool testing::ProviderList::known ( std::string  label = "") const
inline

Returns whether we have a slot for this object.

Definition at line 371 of file ProviderList.h.

372  {
373  return find<T>(label) != data.end();
374  }
std::unordered_map< key_type, pointer_t > data
all our singletons
Definition: ProviderList.h:387
template<typename Prov , typename Alias >
bool testing::ProviderList::set_alias ( std::string  alias_label = "",
std::string  prov_label = "" 
)
inline

Sets the Alias type as an alias of the Prov provider (with labels)

Definition at line 305 of file ProviderList.h.

306  {
307  // find the alias location
308  auto alias_k = key<Alias>(alias_label); // key
309  auto alias_it = data.find(alias_k);
310  if (alias_it != data.end()) return false;
311 
312  // find the original provider location
313  auto prov_elem = get_elem<Prov>(prov_label);
314 
315  // register the shared object to the alias
316  data.emplace_hint(alias_it,
317  std::move(alias_k),
318  std::make_unique<concrete_type_t<Alias>>(
319  prov_elem, typename concrete_type_t<Alias>::share_t()));
320  return true;
321  } // set_alias()
std::unordered_map< key_type, pointer_t > data
all our singletons
Definition: ProviderList.h:387
template<typename T , typename... Args>
bool testing::ProviderList::setup ( Args &&...  args)
inline

Construct and register an object of type T with specified arguments.

Definition at line 248 of file ProviderList.h.

249  {
250  return setup_instance<T>("", std::forward<Args>(args)...);
251  }
template<typename T , typename... Args>
bool testing::ProviderList::setup_instance ( std::string  label,
Args &&...  args 
)
inline

Definition at line 232 of file ProviderList.h.

233  {
234  auto k = key<T>(label); // key
235  auto it = data.find(k);
236  if (it != data.end()) return false;
237 
238  pointer_t ptr =
239  std::make_unique<concrete_type_t<T>>(setupProvider<T>(std::forward<Args>(args)...));
240  data.emplace_hint(it, std::move(k), std::move(ptr));
241  return true;
242  // return custom_setup_instance<T>
243  // (label, setupProvider<T, Args...>, std::forward<Args>(args)...);
244  } // setup_instance()
smart_pointer_t< details::MovableClassWrapperBase > pointer_t
Type of objects contained in the list.
Definition: ProviderList.h:169
std::unordered_map< key_type, pointer_t > data
all our singletons
Definition: ProviderList.h:387
template<typename T >
static std::string testing::ProviderList::type_name ( )
inlinestaticprivate

Convert a type into a (ugly) type name.

Definition at line 391 of file ProviderList.h.

392  {
393  return typeid(T).name();
394  }
template<typename T >
static std::string testing::ProviderList::type_name ( T const *  ptr)
inlinestaticprivate

Convert a pointer to object into a (ugly) type name.

Definition at line 398 of file ProviderList.h.

399  {
400  return typeid(*ptr).name();
401  }
template<typename T >
bool testing::ProviderList::valid ( std::string  label = "") const
inline

Returns whether the specified object is available.

Definition at line 378 of file ProviderList.h.

379  {
380  auto it = find<T>(label);
381  return (it != data.end()) && bool(it->second);
382  }
std::unordered_map< key_type, pointer_t > data
all our singletons
Definition: ProviderList.h:387

Member Data Documentation

std::unordered_map<key_type, pointer_t> testing::ProviderList::data
private

all our singletons

Definition at line 387 of file ProviderList.h.


The documentation for this class was generated from the following file: