LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
MaybeDisplayParent.h
Go to the documentation of this file.
1 #ifndef fhiclcpp_types_detail_MaybeDisplayParent_h
2 #define fhiclcpp_types_detail_MaybeDisplayParent_h
3 
4 #include "cetlib/exempt_ptr.h"
7 
8 #include <cctype>
9 #include <sstream>
10 #include <vector>
11 
12 //===================================================================================
13 namespace fhicl {
14  namespace detail {
15 
17  using base_ptr = cet::exempt_ptr<ParameterBase>;
18 
20  public:
22  bool const showParents,
23  Indentation& ind)
24  : show_{showParents}
25  , names_{showParents ? get_parents(p.key()) :
26  std::vector<std::string>{}}
27  , indent_{ind}
28  {
29  std::ostringstream os;
30 
31  if (show_) {
32 
33  for (auto it = names_.begin(), e = names_.end() - 1; it != e; ++it) {
34  std::string const& name = *it;
35  std::string const& next_name = *std::next(it);
36 
37  // We use the indent_ member so that we can properly
38  // update the indentation level. However, the literal
39  // indented string should not include any characters in it
40  // for the parents of the parameter in question.
41  std::string const indent(indent_().size(), ' ');
42  if (!std::isdigit(name[0])) {
43  if (std::isdigit(next_name[0])) {
44  os << indent << name << ": [ # index: " << next_name << '\n';
45  closingBraces_.push_back(indent + "]\n");
46  } else {
47  os << indent << name << ": {\n";
48  closingBraces_.push_back(indent + "}\n");
49  }
50  } else {
51  if (!std::isdigit(next_name[0])) {
52  os << indent << "{\n";
53  closingBraces_.push_back(indent + "}\n");
54  } else {
55  os << indent << "[ # index: " << next_name << '\n';
56  closingBraces_.push_back(indent + "]\n");
57  }
58  }
59  indent_.push();
60  }
61  } else {
62  }
63 
64  fullPayload_ = os.str();
65  }
66 
67  std::string const&
68  parent_names() const
69  {
70  return fullPayload_;
71  }
72 
73  std::string
75  {
76  if (!show_)
77  return "";
78 
79  std::string result;
80  for (auto it = closingBraces_.crbegin(), re = closingBraces_.crend();
81  it != re;
82  ++it) {
83  result.append(*it);
84  indent_.pop();
85  }
86  return result;
87  }
88 
89  private:
90  bool show_;
91  std::string fullPayload_{};
92  std::vector<std::string> names_;
93  std::vector<std::string> closingBraces_{};
95 
96  std::vector<std::string> get_parents(std::string const& k);
97 
98  bool
100  {
101  auto pos = p.key().find_last_of("]");
102  return pos != std::string::npos && pos == p.key().size() - 1;
103  }
104  };
105  }
106 }
107 
108 #endif /* fhiclcpp_types_detail_MaybeDisplayParent_h */
109 
110 // Local variables:
111 // mode: c++
112 // End:
cet::exempt_ptr< ParameterBase > base_ptr
bool is_sequence_element(ParameterBase const &p)
std::vector< std::string > closingBraces_
parameter set interface
std::string indent(std::size_t const i)
std::vector< std::string > names_
std::string const & parent_names() const
MaybeDisplayParent(ParameterBase const &p, bool const showParents, Indentation &ind)
Float_t e
Definition: plot.C:34
std::vector< std::string > get_parents(std::string const &k)
std::string key() const
Definition: ParameterBase.h:44