LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
PolicyFactory.h
Go to the documentation of this file.
1 
13 #ifndef NURANDOM_RANDOMUTILS_PROVIDERS_POLICYFACTORY_H
14 #define NURANDOM_RANDOMUTILS_PROVIDERS_POLICYFACTORY_H
15 
16 
17 // nurandom libraries
21 
22 // framework libraries
23 #include "fhiclcpp/ParameterSet.h"
24 #include "cetlib_except/exception.h"
25 
26 // C/C++ standard libraries
27 #include <memory> // std::make_unique()
28 #include <string>
29 
30 
31 // -----------------------------------------------------------------------------
32 namespace rndm::details {
33 
34  // ---------------------------------------------------------------------------
37  template <typename SEED>
38  struct PolicyStruct_t {
39  Policy policy { Policy::unDefined };
40  std::unique_ptr<RandomSeedPolicyBase<SEED>> ptr;
41 
42  operator bool() const { return bool(ptr); }
43  bool operator!() const { return !ptr; }
44  RandomSeedPolicyBase<SEED> const* operator->() const { return ptr.get(); }
45  RandomSeedPolicyBase<SEED>* operator->() { return ptr.get(); }
46 
47  }; // PolicyStruct_t
48 
49 
62  template <typename SEED>
64  std::string const& policyName = config.get<std::string>("policy");
65 
66  // Throws if policy is not recognized.
67  Policy const policy = policyFromName(policyName);
68 
69  switch (policy) {
70  case Policy::autoIncrement:
71  return { policy, std::make_unique<AutoIncrementPolicy<SEED>>(config) };
72  case Policy::linearMapping:
73  return { policy, std::make_unique<LinearMappingPolicy<SEED>>(config) };
74  case Policy::preDefinedOffset:
75  return { policy, std::make_unique<PredefinedOffsetPolicy<SEED>>(config) };
76  case Policy::preDefinedSeed:
77  return { policy, std::make_unique<PredefinedSeedPolicy<SEED>>(config) };
78  case Policy::random:
79  return { policy, std::make_unique<RandomPolicy<SEED>>(config) };
80  case Policy::perEvent:
81  return { policy, std::make_unique<PerEventPolicy<SEED>>(config) };
82  case Policy::unDefined:
83  default:
84  // this should have been prevented by an exception by `policyFromName()`
85  throw cet::exception("rndm::details::makeRandomSeedPolicy")
86  << "Internal error: unknown policy '" << policyName << "'\n";
87  } // switch
88 
89  } // rndm::details::makeRandomSeedPolicy()
90 
91 
92  // ---------------------------------------------------------------------------
93 
94 
95 } // namespace rndm::details
96 
97 
98 #endif // NURANDOM_RANDOMUTILS_PROVIDERS_POLICYFACTORY_H
PolicyStruct_t< SEED > makeRandomSeedPolicy(fhicl::ParameterSet const &config)
Constructs and returns a RandomSeedPolicyBase based on config.
Definition: PolicyFactory.h:63
Declaration of policy enumerator and names.
Forward declaration of random policy classes.
std::unique_ptr< RandomSeedPolicyBase< SEED > > ptr
Definition: PolicyFactory.h:40
Policy
Enumeration of all supported random seed policies.
Definition: PolicyNames.h:38
std::string const & policyName(Policy policy)
Returns the name of the specified policy.
Definition: PolicyNames.cxx:56
RandomSeedPolicyBase< SEED > * operator->()
Definition: PolicyFactory.h:45
RandomSeedPolicyBase< SEED > const * operator->() const
Definition: PolicyFactory.h:44
Interface for a policy implementation.
Definition: BasePolicies.h:43
T get(std::string const &key) const
Definition: ParameterSet.h:314
Defines an interface for random seed assignment policies.
Policy policyFromName(std::string const &policyName)
Returns the policy with the specified name.
Definition: PolicyNames.cxx:67
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33