18 #ifndef LARCORE_TESTUTILS_PROVIDERLIST_H 19 #define LARCORE_TESTUTILS_PROVIDERLIST_H 1 25 #include <unordered_map> 82 { ptr = std::move(from);
return *
this; }
101 datum_t const*
get()
const {
return ptr.get(); }
104 bool valid()
const {
return bool(ptr); }
107 operator bool()
const {
return valid(); }
166 template <
typename T>
173 template <
typename T>
176 template <
typename T>
182 {
using std::runtime_error::runtime_error; };
209 template <
typename T,
typename SetupProc,
typename... Args>
210 bool custom_setup_instance
211 (std::string
label, SetupProc&& provSetup, Args&&... args)
213 auto k = key<T>(
label);
214 auto it = data.find(k);
215 if (it != data.end())
return false;
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));
224 template <
typename T,
typename SetupProc,
typename... Args>
227 return custom_setup_instance<T, SetupProc, Args...>(
229 std::forward<SetupProc>(provSetup),
230 std::forward<Args>(args)...
234 template <
typename T,
typename... Args>
236 (std::string label, Args&&... args)
238 auto k = key<T>(
label);
239 auto it = data.find(k);
240 if (it != data.end())
return false;
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));
251 template <
typename T,
typename... Args>
253 {
return setup_instance<T>(
"", std::forward<Args>(args)...); }
267 template <
typename T>
268 bool acquire(std::unique_ptr<T>&& obj_ptr, std::string label =
"")
270 auto k = key<T>(
label);
271 auto it = data.find(k);
272 if (it != data.end())
return false;
275 = std::make_unique<concrete_type_t<T>>(std::move(obj_ptr));
276 data.emplace_hint(it, std::move(k), std::move(ptr));
289 template <
typename T>
292 auto k = key<T>(
label);
293 auto target_it = data.find(k);
294 if (target_it == data.end())
return false;
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);
308 template <
typename Prov,
typename Alias>
309 bool set_alias(std::string alias_label =
"", std::string prov_label =
"")
312 auto alias_k = key<Alias>(alias_label);
313 auto alias_it = data.find(alias_k);
314 if (alias_it != data.end())
return false;
317 auto prov_elem = get_elem<Prov>(prov_label);
320 data.emplace_hint(alias_it, std::move(alias_k),
338 template <
typename T>
339 T
const&
get(std::string label =
"")
const 340 {
return get_elem<T>(
label).ref(); }
342 template <
typename T>
343 T&
get(std::string label =
"")
344 {
return get_elem<T>(
label).ref(); }
357 template <
typename T>
359 {
return get_elem<T>(
label).
get(); }
361 template <
typename T>
363 {
return get_elem<T>(
label).
get(); }
367 template <
typename T>
368 bool known(std::string label =
"")
const 369 {
return find<T>(
label) != data.end(); }
372 template <
typename T>
373 bool valid(std::string label =
"")
const 375 auto it = find<T>(
label);
376 return (it != data.end()) &&
bool(it->second);
382 std::unordered_map<key_type, pointer_t>
data;
385 template <
typename T>
386 static std::string
type_name() {
return typeid(T).name(); }
389 template <
typename T>
390 static std::string
type_name(T
const* ptr) {
return typeid(*ptr).name(); }
394 template <
typename T>
395 auto find(std::string label =
"")
const 396 {
return data.find(key<T>(label)); }
398 template <
typename T>
399 auto find(std::string label =
"") {
return data.find(key<T>(label)); }
402 template <
typename T>
405 auto it = find<T>(
label);
406 if (it == data.end())
414 +
" [requested: " + type_name<T>() +
"]");
419 template <
typename T>
422 auto it = find<T>(
label);
423 if (it == data.end())
431 +
" [requested: " + type_name<T>() +
"]");
437 template <
typename T>
440 return typeid(std::decay_t<T>).hash_code()
441 ^ std::hash<std::string>()(label);
451 #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
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.
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)
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