12 #ifndef CHECKPOINTABLE_FILE_H 13 #define CHECKPOINTABLE_FILE_H 47 void open(
const std::string& n,
const char* mode=
"w") {
48 f=fopen(n.c_str(),mode);
49 if (!f)
throw error(
"error opening %s: %s",n.c_str(),strerror(errno));
52 void close() {fclose(f); name=
"";}
53 bool opened()
const {
return f;}
56 void write(
const void*buf, std::size_t size, std::size_t nobj) {
57 if (!f)
throw error(
"file %s not open",name.c_str());
58 std::size_t obj_written=fwrite(buf,size,nobj,f);
59 if (obj_written!=nobj)
60 throw error(
"written only %d of %d objects: %s",
61 obj_written,nobj,strerror(errno));
65 template <
class T,
class A>
66 void write(
const std::vector<T,A>& buf)
67 {
if (!buf.empty())
write(&buf[0],
sizeof(T),buf.size());}
74 write(o.str().c_str(),1,o.str().length());
83 throw error(
"flush failure: %s",strerror(errno));
84 long p=ftell(f); buf<<p;
85 if (p==-1L)
throw error(
"ftell error: %s",strerror(errno));
90 if (opened()) close();
91 bool is_open; buf>>is_open;
96 if (fseek(f,p,SEEK_SET))
97 throw error(
"fseek error: %s",strerror(errno));
checkpointable binary file writer
Definition: CheckpointableFile.h:34
void write(const std::vector< T, A > &buf)
write a vector of data
Definition: CheckpointableFile.h:66
EcoLab exception class.
Definition: error.h:25
void open(const std::string &n, const char *mode="w")
open file n in mode mode (taking modes acceptible to fopen)
Definition: CheckpointableFile.h:47
Checkpointable_file & operator<<(const T &x)
stream data as text like std::iostream
Definition: CheckpointableFile.h:71
class to allow access to private members
Definition: classdesc_access.h:21
class to allow access to private members
Definition: classdesc_access.h:22
serialisation for standard containers
Definition: pack_base.h:124
Definition: TCL_obj_base.h:124
_OPENMP
Definition: accessor.h:16
void write(const void *buf, std::size_t size, std::size_t nobj)
low level write call - like std::fwrite
Definition: CheckpointableFile.h:56
Contains access_* structs, and nothing else. These structs are used to gain access to private members...
Definition: accessor.h:55