LArSoft  v10_04_05
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 demangled type name. More...
 
template<typename T >
static std::string type_name (T const *)
 Convert a pointer to object into a demangled 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 162 of file ProviderList.h.

Member Typedef Documentation

Type of smart pointer to typed list element.

Definition at line 179 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 176 of file ProviderList.h.

using testing::ProviderList::key_type = size_t
private

type used for key in the internal registry

Definition at line 386 of file ProviderList.h.

Type of objects contained in the list.

Definition at line 172 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 169 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 266 of file ProviderList.h.

267  {
268  auto k = key<T>(label); // key
269  auto it = data.find(k);
270  if (it != data.end()) return false;
271 
272  pointer_t ptr = std::make_unique<concrete_type_t<T>>(std::move(obj_ptr));
273  data.emplace_hint(it, std::move(k), std::move(ptr));
274  return true;
275  } // acquire()
smart_pointer_t< details::MovableClassWrapperBase > pointer_t
Type of objects contained in the list.
Definition: ProviderList.h:172
std::unordered_map< key_type, pointer_t > data
all our singletons
Definition: ProviderList.h:388
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 228 of file ProviderList.h.

229  {
230  return custom_setup_instance<T, SetupProc, Args...>(
231  "", std::forward<SetupProc>(provSetup), std::forward<Args>(args)...);
232  } // custom_setup()
bool custom_setup_instance(std::string label, SetupProc &&provSetup, Args &&...args)
Construct and register an object of type T.
Definition: ProviderList.h:215
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 215 of file ProviderList.h.

216  {
217  auto k = key<T>(label); // key
218  auto it = data.find(k);
219  if (it != data.end()) return false;
220 
221  pointer_t ptr = std::make_unique<concrete_type_t<T>>(provSetup(std::forward<Args>(args)...));
222  data.emplace_hint(it, std::move(k), std::move(ptr));
223  return true;
224  } // custom_setup_instance()
smart_pointer_t< details::MovableClassWrapperBase > pointer_t
Type of objects contained in the list.
Definition: ProviderList.h:172
std::unordered_map< key_type, pointer_t > data
all our singletons
Definition: ProviderList.h:388
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 286 of file ProviderList.h.

287  {
288  auto k = key<T>(label); // key
289  auto target_it = data.find(k);
290  if (target_it == data.end()) return false;
291 
292  // erase this and all the aliases pointing to it
293  auto const* target_ptr = target_it->second.get();
294  auto it = data.begin();
295  while (it != data.end()) {
296  if (it->second.get() == target_ptr)
297  it = data.erase(it);
298  else
299  ++it;
300  } // while
301  return true;
302  } // erase()
std::unordered_map< key_type, pointer_t > data
all our singletons
Definition: ProviderList.h:388
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 407 of file ProviderList.h.

408  {
409  return data.find(key<T>(label));
410  }
std::unordered_map< key_type, pointer_t > data
all our singletons
Definition: ProviderList.h:388
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 413 of file ProviderList.h.

414  {
415  return data.find(key<T>(label));
416  }
std::unordered_map< key_type, pointer_t > data
all our singletons
Definition: ProviderList.h:388
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 335 of file ProviderList.h.

336  {
337  return get_elem<T>(label).ref();
338  }
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 341 of file ProviderList.h.

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

Definition at line 420 of file ProviderList.h.

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

Definition at line 434 of file ProviderList.h.

435  {
436  auto it = find<T>(label);
437  if (it == data.end()) throw provider_not_available("Not available: " + type_name<T>());
438  if (!(it->second)) throw provider_deleted("Deleted: " + type_name<T>());
439  auto* ptr = dynamic_cast<details::MovableClassWrapper<T>*>(it->second.get());
440  if (!ptr) {
441  throw provider_wrong("Wrong: " + type_name(it->second.get()) +
442  " [requested: " + type_name<T>() + "]");
443  }
444  return *ptr;
445  } // get_elem()
std::unordered_map< key_type, pointer_t > data
all our singletons
Definition: ProviderList.h:388
static std::string type_name()
Convert a type into a demangled type name.
Definition: ProviderList.h:392
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 358 of file ProviderList.h.

359  {
360  return get_elem<T>(label).get();
361  }
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 364 of file ProviderList.h.

365  {
366  return get_elem<T>(label).get();
367  }
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 449 of file ProviderList.h.

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

Returns whether we have a slot for this object.

Definition at line 372 of file ProviderList.h.

373  {
374  return find<T>(label) != data.end();
375  }
std::unordered_map< key_type, pointer_t > data
all our singletons
Definition: ProviderList.h:388
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 306 of file ProviderList.h.

307  {
308  // find the alias location
309  auto alias_k = key<Alias>(alias_label); // key
310  auto alias_it = data.find(alias_k);
311  if (alias_it != data.end()) return false;
312 
313  // find the original provider location
314  auto prov_elem = get_elem<Prov>(prov_label);
315 
316  // register the shared object to the alias
317  data.emplace_hint(alias_it,
318  std::move(alias_k),
319  std::make_unique<concrete_type_t<Alias>>(
320  prov_elem, typename concrete_type_t<Alias>::share_t()));
321  return true;
322  } // set_alias()
std::unordered_map< key_type, pointer_t > data
all our singletons
Definition: ProviderList.h:388
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 249 of file ProviderList.h.

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

Definition at line 235 of file ProviderList.h.

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

Convert a type into a demangled type name.

Definition at line 392 of file ProviderList.h.

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

Convert a pointer to object into a demangled type name.

Definition at line 399 of file ProviderList.h.

400  {
401  return type_name<T>();
402  }
template<typename T >
bool testing::ProviderList::valid ( std::string  label = "") const
inline

Returns whether the specified object is available.

Definition at line 379 of file ProviderList.h.

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

Member Data Documentation

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

all our singletons

Definition at line 388 of file ProviderList.h.


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