Prettifier.cc
Go to the documentation of this file.
3 #include "fhiclcpp/coding.h"
6 
7 #include <limits>
8 
9 using namespace fhicl;
10 using namespace fhicl::detail;
11 
12 namespace {
13  constexpr auto size_t_max = std::numeric_limits<std::size_t>::max();
14 }
15 
16 //==========================================================================
17 
18 Prettifier::Prettifier(unsigned const initial_indent_level)
19  : indent_{initial_indent_level}
20  , sequence_sizes_{{size_t_max}}
21  , seq_size_{sequence_sizes_.top()}
22  , table_size_{0u}
23 {}
24 
25 //==========================================================================
26 void
28  any_t const& a,
29  ParameterSet const* ps)
30 {
31  if (!is_table(a))
32  return;
33  table_size_ = ps->get<fhicl::ParameterSet>(key).get_all_keys().size();
34 }
35 
36 //==========================================================================
37 
38 void
39 Prettifier::enter_table(std::string const& key, std::any const&)
40 {
42  indent_.push();
43 }
44 
45 void
46 Prettifier::exit_table(std::string const& key, std::any const&)
47 {
48  indent_.pop();
50  << printed_suffix(key, seq_size_) << nl();
51 }
52 
53 //==========================================================================
54 
55 void
56 Prettifier::enter_sequence(std::string const& key, std::any const& a)
57 {
58  push_size_(a);
60  indent_.push();
61 }
62 
63 void
64 Prettifier::exit_sequence(std::string const& key, std::any const&)
65 {
66  // FIXME: To support a printout like:
67  //
68  // empty: []
69  //
70  // We need to first capture the size of the sequence (0) before we
71  // call pop_size_(). However, for non-empty sequences (I think...)
72  // pop_size_ needs to be called before we determine if an
73  // indentation should take place. This needs to be cleaned up.
74  bool const empty_sequence = seq_size_ == 0;
75  indent_.pop();
76  pop_size_();
77  buffer_ << (empty_sequence ? "" : maybe_indent_(seq_size_))
79  << nl();
80 }
81 
82 //==========================================================================
83 
84 void
85 Prettifier::atom(std::string const& key, std::any const& a)
86 {
88  << printed_suffix(key, seq_size_) << nl();
89 }
90 
91 //=========================================================================
92 
93 void
94 Prettifier::push_size_(std::any const& a)
95 {
96  sequence_sizes_.emplace(std::any_cast<ps_sequence_t>(a).size());
97  seq_size_ = sequence_sizes_.top();
98 }
99 
100 void
102 {
103  sequence_sizes_.pop();
104  seq_size_ = sequence_sizes_.top();
105 }
106 
108 Prettifier::maybe_indent_(std::size_t const sz)
109 {
110  return sz ? indent_() : "";
111 }
112 
114 Prettifier::maybe_nl_(std::size_t const sz)
115 {
116  return sz ? nl() : "";
117 }
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:27
std::string printed_prefix(std::string const &key)
std::string string
Definition: nybbler.cc:12
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
void exit_table(key_t const &, any_t const &) override
Definition: Prettifier.cc:46
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
void push_size_(any_t const &)
Definition: Prettifier.cc:94
std::string printed_prefix(std::string const &key)
def key(type, name=None)
Definition: graph.py:13
const double a
T get(std::string const &key) const
Definition: ParameterSet.h:271
std::string value(std::any const &)
std::stack< std::size_t > sequence_sizes_
Definition: Prettifier.h:116
static int max(int a, int b)
static constexpr double ps
Definition: Units.h:99
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
bool is_table(std::any const &val)
Definition: coding.h:55
std::string maybe_nl_(std::size_t)
Definition: Prettifier.cc:114
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:56