LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
ValuePrinter.h
Go to the documentation of this file.
1 #ifndef fhiclcpp_detail_ValuePrinter_h
2 #define fhiclcpp_detail_ValuePrinter_h
3 
4 /*
5  ======================================================================
6 
7  ValuePrinter
8 
9  ======================================================================
10 
11  Class used when
12 
13  'ParameterSet::to_indented_string(unsigned, print_mode::raw)'
14 
15  is called. This class provides a human-readable string
16  representing the entire (nested) contents of a ParameterSet object.
17 
18  Currently supported format:
19  ===========================
20 
21  This will provide a print out that looks like:
22 
23 
24  ^<-IND->
25  ^ p1: {}$ p1: {}
26  ^ p2: {$ p2: {
27  ^ a: something$ a: something
28  ^ }$ }
29  ^ p3: {$ p3: {
30  ^ a: else$ a: else
31  ^ b: []$ b: []
32  ^ c: [$ Rendered c: [
33  ^ 11$ ========> 11
34  ^ ]$ ]
35  ^ d: [$ d: [
36  ^ 11,$ 11,
37  ^ 12$ 12
38  ^ ]$ ]
39  ^ p4: {$ p4: {
40  ^ d: e$ d: e
41  ^ }$ }
42  ^ }$ }
43  ^<-IND->
44 
45  Note that the caret ^ (dollar-sign $) represents the beginning
46  (end) of the line and is not printed, nor is the <-IND-> string,
47  which represents a user-provided width value for the initial
48  indentation level.
49 
50  Maintenance notes:
51  ==================
52 
53  [1] A couple stack objects are used: the Indentation class, as well
54  as std::stack<std::size_t>, where the latter represents the
55  sizes of the stacked sequences. Keeping track of the sequence
56  is necessary so that the last sequence element does not have a
57  ',' character that follows it.
58 
59  To use these classes correctly, the Indentation must be updated
60  during each {enter,exit}_{table,sequence} call, and the
61  sequence-sizes stack must be updated during each
62  {enter,exit}_sequence call.
63 
64  [2] The 'maybe_{indent,nl}_' functions are provided so that empty
65  sequences and tables do not appear as
66 
67  seq: [ and table: {
68  ] }
69 
70  but rather
71 
72  seq: [] and table: {}
73 
74 */
75 
78 #include "fhiclcpp/fwd.h"
79 
80 #include <sstream>
81 #include <stack>
82 #include <string>
83 
84 namespace fhicl::detail {
85 
87  public:
88  ValuePrinter(std::string const& key, unsigned initial_indent_level = 0);
89 
90  std::string
91  result() const
92  {
93  return buffer_.str();
94  }
95 
96  private:
97  void enter_table(key_t const&, any_t const&) override;
98  void enter_sequence(key_t const&, any_t const&) override;
99 
100  void exit_table(key_t const&, any_t const&) override;
101  void exit_sequence(key_t const&, any_t const&) override;
102 
103  void atom(key_t const&, any_t const&) override;
104 
105  void before_action(key_t const&,
106  any_t const&,
107  ParameterSet const*) override;
108 
109  void after_action(key_t const&) override;
110 
111  void push_size_(any_t const&);
112  void pop_size_();
113  std::string maybe_indent_(std::size_t);
114  std::string maybe_nl_(std::size_t);
115 
116  std::ostringstream buffer_{};
117  std::string key_;
119  std::stack<std::size_t> sequence_sizes_;
120  std::size_t seq_size_;
121  std::size_t table_size_;
123  };
124 }
125 
126 #endif /* fhiclcpp_detail_ValuePrinter_h */
127 
128 // Local variables:
129 // mode: c++
130 // End:
std::stack< std::size_t > sequence_sizes_
Definition: ValuePrinter.h:119
void after_action(key_t const &) override
Definition: ValuePrinter.cc:42
void exit_sequence(key_t const &, any_t const &) override
Definition: ValuePrinter.cc:83
std::string maybe_nl_(std::size_t)
std::string result() const
Definition: ValuePrinter.h:91
void push_size_(any_t const &)
ValuePrinter(std::string const &key, unsigned initial_indent_level=0)
Definition: ValuePrinter.cc:18
void enter_sequence(key_t const &, any_t const &) override
Definition: ValuePrinter.cc:73
void atom(key_t const &, any_t const &) override
std::string maybe_indent_(std::size_t)
void before_action(key_t const &, any_t const &, ParameterSet const *) override
Definition: ValuePrinter.cc:29
void enter_table(key_t const &, any_t const &) override
Definition: ValuePrinter.cc:52
std::ostringstream buffer_
Definition: ValuePrinter.h:116
void exit_table(key_t const &, any_t const &) override
Definition: ValuePrinter.cc:61