LArSoft  v06_85_00
Liquid Argon Software toolkit - http://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 160 of file ProviderList.h.

Member Typedef Documentation

Type of smart pointer to typed list element.

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

using testing::ProviderList::key_type = size_t
private

type used for key in the internal registry

Definition at line 380 of file ProviderList.h.

Type of objects contained in the list.

Definition at line 170 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 167 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 268 of file ProviderList.h.

References label.

269  {
270  auto k = key<T>(label); // key
271  auto it = data.find(k);
272  if (it != data.end()) return false;
273 
274  pointer_t ptr
275  = std::make_unique<concrete_type_t<T>>(std::move(obj_ptr));
276  data.emplace_hint(it, std::move(k), std::move(ptr));
277  return true;
278  } // acquire()
smart_pointer_t< details::MovableClassWrapperBase > pointer_t
Type of objects contained in the list.
Definition: ProviderList.h:170
const std::string label
std::unordered_map< key_type, pointer_t > data
all our singletons
Definition: ProviderList.h:382
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  "",
229  std::forward<SetupProc>(provSetup),
230  std::forward<Args>(args)...
231  );
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:211
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 211 of file ProviderList.h.

References label.

212  {
213  auto k = key<T>(label); // key
214  auto it = data.find(k);
215  if (it != data.end()) return false;
216 
217  pointer_t ptr = std::make_unique<concrete_type_t<T>>
218  (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:170
const std::string label
std::unordered_map< key_type, pointer_t > data
all our singletons
Definition: ProviderList.h:382
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 290 of file ProviderList.h.

References label.

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

396  { return data.find(key<T>(label)); }
const std::string label
std::unordered_map< key_type, pointer_t > data
all our singletons
Definition: ProviderList.h:382
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 399 of file ProviderList.h.

399 { return data.find(key<T>(label)); }
const std::string label
std::unordered_map< key_type, pointer_t > data
all our singletons
Definition: ProviderList.h:382
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 339 of file ProviderList.h.

References label.

340  { return get_elem<T>(label).ref(); }
const std::string label
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 343 of file ProviderList.h.

References label.

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

Definition at line 403 of file ProviderList.h.

References label.

404  {
405  auto it = find<T>(label);
406  if (it == data.end())
407  throw provider_not_available("Not available: " + type_name<T>());
408  if (!(it->second))
409  throw provider_deleted("Deleted: " + type_name<T>());
410  auto* ptr = dynamic_cast<details::MovableClassWrapper<T>*>
411  (it->second.get());
412  if (!ptr) {
413  throw provider_wrong("Wrong: " + type_name(it->second.get())
414  + " [requested: " + type_name<T>() + "]");
415  }
416  return *ptr;
417  } // get_elem()
const std::string label
std::unordered_map< key_type, pointer_t > data
all our singletons
Definition: ProviderList.h:382
static std::string type_name()
Convert a type into a (ugly) type name.
Definition: ProviderList.h:386
template<typename T >
concrete_type_t<T>& testing::ProviderList::get_elem ( std::string  label = "")
inlineprivate

Definition at line 420 of file ProviderList.h.

References label.

421  {
422  auto it = find<T>(label);
423  if (it == data.end())
424  throw provider_not_available("Not available: " + type_name<T>());
425  if (!(it->second))
426  throw provider_deleted("Deleted: " + type_name<T>());
427  auto* ptr = dynamic_cast<details::MovableClassWrapper<T>*>
428  (it->second.get());
429  if (!ptr) {
430  throw provider_wrong("Wrong: " + type_name(it->second.get())
431  + " [requested: " + type_name<T>() + "]");
432  }
433  return *ptr;
434  } // get_elem()
const std::string label
std::unordered_map< key_type, pointer_t > data
all our singletons
Definition: ProviderList.h:382
static std::string type_name()
Convert a type into a (ugly) type name.
Definition: ProviderList.h:386
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.

References label.

359  { return get_elem<T>(label).get(); }
const std::string label
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 362 of file ProviderList.h.

References label.

363  { return get_elem<T>(label).get(); }
const std::string label
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 438 of file ProviderList.h.

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

Returns whether we have a slot for this object.

Definition at line 368 of file ProviderList.h.

References label.

369  { return find<T>(label) != data.end(); }
const std::string label
std::unordered_map< key_type, pointer_t > data
all our singletons
Definition: ProviderList.h:382
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 309 of file ProviderList.h.

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

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

Definition at line 236 of file ProviderList.h.

References label.

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

Convert a type into a (ugly) type name.

Definition at line 386 of file ProviderList.h.

386 { return typeid(T).name(); }
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 390 of file ProviderList.h.

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

Returns whether the specified object is available.

Definition at line 373 of file ProviderList.h.

References label.

374  {
375  auto it = find<T>(label);
376  return (it != data.end()) && bool(it->second);
377  }
const std::string label
std::unordered_map< key_type, pointer_t > data
all our singletons
Definition: ProviderList.h:382

Member Data Documentation

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

all our singletons

Definition at line 382 of file ProviderList.h.


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