www.cemf.ir
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 
62  static constexpr const unsigned short entryIndentation_ = 16;
63 
65  unsigned short indentSize_ = 4;
66 
68  unsigned short indentLevel_ = 0;
69 
70 
71 public:
72 
73 
74  // Constructors
75 
77  explicit iOstream()
78  {}
79 
81  explicit iOstream(writeFormat wF):
82  IOstream(wF)
83  {}
84 
86  iOstream(const iOstream&) = default;
87 
89  virtual ~iOstream() = default;
90 
91 
93 
96  virtual bool write(const token& tok) = 0;
97 
99  virtual iOstream& write(const char c) = 0;
100 
102  virtual iOstream& write(const char* str) = 0;
103 
105  virtual iOstream& write(const word& str) = 0;
106 
109  virtual iOstream& writeQuoted
110  (
111  const word& str,
112  const bool quoted=true
113  ) = 0;
114 
116  virtual iOstream& write(const int64 val) = 0;
117 
119  virtual iOstream& write(const int32 val) = 0;
120 
122  virtual iOstream& write(const label val) = 0;
123 
125  virtual iOstream& write(const uint32 val) = 0;
126 
128  virtual iOstream& write(const uint16 val) = 0;
129 
131  virtual iOstream& write(const float val) = 0;
132 
134  virtual iOstream& write(const double val) = 0;
135 
137  virtual iOstream& write(const char* binaryData, std::streamsize count) = 0;
138 
139 
140  // - Indent
141 
143  virtual void indent() = 0;
144 
146  unsigned short indentSize() const
147  {
148  return indentSize_;
149  }
150 
152  unsigned short& indentSize()
153  {
154  return indentSize_;
155  }
156 
158  unsigned short indentLevel() const
159  {
160  return indentLevel_;
161  }
162 
164  unsigned short& indentLevel()
165  {
166  return indentLevel_;
167  }
168 
170  void incrIndent()
171  {
172  ++indentLevel_;
173  }
174 
176  void decrIndent();
177 
178  //- Punctuations
179 
182  virtual iOstream& beginBlock(const word& kw);
183 
186  virtual iOstream& beginBlock();
187 
190  virtual iOstream& endBlock();
191 
193  virtual iOstream& beginList();
194 
196  virtual iOstream& beginList(const word& kw);
197 
199  virtual iOstream& endList();
200 
202  virtual iOstream& beginSquare();
203 
205  virtual iOstream& beginSquare(const word& kw);
206 
208  virtual iOstream& endSquare();
209 
211  virtual iOstream& endEntry();
212 
214  virtual iOstream& newLine();
215 
217  virtual iOstream& space(int32 n=1);
218 
220  virtual iOstream& writeWordKeyword(const word& kw);
221 
223  template<class T>
224  iOstream& writeWordEntry(const word& key, const T& value)
225  {
226  writeWordKeyword(key) << value;
227  return endEntry();
228  }
229 
230 
231  //- Stream state functions
232 
234  virtual void flush() = 0;
235 
237  virtual void endl() = 0;
238 
240  virtual char fill() const = 0;
241 
243  virtual char fill(const char fillch) = 0;
244 
246  virtual int width() const = 0;
247 
249  virtual int width(const int w) = 0;
250 
252  virtual int precision() const = 0;
253 
255  virtual int precision(const int p) = 0;
256 
257 
258  //- Member Operators
259 
264  {
265  return const_cast<iOstream&>(*this);
266  }
267 };
268 
269 
270 
272 typedef iOstream& (*iOstreamManip)(iOstream&);
273 
274 
277 {
278  return f(os);
279 }
280 
283 {
284  f(os);
285  return os;
286 }
287 
288 
291 {
292  os.indent();
293  return os;
294 }
295 
298 {
299  os.incrIndent();
300  return os;
301 }
302 
305 {
306  os.decrIndent();
307  return os;
308 }
309 
310 
312 inline iOstream& flush(iOstream& os)
313 {
314  os.flush();
315  return os;
316 }
317 
318 
320 inline iOstream& endl(iOstream& os)
321 {
322  os.endl();
323  return os;
324 }
325 
326 
328 // Increments indentation, adds newline.
330 {
331  os.beginBlock();
332  return os;
333 }
334 
335 
337 // Decrements indentation, adds newline.
339 {
340  os.endBlock();
341  return os;
342 }
343 
344 
347 {
348  os.endEntry();
349  return os;
350 }
351 
352 
353 // overloading for basic types
354 inline iOstream& operator<<( iOstream& os, const char c)
355 {
356  return os.write(c);
357 }
358 
359 inline iOstream& operator<<( iOstream& os, const char * buf)
360 {
361  return os.write(buf);
362 }
363 
364 inline iOstream& operator<<( iOstream& os, const word& w)
365 {
366  return os.write(w);
367 }
368 
369 
370 inline iOstream& operator<<( iOstream& os, const int64& val)
371 {
372  return os.write(val);
373 }
374 
375 inline iOstream& operator<<( iOstream& os, const int32& val)
376 {
377  return os.write(val);
378 }
379 
380 inline iOstream& operator<<( iOstream& os, const int16& val)
381 {
382  return os.write(val);
383 }
384 
385 inline iOstream& operator<<( iOstream& os, const int8& val)
386 {
387  return os.write(val);
388 }
389 
390 inline iOstream& operator<<( iOstream& os, const label& val)
391 {
392  return os.write(val);
393 }
394 
395 inline iOstream& operator<<( iOstream& os, const uint32& val)
396 {
397  return os.write(val);
398 }
399 
400 inline iOstream& operator<<( iOstream& os, const uint16& val)
401 {
402  return os.write(val);
403 }
404 
405 inline iOstream& operator<<( iOstream& os, const float& val)
406 {
407  return os.write(val);
408 }
409 
410 
411 inline iOstream& operator<<( iOstream& os, const double& val)
412 {
413  return os.write(val);
414 }
415 // Useful aliases for tab and newline characters
416 constexpr char tab = '\t';
417 constexpr char nl = '\n';
418 
419 
420 
421 
422 } // pFlow
423 
424 
425 #endif
426 
427 // ************************************************************************* //
pFlow::iOstream::~iOstream
virtual ~iOstream()=default
Destructor.
blackColor
const char * blackColor
Definition: iOstream.hpp:32
pFlow::iOstream::beginBlock
virtual iOstream & beginBlock(const word &kw)
Write begin block group with a name Increments indentation, adds newline.
Definition: iOstream.cpp:70
pFlow::iOstream::write
virtual bool write(const token &tok)=0
Write Functions.
pFlow::endBlock
iOstream & endBlock(iOstream &os)
Write end block group.
Definition: iOstream.hpp:338
pFlow::token
Definition: token.hpp:42
pFlow::iOstream::precision
virtual int precision() const =0
Get precision of output field.
pFlow::tab
constexpr char tab
Definition: iOstream.hpp:416
pFlow::iOstream::endl
virtual void endl()=0
Add newline and flush stream.
pFlow::iOstream::endBlock
virtual iOstream & endBlock()
Write end block group Decrements indentation, adds newline.
Definition: iOstream.cpp:88
pFlow::iOstream::newLine
virtual iOstream & newLine()
Write a newLine to stream.
Definition: iOstream.cpp:105
pFlow::endEntry
iOstream & endEntry(iOstream &os)
Write end entry (';') followed by newline.
Definition: iOstream.hpp:346
blueColor
const char * blueColor
Definition: iOstream.hpp:36
pFlow::iOstream::indent
virtual void indent()=0
Add indentation characters.
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
Get width of output field.
pFlow::decrIndent
iOstream & decrIndent(iOstream &os)
Decrement the indent level.
Definition: iOstream.hpp:304
pFlow::iOstream::indentSize
unsigned short & indentSize()
Access to indent size.
Definition: iOstream.hpp:152
pFlow::int64
long long int int64
Definition: builtinTypes.hpp:55
pFlow::iOstream::indentLevel
unsigned short indentLevel() const
Return indent level.
Definition: iOstream.hpp:158
pFlow::flush
iOstream & flush(iOstream &os)
Flush stream.
Definition: iOstream.hpp:312
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:320
boldChar
const char * boldChar
Definition: iOstream.hpp:41
pFlow::iOstream::operator()
iOstream & operator()() const
Return a non-const reference to const iOstream Needed for write functions where the stream argument i...
Definition: iOstream.hpp:263
pFlow::indent
iOstream & indent(iOstream &os)
Indent stream.
Definition: iOstream.hpp:290
pFlow::iOstream::indentSize
unsigned short indentSize() const
Return indent level.
Definition: iOstream.hpp:146
pFlow
Definition: demComponent.hpp:28
pFlow::IOstreamManip
IOstream &(* IOstreamManip)(IOstream &)
An IOstream manipulator.
Definition: IOstream.hpp:307
pFlow::iOstream::indentLevel
unsigned short & indentLevel()
Access to indent level.
Definition: iOstream.hpp:164
cyanColor
const char * cyanColor
Definition: iOstream.hpp:38
pFlow::iOstream::incrIndent
void incrIndent()
Increment the indent level.
Definition: iOstream.hpp:170
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()
Decrement the indent level.
Definition: iOstream.cpp:27
pFlow::incrIndent
iOstream & incrIndent(iOstream &os)
Increment the indent level.
Definition: iOstream.hpp:297
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
Flush stream.
pFlow::iOstream::beginSquare
virtual iOstream & beginSquare()
Write begin list "[".
Definition: iOstream.cpp:153
pFlow::iOstream::endEntry
virtual iOstream & endEntry()
Write end entry (';') followed by newline.
Definition: iOstream.cpp:97
pFlow::count
auto count(const Vector< T, Allocator > &vec, const T &val)
Definition: VectorAlgorithm.hpp:26
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
Write std::string surrounded by quotes.
pFlow::iOstream::iOstream
iOstream(writeFormat wF)
Construct from writeFormat.
Definition: iOstream.hpp:81
whiteColor
const char * whiteColor
Definition: iOstream.hpp:39
pFlow::iOstream::endSquare
virtual iOstream & endSquare()
Write end list "]".
Definition: iOstream.cpp:172
IOstream.hpp
magentaColor
const char * magentaColor
Definition: iOstream.hpp:37
pFlow::iOstream::indentSize_
unsigned short indentSize_
Number of spaces per indent level.
Definition: iOstream.hpp:65
pFlow::iOstream::iOstream
iOstream()
Default.
Definition: iOstream.hpp:77
pFlow::iOstream::endList
virtual iOstream & endList()
Write end list ")".
Definition: iOstream.cpp:144
pFlow::beginBlock
iOstream & beginBlock(iOstream &os)
Write begin block group without a name.
Definition: iOstream.hpp:329
pFlow::label
std::size_t label
Definition: builtinTypes.hpp:61
pFlow::iOstream::beginList
virtual iOstream & beginList()
Write begin list "(".
Definition: iOstream.cpp:125
pFlow::int8
signed char int8
Definition: builtinTypes.hpp:49
pFlow::IOstream::writeFormat
writeFormat
Definition: IOstream.hpp:57
yellowColor
const char * yellowColor
Definition: iOstream.hpp:35
pFlow::iOstream::entryIndentation_
static constexpr const unsigned short entryIndentation_
Indentation of the entry from the start of the keyword.
Definition: iOstream.hpp:62
pFlow::iOstream::fill
virtual char fill() const =0
Get padding character.
pFlow::iOstream::space
virtual iOstream & space(int32 n=1)
Write space to stream.
Definition: iOstream.cpp:112
pFlow::nl
constexpr char nl
Definition: iOstream.hpp:417
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)
Write the keyword followed by an appropriate indentation.
Definition: iOstream.cpp:41
pFlow::iOstream::writeWordEntry
iOstream & writeWordEntry(const word &key, const T &value)
Write a keyword/value entry.
Definition: iOstream.hpp:224
pFlow::iOstream::beginBlock
virtual iOstream & beginBlock()
Write begin block group without a name Increments indentation, adds newline.
Definition: iOstream.cpp:79
pFlow::iOstreamManip
iOstream &(* iOstreamManip)(iOstream &)
An iOstream manipulator.
Definition: iOstream.hpp:272
pFlow::iOstream::indentLevel_
unsigned short indentLevel_
Current indent level.
Definition: iOstream.hpp:68