iOstream.hpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 
21 
22 #ifndef __iOstream_hpp__
23 #define __iOstream_hpp__
24 
25 // based on OpenFOAM stream, with some modifications/simplifications
26 // to be tailored to our needs
27 
28 
29 #include "IOstream.hpp"
30 
31 const inline char* defaultColor = "\033[0m";
32 const inline char* blackColor = "\033[30m";
33 const inline char* redColor = "\033[31m";
34 const inline char* greenColor = "\033[32m";
35 const inline char* yellowColor = "\033[33m";
36 const inline char* blueColor = "\033[34m";
37 const inline char* magentaColor = "\033[35m";
38 const inline char* cyanColor = "\033[36m";
39 const inline char* whiteColor = "\033[37m";
40 
41 const inline char* boldChar = "\033[1m";
42 
43 
44 
45 namespace pFlow
46 {
47 
48 // Forward Declarations
49 class token;
50 
51 
52 
53 class iOstream
54 :
55  public IOstream
56 {
57 protected:
58 
59  // Protected Data
60 
61  //- Indentation of the entry from the start of the keyword
62  static constexpr const unsigned short entryIndentation_ = 16;
63 
64  //- Number of spaces per indent level
65  unsigned short indentSize_ = 4;
66 
67  //- Current indent level
68  unsigned short indentLevel_ = 0;
69 
70 
71 public:
72 
73 
74  // Constructor
75  explicit iOstream()
76  {}
77 
78  //- Copy construct
79  iOstream(const iOstream&) = default;
80 
81  //- Destructor
82  virtual ~iOstream() = default;
83 
84 
85  // Write Functions
86 
87  //- Write token to stream or otherwise handle it.
88  // \return false if the token type was not handled by this method
89  virtual bool write(const token& tok) = 0;
90 
91  //- Write character
92  virtual iOstream& write(const char c) = 0;
93 
94  //- Write character string
95  virtual iOstream& write(const char* str) = 0;
96 
97  //- Write word
98  virtual iOstream& write(const word& str) = 0;
99 
100 
101  //- Write std::string surrounded by quotes.
102  // Optional write without quotes.
103  virtual iOstream& writeQuoted
104  (
105  const word& str,
106  const bool quoted=true
107  ) = 0;
108 
109 
110  //- Write int64
111  virtual iOstream& write(const int64 val) = 0;
112 
113  //- Write int32
114  virtual iOstream& write(const int32 val) = 0;
115 
116  //- Write label
117  virtual iOstream& write(const label val) = 0;
118 
119  //- Write uint32
120  virtual iOstream& write(const uint32 val) = 0;
121 
122  //- Write uint16
123  virtual iOstream& write(const uint16 val) = 0;
124 
125  //- Write float
126  virtual iOstream& write(const float val) = 0;
127 
128  //- Write double
129  virtual iOstream& write(const double val) = 0;
130 
131 
132 
133  //- Add indentation characters
134  virtual void indent() = 0;
135 
136  //- Return indent level
137  unsigned short indentSize() const
138  {
139  return indentSize_;
140  }
141 
142  //- Access to indent size
143  unsigned short& indentSize()
144  {
145  return indentSize_;
146  }
147 
148  //- Return indent level
149  unsigned short indentLevel() const
150  {
151  return indentLevel_;
152  }
153 
154  //- Access to indent level
155  unsigned short& indentLevel()
156  {
157  return indentLevel_;
158  }
159 
160  //- Increment the indent level
161  void incrIndent()
162  {
163  ++indentLevel_;
164  }
165 
166  //- Decrement the indent level
167  void decrIndent();
168 
169 
170 
171 
172  //- Write begin block group with a name
173  // Increments indentation, adds newline.
174  virtual iOstream& beginBlock(const word& kw);
175 
176  //- Write begin block group without a name
177  // Increments indentation, adds newline.
178  virtual iOstream& beginBlock();
179 
180  //- Write end block group
181  // Decrements indentation, adds newline.
182  virtual iOstream& endBlock();
183 
184  //- Write begin list "("
185  virtual iOstream& beginList();
186 
187  //- Write begin list with keyword "kw ("
188  virtual iOstream& beginList(const word& kw);
189 
190  //- Write end list ")"
191  virtual iOstream& endList();
192 
193  //- Write begin list "["
194  virtual iOstream& beginSquare();
195 
196  //- Write begin list with keyword "kw ["
197  virtual iOstream& beginSquare(const word& kw);
198 
199  //- Write end list "]"
200  virtual iOstream& endSquare();
201 
202  //- Write end entry (';') followed by newline.
203  virtual iOstream& endEntry();
204 
205  //- Write a newLine to stream
206  virtual iOstream& newLine();
207 
208  //- Write space to stream
209  virtual iOstream& space(int32 n=1);
210 
211 
212  //- Write the keyword followed by an appropriate indentation
213  virtual iOstream& writeWordKeyword(const word& kw);
214 
215  //- Write a keyword/value entry.
216  template<class T>
217  iOstream& writeWordEntry(const word& key, const T& value)
218  {
219  writeWordKeyword(key) << value;
220  return endEntry();
221  }
222 
224 
225  //- Flush stream
226  virtual void flush() = 0;
227 
228  //- Add newline and flush stream
229  virtual void endl() = 0;
230 
231  //- Get padding character
232  virtual char fill() const = 0;
233 
234  //- Set padding character for formatted field up to field width
235  virtual char fill(const char fillch) = 0;
236 
237  //- Get width of output field
238  virtual int width() const = 0;
239 
240  //- Set width of output field (and return old width)
241  virtual int width(const int w) = 0;
242 
243  //- Get precision of output field
244  virtual int precision() const = 0;
245 
246  //- Set precision of output field (and return old precision)
247  virtual int precision(const int p) = 0;
248 
249 
250  // Member Operators
251 
252  //- Return a non-const reference to const iOstream
253  // Needed for write functions where the stream argument is temporary:
254  // e.g. thing thisThing(OFstream("thingFileName")());
256  {
257  return const_cast<iOstream&>(*this);
258  }
259 };
260 
261 
262 
263 //- An iOstream manipulator
264 typedef iOstream& (*iOstreamManip)(iOstream&);
265 
266 
267 //- operator<< handling for manipulators without arguments
269 {
270  return f(os);
271 }
272 
273 //- operator<< handling for manipulators without arguments
275 {
276  f(os);
277  return os;
278 }
279 
280 
281 //- Indent stream
283 {
284  os.indent();
285  return os;
286 }
287 
288 //- Increment the indent level
290 {
291  os.incrIndent();
292  return os;
293 }
294 
295 //- Decrement the indent level
297 {
298  os.decrIndent();
299  return os;
300 }
301 
302 
303 //- Flush stream
304 inline iOstream& flush(iOstream& os)
305 {
306  os.flush();
307  return os;
308 }
309 
310 
311 //- Add newline and flush stream
312 inline iOstream& endl(iOstream& os)
313 {
314  os.endl();
315  return os;
316 }
317 
318 
319 //- Write begin block group without a name
320 // Increments indentation, adds newline.
322 {
323  os.beginBlock();
324  return os;
325 }
326 
327 
328 //- Write end block group
329 // Decrements indentation, adds newline.
331 {
332  os.endBlock();
333  return os;
334 }
335 
336 
337 //- Write end entry (';') followed by newline.
339 {
340  os.endEntry();
341  return os;
342 }
343 
344 
345 // overloading for basic types
346 inline iOstream& operator<<( iOstream& os, const char c)
347 {
348  return os.write(c);
349 }
350 
351 inline iOstream& operator<<( iOstream& os, const char * buf)
352 {
353  return os.write(buf);
354 }
355 
356 inline iOstream& operator<<( iOstream& os, const word& w)
357 {
358  return os.write(w);
359 }
360 
361 
362 inline iOstream& operator<<( iOstream& os, const int64& val)
363 {
364  return os.write(val);
365 }
366 
367 inline iOstream& operator<<( iOstream& os, const int32& val)
368 {
369  return os.write(val);
370 }
371 
372 inline iOstream& operator<<( iOstream& os, const int16& val)
373 {
374  return os.write(val);
375 }
376 
377 inline iOstream& operator<<( iOstream& os, const int8& val)
378 {
379  return os.write(val);
380 }
381 
382 inline iOstream& operator<<( iOstream& os, const label& val)
383 {
384  return os.write(val);
385 }
386 
387 inline iOstream& operator<<( iOstream& os, const uint32& val)
388 {
389  return os.write(val);
390 }
391 
392 inline iOstream& operator<<( iOstream& os, const uint16& val)
393 {
394  return os.write(val);
395 }
396 
397 inline iOstream& operator<<( iOstream& os, const float& val)
398 {
399  return os.write(val);
400 }
401 
402 
403 inline iOstream& operator<<( iOstream& os, const double& val)
404 {
405  return os.write(val);
406 }
407 // Useful aliases for tab and newline characters
408 constexpr char tab = '\t';
409 constexpr char nl = '\n';
410 
411 
412 
413 
414 } // pFlow
415 
416 
417 #endif
418 
419 // ************************************************************************* //
pFlow::iOstream::~iOstream
virtual ~iOstream()=default
blackColor
const char * blackColor
Definition: iOstream.hpp:32
pFlow::iOstream::beginBlock
virtual iOstream & beginBlock(const word &kw)
Definition: iOstream.cpp:70
pFlow::iOstream::write
virtual bool write(const token &tok)=0
pFlow::endBlock
iOstream & endBlock(iOstream &os)
Definition: iOstream.hpp:330
pFlow::token
Definition: token.hpp:42
pFlow::iOstream::precision
virtual int precision() const =0
pFlow::tab
constexpr char tab
Definition: iOstream.hpp:408
pFlow::iOstream::endl
virtual void endl()=0
pFlow::iOstream::endBlock
virtual iOstream & endBlock()
Definition: iOstream.cpp:88
pFlow::iOstream::newLine
virtual iOstream & newLine()
Definition: iOstream.cpp:105
pFlow::endEntry
iOstream & endEntry(iOstream &os)
Definition: iOstream.hpp:338
blueColor
const char * blueColor
Definition: iOstream.hpp:36
pFlow::iOstream::indent
virtual void indent()=0
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:59
pFlow::word
std::string word
Definition: builtinTypes.hpp:63
pFlow::iOstream::width
virtual int width() const =0
pFlow::decrIndent
iOstream & decrIndent(iOstream &os)
Definition: iOstream.hpp:296
pFlow::iOstream::indentSize
unsigned short & indentSize()
Definition: iOstream.hpp:143
pFlow::int64
long long int int64
Definition: builtinTypes.hpp:55
pFlow::iOstream::indentLevel
unsigned short indentLevel() const
Definition: iOstream.hpp:149
pFlow::flush
iOstream & flush(iOstream &os)
Definition: iOstream.hpp:304
pFlow::endl
iOstream & endl(iOstream &os)
Definition: iOstream.hpp:312
boldChar
const char * boldChar
Definition: iOstream.hpp:41
pFlow::iOstream::operator()
iOstream & operator()() const
Definition: iOstream.hpp:255
pFlow::indent
iOstream & indent(iOstream &os)
Definition: iOstream.hpp:282
pFlow::iOstream::indentSize
unsigned short indentSize() const
Definition: iOstream.hpp:137
pFlow
Definition: demComponent.hpp:28
pFlow::IOstreamManip
IOstream &(* IOstreamManip)(IOstream &)
Definition: IOstream.hpp:273
pFlow::iOstream::indentLevel
unsigned short & indentLevel()
Definition: iOstream.hpp:155
cyanColor
const char * cyanColor
Definition: iOstream.hpp:38
pFlow::iOstream::incrIndent
void incrIndent()
Definition: iOstream.hpp:161
n
int32 n
Definition: NBSCrossLoop.hpp:24
pFlow::int16
short int int16
Definition: builtinTypes.hpp:51
pFlow::uint16
unsigned short int uint16
Definition: builtinTypes.hpp:57
pFlow::iOstream::decrIndent
void decrIndent()
Definition: iOstream.cpp:27
pFlow::incrIndent
iOstream & incrIndent(iOstream &os)
Definition: iOstream.hpp:289
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
defaultColor
const char * defaultColor
Definition: iOstream.hpp:31
redColor
const char * redColor
Definition: iOstream.hpp:33
pFlow::iOstream::flush
virtual void flush()=0
pFlow::iOstream::beginSquare
virtual iOstream & beginSquare()
Definition: iOstream.cpp:153
pFlow::iOstream::endEntry
virtual iOstream & endEntry()
Definition: iOstream.cpp:97
pFlow::operator<<
INLINE_FUNCTION iOstream & operator<<(iOstream &str, const AB3History &ab3)
Definition: AdamsBashforth3.hpp:57
pFlow::iOstream::writeQuoted
virtual iOstream & writeQuoted(const word &str, const bool quoted=true)=0
whiteColor
const char * whiteColor
Definition: iOstream.hpp:39
pFlow::iOstream::endSquare
virtual iOstream & endSquare()
Definition: iOstream.cpp:172
IOstream.hpp
magentaColor
const char * magentaColor
Definition: iOstream.hpp:37
pFlow::iOstream::indentSize_
unsigned short indentSize_
Definition: iOstream.hpp:65
pFlow::iOstream::iOstream
iOstream()
Definition: iOstream.hpp:75
pFlow::iOstream::endList
virtual iOstream & endList()
Definition: iOstream.cpp:144
pFlow::beginBlock
iOstream & beginBlock(iOstream &os)
Definition: iOstream.hpp:321
pFlow::label
std::size_t label
Definition: builtinTypes.hpp:61
pFlow::iOstream::beginList
virtual iOstream & beginList()
Definition: iOstream.cpp:125
pFlow::int8
signed char int8
Definition: builtinTypes.hpp:49
yellowColor
const char * yellowColor
Definition: iOstream.hpp:35
pFlow::iOstream::entryIndentation_
static constexpr const unsigned short entryIndentation_
Definition: iOstream.hpp:62
pFlow::iOstream::fill
virtual char fill() const =0
pFlow::iOstream::space
virtual iOstream & space(int32 n=1)
Definition: iOstream.cpp:112
pFlow::nl
constexpr char nl
Definition: iOstream.hpp:409
pFlow::IOstream
Definition: IOstream.hpp:45
pFlow::iOstream
Definition: iOstream.hpp:53
greenColor
const char * greenColor
Definition: iOstream.hpp:34
pFlow::iOstream::writeWordKeyword
virtual iOstream & writeWordKeyword(const word &kw)
Definition: iOstream.cpp:41
pFlow::iOstream::writeWordEntry
iOstream & writeWordEntry(const word &key, const T &value)
Definition: iOstream.hpp:217
pFlow::iOstream::beginBlock
virtual iOstream & beginBlock()
Definition: iOstream.cpp:79
pFlow::iOstreamManip
iOstream &(* iOstreamManip)(iOstream &)
Definition: iOstream.hpp:264
pFlow::iOstream::indentLevel_
unsigned short indentLevel_
Definition: iOstream.hpp:68