LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
PolicyNames.cxx
Go to the documentation of this file.
1 
9 // library header
11 
12 // framework libraries
13 #include "cetlib_except/exception.h"
14 
15 // C/C++ standard libraries
16 #include <algorithm> // std::find()
17 #include <vector>
18 #include <string>
19 #include <cassert>
20 
21 
22 // -- BEGIN --- Policy names management ---------------------------------------
23 
24 namespace {
25 
26  std::vector<std::string> BuildPolicyNames() {
27  const char* cnames[] = {
28  #define NURANDOM_SEED_SERVICE_POLICY(x) #x,
30  #undef NURANDOM_SEED_SERVICE_POLICY
31  };
32  return std::vector<std::string>
33  { cnames, cnames + sizeof(cnames)/sizeof(cnames[0]) };
34  } // BuildPolicyNames()
35 
36  std::vector<std::string> PolicyNames { BuildPolicyNames() };
37 
38 } // local namespace
39 
40 // -- END ----- Policy names management ---------------------------------------
41 
42 
43 
44 // -----------------------------------------------------------------------------
45 namespace rndm::details {
46 
47  static_assert(static_cast<unsigned>(Policy::unDefined) == 0,
48  "\"unDefined\" policy needs to be the first one.");
49 
50 
51  //----------------------------------------------------------------------------
52  std::vector<std::string> const& policyNames() { return ::PolicyNames; }
53 
54 
55  //----------------------------------------------------------------------------
56  std::string const& policyName(Policy policy) {
57  std::size_t const index { static_cast<unsigned>(policy) };
58  if (index < policyNames().size()) return policyNames()[index];
59 
60  throw cet::exception("rndm::details::policyName")
61  << "Invalid policy (index #" << index << ")\n";
62 
63  } // policyName()
64 
65 
66  // ---------------------------------------------------------------------------
67  Policy policyFromName(std::string const& policyName) {
68 
69  auto const& names = policyNames();
70  assert(!names.empty());
71  auto const nbegin { begin(names) }, nend { end(names) };
72 
73  auto const iter = std::find(nbegin, nend, policyName);
74 
75  Policy const policy = (iter == nend)
76  ? Policy::unDefined
77  : static_cast<Policy>(std::distance(begin(names), iter))
78  ;
79 
80  if (policy != Policy::unDefined) return policy;
81 
82  // the first policy, `unDefined`, is deliberately omitted from this message
83  cet::exception e { "rndm::details::policyFromName" };
84  e << "rndm::details::policyFromName(\"" << policyName
85  << "\"): unrecognized policy.\nKnown policies are: ";
86  auto iName = nbegin;
87  while (++iName != nend) e << " '" << (*iName) << '\'';
88  throw e << ".\n";
89 
90  } // policyFromName()
91 
92 
93  // ---------------------------------------------------------------------------
94 
95 
96 } // namespace rndm::details
97 
std::vector< std::string > const & policyNames()
Returns a list of names of policies, in the same order as Policy enum.
Definition: PolicyNames.cxx:52
Declaration of policy enumerator and names.
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
#define NURANDOM_SEED_SERVICE_POLICIES
Definition: PolicyNames.h:22
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:77
decltype(auto) constexpr begin(T &&obj)
ADL-aware version of std::begin.
Definition: StdUtils.h:69
Float_t e
Definition: plot.C:35
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