LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
DumperBase.h
Go to the documentation of this file.
1 
11 #ifndef LARDATAALG_DUMPERS_DUMPERBASE_H
12 #define LARDATAALG_DUMPERS_DUMPERBASE_H
13 
14 // LArSoft includes
15 #include "lardataalg/Utilities/StatCollector.h" // lar::util::MinMaxCollector
17 
18 // C//C++ standard libraries
19 #include <algorithm> // std::min()
20 #include <cassert>
21 #include <iomanip> // std::setprecision(), std::setw()
22 #include <ios> // std::fixed
23 #include <string>
24 #include <utility> // std::forward(), std::swap(), std::move()
25 #include <vector>
26 
28 namespace dump {
29 
37  class DumperBase {
38 
39  struct IndentSettings {
40  std::string indent = "";
41  std::string firstIndent = "";
42 
43  void set(std::string const& newIndent, std::string const& newFirstIndent)
44  {
45  indent = newIndent;
46  firstIndent = newFirstIndent;
47  }
48  void set(std::string&& newIndent, std::string&& newFirstIndent)
49  {
50  indent = std::move(newIndent);
51  firstIndent = std::move(newFirstIndent);
52  }
53  void set(std::string const& newIndent) { set(newIndent, newIndent); }
54 
55  }; // struct IndentSettings
56 
57  std::vector<IndentSettings> fIndentSettings;
58 
59  public:
61  DumperBase() : fIndentSettings{{}} {}
62 
69  DumperBase(std::string const& indent, std::string const& firstIndent)
70  : fIndentSettings{{indent, firstIndent}}
71  {}
72 
77  DumperBase(std::string const& indent) : DumperBase(indent, indent) {}
78 
92 
95  std::string const& indent() const { return indentSettings().indent; }
96 
98  std::string const& firstIndent() const { return indentSettings().firstIndent; }
99 
101  void setIndent(std::string const& indent, std::string const& firstIndent)
102  {
103  indentSettings() = {indent, firstIndent};
104  }
105 
107  void setIndent(std::string const& indent) { setIndent(indent, indent); }
108 
110  template <typename Stream>
111  Stream& indented(Stream&& out, bool first = false) const
112  {
113  out << (first ? firstIndent() : indent());
114  return out;
115  }
116 
119  template <typename Stream>
120  Stream& firstIndented(Stream&& out) const
121  {
122  return indented(std::forward<Stream>(out), true);
123  }
124 
127  template <typename Stream>
128  Stream& newline(Stream&& out) const
129  {
130  return indented(std::forward<Stream>(out));
131  }
132 
134 
166  template <typename Stream>
167  class Indenter {
168  Stream out;
170 
171  public:
173 
175  Indenter(Stream out, DumperBase const& dumper)
176  : out(std::forward<Stream>(out)), dumper(dumper)
177  {}
178 
180  std::string const& indentString() const { return dumper.indent(); }
181 
183  std::string const& firstIndentString() const { return dumper.firstIndent(); }
184 
186  template <typename T>
188  {
189  out << std::forward<T>(v);
190  return *this;
191  }
192 
194  indenter_t& indent(bool first = false)
195  {
196  dumper.indented(out, first);
197  return *this;
198  }
199 
202  {
203  out << '\n';
204  return indent();
205  }
206 
209  indenter_t& start() { return indent(true); }
210 
211  }; // Indenter<>
212 
214  template <typename Stream>
215  decltype(auto) indenter(Stream&& out) const
216  {
217  return Indenter<Stream>(std::forward<Stream>(out), *this);
218  }
219 
220  protected:
221  IndentSettings& indentSettings() { return fIndentSettings.back(); }
222  IndentSettings const& indentSettings() const { return fIndentSettings.back(); }
223 
226  {
227  auto oldSettings = indentSettings();
228  fIndentSettings.push_back(std::move(oldSettings));
229  return indentSettings();
230  }
231 
234  {
235  if (fIndentSettings.size() > 1U) fIndentSettings.pop_back();
236  assert(!fIndentSettings.empty());
237  return indentSettings();
238  }
239 
240  }; // DumperBase
241 
243  template <typename Dumper>
244  auto withIndentation(Dumper&& dumper, std::string const& indent, std::string const& firstIndent)
245  {
246  dumper.setIndent(indent, firstIndent);
247  return dumper;
248  }
249 
251  template <typename Dumper>
252  auto withIndentation(Dumper&& dumper, std::string const& indent)
253  {
254  return withIndentation(std::forward<Dumper>(dumper), indent, indent);
255  }
256 
257 } // namespace dump
258 
259 //----------------------------------------------------------------------------
260 
261 #endif // LARDATAALG_DUMPERS_DUMPERBASE_H
std::string const & indentString() const
Returns the default indentation string.
Definition: DumperBase.h:180
indenter_t & operator<<(T &&v)
Forwards data to the underlying stream.
Definition: DumperBase.h:187
DumperBase(std::string const &indent, std::string const &firstIndent)
Constructor: sets indentation.
Definition: DumperBase.h:69
void setIndent(std::string const &indent, std::string const &firstIndent)
Sets indentation strings to the specified values.
Definition: DumperBase.h:101
Stream & newline(Stream &&out) const
Definition: DumperBase.h:128
Stream & firstIndented(Stream &&out) const
Definition: DumperBase.h:120
Helper class to keep track of indenting.
Definition: DumperBase.h:167
IndentSettings & restoreIndentSettings()
Restores and returns the last saved settings.
Definition: DumperBase.h:233
DumperBase(std::string const &indent)
Constructor: sets indentation.
Definition: DumperBase.h:77
STL namespace.
void setIndent(std::string const &indent)
Sets both indentation strings to the same specified value.
Definition: DumperBase.h:107
indenter_t & indent(bool first=false)
Inserts an indentation and returns the indenter for further output.
Definition: DumperBase.h:194
Classes gathering simple statistics.
indenter_t & newline()
Breaks the current line and returns the indenter for further output.
Definition: DumperBase.h:201
std::string const & firstIndent() const
Returns the indentation string currently configured for the first line.
Definition: DumperBase.h:98
auto withIndentation(Dumper &&dumper, std::string const &indent, std::string const &firstIndent)
Changes the indentation settings of a dumper class and returns it back.
Definition: DumperBase.h:244
DumperBase const & dumper
Definition: DumperBase.h:169
IndentSettings & indentSettings()
Definition: DumperBase.h:221
std::string const & indent() const
Returns the indentation string currently configured for all lines.
Definition: DumperBase.h:95
std::string const & firstIndentString() const
Returns the indentation string for the first line.
Definition: DumperBase.h:183
decltype(auto) indenter(Stream &&out) const
Returns an Indenter object tied to this dumper and out stream.
Definition: DumperBase.h:215
std::string firstIndent
Indentation string for the first line.
Definition: DumperBase.h:41
Stream & indented(Stream &&out, bool first=false) const
Writes the indentation into a stream, and returns it for further output.
Definition: DumperBase.h:111
Collection of utilities for dumping data on screen.
Definition: DumperBase.h:28
Indenter(Stream out, DumperBase const &dumper)
Records the underlying stream and the dumper associated.
Definition: DumperBase.h:175
IndentSettings & saveIndentSettings()
Stacks a copy of the current settings, and returns the "new" ones.
Definition: DumperBase.h:225
DumperBase()
Default constructor: no indentation.
Definition: DumperBase.h:61
std::string indent
Default indentation string.
Definition: DumperBase.h:40
IndentSettings const & indentSettings() const
Definition: DumperBase.h:222
Base class for data dumpers.
Definition: DumperBase.h:37
std::vector< IndentSettings > fIndentSettings
All indentation settings.
Definition: DumperBase.h:57