All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
MarleyParameterSetWalker.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////
2 /// \file MarleyParameterSetWalker.h
3 /// \brief Concrete fhicl::ParameterSetWalker that converts a
4 /// fhicl::ParameterSet into a marley::JSON object
5 ///
6 /// \author Steven Gardiner <gardiner@fnal.gov>
7 //////////////////////////////////////////////////////////////////////////////
8 
9 #ifndef LARSIM_MARLEY_PARAMETERSET_WALKER_H
10 #define LARSIM_MARLEY_PARAMETERSET_WALKER_H
11 
12 // standard library includes
13 #include <string>
14 #include <sstream>
15 #include <vector>
16 
17 // framework includes
18 #include "fhiclcpp/ParameterSetWalker.h"
19 #include "fhiclcpp/detail/printing_helpers.h"
20 
21 // MARLEY includes
22 #include "marley/JSON.hh"
23 
24 namespace evgen {
25 
26  class MarleyParameterSetWalker : public fhicl::ParameterSetWalker {
27 
28  public:
29 
30  inline const marley::JSON& get_json() const { return full_json_; }
31  inline marley::JSON& get_json() { return full_json_; }
32 
33  private:
34 
35  inline void enter_table( const key_t& key, const any_t& ) override {
36  auto& json = json_refs_.back().get();
37  json_refs_.emplace_back( json[ key ]
38  = marley::JSON::make(marley::JSON::DataType::Object) );
39  }
40 
41  inline void enter_sequence( const key_t& key, const any_t& ) override {
42  auto& json = json_refs_.back().get();
43  in_seq_ = true;
44  seq_index_ = 0u;
45  json_refs_.emplace_back( json[ key ]
46  = marley::JSON::make(marley::JSON::DataType::Array) );
47  }
48 
49  inline void atom( const key_t& key, const any_t& any ) override {
50  auto& json = json_refs_.back().get();
51  // Convert the atom to a string. Add an extra space to keep some
52  // of MARLEY's JSON parsing routines (which rely on looking ahead
53  // in some cases) happy
54  std::string atom_val = fhicl::detail::atom::value( any ) + ' ';
55  marley::JSON json_atom;
56  // Hard-coded value taken from fhiclcpp/detail/printing_helpers.cc
57  if ( atom_val != "@nil" ) {
58  std::istringstream iss( atom_val );
59  // Utility function defined in marley/JSON.hh
60  json_atom = marley::parse_next( iss );
61  }
62  if ( in_seq_ ) {
63  json[ seq_index_++ ] = json_atom;
64  }
65  else {
66  json[ key ] = json_atom;
67  }
68  }
69 
70  inline void exit_table( const key_t&, const any_t& ) override {
71  json_refs_.pop_back();
72  }
73 
74  inline void exit_sequence( const key_t&, const any_t& ) override {
75  json_refs_.pop_back();
76  in_seq_ = false;
77  }
78 
79  /// Owned JSON object used to store the converted FHiCL parameters
80  marley::JSON full_json_;
81 
82  /// References to the owned JSON object or a sub-object thereof
83  std::vector< std::reference_wrapper<marley::JSON> >
85 
86  unsigned seq_index_ = 0u;
87  bool in_seq_ = false;
88  };
89 
90 }
91 
92 #endif // LARSIM_MARLEY_PARAMETERSET_WALKER_H
const marley::JSON & get_json() const
void exit_sequence(const key_t &, const any_t &) override
void enter_sequence(const key_t &key, const any_t &) override
marley::JSON full_json_
Owned JSON object used to store the converted FHiCL parameters.
void enter_table(const key_t &key, const any_t &) override
void exit_table(const key_t &, const any_t &) override
std::vector< std::reference_wrapper< marley::JSON > > json_refs_
References to the owned JSON object or a sub-object thereof.
void atom(const key_t &key, const any_t &any) override
temporary value