LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
Prettifier.cc
Go to the documentation of this file.
5 
6 #include <limits>
7 
8 using namespace fhicl;
9 using namespace fhicl::detail;
10 
11 namespace {
12  constexpr auto size_t_max = std::numeric_limits<std::size_t>::max();
13 }
14 
15 //==========================================================================
16 
17 Prettifier::Prettifier(unsigned const initial_indent_level)
18  : indent_{initial_indent_level}
19  , sequence_sizes_{{size_t_max}}
20  , seq_size_{sequence_sizes_.top()}
21  , table_size_{0u}
22 {}
23 
24 //==========================================================================
25 void
27  any_t const& a,
28  ParameterSet const* ps)
29 {
30  if (!is_table(a))
31  return;
32  table_size_ = ps->get<fhicl::ParameterSet>(key).get_all_keys().size();
33 }
34 
35 //==========================================================================
36 
37 void
38 Prettifier::enter_table(std::string const& key, boost::any const&)
39 {
41  indent_.push();
42 }
43 
44 void
45 Prettifier::exit_table(std::string const& key, boost::any const&)
46 {
47  indent_.pop();
49  << printed_suffix(key, seq_size_) << nl();
50 }
51 
52 //==========================================================================
53 
54 void
55 Prettifier::enter_sequence(std::string const& key, boost::any const& a)
56 {
57  push_size_(a);
59  indent_.push();
60 }
61 
62 void
63 Prettifier::exit_sequence(std::string const& key, boost::any const&)
64 {
65  // FIXME: To support a printout like:
66  //
67  // empty: []
68  //
69  // We need to first capture the size of the sequence (0) before we
70  // call pop_size_(). However, for non-empty sequences (I think...)
71  // pop_size_ needs to be called before we determine if an
72  // indentation should take place. This needs to be cleaned up.
73  bool const empty_sequence = seq_size_ == 0;
74  indent_.pop();
75  pop_size_();
76  buffer_ << (empty_sequence ? "" : maybe_indent_(seq_size_))
78  << nl();
79 }
80 
81 //==========================================================================
82 
83 void
84 Prettifier::atom(std::string const& key, boost::any const& a)
85 {
87  << printed_suffix(key, seq_size_) << nl();
88 }
89 
90 //=========================================================================
91 
92 void
93 Prettifier::push_size_(boost::any const& a)
94 {
95  sequence_sizes_.emplace(boost::any_cast<ps_sequence_t>(a).size());
96  seq_size_ = sequence_sizes_.top();
97 }
98 
99 void
101 {
102  sequence_sizes_.pop();
103  seq_size_ = sequence_sizes_.top();
104 }
105 
106 std::string
107 Prettifier::maybe_indent_(std::size_t const sz)
108 {
109  return sz ? indent_() : "";
110 }
111 
112 std::string
113 Prettifier::maybe_nl_(std::size_t const sz)
114 {
115  return sz ? nl() : "";
116 }
std::string printed_suffix(std::string const &key, std::size_t const sz)
void before_action(key_t const &, any_t const &, ParameterSet const *) override
Definition: Prettifier.cc:26
std::string printed_prefix(std::string const &key)
Prettifier(unsigned initial_indent_level=0)
Definition: Prettifier.cc:17
void exit_sequence(key_t const &, any_t const &) override
Definition: Prettifier.cc:63
bool is_table(boost::any const &val)
Definition: coding.h:56
std::string maybe_indent_(std::size_t)
Definition: Prettifier.cc:107
void exit_table(key_t const &, any_t const &) override
Definition: Prettifier.cc:45
void push_size_(any_t const &)
Definition: Prettifier.cc:93
Int_t max
Definition: plot.C:27
std::string printed_prefix(std::string const &key)
parameter set interface
T get(std::string const &key) const
Definition: ParameterSet.h:231
std::stack< std::size_t > sequence_sizes_
Definition: Prettifier.h:118
std::ostringstream buffer_
Definition: Prettifier.h:116
std::string value(boost::any const &)
void atom(key_t const &, any_t const &) override
Definition: Prettifier.cc:84
void enter_table(key_t const &, any_t const &) override
Definition: Prettifier.cc:38
std::string maybe_nl_(std::size_t)
Definition: Prettifier.cc:113
std::string printed_prefix(std::string const &key)
std::string nl(std::size_t i=1)
std::string closing_brace()
void enter_sequence(key_t const &, any_t const &) override
Definition: Prettifier.cc:55