Public Member Functions | Private Member Functions | Private Attributes | List of all members
fhicl::detail::ValidateThenSet Class Reference

#include <ValidateThenSet.h>

Inheritance diagram for fhicl::detail::ValidateThenSet:
fhicl::detail::ParameterWalker< tt::const_flavor::require_non_const >

Public Member Functions

 ValidateThenSet (fhicl::ParameterSet const &pset, std::set< std::string > const &keysToIgnore)
 
void check_keys ()
 
- Public Member Functions inherited from fhicl::detail::ParameterWalker< tt::const_flavor::require_non_const >
 ParameterWalker ()=default
 
virtual ~ParameterWalker ()=default
 
void walk_over (tt::maybe_const_t< ParameterBase, C > &)
 
bool do_before_action (tt::maybe_const_t< ParameterBase, C > &p)
 
void do_after_action (tt::maybe_const_t< ParameterBase, C > &p)
 
void do_enter_table (tt::maybe_const_t< TableBase, C > &t)
 
void do_exit_table (tt::maybe_const_t< TableBase, C > &t)
 
void do_enter_sequence (tt::maybe_const_t< SequenceBase, C > &s)
 
void do_exit_sequence (tt::maybe_const_t< SequenceBase, C > &s)
 
void do_atom (tt::maybe_const_t< AtomBase, C > &a)
 
void do_delegated_parameter (tt::maybe_const_t< DelegateBase, C > &dp)
 

Private Member Functions

bool before_action (ParameterBase &p) override
 
void after_action (ParameterBase &p) override
 
void enter_sequence (SequenceBase &p) override
 
void delegated_parameter (DelegateBase &) override
 
void enter_table (TableBase &) override
 
void atom (AtomBase &) override
 

Private Attributes

ParameterSet const & pset_
 
std::set< std::stringignorableKeys_
 
std::vector< std::stringuserKeys_
 
std::vector< cet::exempt_ptr< ParameterBase > > missingParameters_
 

Detailed Description

Definition at line 15 of file ValidateThenSet.h.

Constructor & Destructor Documentation

fhicl::detail::ValidateThenSet::ValidateThenSet ( fhicl::ParameterSet const &  pset,
std::set< std::string > const &  keysToIgnore 
)
inline

Definition at line 18 of file ValidateThenSet.h.

20  : pset_{pset}
21  , ignorableKeys_{keysToIgnore}
22  , userKeys_{pset.get_all_keys()}
24  {
26  }
std::set< std::string > ignorableKeys_
std::vector< std::string > userKeys_
void sort_all(RandCont &)
ParameterSet const & pset_
std::vector< cet::exempt_ptr< ParameterBase > > missingParameters_

Member Function Documentation

void fhicl::detail::ValidateThenSet::after_action ( ParameterBase p)
overrideprivate

Definition at line 53 of file ValidateThenSet.cc.

54 {
55  p.set_value(pset_);
56 }
ParameterSet const & pset_
p
Definition: test.py:223
void fhicl::detail::ValidateThenSet::atom ( AtomBase )
inlineoverrideprivate

Definition at line 40 of file ValidateThenSet.h.

41  {}
bool fhicl::detail::ValidateThenSet::before_action ( ParameterBase p)
overrideprivate

Definition at line 17 of file ValidateThenSet.cc.

18 {
19  // 'ConfigPredicate' condition must be satisfied to continue.
20  if (!p.should_use())
21  return false;
22 
23  // Check that key exists; allow defaulted or optional keys to be
24  // absent.
26  if (!pset_.has_key(k) and !cet::search_all(ignorableKeys_, k)) {
27  if (!p.has_default() and !p.is_optional()) {
28  missingParameters_.emplace_back(&p);
29  }
30  return false;
31  }
32 
33  // Sometimes we are able to short-circuit walking the parameters
34  // that belong to a sequence. This is especially helpful for
35  // sequences with many entries of atomic type.
36  if (is_sequence(p.parameter_type()) and p.preset_value(pset_)) {
37  // Remove all entries that match the sequence key and any elements
38  // of that sequence.
39  auto erase_from =
40  std::remove_if(userKeys_.begin(), userKeys_.end(), [&k](auto const& e) {
41  return e.find(k + '[') == 0ull or e == k;
42  });
43  userKeys_.erase(erase_from, userKeys_.cend());
44  return false;
45  }
46 
47  auto erase_from = std::remove(userKeys_.begin(), userKeys_.end(), k);
48  userKeys_.erase(erase_from, userKeys_.cend());
49  return true;
50 }
std::string string
Definition: nybbler.cc:12
std::set< std::string > ignorableKeys_
std::vector< std::string > userKeys_
std::string strip_first_containing_name(std::string const &key)
bool search_all(FwdCont const &, Datum const &)
const double e
ParameterSet const & pset_
p
Definition: test.py:223
bool has_key(std::string const &key) const
std::vector< cet::exempt_ptr< ParameterBase > > missingParameters_
bool is_sequence(std::any const &val)
Definition: coding.h:49
void fhicl::detail::ValidateThenSet::check_keys ( )

Definition at line 189 of file ValidateThenSet.cc.

190 {
191  removeIgnorableKeys(ignorableKeys_, userKeys_, missingParameters_);
192  std::string errmsg;
193  errmsg += fillMissingKeysMsg(missingParameters_);
194  errmsg += fillExtraKeysMsg(pset_, userKeys_);
195  if (!errmsg.empty()) {
197  fullmsg += "\n";
198  fullmsg += errmsg;
199  throw validationException{fullmsg.c_str()};
200  }
201 }
std::string string
Definition: nybbler.cc:12
std::set< std::string > ignorableKeys_
std::vector< std::string > userKeys_
std::string optional_parameter_message(bool const with_comment=true)
ParameterSet const & pset_
std::vector< cet::exempt_ptr< ParameterBase > > missingParameters_
void fhicl::detail::ValidateThenSet::delegated_parameter ( DelegateBase dp)
overrideprivate

Definition at line 83 of file ValidateThenSet.cc.

84 {
85  // A delegated parameter must itself be present, but any nested
86  // parameters do not need to be present since the nested parameters
87  // are potentially validated elsewhere.
88  auto const& name = dp.name();
89  std::string const pattern{fhicl::Name::regex_safe(name) + R"((\.|\[))"};
90  std::regex const r{pattern};
91  auto erase_from =
92  std::remove_if(userKeys_.begin(), userKeys_.end(), [&r](auto const& k) {
93  return std::regex_search(k, r);
94  });
95  userKeys_.erase(erase_from, userKeys_.end());
96 }
static QCString name
Definition: declinfo.cpp:673
std::string string
Definition: nybbler.cc:12
std::vector< std::string > userKeys_
static std::string regex_safe(std::string const &key)
Definition: Name.h:31
std::string pattern
Definition: regex_t.cc:35
void fhicl::detail::ValidateThenSet::enter_sequence ( SequenceBase p)
overrideprivate

Definition at line 61 of file ValidateThenSet.cc.

62 {
63  // Ensure that the supplied parameter represents a sequence.
64  auto const& key = strip_first_containing_name(s.key());
66  throw fhicl::exception(type_mismatch, "error converting to sequence:\n")
67  << "The supplied value of the parameter:\n"
68  << " " << s.key() << '\n'
69  << "does not represent a sequence.\n";
70  }
71 
72  std::regex const r{fhicl::Name::regex_safe(key) + "\\[\\d+\\]"};
73  std::size_t const nElems =
74  std::count_if(userKeys_.begin(), userKeys_.end(), [&r](auto const& k) {
75  return std::regex_match(k, r);
76  });
77  s.prepare_elements_for_validation(nElems);
78 }
std::vector< std::string > userKeys_
bool is_key_to_sequence(std::string const &key) const
Definition: ParameterSet.h:171
std::string strip_first_containing_name(std::string const &key)
ParameterSet const & pset_
def key(type, name=None)
Definition: graph.py:13
static std::string regex_safe(std::string const &key)
Definition: Name.h:31
static QCString * s
Definition: config.cpp:1042
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
void fhicl::detail::ValidateThenSet::enter_table ( TableBase )
inlineoverrideprivate

Definition at line 37 of file ValidateThenSet.h.

38  {}

Member Data Documentation

std::set<std::string> fhicl::detail::ValidateThenSet::ignorableKeys_
private

Definition at line 44 of file ValidateThenSet.h.

std::vector<cet::exempt_ptr<ParameterBase> > fhicl::detail::ValidateThenSet::missingParameters_
private

Definition at line 46 of file ValidateThenSet.h.

ParameterSet const& fhicl::detail::ValidateThenSet::pset_
private

Definition at line 43 of file ValidateThenSet.h.

std::vector<std::string> fhicl::detail::ValidateThenSet::userKeys_
private

Definition at line 45 of file ValidateThenSet.h.


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