LArSoft  v09_90_00
Liquid Argon Software toolkit - https://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 namespace recob {
16  namespace dumper {
17 
19  struct IndentOptions_t {
20  std::string indent;
21  bool appendFirst = false;
22 
23  IndentOptions_t(std::string ind = "", bool followLine = false)
24  : indent(ind), appendFirst(followLine)
25  {}
26 
28  {
29  indent += more;
30  appendFirst = false;
31  return *this;
32  }
34  {
35  indent.erase(std::max(indent.length() - less.length(), size_t(0)));
36  return *this;
37  }
38 
39  }; // IndentOptions_t
40 
75  template <typename Stream>
76  class NewLine {
77  public:
87  NewLine(Stream& stream, IndentOptions_t indentOptions)
88  : out(stream), options(std::move(indentOptions)), nLines(0)
89  {}
90 
101  NewLine(Stream& stream, std::string indent = "", bool followLine = false)
102  : NewLine(stream, IndentOptions_t{indent, followLine})
103  {}
104 
107 
109  unsigned int lines() const { return nLines; }
110 
112  std::string indent() const { return options.indent; }
113 
115 
117  Stream& newLine()
118  {
119  if (!append()) forceNewLine();
120  ++nLines;
121  return out;
122  }
123 
125  Stream& operator()() { return newLine(); }
126 
128  void forceNewLine() { out << "\n" << options.indent; }
129 
131  bool append() const { return (lines() == 0) && options.appendFirst; }
132 
134  void setIndent(std::string newIndent) { options.indent = newIndent; }
135 
137  void addIndent(std::string moreIndent) { options.indent += moreIndent; }
138 
139  protected:
140  Stream& out;
142  unsigned int nLines;
143 
144  }; // class NewLine
145 
147  template <typename Stream>
148  inline NewLine<Stream> makeNewLine(Stream& stream, std::string indent, bool followLine = false)
149  {
150  return NewLine<Stream>(stream, indent, followLine);
151  }
152 
154  template <typename Stream>
155  inline NewLine<Stream> makeNewLine(Stream& stream, IndentOptions_t const& options)
156  {
157  return NewLine<Stream>(stream, options);
158  }
159 
160  } // namespace dumper
161 } // namespace recob
162 
163 #endif // LARDATA_RECOBASE_DUMPERS_NEWLINE_H
unsigned int nLines
number of lines in output
Definition: NewLine.h:142
unsigned int lines() const
Returns the number of inserted lines.
Definition: NewLine.h:109
Starts a new line in a output stream.
Definition: NewLine.h:76
void addIndent(std::string moreIndent)
Adds to the end to the indentation string.
Definition: NewLine.h:137
IndentOptions_t(std::string ind="", bool followLine=false)
Definition: NewLine.h:23
Stream & operator()()
Calls and returns newLine(). Candy.
Definition: NewLine.h:125
std::string indent() const
Returns the current indentation string.
Definition: NewLine.h:112
Reconstruction base classes.
std::string indent
indentation string
Definition: NewLine.h:20
IndentOptions_t & appendIndentation(std::string more)
Definition: NewLine.h:27
STL namespace.
void setIndent(std::string newIndent)
Replaces the indentation string.
Definition: NewLine.h:134
NewLine(Stream &stream, std::string indent="", bool followLine=false)
Constructor: associates with the stream.
Definition: NewLine.h:101
IndentOptions_t & removeIndentation(std::string less)
Definition: NewLine.h:33
Stream & out
reference to the output stream
Definition: NewLine.h:140
bool appendFirst
skip indentation on the first line
Definition: NewLine.h:21
NewLine(Stream &stream, IndentOptions_t indentOptions)
Constructor: associates with the stream.
Definition: NewLine.h:87
Structure collecting indentation options.
Definition: NewLine.h:19
void forceNewLine()
Starts a new line (no matter what)
Definition: NewLine.h:128
Stream & newLine()
Starts a new line.
Definition: NewLine.h:117
bool append() const
Returns whether newLine() will append text on the current line.
Definition: NewLine.h:131
NewLine< Stream > makeNewLine(Stream &stream, std::string indent, bool followLine=false)
Convenience function to create a temporary NewLine.
Definition: NewLine.h:148
IndentOptions_t options
all indentation options
Definition: NewLine.h:141