NewLine.h
Go to the documentation of this file.
1 /**
2  * @file NewLine.h
3  * @brief Simple class managing a repetitive output task
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date December 18th, 2015
6  */
7 
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 
19  /// Structure collecting indentation options
20  struct IndentOptions_t {
21  std::string indent; ///< indentation string
22  bool appendFirst = false; ///< skip indentation on the first line
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 
39  /**
40  * @brief Starts a new line in a output stream
41  * @tparam Stream type of output stream
42  *
43  * Example of usage:
44  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
45  * std::cout << "Preamble on its own line." << std::endl;
46  * NewLine OutLn(std::cout, "> ");
47  * OutLn() << "An indented line.";
48  * OutLn() << "Another indented line.";
49  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
50  *
51  * that (after flush) will result in the output
52  *
53  * Preamble on its own line.
54  *
55  * > An indented line.
56  * > Another indented line.
57  *
58  * Asking to consider the first line already started, instead:
59  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
60  * std::cout << "Preamble on its own line." << std::endl;
61  * NewLine OutLn(std::cout, "> ", true);
62  * OutLn() << "An indented line.";
63  * OutLn() << "Another indented line.";
64  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
65  * will instead result in the output
66  *
67  * Preamble on its own line.
68  * > An indented line.
69  * > Another indented line.
70  *
71  * (note that the line that we consider started was actually an empty one).
72  */
73  template <typename Stream>
74  class NewLine {
75  public:
76 
77  /**
78  * @brief Constructor: associates with the stream
79  * @param stream a reference to the stream where to insert new lines
80  * @param indentOptions all indentation options (will be copied)
81  *
82  * The constructor does not start a new line.
83  * If followLine is true, the first line is supposed to be already started
84  * and no indentation nor new line will be set on it.
85  */
86  NewLine(Stream& stream, IndentOptions_t indentOptions)
87  : out(stream), options(std::move(indentOptions)), nLines(0)
88  {}
89 
90  /**
91  * @brief Constructor: associates with the stream
92  * @param stream a reference to the stream where to insert new lines
93  * @param indent string used for indentation (default: none)
94  * @param followLine whether first line is already started (default: no)
95  *
96  * The constructor does not start a new line.
97  * If followLine is true, the first line is supposed to be already started
98  * and no indentation nor new line will be set on it.
99  */
100  NewLine(Stream& stream, std::string indent = "", bool followLine = false)
101  : NewLine(stream, IndentOptions_t{ indent, followLine })
102  {}
103 
104  /// @{
105  /// @name Accessors
106 
107  /// Returns the number of inserted lines
108  unsigned int lines() const { return nLines; }
109 
110  /// Returns the current indentation string
111  std::string indent() const { return options.indent; }
112 
113  /// @}
114 
115  /// Starts a new line
116  Stream& newLine() { if (!append()) forceNewLine(); ++nLines; return out; }
117 
118  /// Calls and returns newLine(). Candy.
119  Stream& operator() () { return newLine(); }
120 
121  /// Starts a new line (no matter what)
122  void forceNewLine() { out << "\n" << options.indent; }
123 
124  /// Returns whether newLine() will append text on the current line
125  bool append() const { return (lines() == 0) && options.appendFirst; }
126 
127 
128  /// Replaces the indentation string
129  void setIndent(std::string newIndent) { options.indent = newIndent; }
130 
131  /// Adds to the end to the indentation string
132  void addIndent(std::string moreIndent) { options.indent += moreIndent; }
133 
134 
135  protected:
136  Stream& out; ///< reference to the output stream
137  IndentOptions_t options; ///< all indentation options
138  unsigned int nLines; ///< number of lines in output
139 
140  }; // class NewLine
141 
142 
143  /// Convenience function to create a temporary NewLine
144  template <typename Stream>
146  (Stream& stream, std::string indent, bool followLine = false)
147  { return NewLine<Stream>(stream, indent, followLine); }
148 
149  /// Convenience function to create a temporary NewLine
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
std::string string
Definition: nybbler.cc:12
IndentOptions_t & appendIndentation(std::string more)
Definition: NewLine.h:28
static void newLine()
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
NewLine(Stream &stream, IndentOptions_t indentOptions)
Constructor: associates with the stream.
Definition: NewLine.h:86
def move(depos, offset)
Definition: depos.py:107
Structure collecting indentation options.
Definition: NewLine.h:20
static int max(int a, int b)
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