18 #ifndef LARCORE_TESTUTILS_PROVIDERLIST_H 19 #define LARCORE_TESTUTILS_PROVIDERLIST_H 1 28 #include <unordered_map> 82 ptr = std::move(from);
102 datum_t const*
get()
const {
return ptr.get(); }
105 bool valid()
const {
return bool(ptr); }
108 operator bool()
const {
return valid(); }
165 template <
typename T>
172 template <
typename T>
175 template <
typename T>
181 using std::runtime_error::runtime_error;
211 template <
typename T,
typename SetupProc,
typename... Args>
214 auto k = key<T>(label);
215 auto it = data.find(k);
216 if (it != data.end())
return false;
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));
224 template <
typename T,
typename SetupProc,
typename... Args>
227 return custom_setup_instance<T, SetupProc, Args...>(
228 "", std::forward<SetupProc>(provSetup), std::forward<Args>(args)...);
231 template <
typename T,
typename... Args>
234 auto k = key<T>(label);
235 auto it = data.find(k);
236 if (it != data.end())
return false;
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));
247 template <
typename T,
typename... Args>
250 return setup_instance<T>(
"", std::forward<Args>(args)...);
264 template <
typename T>
265 bool acquire(std::unique_ptr<T>&& obj_ptr, std::string label =
"")
267 auto k = key<T>(label);
268 auto it = data.find(k);
269 if (it != data.end())
return false;
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));
284 template <
typename T>
287 auto k = key<T>(label);
288 auto target_it = data.find(k);
289 if (target_it == data.end())
return false;
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)
304 template <
typename Prov,
typename Alias>
305 bool set_alias(std::string alias_label =
"", std::string prov_label =
"")
308 auto alias_k = key<Alias>(alias_label);
309 auto alias_it = data.find(alias_k);
310 if (alias_it != data.end())
return false;
313 auto prov_elem = get_elem<Prov>(prov_label);
316 data.emplace_hint(alias_it,
333 template <
typename T>
334 T
const&
get(std::string label =
"")
const 336 return get_elem<T>(label).ref();
339 template <
typename T>
340 T&
get(std::string label =
"")
342 return get_elem<T>(label).ref();
356 template <
typename T>
359 return get_elem<T>(label).
get();
362 template <
typename T>
365 return get_elem<T>(label).
get();
370 template <
typename T>
371 bool known(std::string label =
"")
const 373 return find<T>(label) != data.end();
377 template <
typename T>
378 bool valid(std::string label =
"")
const 380 auto it = find<T>(label);
381 return (it != data.end()) &&
bool(it->second);
387 std::unordered_map<key_type, pointer_t>
data;
390 template <
typename T>
393 return typeid(T).name();
397 template <
typename T>
400 return typeid(*ptr).name();
405 template <
typename T>
406 auto find(std::string label =
"")
const 408 return data.find(key<T>(label));
411 template <
typename T>
412 auto find(std::string label =
"")
414 return data.find(key<T>(label));
418 template <
typename T>
421 auto it = find<T>(label);
427 " [requested: " + type_name<T>() +
"]");
432 template <
typename T>
435 auto it = find<T>(label);
441 " [requested: " + type_name<T>() +
"]");
447 template <
typename T>
450 return typeid(std::decay_t<T>).hash_code() ^ std::hash<std::string>()(label);
457 #endif // LARCORE_TESTUTILS_PROVIDERLIST_H smart_pointer_t< details::MovableClassWrapperBase > pointer_t
Type of objects contained in the list.
Container of service providers accessed by type and optional label.
T const * getPointer(std::string label="") const
Retrieve the object of type T stored with the specified label.
MovableClassWrapper(MovableClassWrapper< U > const &from, share_t)
Share constructor (kind of copy)
Helper classes to be used together with LArSoft's unit test.
Exception thrown on a request about an unregistered type.
concrete_type_t< T > & get_elem(std::string label="")
Exception thrown on a invalid type request.
MovableClassWrapper(std::unique_ptr< U > &&from)
datum_t const & ref() const
Returns a constant reference to the datum with the correct type.
virtual ~MovableClassWrapperBase()=default
bool known(std::string label="") const
Returns whether we have a slot for this object.
std::unique_ptr< T > smart_pointer_t
type of smart pointer we use to store elements
bool setup_instance(std::string label, Args &&...args)
MovableClassWrapper()
Default constructor: no datum present (move one in later on)
smart_pointer_t< concrete_type_t< T >> concrete_pointer_t
Type of smart pointer to typed list element.
datum_t & ref()
Returns a reference to the datum with the correct type.
bool acquire(std::unique_ptr< T > &&obj_ptr, std::string label="")
Registers and gets ownership of the specified object.
MovableClassWrapper & operator=(pointer_t &&from)
Constructor and assignment from a unique pointer: steal the content.
bool erase(std::string label="")
Drops the object with the specified type and label.
concrete_type_t< T > const & get_elem(std::string label="") const
A base class with a virtual table.
std::shared_ptr< datum_t > pointer_t
pointer storing datum
bool set_alias(std::string alias_label="", std::string prov_label="")
Sets the Alias type as an alias of the Prov provider (with labels)
bool custom_setup_instance(std::string label, SetupProc &&provSetup, Args &&...args)
Construct and register an object of type T.
T * getPointer(std::string label="")
Retrieve the object of type T stored with the specified label.
static std::string type_name(T const *ptr)
Convert a pointer to object into a (ugly) type name.
bool valid(std::string label="") const
Returns whether the specified object is available.
bool custom_setup(SetupProc &&provSetup, Args &&...args)
Construct and register an object of type T with specified arguments.
base exception class for ProviderList
bool setup(Args &&...args)
Construct and register an object of type T with specified arguments.
Exception thrown on when object is not available any more.
auto find(std::string label="")
auto find(std::string label="") const
static key_type key(std::string label="")
Extracts and returns the key out of a type and label.
std::unordered_map< key_type, pointer_t > data
all our singletons
static std::string type_name()
Convert a type into a (ugly) type name.
A class containing an owned object.
size_t key_type
type used for key in the internal registry
cet::coded_exception< error, detail::translate > exception