LArSoft  v07_13_02
Liquid Argon Software toolkit - http://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 389 of file unit_test_base.h.

Member Typedef Documentation

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

Definition at line 390 of file unit_test_base.h.

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

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

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

400  { Resources[res_name] = res_ptr; }
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 459 of file unit_test_base.h.

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

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

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

451  {
452  ResourcePtr_t res_ptr(new Resource_t(std::forward<Args>(args)...));
453  AddSharedResource(res_name, res_ptr);
454  return res_ptr;
455  }
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.

509  { 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 488 of file unit_test_base.h.

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

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

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

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

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

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

410  {
411  if (hasResource(res_name)) return ResourcePtr_t();
412  AddSharedResource(res_name, res_ptr);
413  return res_ptr;
414  }
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  { return ReplaceSharedResource(std::string(), old_res_ptr, res_ptr); }
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 444 of file unit_test_base.h.

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

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

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


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