LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
testing::TestSharedGlobalResource< RES > Class Template Reference

Utility class providing singleton objects to the derived classes. More...

#include "unit_test_base.h"

Public Types

using ResourcePtr_t = std::shared_ptr< Resource_t >
 

Static Public Member Functions

template<typename... Args>
static ResourcePtr_t CreateResource (std::string res_name, Args &&...args)
 Constructs and registers a new resource with a specified name. More...
 
template<typename... Args>
static void CreateDefaultResource (Args &&...args)
 Constructs and registers a new resource with no name. More...
 
template<typename... Args>
static ResourcePtr_t ProposeSharedResource (std::string res_name, Args &&...args)
 Creates a shared resource only if none exists yet. More...
 
template<typename... Args>
static ResourcePtr_t ProposeDefaultSharedResource (Args &&...args)
 Creates a shared resource as default only if none exists yet. More...
 
static Resource_tDestroyResource (std::string name="")
 Destroys the specified resource (does nothing if no such resource) More...
 
Add and share resources
static void AddSharedResource (std::string res_name, ResourcePtr_t res_ptr)
 Adds a shared resource to the resource registry. More...
 
static void AddDefaultSharedResource (ResourcePtr_t res_ptr)
 Adds a shared resource to the resource registry (empty name) More...
 
template<typename... Args>
static ResourcePtr_t ProvideSharedResource (std::string res_name, ResourcePtr_t res_ptr)
 Registers a shared resource only if none exists yet. More...
 
template<typename... Args>
static ResourcePtr_t ProvideDefaultSharedResource (ResourcePtr_t res_ptr)
 Creates a shared resource as default only if none exists yet. More...
 
static bool ReplaceSharedResource (std::string res_name, Resource_t const *old_res_ptr, ResourcePtr_t res_ptr)
 Adds a shared resource only if it is old_res_ptr. More...
 
static bool ReplaceSharedResource (std::string res_name, ResourcePtr_t old_res_ptr, ResourcePtr_t res_ptr)
 Adds a shared resource to the resource registry. More...
 
static bool ReplaceDefaultSharedResource (Resource_t const *old_res_ptr, ResourcePtr_t res_ptr)
 Adds a shared resource as default resource only if it is old_res_ptr. More...
 
static bool ReplaceDefaultSharedResource (ResourcePtr_t old_res_ptr, ResourcePtr_t res_ptr)
 Adds a shared resource as default resource only if it is old_res_ptr. More...
 
Resource access
static bool hasResource (std::string name="")
 
static ResourcePtr_t ShareResource (std::string name="")
 Retrieves the specified resource for sharing (nullptr if none) More...
 
static Resource_tResource (std::string name="")
 Retrieves the specified resource, or throws if not available. More...
 

Private Types

using Resource_t = RES
 

Static Private Attributes

static std::map< std::string, ResourcePtr_tResources
 

Detailed Description

template<typename RES>
class testing::TestSharedGlobalResource< RES >

Utility class providing singleton objects to the derived classes.


Template Parameters
RESthe type of object (include constantness if needed)

The object is expected to be shared.

Definition at line 385 of file unit_test_base.h.

Member Typedef Documentation

template<typename RES >
using testing::TestSharedGlobalResource< RES >::Resource_t = RES
private

Definition at line 386 of file unit_test_base.h.

template<typename RES >
using testing::TestSharedGlobalResource< RES >::ResourcePtr_t = std::shared_ptr<Resource_t>

Definition at line 389 of file unit_test_base.h.

Member Function Documentation

template<typename RES >
static void testing::TestSharedGlobalResource< RES >::AddDefaultSharedResource ( ResourcePtr_t  res_ptr)
inlinestatic

Adds a shared resource to the resource registry (empty name)

Definition at line 401 of file unit_test_base.h.

402  {
403  AddSharedResource(std::string(), res_ptr);
404  }
static void AddSharedResource(std::string res_name, ResourcePtr_t res_ptr)
Adds a shared resource to the resource registry.
template<typename RES >
static void testing::TestSharedGlobalResource< RES >::AddSharedResource ( std::string  res_name,
ResourcePtr_t  res_ptr 
)
inlinestatic

Adds a shared resource to the resource registry.

Definition at line 395 of file unit_test_base.h.

396  {
397  Resources[res_name] = res_ptr;
398  }
static std::map< std::string, ResourcePtr_t > Resources
template<typename RES >
template<typename... Args>
static void testing::TestSharedGlobalResource< RES >::CreateDefaultResource ( Args &&...  args)
inlinestatic

Constructs and registers a new resource with no name.

Definition at line 464 of file unit_test_base.h.

References testing::details::CommandLineArguments::args.

465  {
466  CreateResource(std::string(), std::forward<Args>(args)...);
467  }
static ResourcePtr_t CreateResource(std::string res_name, Args &&...args)
Constructs and registers a new resource with a specified name.
template<typename RES >
template<typename... Args>
static ResourcePtr_t testing::TestSharedGlobalResource< RES >::CreateResource ( std::string  res_name,
Args &&...  args 
)
inlinestatic

Constructs and registers a new resource with a specified name.

Definition at line 455 of file unit_test_base.h.

References testing::details::CommandLineArguments::args.

456  {
457  ResourcePtr_t res_ptr(new Resource_t(std::forward<Args>(args)...));
458  AddSharedResource(res_name, res_ptr);
459  return res_ptr;
460  }
std::shared_ptr< Resource_t > ResourcePtr_t
static void AddSharedResource(std::string res_name, ResourcePtr_t res_ptr)
Adds a shared resource to the resource registry.
template<typename RES >
static Resource_t& testing::TestSharedGlobalResource< RES >::DestroyResource ( std::string  name = "")
inlinestatic

Destroys the specified resource (does nothing if no such resource)

Definition at line 510 of file unit_test_base.h.

510 { Resources.erase(name); }
static std::map< std::string, ResourcePtr_t > Resources
template<typename RES >
static bool testing::TestSharedGlobalResource< RES >::hasResource ( std::string  name = "")
inlinestatic

Returns whether a resource exists

Exceptions
std::out_of_rangeif not available

Definition at line 491 of file unit_test_base.h.

492  {
493  auto iRes = Resources.find(name);
494  return (iRes != Resources.end()) && bool(iRes->second);
495  }
static std::map< std::string, ResourcePtr_t > Resources
template<typename RES >
template<typename... Args>
static ResourcePtr_t testing::TestSharedGlobalResource< RES >::ProposeDefaultSharedResource ( Args &&...  args)
inlinestatic

Creates a shared resource as default only if none exists yet.

Definition at line 479 of file unit_test_base.h.

References testing::details::CommandLineArguments::args.

480  {
481  return ProposeSharedResource(std::string(), std::forward<Args>(args)...);
482  }
static ResourcePtr_t ProposeSharedResource(std::string res_name, Args &&...args)
Creates a shared resource only if none exists yet.
template<typename RES >
template<typename... Args>
static ResourcePtr_t testing::TestSharedGlobalResource< RES >::ProposeSharedResource ( std::string  res_name,
Args &&...  args 
)
inlinestatic

Creates a shared resource only if none exists yet.

Definition at line 471 of file unit_test_base.h.

References testing::details::CommandLineArguments::args.

472  {
473  return hasResource(res_name) ? ResourcePtr_t() :
474  CreateResource(res_name, std::forward<Args>(args)...);
475  }
std::shared_ptr< Resource_t > ResourcePtr_t
STL namespace.
static bool hasResource(std::string name="")
static ResourcePtr_t CreateResource(std::string res_name, Args &&...args)
Constructs and registers a new resource with a specified name.
template<typename RES >
template<typename... Args>
static ResourcePtr_t testing::TestSharedGlobalResource< RES >::ProvideDefaultSharedResource ( ResourcePtr_t  res_ptr)
inlinestatic

Creates a shared resource as default only if none exists yet.

Definition at line 417 of file unit_test_base.h.

418  {
419  return ProvideSharedResource(std::string(), res_ptr);
420  }
static ResourcePtr_t ProvideSharedResource(std::string res_name, ResourcePtr_t res_ptr)
Registers a shared resource only if none exists yet.
template<typename RES >
template<typename... Args>
static ResourcePtr_t testing::TestSharedGlobalResource< RES >::ProvideSharedResource ( std::string  res_name,
ResourcePtr_t  res_ptr 
)
inlinestatic

Registers a shared resource only if none exists yet.

Definition at line 408 of file unit_test_base.h.

409  {
410  if (hasResource(res_name)) return ResourcePtr_t();
411  AddSharedResource(res_name, res_ptr);
412  return res_ptr;
413  }
std::shared_ptr< Resource_t > ResourcePtr_t
static bool hasResource(std::string name="")
static void AddSharedResource(std::string res_name, ResourcePtr_t res_ptr)
Adds a shared resource to the resource registry.
template<typename RES >
static bool testing::TestSharedGlobalResource< RES >::ReplaceDefaultSharedResource ( Resource_t const *  old_res_ptr,
ResourcePtr_t  res_ptr 
)
inlinestatic

Adds a shared resource as default resource only if it is old_res_ptr.

Definition at line 443 of file unit_test_base.h.

444  {
445  return ReplaceSharedResource(std::string(), old_res_ptr, res_ptr);
446  }
static bool ReplaceSharedResource(std::string res_name, Resource_t const *old_res_ptr, ResourcePtr_t res_ptr)
Adds a shared resource only if it is old_res_ptr.
template<typename RES >
static bool testing::TestSharedGlobalResource< RES >::ReplaceDefaultSharedResource ( ResourcePtr_t  old_res_ptr,
ResourcePtr_t  res_ptr 
)
inlinestatic

Adds a shared resource as default resource only if it is old_res_ptr.

Definition at line 447 of file unit_test_base.h.

448  {
449  return ReplaceSharedResource(std::string(), old_res_ptr, res_ptr);
450  }
static bool ReplaceSharedResource(std::string res_name, Resource_t const *old_res_ptr, ResourcePtr_t res_ptr)
Adds a shared resource only if it is old_res_ptr.
template<typename RES >
static bool testing::TestSharedGlobalResource< RES >::ReplaceSharedResource ( std::string  res_name,
Resource_t const *  old_res_ptr,
ResourcePtr_t  res_ptr 
)
inlinestatic

Adds a shared resource only if it is old_res_ptr.

Definition at line 424 of file unit_test_base.h.

427  {
428  ResourcePtr_t current_res_ptr = ShareResource();
429  if (current_res_ptr.get() != old_res_ptr) return false;
430  AddSharedResource(res_name, res_ptr);
431  return true;
432  }
std::shared_ptr< Resource_t > ResourcePtr_t
static ResourcePtr_t ShareResource(std::string name="")
Retrieves the specified resource for sharing (nullptr if none)
static void AddSharedResource(std::string res_name, ResourcePtr_t res_ptr)
Adds a shared resource to the resource registry.
template<typename RES >
static bool testing::TestSharedGlobalResource< RES >::ReplaceSharedResource ( std::string  res_name,
ResourcePtr_t  old_res_ptr,
ResourcePtr_t  res_ptr 
)
inlinestatic

Adds a shared resource to the resource registry.

Definition at line 433 of file unit_test_base.h.

436  {
437  return ReplaceSharedResource(res_name, old_res_ptr.get(), res_ptr);
438  }
static bool ReplaceSharedResource(std::string res_name, Resource_t const *old_res_ptr, ResourcePtr_t res_ptr)
Adds a shared resource only if it is old_res_ptr.
template<typename RES >
static Resource_t& testing::TestSharedGlobalResource< RES >::Resource ( std::string  name = "")
inlinestatic

Retrieves the specified resource, or throws if not available.

Definition at line 505 of file unit_test_base.h.

505 { return *(Resources.at(name).get()); }
static std::map< std::string, ResourcePtr_t > Resources
template<typename RES >
static ResourcePtr_t testing::TestSharedGlobalResource< RES >::ShareResource ( std::string  name = "")
inlinestatic

Retrieves the specified resource for sharing (nullptr if none)

Definition at line 498 of file unit_test_base.h.

499  {
500  auto iRes = Resources.find(name);
501  return (iRes == Resources.end()) ? ResourcePtr_t() : iRes->second;
502  }
std::shared_ptr< Resource_t > ResourcePtr_t
static std::map< std::string, ResourcePtr_t > Resources

Member Data Documentation

template<typename RES >
std::map< std::string, typename TestSharedGlobalResource< RES >::ResourcePtr_t > testing::TestSharedGlobalResource< RES >::Resources
staticprivate

Definition at line 513 of file unit_test_base.h.


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