LArSoft  v07_13_02
Liquid Argon Software toolkit - http://larsoft.org/
NewLine.h
Go to the documentation of this file.
1 
8 #ifndef LARDATA_RECOBASE_DUMPERS_NEWLINE_H
9 #define LARDATA_RECOBASE_DUMPERS_NEWLINE_H 1
10 
11 // C/C++ standard libraries
12 #include <string>
13 #include <utility> // std::move()
14 
15 
16 namespace recob {
17  namespace dumper {
18 
20  struct IndentOptions_t {
21  std::string indent;
22  bool appendFirst = false;
23 
24  IndentOptions_t(std::string ind = "", bool followLine = false)
25  : indent(ind), appendFirst(followLine)
26  {}
27 
29  { indent += more; appendFirst = false; return *this; }
31  {
32  indent.erase(std::max(indent.length() - less.length(), size_t(0)));
33  return *this;
34  }
35 
36  }; // IndentOptions_t
37 
38 
73  template <typename Stream>
74  class NewLine {
75  public:
76 
86  NewLine(Stream& stream, IndentOptions_t indentOptions)
87  : out(stream), options(std::move(indentOptions)), nLines(0)
88  {}
89 
100  NewLine(Stream& stream, std::string indent = "", bool followLine = false)
101  : NewLine(stream, IndentOptions_t{ indent, followLine })
102  {}
103 
106 
108  unsigned int lines() const { return nLines; }
109 
111  std::string indent() const { return options.indent; }
112 
114 
116  Stream& newLine() { if (!append()) forceNewLine(); ++nLines; return out; }
117 
119  Stream& operator() () { return newLine(); }
120 
122  void forceNewLine() { out << "\n" << options.indent; }
123 
125  bool append() const { return (lines() == 0) && options.appendFirst; }
126 
127 
129  void setIndent(std::string newIndent) { options.indent = newIndent; }
130 
132  void addIndent(std::string moreIndent) { options.indent += moreIndent; }
133 
134 
135  protected:
136  Stream& out;
138  unsigned int nLines;
139 
140  }; // class NewLine
141 
142 
144  template <typename Stream>
146  (Stream& stream, std::string indent, bool followLine = false)
147  { return NewLine<Stream>(stream, indent, followLine); }
148 
150  template <typename Stream>
152  (Stream& stream, IndentOptions_t const& options)
153  { return NewLine<Stream>(stream, options); }
154 
155 
156  } // namespace dumper
157 } // namespace recob
158 
159 
160 #endif // LARDATA_RECOBASE_DUMPERS_NEWLINE_H
unsigned int nLines
number of lines in output
Definition: NewLine.h:138
unsigned int lines() const
Returns the number of inserted lines.
Definition: NewLine.h:108
Starts a new line in a output stream.
Definition: NewLine.h:74
void addIndent(std::string moreIndent)
Adds to the end to the indentation string.
Definition: NewLine.h:132
IndentOptions_t(std::string ind="", bool followLine=false)
Definition: NewLine.h:24
std::string indent() const
Returns the current indentation string.
Definition: NewLine.h:111
Reconstruction base classes.
std::string indent
indentation string
Definition: NewLine.h:21
IndentOptions_t & appendIndentation(std::string more)
Definition: NewLine.h:28
STL namespace.
void setIndent(std::string newIndent)
Replaces the indentation string.
Definition: NewLine.h:129
NewLine(Stream &stream, std::string indent="", bool followLine=false)
Constructor: associates with the stream.
Definition: NewLine.h:100
IndentOptions_t & removeIndentation(std::string less)
Definition: NewLine.h:30
Stream & out
reference to the output stream
Definition: NewLine.h:136
bool appendFirst
skip indentation on the first line
Definition: NewLine.h:22
Int_t max
Definition: plot.C:27
NewLine(Stream &stream, IndentOptions_t indentOptions)
Constructor: associates with the stream.
Definition: NewLine.h:86
Structure collecting indentation options.
Definition: NewLine.h:20
void forceNewLine()
Starts a new line (no matter what)
Definition: NewLine.h:122
Stream & newLine()
Starts a new line.
Definition: NewLine.h:116
bool append() const
Returns whether newLine() will append text on the current line.
Definition: NewLine.h:125
NewLine< Stream > makeNewLine(Stream &stream, std::string indent, bool followLine=false)
Convenience function to create a temporary NewLine.
Definition: NewLine.h:146
IndentOptions_t options
all indentation options
Definition: NewLine.h:137