C++ mpi module for stochmagnet_main Package
CORE_Out.h
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 "shared_pointer.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:
45  static const tUCInt NO_OUTPUT=0;
46 
49  static const tUCInt SCREEN_OUTPUT=1;
50 
53  static const tUCInt FILE_OUTPUT=2;
54 
57  static const tUCInt ALL_OUTPUT=3;
58 
61  static const tFlag APPEND=1;
62 
65  static const tFlag CREATE=0;
66 
67 private:
68  //file output mode APPEND `| CREATE
69  tString mOutputFileName;
70  tString mOutputFileName_back;
71 
72  //current output file
73  std::ofstream *mOutputFileStream;
74 
75  //mOutput =mScreen+2*mFile
76  tUCInt mOutput;
77  //file output
78  tUCInt mOutputFileMode;
79 
80  //backup & restore;
81  tFlag mOutput_back;
82  tFlag mOutputFileMode_back;
83 
84 
85 
86 
87 public:
88  // CONSTRUCTORS
92  mOutputFileStream=null;
93  reset();
94  }
95 
96 
97 
98 
99  // DESTRUCTORS
103  virtual ~CORE_Out(void) {
104  release();
105  }
106 
107 
108 
109 
110 public:
111 
112 
113  //New defaut output
117  inline static CORE_UniquePointer<CORE_Out> New() {
118  return CORE_UniquePointer<CORE_Out>(new CORE_Out(),CORE_Out::Delete());
119  }
120 
121 
122 public:
123 
124  // OTHERS
125 
134  virtual tMemSize getMemorySize() const override {
135  return sizeof(*this)+getContentsMemorySize();
136  }
145  virtual tMemSize getContentsMemorySize() const override {
146  return CORE_Object::getContentsMemorySize()+mOutputFileName.size()+mOutputFileName_back.size();
147  }
148  public:
151  inline void update() {
152  //release
153  release();
154 
155  //create file output if used
156  if (isOutput(FILE_OUTPUT)) {
157  if (mOutputFileMode==APPEND) {
158  mOutputFileStream=new std::ofstream(mOutputFileName.c_str(),std::ios::app);
159  } else {
160  mOutputFileStream=new std::ofstream(mOutputFileName.c_str(),std::ios::out);
161  }
162  if ((mOutputFileStream!=null) && (!(*mOutputFileStream))) {
163  throw CORE_Exception("core","CORE_Out",
164  "impossible to write in output file "+mOutputFileName);
165  }
166 
167  }
168  }
171  inline void release() {
172  if (mOutputFileStream!=null) {
173  mOutputFileStream->close();
174  delete mOutputFileStream;
175  mOutputFileStream=null;
176  }
177  }
180  inline void reset() {
181  mOutput=SCREEN_OUTPUT;
182  mOutputFileMode=CREATE;
183  mOutputFileName="./core.log";
184  backup();
185  update();
186  }
199  inline void setOutput(const tUSInt& mode) {
200  mOutput=mode;
201  }
214  inline void setOutput(const tString& mode) {
215  tUCInt p=1;//2^0
216  tUCInt m=0;
217  std::string::const_reverse_iterator iS=mode.rbegin();
218  while (iS!=mode.rend()) {
219  //m+=2^p.mode[l-1-p]
220  m+=p*(*iS);
221 
222  //next element
223  p*=2;
224  iS++;
225  }
226  setOutput(m);
227  }
228 
232  inline tBoolean isOutput(const tUCInt& mode) const {
233  return ((mOutput & mode ) / mode == 1);
234  }
238  inline tBoolean hasOutput() const {
239  return (mOutput!=NO_OUTPUT);
240  }
241 
242 
247  inline void setOutputFile(const tString& fileName,const tFlag& mode) {
248  mOutputFileName=fileName;
249  mOutputFileMode=mode;
250  }
253  inline void setOutputFile(const tString& fileName) {
254  setOutputFile(fileName,CREATE);
255  }
258  inline void updateOutputFile(const tString& fileName) {
259  mOutputFileName=fileName;
260  update();
261  }
262 
263 
267  inline const tString& getOutputFileName() const {
268  return mOutputFileName;
269  }
270 
273  inline void backup() {
274  mOutputFileMode_back= mOutputFileMode;
275  mOutputFileName_back= mOutputFileName;
276  mOutput_back= mOutput;
277  }
278 
281  inline void restore() {
282  mOutputFileMode= mOutputFileMode_back;
283  mOutputFileName= mOutputFileName_back;
284  mOutput= mOutput_back;
285 
286  //update the state
287  update();
288  }
289 
293  inline void println(const tString& msg) {
294  (*this)<<msg<<"\n";
295  }
296 
300  inline void print(const tString& msg) {
301  (*this)<<msg;
302  }
306  inline void printError(const tString& msg) {
307  std::cerr<<msg<<std::flush<<"\n";
308  }
309 
315  inline static void PrintProgressBar(const tString& message,
316  const tInt& percent,tInt& oldPercent) {
317  if (percent>oldPercent) {
318  std::cout << "\r "<<message<<" [" << std::setw(3) << percent << "%] "<<std::flush;
319  oldPercent=percent;
320  }
321  }
322 
323 
324  // << operators
325  // =============
326 
332  template<typename T>
333  friend CORE_Out& operator <<(CORE_Out& out,const T& obj) {
334  if (out.isOutput(SCREEN_OUTPUT)) std::cout<<obj<<std::flush;
335  if (out.isOutput(FILE_OUTPUT) && (out.mOutputFileStream!=null)) (*out.mOutputFileStream)<<obj<<std::flush;
336  return out;
337  };
338 
339 
344  virtual tString toString() const override {
345  std::stringstream sstr;
346  sstr<<CORE_Object::toString()<<"\n";
347  sstr<<"\t"<<" outputFileName:"<<mOutputFileName<<"\n";
348  sstr<<"\t"<<" output Mode:"<<((int)mOutput)<<"\n";
349  sstr<<"\t"<<" outputFileMode:"<<((int)mOutputFileMode)<<"\n";
350 
351  return sstr.str();
352  }
353 
354 };
355 
356 #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:113
abstract base class for most classes.
Definition: CORE_Object.h:65
virtual tMemSize getContentsMemorySize() const
return nthe memory size of the included associations
Definition: CORE_Object.h:278
virtual tString toString() const
return the string representation of the object node
Definition: CORE_Object.h:333
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:180
virtual tMemSize getContentsMemorySize() const override
return nthe memory size of the included associations
Definition: CORE_Out.h:145
void restore()
restore the state after change
Definition: CORE_Out.h:281
void printError(const tString &msg)
print the message on error output
Definition: CORE_Out.h:306
static const tUCInt ALL_OUTPUT
for printing in all
Definition: CORE_Out.h:57
static void PrintProgressBar(const tString &message, const tInt &percent, tInt &oldPercent)
print a progress bar if percent greater than oldpercent
Definition: CORE_Out.h:315
void setOutput(const tUSInt &mode)
set outputType of the binary form v=2*f+s
Definition: CORE_Out.h:199
void setOutputFile(const tString &fileName)
set output file
Definition: CORE_Out.h:253
friend CORE_Out & operator<<(CORE_Out &out, const T &obj)
print Operators
Definition: CORE_Out.h:333
virtual tString toString() const override
return the string repreentation of the class
Definition: CORE_Out.h:344
void print(const tString &msg)
print the message
Definition: CORE_Out.h:300
virtual ~CORE_Out(void)
destroy a CORE_Out
Definition: CORE_Out.h:103
void release()
release the pointers
Definition: CORE_Out.h:171
static const tUCInt FILE_OUTPUT
for printing in file
Definition: CORE_Out.h:53
static CORE_UniquePointer< CORE_Out > New()
build a new instance of default output
Definition: CORE_Out.h:117
static const tUCInt SCREEN_OUTPUT
for printing in screen
Definition: CORE_Out.h:49
void println(const tString &msg)
print the message
Definition: CORE_Out.h:293
static const tFlag CREATE
output file mode : create file
Definition: CORE_Out.h:65
static const tUCInt NO_OUTPUT
for printing in screen
Definition: CORE_Out.h:45
tBoolean hasOutput() const
return true if the output is of the mode
Definition: CORE_Out.h:238
CORE_Out()
build a CORE_Out
Definition: CORE_Out.h:91
const tString & getOutputFileName() const
get the output file name
Definition: CORE_Out.h:267
void backup()
backup the state before change
Definition: CORE_Out.h:273
void setOutputFile(const tString &fileName, const tFlag &mode)
set output file
Definition: CORE_Out.h:247
void update()
update the state of the output
Definition: CORE_Out.h:151
void updateOutputFile(const tString &fileName)
update output file
Definition: CORE_Out.h:258
tBoolean isOutput(const tUCInt &mode) const
return true if the output is of the mode
Definition: CORE_Out.h:232
void setOutput(const tString &mode)
set outputType of the binary form v='FS'
Definition: CORE_Out.h:214
static const tFlag APPEND
output file mode : append to file
Definition: CORE_Out.h:61
virtual tMemSize getMemorySize() const override
return the memory size in byte
Definition: CORE_Out.h:134