C++ main module for stochmagnet Package  1.0
CORE_OutputFile.h
Go to the documentation of this file.
1 #ifndef CORE_OutputFile_H
2 #define CORE_OutputFile_H
3 
4 
5 //base class header
6 #include "CORE_IO.h"
7 
8 //file hedader
9 #include <fstream>
10 #include <iomanip>
16 class CORE_OutputFile : public CORE_IO { // class
17 
18  // ATTRIBUTES
19 
20 
21 private:
22 
23 
24  //legnth of the file
26 
27  // ASSOCIATIONS
28  std::unique_ptr<std::ofstream> mFile;
29 
30  // METHODS
31 
32 
33 public:
34  // CONSTRUCTORS
38  mLength=0;
39  }
40 
41 
42 
43 
44 
45  // DESTRUCTORS
49  virtual ~CORE_OutputFile(void) {
50  close();
51  }
52 
53 
54 public:
55  // NEW
60  };
61 
62 
63 
64  // SET
65 
66 
74  virtual tMemSize getMemorySize() const override {
75  return sizeof(*this)+getContentsMemorySize();
76  }
77 
81  inline void setPrecision(const tUSInt& p) {
82  std::setprecision(p);
83  }
84 
89  inline tBoolean open(const tString& fileName) {
90  if (mFile.get()!=null) {
91  //close the old file
92  close();
93  }
94  //create a new file
95  mFile=std::make_unique<std::ofstream>(fileName.c_str(),std::ios::out);
96  if (mFile.get()==null)return false;
97  if (!(*mFile.get())) return false;
98  //set the length of the file
99  mLength=0;
100  return true;
101  }
102 
107  inline tBoolean append(const tString& fileName) {
108  if (mFile.get()!=null) {
109  //close the old file
110  close();
111  }
112  mFile=std::make_unique<std::ofstream>(fileName.c_str(),std::ios::app);
113  if (mFile.get()==null)return false;
114  if (!(*mFile.get())) return false;
115 
116  //go to the end
117  mFile->seekp(0,mFile->end);
118  //read the length
119  mLength=mFile->tellp();
120  }
121 
122 
125  inline void close() {
126  if (mFile.get()!=null) {
127  mFile->close();
128  }
129  }
130 
134  inline tULInt getCurrentIndex() const {
135  if (mFile.get()!=null) return mFile->tellp();
136  return 0;
137  }
138 
143  inline tBoolean moveTo(const tULInt& index) {
144  if (index>=mLength) return false;
145  if (mFile.get()!=null) {
146  mFile->seekp(index);
147  return true;
148  }
149  return false;
150  }
151 
152 
157  inline tBoolean translate(const tLInt& dx) {
158  return moveTo(getCurrentIndex()+dx);
159  }
160 
161 
165  inline tBoolean isEndOfFile() const {
166  return ((mFile.get()==null) || mFile->eof());
167  }
171  template<class T>
172  inline CORE_OutputFile& operator << (const T& obj) {
173  ASSERT_IN(mFile.get()!=null);
174 
175  (*mFile.get())<<obj;
176  mLength=mFile->tellp();
177 
178  return (*this);
179  }
180 
181 
182 };
183 
184 #endif
this class describes the file system IO by default write on standart output
Definition: CORE_IO.h:20
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:94
virtual tMemSize getContentsMemorySize() const
return nthe memory size of the included associations
Definition: CORE_Object.h:259
this class describes a writing file
Definition: CORE_OutputFile.h:16
tULInt mLength
Definition: CORE_OutputFile.h:25
CORE_OutputFile & operator<<(const T &obj)
writing operator
Definition: CORE_OutputFile.h:172
void close()
close the file
Definition: CORE_OutputFile.h:125
tBoolean open(const tString &fileName)
open file fileName for writing
Definition: CORE_OutputFile.h:89
virtual tMemSize getMemorySize() const override
return the memory size of the class
Definition: CORE_OutputFile.h:74
tBoolean append(const tString &fileName)
open file fileName for appending
Definition: CORE_OutputFile.h:107
static CORE_UniquePointer< CORE_OutputFile > New()
create a new instance of CORE_OutputFile class
Definition: CORE_OutputFile.h:58
tBoolean moveTo(const tULInt &index)
move the current index position of the file to index
Definition: CORE_OutputFile.h:143
CORE_OutputFile()
build a CORE_OutputClass object
Definition: CORE_OutputFile.h:37
void setPrecision(const tUSInt &p)
set precision
Definition: CORE_OutputFile.h:81
tBoolean isEndOfFile() const
test if the cursor is at the end of file
Definition: CORE_OutputFile.h:165
std::unique_ptr< std::ofstream > mFile
Definition: CORE_OutputFile.h:28
tULInt getCurrentIndex() const
get the current index of the file
Definition: CORE_OutputFile.h:134
virtual ~CORE_OutputFile(void)
destroy a CORE_OutputClass object
Definition: CORE_OutputFile.h:49
tBoolean translate(const tLInt &dx)
translate the current index position of the file
Definition: CORE_OutputFile.h:157
#define ASSERT_IN(a)
Definition: functions.h:601
typename std::unique_ptr< T, CORE_Object::Delete > CORE_UniquePointer
Definition: sp.h:8
#define tLInt
Definition: types.h:55
#define tUSInt
Definition: types.h:38
#define tString
Definition: types.h:147
#define tMemSize
Definition: types.h:166
#define tBoolean
Definition: types.h:151
#define tULInt
Definition: types.h:51