C++ main module for stochmagnet Package  1.0
CORE_Out.h
Go to the documentation of this file.
1 #ifndef CORE_Out_H
2 #define CORE_Out_H
3 
4 
5 //base class header
6 #include "CORE_Object.h"
7 
8 //shared pointer header
9 #include "sp.h"
10 
11 //iostream header
12 #include <iostream>
13 
14 //files header
15 #include <fstream>
16 
17 //exception header
18 #include "CORE_Exception.h"
19 
28 class CORE_Out : public CORE_Object {
29  // ATTRIBUTES
30 
31  public:
32 
33 
34 private:
35  // ASSOCIATIONS
36 
37 
38  // METHODS
39 
40 
41 
42 public:
43  //for printing in screen
44  static const tUCInt NO_OUTPUT=0;
45 
46  //for printing in screen
47  static const tUCInt SCREEN_OUTPUT=1;
48 
49  //for printing in file
50  static const tUCInt FILE_OUTPUT=2;
51 
52  //for printing in all
53  static const tUCInt ALL_OUTPUT=3;
54 
55  //output file mode : append to file
56  static const tFlag APPEND=1;
57 
58  //output file mode : create file
59  static const tFlag CREATE=0;
60 
61 private:
62  //file output mode APPEND `| CREATE
65 
66  //current output file
67  std::ofstream *mOutputFileStream;
68 
69  //mOutput =mScreen+2*mFile
71  //file output
73 
74  //backup & restore;
77 
78 
79 
80 
81 public:
82  // CONSTRUCTORS
86  mOutputFileStream=null;
87  reset();
88  }
89 
90 
91 
92 
93  // DESTRUCTORS
97  virtual ~CORE_Out(void) {
98  release();
99  }
100 
101 
102 
103 
104 public:
105 
106 
107  //New defaut output
113  }
114 
115 
116 public:
117 
118  // OTHERS
119 
128  virtual tMemSize getMemorySize() const override {
129  return sizeof(*this)+getContentsMemorySize();
130  }
139  virtual tMemSize getContentsMemorySize() const {
141  }
142  public:
145  inline void update() {
146  //release
147  release();
148 
149  //create file output if used
150  if (isOutput(FILE_OUTPUT)) {
151  if (mOutputFileMode==APPEND) {
152  mOutputFileStream=new std::ofstream(mOutputFileName.c_str(),std::ios::app);
153  } else {
154  mOutputFileStream=new std::ofstream(mOutputFileName.c_str(),std::ios::out);
155  }
156  if (mOutputFileStream==NULL) {
157  throw CORE_Exception("core","CORE_Out",
158  "impossible to write in output file "+mOutputFileName);
159  }
160 
161  }
162  }
165  inline void release() {
166  if (mOutputFileStream!=null) {
167  mOutputFileStream->close();
168  delete mOutputFileStream;
169  mOutputFileStream=null;
170  }
171  }
174  inline void reset() {
177  mOutputFileName="./core.log";
178  backup();
179  update();
180  }
193  inline void setOutput(const tUSInt& mode) {
194  mOutput=mode;
195  }
208  inline void setOutput(const tString& mode) {
209  tUCInt p=1;//2^0
210  tUCInt m=0;
211  std::string::const_reverse_iterator iS=mode.rbegin();
212  while (iS!=mode.rend()) {
213  //m+=2^p.mode[l-1-p]
214  m+=p*(*iS);
215 
216  //next element
217  p*=2;
218  iS++;
219  }
220  setOutput(m);
221  }
222 
226  inline tBoolean isOutput(const tUCInt& mode) const {
227  return ((mOutput & mode ) / mode == 1);
228  }
229 
230 
235  inline void setOutputFile(const tString& fileName,const tFlag& mode) {
236  mOutputFileName=fileName;
237  mOutputFileMode=mode;
238  }
241  inline void setOutputFile(const tString& fileName) {
242  setOutputFile(fileName,CREATE);
243  }
244 
245 
249  inline const tString& getOutputFileName() const {
250  return mOutputFileName;
251  }
252 
255  inline void backup() {
259  }
260 
263  inline void restore() {
267 
268  //update the state
269  update();
270  }
271 
275  inline void println(const tString& msg) {
276  (*this)<<msg<<"\n";
277  }
278 
282  inline void print(const tString& msg) {
283  (*this)<<msg;
284  }
288  inline void printError(const tString& msg) {
289  std::cerr<<msg<<std::flush<<"\n";
290  }
291 
292  inline static void PrintProgressBar(const tString& message,const tInt& percent,tInt& oldPercent) {
293  if (percent>oldPercent) {
294  std::cout << "\r "<<message<<" [" << std::setw(3) << percent << "%] "<<std::flush;
295  oldPercent=percent;
296  }
297  }
298 
299 
300  // << operators
301  // =============
302 
308  template<typename T>
309  friend CORE_Out& operator <<(CORE_Out& out,const T& obj) {
310  if (out.isOutput(SCREEN_OUTPUT)) std::cout<<obj<<std::flush;
311  if (out.isOutput(FILE_OUTPUT) && (out.mOutputFileStream!=null)) (*out.mOutputFileStream)<<obj<<std::flush;
312  return out;
313  };
314 
315 
316 
317 
318 };
319 
320 #endif
this class describes the exceptions raised for CORE package
Definition: CORE_Exception.h:17
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:94
abstract base class for most classes.
Definition: CORE_Object.h:48
virtual tMemSize getContentsMemorySize() const
return nthe memory size of the included associations
Definition: CORE_Object.h:259
this class describes the output stream by default write on standart output
Definition: CORE_Out.h:28
void reset()
reset the state of the class
Definition: CORE_Out.h:174
virtual tMemSize getContentsMemorySize() const
return nthe memory size of the included associations
Definition: CORE_Out.h:139
void restore()
restore the state after change
Definition: CORE_Out.h:263
void printError(const tString &msg)
print the message on error output
Definition: CORE_Out.h:288
static const tUCInt ALL_OUTPUT
Definition: CORE_Out.h:53
static void PrintProgressBar(const tString &message, const tInt &percent, tInt &oldPercent)
Definition: CORE_Out.h:292
void setOutput(const tUSInt &mode)
set outputType of the binary form v=2*f+s
Definition: CORE_Out.h:193
void setOutputFile(const tString &fileName)
set output file
Definition: CORE_Out.h:241
friend CORE_Out & operator<<(CORE_Out &out, const T &obj)
print Operators
Definition: CORE_Out.h:309
tFlag mOutput_back
Definition: CORE_Out.h:75
void print(const tString &msg)
print the message
Definition: CORE_Out.h:282
virtual ~CORE_Out(void)
destroy a CORE_Out
Definition: CORE_Out.h:97
tString mOutputFileName_back
Definition: CORE_Out.h:64
void release()
release the pointers
Definition: CORE_Out.h:165
static const tUCInt FILE_OUTPUT
Definition: CORE_Out.h:50
static CORE_UniquePointer< CORE_Out > New()
build a new instance of default output
Definition: CORE_Out.h:111
static const tUCInt SCREEN_OUTPUT
Definition: CORE_Out.h:47
void println(const tString &msg)
print the message
Definition: CORE_Out.h:275
static const tFlag CREATE
Definition: CORE_Out.h:59
tFlag mOutputFileMode_back
Definition: CORE_Out.h:76
tUCInt mOutput
Definition: CORE_Out.h:70
static const tUCInt NO_OUTPUT
Definition: CORE_Out.h:44
CORE_Out()
build a CORE_Out
Definition: CORE_Out.h:85
const tString & getOutputFileName() const
get the output file name
Definition: CORE_Out.h:249
void backup()
backup the state before change
Definition: CORE_Out.h:255
void setOutputFile(const tString &fileName, const tFlag &mode)
set output file
Definition: CORE_Out.h:235
void update()
update the state of the output
Definition: CORE_Out.h:145
tBoolean isOutput(const tUCInt &mode) const
return true if the output is of the mode
Definition: CORE_Out.h:226
std::ofstream * mOutputFileStream
Definition: CORE_Out.h:67
void setOutput(const tString &mode)
set outputType of the binary form v='FS'
Definition: CORE_Out.h:208
tString mOutputFileName
Definition: CORE_Out.h:63
static const tFlag APPEND
Definition: CORE_Out.h:56
virtual tMemSize getMemorySize() const override
return the memory size in byte
Definition: CORE_Out.h:128
tUCInt mOutputFileMode
Definition: CORE_Out.h:72
typename std::unique_ptr< T, CORE_Object::Delete > CORE_UniquePointer
Definition: sp.h:8
#define tFlag
Definition: types.h:91
#define tUSInt
Definition: types.h:38
#define tString
Definition: types.h:147
#define tInt
Definition: types.h:47
#define tMemSize
Definition: types.h:166
#define tUCInt
Definition: types.h:26
#define tBoolean
Definition: types.h:151