CheckpointableFile.h
Go to the documentation of this file.
1 /*
2  @copyright Russell Standish 2000-2013
3  @author Russell Standish
4  This file is part of EcoLab
5 
6  Open source licensed under the MIT license. See LICENSE for details.
7 */
8 
12 #ifndef CHECKPOINTABLE_FILE_H
13 #define CHECKPOINTABLE_FILE_H
14 
15 #include <cstdio>
16 #include <string>
17 #include <vector>
18 #include <iostream>
19 #include <sstream>
20 #include "error.h"
21 #include "pack_base.h"
22 #include "pack_stl.h"
23 
24 namespace ecolab
25 {
26 
28 
35  {
36  // we use std streams because we can get an integral position type
37  std::FILE *f;
38  std::string name;
39  void operator=(const Checkpointable_file&);
41  public:
42  Checkpointable_file(): f(NULL) {}
43  Checkpointable_file(const std::string& name) {open(name);}
44  ~Checkpointable_file() {close();}
45 
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));
50  name=n;
51  }
52  void close() {fclose(f); name="";}
53  bool opened() const {return f;}
54 
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));
62  }
63 
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());}
68 
70  template <class T>
72  std::ostringstream o;
73  o<<x;
74  write(o.str().c_str(),1,o.str().length());
75  return *this;
76  }
77 
78  void pack(classdesc::pack_t& buf) const {
79  buf<<opened();
80  if (opened())
81  {
82  if (fflush(f))
83  throw error("flush failure: %s",strerror(errno));
84  long p=ftell(f); buf<<p; // 32bit warning!
85  if (p==-1L) throw error("ftell error: %s",strerror(errno));
86  buf<<name;
87  }
88  }
89  void unpack(classdesc::pack_t& buf) {
90  if (opened()) close();
91  bool is_open; buf>>is_open;
92  if (is_open) {
93  long p; buf>>p;
94  buf>>name;
95  open(name,"r+");
96  if (fseek(f,p,SEEK_SET))
97  throw error("fseek error: %s",strerror(errno));
98  }
99  }
100  };
101 
102 }
103 
104 namespace classdesc_access
105 {
106  template <> struct access_pack<ecolab::Checkpointable_file>
107  {
108  template <class U>
109  void operator()(classdesc::pack_t& b, const classdesc::string& d, U& a)
110  {a.pack(b);}
111  };
112 
113  template <> struct access_unpack<ecolab::Checkpointable_file>
114  {
115  template <class U>
116  void operator()(classdesc::pack_t& b, const classdesc::string& d, U& a)
117  {a.unpack(b);}
118  };
119 }
120 
121 #endif
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
serialisation descriptor
EcoLab exception class.
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