Bayeux  3.4.1
Core Foundation library for SuperNEMO
ioutils.h
Go to the documentation of this file.
1 #ifndef DATATOOLS_IOUTILS_H
3 #define DATATOOLS_IOUTILS_H
4 
5 // Standard Library:
6 #include <iostream>
7 #include <string>
8 #include <vector>
9 
10 // Third Party:
11 // - Boost:
12 #include <boost/cstdint.hpp>
13 
14 // This project:
15 #include <datatools/bit_mask.h>
16 
17 namespace datatools {
18 
19  void print_multi_lines(std::ostream & out_,
20  const std::string & text_,
21  const std::string & indent_ = "");
22 
23  //----------------------------------------------------------------------
26  template <class T>
28  {
29  public:
30  ostream_manipulator(std::ostream& (*function_)(std::ostream &, const T &),
31  const T & value_)
32  : _function_(function_), _value_(value_)
33  {
34  return;
35  }
36 
37  friend std::ostream& operator<<(std::ostream & os_,
38  const ostream_manipulator & manip_)
39  {
40  return manip_._function_(os_, manip_._value_);
41  }
42 
43  private:
44 
45  std::ostream & (*_function_)(std::ostream &, const T &);
46  T _value_;
47 
48  };
49 
50 
51  //----------------------------------------------------------------------
54  template <class T>
56  {
57  public:
58  ostream_manipulator_ref(std::ostream & (*function_)(std::ostream &, T &),
59  T & value_)
60  : _function_(function_), _value_(value_)
61  {
62  return;
63  }
64 
65  friend std::ostream & operator<<(std::ostream & os_,
66  const ostream_manipulator_ref & manip_)
67  {
68  return manip_._function_(os_, manip_._value_);
69  }
70 
71  private:
72 
73  std::ostream & (*_function_)(std::ostream &, T &);
74  T & _value_;
75 
76  };
77 
78 
79  //----------------------------------------------------------------------
82  struct io {
83  public:
84 
85  struct constants {
87  static const std::string & nan_real_repr();
89  static const std::string & plus_infinity_real_repr();
91  static const std::string & minus_infinity_real_repr();
92  };
93 
94  enum reader_flags {
100  };
101 
108  };
109 
110  static const int REAL_PRECISION = 16;
111  static const int REAL8_PRECISION = 16;
112  static const int REAL4_PRECISION = 8;
113 
116  class indenter {
117 
118  public:
119 
121  indenter();
122 
124  size_t get_width() const;
125 
127  size_t get_level() const;
128 
130  indenter& operator++(int);
131 
133  indenter& operator--(int);
134 
135  std::ostream& operator()(std::ostream&) const;
136 
137  indenter& operator()(size_t);
138 
139  friend std::ostream& operator<<(std::ostream &, const indenter &);
140 
141  private:
142 
143  size_t _width_;
144  size_t _level_;
145 
146  };
147 
150  static void convert_command_line_args(int argc_,
151  char ** argv_,
152  std::string & app_name_,
153  std::vector<std::string> & args_);
154 
156  static bool read_boolean(std::istream&, bool&, uint32_t flags = 0);
157 
159  static bool read_boolean(const std::string&, bool&, uint32_t flags = 0);
160 
162  static void write_boolean(std::ostream& a_out, bool a_bool, bool as_alpha = false);
163 
165  static bool read_integer(std::istream&, int&, uint32_t flags = 0);
166 
168  static bool read_integer(const std::string&, int&, uint32_t flags = 0);
169 
171  static void write_integer(std::ostream&, int);
172 
174  static bool read_quoted_string(std::istream&, std::string&, uint32_t flags = 0);
175 
177  static bool read_quoted_string(const std::string&, std::string&, uint32_t flags = 0);
178 
180  static void write_quoted_string(std::ostream&, const std::string& a_str);
181 
183  static bool read_real_number(std::istream & in_, double & val, bool & normal, uint32_t flags = 0);
184 
186  static bool read_real_number(const std::string &, double & val, bool & normal, uint32_t flags = 0);
187 
189  static void write_real_number(std::ostream & out_,
190  double val_,
191  int precision_ = REAL_PRECISION,
192  const std::string& unit_symbol_ = "",
193  const std::string& unit_label_ = "",
194  double unit_value_ = 1.0,
195  uint32_t flags_ = 0);
196 
197  bool is_colored() const;
198 
199  void set_colored(bool);
200 
201  static std::ostream& normal(std::ostream&);
202 
203  static std::ostream& reverse(std::ostream&);
204 
205  static std::ostream& bold(std::ostream&);
206 
207  static std::ostream& underline(std::ostream&);
208 
209  static std::ostream& red(std::ostream&);
210 
211  static std::ostream& green(std::ostream&);
212 
213  static std::ostream& debug(std::ostream&);
214 
215  static std::ostream& devel(std::ostream&);
216 
217  static std::ostream& notice(std::ostream&);
218 
219  static std::ostream& warning(std::ostream&);
220 
221  static std::ostream& error(std::ostream&);
222 
223  static std::ostream& verbose(std::ostream&);
224 
225  static std::ostream& tab(std::ostream&);
226 
227  static std::ostream& vtab(std::ostream&);
228 
229  static std::ostream& page_break(std::ostream&);
230 
231  static std::ostream& left(std::ostream&);
232 
233  static std::ostream& right(std::ostream&);
234 
235  static std::ostream& internal(std::ostream&);
236 
237  static std::ostream& showbase(std::ostream&);
238 
239  static std::ostream& ostream_width(std::ostream& os, const int& n);
240 
241  static ostream_manipulator<int> width(const int& n);
242 
243  static std::ostream& ostream_precision(std::ostream& os, const int& n);
244 
245  static ostream_manipulator<int> precision(const int & n);
246 
247  static std::string to_binary(const uint32_t& val, bool show_all = false);
248 
249  static std::string to_binary_2(const uint32_t& val);
250 
251  // Return singleton indenter
252  static indenter & indent();
253 
254  // Return singleton I/O object
255  static io & instance();
256 
257  protected:
258 
260  io();
261 
262  public:
263 
265  // boost::check_delete<> needs it to be public
266  ~io();
267 
268  private:
269 
270  bool _colored_stream_;
271 
272  };
273 
274 } // namespace datatools
275 
276 #endif // DATATOOLS_IOUTILS_H
277 
278 // Local Variables: --
279 // mode: c++ --
280 // c-file-style: "gnu" --
281 // tab-width: 2 --
282 // End: --
reader_flags
Definition: ioutils.h:94
Definition: ioutils.h:107
static std::ostream & left(std::ostream &)
indenter & operator++(int)
Increment indendation level by one unit.
static ostream_manipulator< int > width(const int &n)
static std::ostream & vtab(std::ostream &)
static const int REAL4_PRECISION
Default precision for float.
Definition: ioutils.h:112
ostream_manipulator(std::ostream &(*function_)(std::ostream &, const T &), const T &value_)
Definition: ioutils.h:30
indenter()
Default constructor.
static std::ostream & debug(std::ostream &)
static void write_quoted_string(std::ostream &, const std::string &a_str)
Write a quoted string.
io()
Default constructor.
static std::string to_binary(const uint32_t &val, bool show_all=false)
Definition: ioutils.h:106
static std::ostream & tab(std::ostream &)
Definition: ioutils.h:105
static bool read_integer(std::istream &, int &, uint32_t flags=0)
Parse an integer.
I/O utilities.
Definition: ioutils.h:82
static const uint32_t bit01
Definition: bit_mask.h:28
ostream_manipulator_ref(std::ostream &(*function_)(std::ostream &, T &), T &value_)
Definition: ioutils.h:58
static const int REAL_PRECISION
Default precision for double.
Definition: ioutils.h:110
static const uint32_t bit03
Definition: bit_mask.h:30
static void write_integer(std::ostream &, int)
Write an integer.
static void write_real_number(std::ostream &out_, double val_, int precision_=REAL_PRECISION, const std::string &unit_symbol_="", const std::string &unit_label_="", double unit_value_=1.0, uint32_t flags_=0)
Write a double value in an ASCII stream with unit support.
static std::ostream & error(std::ostream &)
static bool read_real_number(std::istream &in_, double &val, bool &normal, uint32_t flags=0)
Read a double value from an ASCII stream.
Definition: ioutils.h:85
I/O indenter class.
Definition: ioutils.h:116
static std::ostream & reverse(std::ostream &)
static std::ostream & ostream_width(std::ostream &os, const int &n)
friend std::ostream & operator<<(std::ostream &os_, const ostream_manipulator &manip_)
Definition: ioutils.h:37
static std::ostream & devel(std::ostream &)
static std::ostream & bold(std::ostream &)
static std::ostream & verbose(std::ostream &)
static indenter & indent()
static std::ostream & notice(std::ostream &)
ostream_manipulator class
Definition: ioutils.h:27
static std::ostream & green(std::ostream &)
friend std::ostream & operator<<(std::ostream &os_, const ostream_manipulator_ref &manip_)
Definition: ioutils.h:65
static bool read_boolean(std::istream &, bool &, uint32_t flags=0)
Parse a boolean.
static std::string to_binary_2(const uint32_t &val)
Definition: ioutils.h:99
bool is_colored() const
static std::ostream & underline(std::ostream &)
void print_multi_lines(std::ostream &out_, const std::string &text_, const std::string &indent_="")
static std::ostream & showbase(std::ostream &)
~io()
Destructor.
static const uint32_t bit02
Definition: bit_mask.h:29
writer_flags
Definition: ioutils.h:102
Definition: ioutils.h:98
static bool read_quoted_string(std::istream &, std::string &, uint32_t flags=0)
Parse a quoted string.
void set_colored(bool)
static ostream_manipulator< int > precision(const int &n)
Definition: ioutils.h:95
static std::ostream & red(std::ostream &)
static std::ostream & warning(std::ostream &)
static const std::string & nan_real_repr()
A portable representation for a NaN.
static const std::string & plus_infinity_real_repr()
A portable representation for +infinity.
The Bayeux/datatools library top-level namespace.
Definition: algo.h:13
Definition: ioutils.h:96
static std::ostream & ostream_precision(std::ostream &os, const int &n)
static std::ostream & normal(std::ostream &)
friend std::ostream & operator<<(std::ostream &, const indenter &)
size_t get_width() const
Returns the with of each indentation level.
Definition: ioutils.h:103
ostream_manipulator_ref class
Definition: ioutils.h:55
static const uint32_t bit00
Definition: bit_mask.h:27
Definition: ioutils.h:104
static void write_boolean(std::ostream &a_out, bool a_bool, bool as_alpha=false)
Write a boolean.
static const std::string & minus_infinity_real_repr()
A portable representation for -infinity.
static std::ostream & page_break(std::ostream &)
size_t get_level() const
Returns the current level of indentation.
static io & instance()
std::ostream & operator()(std::ostream &) const
indenter & operator--(int)
Decrement indendation level by one unit.
static const int REAL8_PRECISION
Default precision for double.
Definition: ioutils.h:111
static void convert_command_line_args(int argc_, char **argv_, std::string &app_name_, std::vector< std::string > &args_)
static std::ostream & right(std::ostream &)