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 #ifndef __iOstream_hpp__
21 #define __iOstream_hpp__
22 
23 
24 #include "IOstream.hpp"
25 
27 const inline char* defaultColor = "\033[0m";
28 const inline char* blackColor = "\033[30m";
29 const inline char* redColor = "\033[31m";
30 const inline char* greenColor = "\033[32m";
31 const inline char* yellowColor = "\033[33m";
32 const inline char* blueColor = "\033[34m";
33 const inline char* magentaColor = "\033[35m";
34 const inline char* cyanColor = "\033[36m";
35 const inline char* whiteColor = "\033[37m";
36 
37 const inline char* boldChar = "\033[1m";
38 
39 #define Red_Text(text) redColor<<text<<defaultColor
40 #define Yellow_Text(text) yellowColor<<text<<defaultColor
41 #define Blue_Text(text) blueColor<<text<<defaultColor
42 #define Green_Text(text) greenColor<<text<<defaultColor
43 #define Magenta_Text(text) magentaColor<<text<<defaultColor
44 #define Cyan_Text(text) cyanColor<<text<<defaultColor
45 #define Bold_Text(text) boldChar<<text<<defaultColor
46 
47 namespace pFlow
48 {
49 
50 // Forward Declarations
51 class token;
52 
59 class iOstream
60 :
61  public IOstream
62 {
63 protected:
64 
65  //- Protected Data
66 
68  static constexpr const unsigned short entryIndentation_ = 16;
69 
71  unsigned short indentSize_ = 4;
72 
74  unsigned short indentLevel_ = 0;
75 
76 
77 public:
78 
79 
81 
83  explicit iOstream()
84  {}
85 
87  explicit iOstream(writeFormat wF):
88  IOstream(wF)
89  {}
90 
92  iOstream(const iOstream&) = default;
93 
95  virtual ~iOstream() = default;
96 
97 
99 
102  virtual bool write(const token& tok) = 0;
103 
105  virtual iOstream& write(const char c) = 0;
106 
108  virtual iOstream& write(const char* str) = 0;
109 
111  virtual iOstream& write(const word& str) = 0;
112 
115  virtual iOstream& writeQuoted
116  (
117  const word& str,
118  const bool quoted=true
119  ) = 0;
120 
122  virtual iOstream& write(const int64 val) = 0;
123 
125  virtual iOstream& write(const int32 val) = 0;
126 
128  virtual iOstream& write(const int8 val) = 0;
129 
131  virtual iOstream& write(const uint64 val) = 0;
132 
134  virtual iOstream& write(const uint32 val) = 0;
135 
137  virtual iOstream& write(const uint8 val) = 0;
138 
140  virtual iOstream& write(const float val) = 0;
141 
143  virtual iOstream& write(const double val) = 0;
144 
146  virtual iOstream& write(const size_t val) = 0;
147 
149  virtual iOstream& write(const char* binaryData, std::streamsize count) = 0;
150 
152  virtual iOstream& writeBinaryBlockFlag();
153 
154  virtual void seek(size_t pos) = 0;
156 
158  virtual void indent() = 0;
159 
161  unsigned short indentSize() const
162  {
163  return indentSize_;
164  }
165 
167  unsigned short& indentSize()
168  {
169  return indentSize_;
170  }
171 
173  unsigned short indentLevel() const
174  {
175  return indentLevel_;
176  }
177 
179  unsigned short& indentLevel()
180  {
181  return indentLevel_;
182  }
183 
185  void incrIndent()
186  {
187  ++indentLevel_;
188  }
189 
191  void decrIndent();
192 
194 
197  virtual iOstream& beginBlock(const word& kw);
198 
201  virtual iOstream& beginBlock();
202 
205  virtual iOstream& endBlock();
206 
208  virtual iOstream& beginList();
209 
211  virtual iOstream& beginList(const word& kw);
212 
214  virtual iOstream& endList();
215 
217  virtual iOstream& beginSquare();
218 
220  virtual iOstream& beginSquare(const word& kw);
221 
223  virtual iOstream& endSquare();
224 
226  virtual iOstream& endEntry();
227 
229  virtual iOstream& newLine();
230 
232  virtual iOstream& space(int32 n=1);
233 
235  virtual iOstream& writeWordKeyword(const word& kw);
236 
238  template<class T>
239  iOstream& writeWordEntry(const word& key, const T& value)
240  {
241  writeWordKeyword(key) << value;
242  return endEntry();
243  }
244 
245 
247 
249  virtual void startOfBinaryStreaming() =0;
250 
252  virtual void endOfBinaryStreaming() = 0 ;
253 
255  virtual void flush() = 0;
256 
258  virtual void endl() = 0;
259 
261  virtual char fill() const = 0;
262 
264  virtual char fill(const char fillch) = 0;
265 
267  virtual int width() const = 0;
268 
270  virtual int width(const int w) = 0;
271 
273  virtual int precision() const = 0;
274 
276  virtual int precision(const int p) = 0;
277 
278 
280 
285  {
286  return const_cast<iOstream&>(*this);
287  }
288 };
289 
290 
291 
293 typedef iOstream& (*iOstreamManip)(iOstream&);
294 
295 
298 {
299  return f(os);
300 }
301 
304 {
305  f(os);
306  return os;
307 }
308 
309 
312 {
313  os.indent();
314  return os;
315 }
316 
319 {
320  os.incrIndent();
321  return os;
322 }
323 
326 {
327  os.decrIndent();
328  return os;
329 }
330 
331 
333 inline iOstream& flush(iOstream& os)
334 {
335  os.flush();
336  return os;
337 }
338 
339 
341 inline iOstream& endl(iOstream& os)
342 {
343  os.endl();
344  return os;
345 }
346 
347 
349 // Increments indentation, adds newline.
351 {
352  os.beginBlock();
353  return os;
354 }
355 
356 
358 // Decrements indentation, adds newline.
360 {
361  os.endBlock();
362  return os;
363 }
364 
365 
368 {
369  os.endEntry();
370  return os;
371 }
372 
373 
374 // overloading for basic types
375 inline iOstream& operator<<( iOstream& os, const char c)
376 {
377  return os.write(c);
378 }
379 
380 inline iOstream& operator<<( iOstream& os, const char * buf)
381 {
382  return os.write(buf);
383 }
384 
385 inline iOstream& operator<<( iOstream& os, const word& w)
386 {
387  return os.write(w);
388 }
389 
390 
391 inline iOstream& operator<<( iOstream& os, const int64& val)
392 {
393  return os.write(val);
394 }
395 
396 inline iOstream& operator<<( iOstream& os, const int32& val)
397 {
398  return os.write(val);
399 }
400 
401 
402 inline iOstream& operator<<( iOstream& os, const int8& val)
403 {
404  return os.write(val);
405 }
406 
407 inline iOstream& operator<<( iOstream& os, const uint64& val)
408 {
409  return os.write(val);
410 }
411 
412 inline iOstream& operator<<( iOstream& os, const uint32& val)
413 {
414  return os.write(val);
415 }
416 
417 inline iOstream& operator<<( iOstream& os, const uint8& val)
418 {
419  return os.write(val);
420 }
421 
422 inline iOstream& operator<<( iOstream& os, const float& val)
423 {
424  return os.write(val);
425 }
426 
427 
428 inline iOstream& operator<<( iOstream& os, const double& val)
429 {
430  return os.write(val);
431 }
432 
433 inline iOstream& operator<<( iOstream& os, const size_t& val)
434 {
435  return os.write(val);
436 }
437 
438 // Useful aliases for tab and newline characters
439 constexpr char tab = '\t';
440 constexpr char nl = '\n';
441 
442 
443 } // pFlow
444 
445 
446 #endif
447 
pFlow::iOstream::~iOstream
virtual ~iOstream()=default
Destructor.
blackColor
const char * blackColor
Definition: iOstream.hpp:28
pFlow::iOstream::beginBlock
virtual iOstream & beginBlock(const word &kw)
Write begin block group with a name Increments indentation, adds newline.
Definition: iOstream.cpp:77
pFlow::iOstream::write
virtual bool write(const token &tok)=0
Write token to stream or otherwise handle it.
pFlow::endBlock
iOstream & endBlock(iOstream &os)
Write end block group.
Definition: iOstream.hpp:359
pFlow::token
Token class based on OpenFOAM stream, with some modifications/simplifications to be tailored to our n...
Definition: token.hpp:44
pFlow::iOstream::precision
virtual int precision() const =0
Get precision of output field.
pFlow::tab
constexpr char tab
Definition: iOstream.hpp:439
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:95
pFlow::iOstream::newLine
virtual iOstream & newLine()
Write a newLine to stream.
Definition: iOstream.cpp:112
pFlow::endEntry
iOstream & endEntry(iOstream &os)
Write end entry (';') followed by newline.
Definition: iOstream.hpp:367
blueColor
const char * blueColor
Definition: iOstream.hpp:32
pFlow::iOstream::seek
virtual void seek(size_t pos)=0
pFlow::iOstream::indent
virtual void indent()=0
Add indentation characters.
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:56
pFlow::word
std::string word
Definition: builtinTypes.hpp:64
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:325
pFlow::iOstream::indentSize
unsigned short & indentSize()
Access to indent size.
Definition: iOstream.hpp:167
pFlow::int64
long long int int64
Definition: builtinTypes.hpp:52
pFlow::iOstream::indentLevel
unsigned short indentLevel() const
Return indent level.
Definition: iOstream.hpp:173
pFlow::flush
iOstream & flush(iOstream &os)
Flush stream.
Definition: iOstream.hpp:333
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:341
boldChar
const char * boldChar
Definition: iOstream.hpp:37
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:284
pFlow::indent
iOstream & indent(iOstream &os)
Indent stream.
Definition: iOstream.hpp:311
pFlow::iOstream::startOfBinaryStreaming
virtual void startOfBinaryStreaming()=0
Add a new line and flush stream.
pFlow::iOstream::indentSize
unsigned short indentSize() const
Return indent level.
Definition: iOstream.hpp:161
pFlow
Definition: demGeometry.hpp:27
pFlow::IOstreamManip
IOstream &(* IOstreamManip)(IOstream &)
An IOstream manipulator.
Definition: IOstream.hpp:308
pFlow::iOstream::endOfBinaryStreaming
virtual void endOfBinaryStreaming()=0
Reach end of file add a new line and flush stream.
pFlow::iOstream::indentLevel
unsigned short & indentLevel()
Access to indent level.
Definition: iOstream.hpp:179
cyanColor
const char * cyanColor
Definition: iOstream.hpp:34
pFlow::iOstream::incrIndent
void incrIndent()
Increment the indent level.
Definition: iOstream.hpp:185
pFlow::iOstream::decrIndent
void decrIndent()
Decrement the indent level.
Definition: iOstream.cpp:34
pFlow::incrIndent
iOstream & incrIndent(iOstream &os)
Increment the indent level.
Definition: iOstream.hpp:318
pFlow::int32
int int32
Definition: builtinTypes.hpp:50
defaultColor
const char * defaultColor
char constants to alter output format and color
Definition: iOstream.hpp:27
redColor
const char * redColor
Definition: iOstream.hpp:29
pFlow::iOstream::flush
virtual void flush()=0
Flush stream.
pFlow::iOstream::beginSquare
virtual iOstream & beginSquare()
Write begin list "[".
Definition: iOstream.cpp:160
pFlow::iOstream::endEntry
virtual iOstream & endEntry()
Write end entry (';') followed by newline.
Definition: iOstream.cpp:104
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:87
whiteColor
const char * whiteColor
Definition: iOstream.hpp:35
n
uint32 n
Definition: NBSLoop.hpp:24
pFlow::iOstream::endSquare
virtual iOstream & endSquare()
Write end list "]".
Definition: iOstream.cpp:179
IOstream.hpp
magentaColor
const char * magentaColor
Definition: iOstream.hpp:33
pFlow::iOstream::indentSize_
unsigned short indentSize_
Number of spaces per indent level.
Definition: iOstream.hpp:71
pFlow::iOstream::iOstream
iOstream()
Default.
Definition: iOstream.hpp:83
pFlow::uint64
unsigned long long int uint64
Definition: builtinTypes.hpp:58
pFlow::iOstream::endList
virtual iOstream & endList()
Write end list ")".
Definition: iOstream.cpp:151
pFlow::beginBlock
iOstream & beginBlock(iOstream &os)
Write begin block group without a name.
Definition: iOstream.hpp:350
pFlow::iOstream::beginList
virtual iOstream & beginList()
Write begin list "(".
Definition: iOstream.cpp:132
pFlow::int8
signed char int8
Definition: builtinTypes.hpp:48
pFlow::iOstream::writeBinaryBlockFlag
virtual iOstream & writeBinaryBlockFlag()
Write the flag to indicate the start of a binary block.
Definition: iOstream.cpp:28
pFlow::IOstream::writeFormat
writeFormat
Definition: IOstream.hpp:59
yellowColor
const char * yellowColor
Definition: iOstream.hpp:31
pFlow::iOstream::entryIndentation_
static constexpr const unsigned short entryIndentation_
Indentation of the entry from the start of the keyword.
Definition: iOstream.hpp:68
pFlow::iOstream::fill
virtual char fill() const =0
Get padding character.
pFlow::uint8
unsigned char uint8
Definition: builtinTypes.hpp:54
pFlow::iOstream::space
virtual iOstream & space(int32 n=1)
Write space to stream.
Definition: iOstream.cpp:119
pFlow::nl
constexpr char nl
Definition: iOstream.hpp:440
pFlow::IOstream
A base calss for input/output streams.
Definition: IOstream.hpp:47
pFlow::iOstream
Interface class for any output stream.
Definition: iOstream.hpp:59
greenColor
const char * greenColor
Definition: iOstream.hpp:30
pFlow::iOstream::writeWordKeyword
virtual iOstream & writeWordKeyword(const word &kw)
Write the keyword followed by an appropriate indentation.
Definition: iOstream.cpp:48
pFlow::iOstream::writeWordEntry
iOstream & writeWordEntry(const word &key, const T &value)
Write a keyword/value entry.
Definition: iOstream.hpp:239
pFlow::iOstream::beginBlock
virtual iOstream & beginBlock()
Write begin block group without a name Increments indentation, adds newline.
Definition: iOstream.cpp:86
pFlow::iOstreamManip
iOstream &(* iOstreamManip)(iOstream &)
An iOstream manipulator.
Definition: iOstream.hpp:293
pFlow::iOstream::indentLevel_
unsigned short indentLevel_
Current indent level.
Definition: iOstream.hpp:74