LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
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 "boost/any.hpp"
68 
69 #include <string>
70 
71 namespace fhicl {
72 
73  class ParameterSet;
74 
76  public:
77  virtual ~ParameterSetWalker() noexcept = default;
78 
79  using key_t = std::string;
80  using any_t = boost::any;
81 
82  void
83  do_enter_table(key_t const& k, any_t const& a)
84  {
85  enter_table(k, a);
86  }
87  void
88  do_enter_sequence(key_t const& k, any_t const& a)
89  {
90  enter_sequence(k, a);
91  }
92  void
93  do_atom(key_t const& k, any_t const& a)
94  {
95  atom(k, a);
96  }
97 
98  void
99  do_before_action(key_t const& k, any_t const& a, ParameterSet const* ps)
100  {
101  before_action(k, a, ps);
102  }
103  void
105  {
106  after_action();
107  }
108 
109  void
110  do_exit_table(key_t const& k, any_t const& a)
111  {
112  exit_table(k, a);
113  }
114  void
115  do_exit_sequence(key_t const& k, any_t const& a)
116  {
117  exit_sequence(k, a);
118  }
119 
120  private:
121  virtual void enter_table(key_t const&, any_t const&) = 0;
122  virtual void enter_sequence(key_t const&, any_t const&) = 0;
123  virtual void atom(key_t const&, any_t const&) = 0;
124 
125  virtual void
126  exit_table(key_t const&, any_t const&)
127  {}
128  virtual void
129  exit_sequence(key_t const&, any_t const&)
130  {}
131 
132  // Pointer to enclosing ParameterSet object provided
133  virtual void
134  before_action(key_t const&, any_t const&, ParameterSet const*)
135  {}
136  virtual void
138  {}
139  };
140 
141 } // fhicl
142 
143 #endif /* fhiclcpp_ParameterSetWalker_h */
144 
145 // Local variables:
146 // mode: c++
147 // End:
void do_before_action(key_t const &k, any_t const &a, ParameterSet const *ps)
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 exit_sequence(key_t const &, any_t const &)
parameter set interface
virtual ~ParameterSetWalker() noexcept=default
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