ParameterSetWalker.h
Go to the documentation of this file.
1 #ifndef fhiclcpp_ParameterSetWalker_h
2 #define fhiclcpp_ParameterSetWalker_h
3 
4 /*
5 
6  ======================================================================
7 
8  ParameterSetWalker
9 
10  ======================================================================
11 
12  There are cases where we must walk over the entire ParameterSet
13  tree, including descending into its nested tables and looping over
14  its sequence elements. The 'ParameterSet::walk_' function and the
15  'ParameterSetWalker' interface below make this possible in a way
16  that allows for flexibility in what kind of actions we want
17  performed for each table, sequence or atom.
18 
19  The tree-walking functionality is provided by
20 
21  'ParameterSet::walk_( ParameterSetWalker& psw)'
22 
23  Heuristically, it looks like:
24 
25  for (auto const& param : pset_parameters) {
26 
27  psw.before_action()
28 
29  if (is_table(param)) {
30  *** psw.enter_table()
31  descend_into_table ...
32  psw.exit_table()
33  }
34  else if (is_sequence(param)) {
35  *** psw.enter_sequence()
36  loop_through_sequence ...
37  psw.exit_sequence()
38  }
39  else {
40  *** psw.atom()
41  }
42 
43  psw.after_action()
44 
45  }
46 
47  The actions that are to be taken per parameter category (table,
48  sequence, or atom) are defined entirely by overrides to the
49  ParameterSetWalker virtual functions that 'psw' calls (as shown
50  above). In other words,
51 
52  << NO ParameterSet TREE WALKING SHOULD BE DONE BY >>
53  << ANY CONCRETE ParameterSetWalker INSTANCE. >>
54 
55  The function calls prefaced with '***' correspond to pure virtual
56  functions, which must have corresponding overrides in any derived
57  classes.
58 
59  The 'exit_{table,sequence}' functions are provided if (e.g.) the
60  derived-class state needs to be restored after the table or sequence
61  traversal. The '{before,after}_action' virtual functions are
62  provided so that category-agnostic instructions can be executed
63  before or after the category-specific ones.
64 
65 */
66 
67 #include <any>
68 #include <string>
69 
70 namespace fhicl {
71 
72  class ParameterSet;
73 
75  public:
76  virtual ~ParameterSetWalker() noexcept = default;
77 
78  using key_t = std::string;
79  using any_t = std::any;
80 
81  void
82  do_enter_table(key_t const& k, any_t const& a)
83  {
84  enter_table(k, a);
85  }
86  void
87  do_enter_sequence(key_t const& k, any_t const& a)
88  {
89  enter_sequence(k, a);
90  }
91  void
92  do_atom(key_t const& k, any_t const& a)
93  {
94  atom(k, a);
95  }
96 
97  void
98  do_before_action(key_t const& k, any_t const& a, ParameterSet const* ps)
99  {
100  before_action(k, a, ps);
101  }
102  void
104  {
105  after_action(k);
106  }
107 
108  void
109  do_exit_table(key_t const& k, any_t const& a)
110  {
111  exit_table(k, a);
112  }
113  void
114  do_exit_sequence(key_t const& k, any_t const& a)
115  {
116  exit_sequence(k, a);
117  }
118 
119  private:
120  virtual void enter_table(key_t const&, any_t const&) = 0;
121  virtual void enter_sequence(key_t const&, any_t const&) = 0;
122  virtual void atom(key_t const&, any_t const&) = 0;
123 
124  virtual void
125  exit_table(key_t const&, any_t const&)
126  {}
127  virtual void
128  exit_sequence(key_t const&, any_t const&)
129  {}
130 
131  // Pointer to enclosing ParameterSet object provided
132  virtual void
133  before_action(key_t const&, any_t const&, ParameterSet const*)
134  {}
135  virtual void
137  {}
138  };
139 
140 } // fhicl
141 
142 #endif /* fhiclcpp_ParameterSetWalker_h */
143 
144 // Local variables:
145 // mode: c++
146 // End:
void do_before_action(key_t const &k, any_t const &a, ParameterSet const *ps)
std::string string
Definition: nybbler.cc:12
void do_enter_sequence(key_t const &k, any_t const &a)
virtual void atom(key_t const &, any_t const &)=0
void do_exit_table(key_t const &k, any_t const &a)
virtual void after_action(key_t const &)
virtual void exit_sequence(key_t const &, any_t const &)
const double a
virtual ~ParameterSetWalker() noexcept=default
static constexpr double ps
Definition: Units.h:99
void do_after_action(key_t const &k)
void do_exit_sequence(key_t const &k, any_t const &a)
virtual void enter_sequence(key_t const &, any_t const &)=0
void do_atom(key_t const &k, any_t const &a)
virtual void before_action(key_t const &, any_t const &, ParameterSet const *)
virtual void exit_table(key_t const &, any_t const &)
void do_enter_table(key_t const &k, any_t const &a)
virtual void enter_table(key_t const &, any_t const &)=0