LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
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 
78 #include "fhiclcpp/fwd.h"
79 
80 #include <sstream>
81 #include <stack>
82 #include <string>
83 
84 namespace fhicl::detail {
85 
86  class Prettifier : public ParameterSetWalker {
87  public:
88  Prettifier(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 push_size_(any_t const&);
110  void pop_size_();
111  std::string maybe_indent_(std::size_t);
112  std::string maybe_nl_(std::size_t);
113 
114  std::ostringstream buffer_{};
116  std::stack<std::size_t> sequence_sizes_;
117  std::size_t seq_size_;
118  std::size_t table_size_;
119  };
120 }
121 
122 #endif /* fhiclcpp_detail_Prettifier_h */
123 
124 // Local variables:
125 // mode: c++
126 // End:
void before_action(key_t const &, any_t const &, ParameterSet const *) override
Definition: Prettifier.cc:27
Prettifier(unsigned initial_indent_level=0)
Definition: Prettifier.cc:18
void exit_sequence(key_t const &, any_t const &) override
Definition: Prettifier.cc:64
std::string maybe_indent_(std::size_t)
Definition: Prettifier.cc:108
std::string result() const
Definition: Prettifier.h:91
void exit_table(key_t const &, any_t const &) override
Definition: Prettifier.cc:46
void push_size_(any_t const &)
Definition: Prettifier.cc:94
std::stack< std::size_t > sequence_sizes_
Definition: Prettifier.h:116
std::ostringstream buffer_
Definition: Prettifier.h:114
void atom(key_t const &, any_t const &) override
Definition: Prettifier.cc:85
void enter_table(key_t const &, any_t const &) override
Definition: Prettifier.cc:39
std::string maybe_nl_(std::size_t)
Definition: Prettifier.cc:114
void enter_sequence(key_t const &, any_t const &) override
Definition: Prettifier.cc:56