LArSoft  v10_04_05
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 383 of file unit_test_base.h.

Member Typedef Documentation

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

Definition at line 384 of file unit_test_base.h.

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

Definition at line 387 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 399 of file unit_test_base.h.

400  {
401  AddSharedResource(std::string(), res_ptr);
402  }
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 393 of file unit_test_base.h.

394  {
395  Resources[res_name] = res_ptr;
396  }
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 462 of file unit_test_base.h.

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

463  {
464  CreateResource(std::string(), std::forward<Args>(args)...);
465  }
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 453 of file unit_test_base.h.

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

454  {
455  ResourcePtr_t res_ptr(new Resource_t(std::forward<Args>(args)...));
456  AddSharedResource(res_name, res_ptr);
457  return res_ptr;
458  }
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 508 of file unit_test_base.h.

508 { 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 489 of file unit_test_base.h.

490  {
491  auto iRes = Resources.find(name);
492  return (iRes != Resources.end()) && bool(iRes->second);
493  }
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 477 of file unit_test_base.h.

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

478  {
479  return ProposeSharedResource(std::string(), std::forward<Args>(args)...);
480  }
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 469 of file unit_test_base.h.

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

470  {
471  return hasResource(res_name) ? ResourcePtr_t() :
472  CreateResource(res_name, std::forward<Args>(args)...);
473  }
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 415 of file unit_test_base.h.

416  {
417  return ProvideSharedResource(std::string(), res_ptr);
418  }
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 406 of file unit_test_base.h.

407  {
408  if (hasResource(res_name)) return ResourcePtr_t();
409  AddSharedResource(res_name, res_ptr);
410  return res_ptr;
411  }
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 441 of file unit_test_base.h.

442  {
443  return ReplaceSharedResource(std::string(), old_res_ptr, res_ptr);
444  }
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 445 of file unit_test_base.h.

446  {
447  return ReplaceSharedResource(std::string(), old_res_ptr, res_ptr);
448  }
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 422 of file unit_test_base.h.

425  {
426  ResourcePtr_t current_res_ptr = ShareResource();
427  if (current_res_ptr.get() != old_res_ptr) return false;
428  AddSharedResource(res_name, res_ptr);
429  return true;
430  }
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 431 of file unit_test_base.h.

434  {
435  return ReplaceSharedResource(res_name, old_res_ptr.get(), res_ptr);
436  }
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 503 of file unit_test_base.h.

503 { 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 496 of file unit_test_base.h.

497  {
498  auto iRes = Resources.find(name);
499  return (iRes == Resources.end()) ? ResourcePtr_t() : iRes->second;
500  }
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 511 of file unit_test_base.h.


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