Prettifier.h
Go to the documentation of this file.
1 #ifndef fhiclcpp_detail_Prettifier_h
2 #define fhiclcpp_detail_Prettifier_h
3 
4 /*
5  ======================================================================
6 
7  Prettifier
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 
77 #include "fhiclcpp/coding.h"
79 #include "fhiclcpp/fwd.h"
80 
81 #include <sstream>
82 #include <stack>
83 #include <string>
84 
85 namespace fhicl {
86  namespace detail {
87 
88  class Prettifier : public ParameterSetWalker {
89  public:
90  Prettifier(unsigned initial_indent_level = 0);
91 
93  result() const
94  {
95  return buffer_.str();
96  }
97 
98  private:
99  void enter_table(key_t const&, any_t const&) override;
100  void enter_sequence(key_t const&, any_t const&) override;
101 
102  void exit_table(key_t const&, any_t const&) override;
103  void exit_sequence(key_t const&, any_t const&) override;
104 
105  void atom(key_t const&, any_t const&) override;
106 
107  void before_action(key_t const&,
108  any_t const&,
109  ParameterSet 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_{};
118  std::stack<std::size_t> sequence_sizes_;
119  std::size_t seq_size_;
120  std::size_t table_size_;
121  };
122  }
123 }
124 
125 #endif /* fhiclcpp_detail_Prettifier_h */
126 
127 // Local variables:
128 // mode: c++
129 // End:
void exit_sequence(key_t const &, any_t const &) override
void atom(key_t const &, any_t const &) override
Prettifier(unsigned initial_indent_level=0)
std::string result() const
Definition: Prettifier.h:93
void enter_table(key_t const &, any_t const &) override
void enter_sequence(key_t const &, any_t const &) override
std::string maybe_nl_(std::size_t)
std::stack< std::size_t > sequence_sizes_
Definition: Prettifier.h:118
std::ostringstream buffer_
Definition: Prettifier.h:116
void before_action(key_t const &, any_t const &, ParameterSet const *) override
void push_size_(any_t const &)
std::string maybe_indent_(std::size_t)
void exit_table(key_t const &, any_t const &) override
enum BeamMode string