18 #ifndef LARCORE_TESTUTILS_PROVIDERLIST_H 19 #define LARCORE_TESTUTILS_PROVIDERLIST_H 1 25 #include "cetlib_except/demangle.h" 31 #include <unordered_map> 85 ptr = std::move(from);
105 datum_t const*
get()
const {
return ptr.get(); }
108 bool valid()
const {
return bool(ptr); }
111 operator bool()
const {
return valid(); }
168 template <
typename T>
175 template <
typename T>
178 template <
typename T>
184 using std::runtime_error::runtime_error;
214 template <
typename T,
typename SetupProc,
typename... Args>
217 auto k = key<T>(label);
218 auto it = data.find(k);
219 if (it != data.end())
return false;
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));
227 template <
typename T,
typename SetupProc,
typename... Args>
230 return custom_setup_instance<T, SetupProc, Args...>(
231 "", std::forward<SetupProc>(provSetup), std::forward<Args>(args)...);
234 template <
typename T,
typename... Args>
237 auto k = key<T>(label);
238 auto it = data.find(k);
239 if (it != data.end())
return false;
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));
248 template <
typename T,
typename... Args>
251 return setup_instance<T>(
"", std::forward<Args>(args)...);
265 template <
typename T>
266 bool acquire(std::unique_ptr<T>&& obj_ptr, std::string label =
"")
268 auto k = key<T>(label);
269 auto it = data.find(k);
270 if (it != data.end())
return false;
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));
285 template <
typename T>
288 auto k = key<T>(label);
289 auto target_it = data.find(k);
290 if (target_it == data.end())
return false;
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)
305 template <
typename Prov,
typename Alias>
306 bool set_alias(std::string alias_label =
"", std::string prov_label =
"")
309 auto alias_k = key<Alias>(alias_label);
310 auto alias_it = data.find(alias_k);
311 if (alias_it != data.end())
return false;
314 auto prov_elem = get_elem<Prov>(prov_label);
317 data.emplace_hint(alias_it,
334 template <
typename T>
335 T
const&
get(std::string label =
"")
const 337 return get_elem<T>(label).ref();
340 template <
typename T>
341 T&
get(std::string label =
"")
343 return get_elem<T>(label).ref();
357 template <
typename T>
360 return get_elem<T>(label).
get();
363 template <
typename T>
366 return get_elem<T>(label).
get();
371 template <
typename T>
372 bool known(std::string label =
"")
const 374 return find<T>(label) != data.end();
378 template <
typename T>
379 bool valid(std::string label =
"")
const 381 auto it = find<T>(label);
382 return (it != data.end()) &&
bool(it->second);
388 std::unordered_map<key_type, pointer_t>
data;
391 template <
typename T>
394 return cet::demangle_symbol(
typeid(T).name());
398 template <
typename T>
401 return type_name<T>();
406 template <
typename T>
407 auto find(std::string label =
"")
const 409 return data.find(key<T>(label));
412 template <
typename T>
413 auto find(std::string label =
"")
415 return data.find(key<T>(label));
419 template <
typename T>
422 auto it = find<T>(label);
428 " [requested: " + type_name<T>() +
"]");
433 template <
typename T>
436 auto it = find<T>(label);
442 " [requested: " + type_name<T>() +
"]");
448 template <
typename T>
451 return typeid(std::decay_t<T>).hash_code() ^ std::hash<std::string>()(label);
458 #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.
static std::string type_name(T const *)
Convert a pointer to object into a demangled type name.
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.
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 demangled 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