LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
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 (ParameterSet const &pset, std::set< std::string > const &keysToIgnore)
 
 ~ValidateThenSet ()
 
void check_keys ()
 
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::string > ignorableKeys_
 
std::vector< std::string > userKeys_
 
std::vector< cet::exempt_ptr< ParameterBase > > missingParameters_
 

Detailed Description

Definition at line 15 of file ValidateThenSet.h.

Constructor & Destructor Documentation

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

Definition at line 16 of file ValidateThenSet.cc.

References ignorableKeys_, missingParameters_, userKeys_, and ~ValidateThenSet().

19  : pset_{pset}
20  , ignorableKeys_{keysToIgnore}
21  , userKeys_{pset.get_all_keys()}
23 {
24  cet::sort_all(userKeys_);
25 }
std::set< std::string > ignorableKeys_
std::vector< std::string > userKeys_
ParameterSet const & pset_
std::vector< cet::exempt_ptr< ParameterBase > > missingParameters_
fhicl::detail::ValidateThenSet::~ValidateThenSet ( )
default

Referenced by ValidateThenSet().

Member Function Documentation

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

Definition at line 74 of file ValidateThenSet.cc.

References pset_, and fhicl::detail::ParameterBase::set_value().

75 {
76  p.set_value(pset_);
77 }
ParameterSet const & pset_
void fhicl::detail::ValidateThenSet::atom ( AtomBase )
overrideprivate

Definition at line 34 of file ValidateThenSet.cc.

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

Definition at line 38 of file ValidateThenSet.cc.

References e, fhicl::detail::ParameterBase::has_default(), fhicl::ParameterSet::has_key(), ignorableKeys_, fhicl::detail::ParameterBase::is_optional(), fhicl::detail::is_sequence(), fhicl::detail::ParameterBase::key(), missingParameters_, fhicl::detail::ParameterBase::parameter_type(), fhicl::detail::ParameterBase::preset_value(), pset_, fhicl::detail::ParameterBase::should_use(), fhicl::detail::strip_first_containing_name(), and userKeys_.

39 {
40  // 'ConfigPredicate' condition must be satisfied to continue.
41  if (!p.should_use())
42  return false;
43 
44  // Check that key exists; allow defaulted or optional keys to be
45  // absent.
46  std::string const& k = strip_first_containing_name(p.key());
47  if (!pset_.has_key(k) and !cet::search_all(ignorableKeys_, k)) {
48  if (!p.has_default() and !p.is_optional()) {
49  missingParameters_.emplace_back(&p);
50  }
51  return false;
52  }
53 
54  // Sometimes we are able to short-circuit walking the parameters
55  // that belong to a sequence. This is especially helpful for
56  // sequences with many entries of atomic type.
57  if (is_sequence(p.parameter_type()) and p.preset_value(pset_)) {
58  // Remove all entries that match the sequence key and any elements
59  // of that sequence.
60  auto erase_from =
61  std::remove_if(userKeys_.begin(), userKeys_.end(), [&k](auto const& e) {
62  return e.find(k + '[') == 0ull or e == k;
63  });
64  userKeys_.erase(erase_from, userKeys_.cend());
65  return false;
66  }
67 
68  auto erase_from = std::remove(userKeys_.begin(), userKeys_.end(), k);
69  userKeys_.erase(erase_from, userKeys_.cend());
70  return true;
71 }
std::set< std::string > ignorableKeys_
std::vector< std::string > userKeys_
std::string strip_first_containing_name(std::string const &key)
ParameterSet const & pset_
bool has_key(std::string const &key) const
std::vector< cet::exempt_ptr< ParameterBase > > missingParameters_
Float_t e
Definition: plot.C:35
bool is_sequence(std::any const &val)
Definition: coding.h:49
void fhicl::detail::ValidateThenSet::check_keys ( )

Definition at line 210 of file ValidateThenSet.cc.

References ignorableKeys_, missingParameters_, fhicl::detail::optional_parameter_message(), pset_, and userKeys_.

211 {
212  removeIgnorableKeys(ignorableKeys_, userKeys_, missingParameters_);
213  std::string errmsg;
214  errmsg += fillMissingKeysMsg(missingParameters_);
215  errmsg += fillExtraKeysMsg(pset_, userKeys_);
216  if (!errmsg.empty()) {
217  std::string fullmsg{detail::optional_parameter_message(false)};
218  fullmsg += "\n";
219  fullmsg += errmsg;
220  throw validationException{fullmsg.c_str()};
221  }
222 }
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 104 of file ValidateThenSet.cc.

References util::end(), fhicl::ParameterSet::get_src_info(), art::left(), fhicl::detail::ParameterBase::name(), r, fhicl::Name::regex_safe(), fhicl::detail::strip_first_containing_name(), userKeys_, lar::dump::vector(), and fhicl::detail::ParameterWalker< C >::walk_over().

105 {
106  // A delegated parameter must itself be present, but any nested
107  // parameters do not need to be present since the nested parameters
108  // are potentially validated elsewhere.
109  auto const& name = dp.name();
110  std::string const pattern{fhicl::Name::regex_safe(name) + R"((\.|\[))"};
111  std::regex const r{pattern};
112  auto erase_from =
113  std::remove_if(userKeys_.begin(), userKeys_.end(), [&r](auto const& k) {
114  return std::regex_search(k, r);
115  });
116  userKeys_.erase(erase_from, userKeys_.end());
117 }
TRandom r
Definition: spectrum.C:23
std::vector< std::string > userKeys_
static std::string regex_safe(std::string const &key)
Definition: Name.h:31
void fhicl::detail::ParameterWalker< C >::do_after_action ( tt::maybe_const_t< ParameterBase, C > &  p)
inlineinherited

Definition at line 96 of file ParameterWalker.h.

97  {
98  after_action(p);
99  }
virtual void after_action(tt::maybe_const_t< ParameterBase, C > &)
void fhicl::detail::ParameterWalker< C >::do_atom ( tt::maybe_const_t< AtomBase, C > &  a)
inlineinherited

Definition at line 124 of file ParameterWalker.h.

125  {
126  atom(a);
127  }
virtual void atom(tt::maybe_const_t< AtomBase, C > &)=0
bool fhicl::detail::ParameterWalker< C >::do_before_action ( tt::maybe_const_t< ParameterBase, C > &  p)
inlineinherited

Definition at line 91 of file ParameterWalker.h.

92  {
93  return before_action(p);
94  }
virtual bool before_action(tt::maybe_const_t< ParameterBase, C > &)
void fhicl::detail::ParameterWalker< C >::do_delegated_parameter ( tt::maybe_const_t< DelegateBase, C > &  dp)
inlineinherited

Definition at line 130 of file ParameterWalker.h.

131  {
133  }
virtual void delegated_parameter(tt::maybe_const_t< DelegateBase, C > &)=0
void fhicl::detail::ParameterWalker< C >::do_enter_sequence ( tt::maybe_const_t< SequenceBase, C > &  s)
inlineinherited

Definition at line 113 of file ParameterWalker.h.

114  {
115  enter_sequence(s);
116  }
virtual void enter_sequence(tt::maybe_const_t< SequenceBase, C > &)=0
void fhicl::detail::ParameterWalker< C >::do_enter_table ( tt::maybe_const_t< TableBase, C > &  t)
inlineinherited

Definition at line 102 of file ParameterWalker.h.

103  {
104  enter_table(t);
105  }
virtual void enter_table(tt::maybe_const_t< TableBase, C > &)=0
void fhicl::detail::ParameterWalker< C >::do_exit_sequence ( tt::maybe_const_t< SequenceBase, C > &  s)
inlineinherited

Definition at line 118 of file ParameterWalker.h.

119  {
120  exit_sequence(s);
121  }
virtual void exit_sequence(tt::maybe_const_t< SequenceBase, C > &)
void fhicl::detail::ParameterWalker< C >::do_exit_table ( tt::maybe_const_t< TableBase, C > &  t)
inlineinherited

Definition at line 107 of file ParameterWalker.h.

108  {
109  exit_table(t);
110  }
virtual void exit_table(tt::maybe_const_t< TableBase, C > &)
void fhicl::detail::ValidateThenSet::enter_sequence ( SequenceBase p)
overrideprivate

Definition at line 82 of file ValidateThenSet.cc.

References fhicl::ParameterSet::is_key_to_sequence(), fhicl::detail::ParameterBase::key(), fhicl::detail::SequenceBase::prepare_elements_for_validation(), pset_, r, fhicl::Name::regex_safe(), fhicl::detail::strip_first_containing_name(), fhicl::type_mismatch, and userKeys_.

83 {
84  // Ensure that the supplied parameter represents a sequence.
85  auto const& key = strip_first_containing_name(s.key());
86  if (!pset_.is_key_to_sequence(key)) {
87  throw fhicl::exception(type_mismatch, "error converting to sequence:\n")
88  << "The supplied value of the parameter:\n"
89  << " " << s.key() << '\n'
90  << "does not represent a sequence.\n";
91  }
92 
93  std::regex const r{fhicl::Name::regex_safe(key) + "\\[\\d+\\]"};
94  std::size_t const nElems =
95  std::count_if(userKeys_.begin(), userKeys_.end(), [&r](auto const& k) {
96  return std::regex_match(k, r);
97  });
98  s.prepare_elements_for_validation(nElems);
99 }
TRandom r
Definition: spectrum.C:23
std::vector< std::string > userKeys_
bool is_key_to_sequence(std::string const &key) const
Definition: ParameterSet.h:214
std::string strip_first_containing_name(std::string const &key)
ParameterSet const & pset_
static std::string regex_safe(std::string const &key)
Definition: Name.h:31
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
void fhicl::detail::ValidateThenSet::enter_table ( TableBase )
overrideprivate

Definition at line 30 of file ValidateThenSet.cc.

31 {}
void fhicl::detail::ParameterWalker< C >::walk_over ( tt::maybe_const_t< ParameterBase, C > &  )
inherited

Member Data Documentation

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

Definition at line 34 of file ValidateThenSet.h.

Referenced by before_action(), check_keys(), and ValidateThenSet().

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

Definition at line 36 of file ValidateThenSet.h.

Referenced by before_action(), check_keys(), and ValidateThenSet().

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

Definition at line 33 of file ValidateThenSet.h.

Referenced by after_action(), before_action(), check_keys(), and enter_sequence().

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

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