SaveLoad.hpp 2.76 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
#ifndef _SAVELOAD_H_
#define _SAVELOAD_H_

#include <cstdio>
#include <rpc/rpc.h>
#include <complex>


#ifdef BUILD_IA

class IAInterval;
12

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class IACInterval;

#endif

/**
 * Provides a Saver and Loader for the basic types. The output ordering has to be the same
 * as the input ordering. Note that no information about the source name / variable is stored
 *
 * Loading and saving operators should be implemented in each class.
 * See e.g. Point::load, Point::save and the corresponding inline operators
 */

bool file_exists(const char *name);

class SaveLoad {
28
29
  FILE *file = nullptr;
  XDR xdrs;
30
public:
31
  SaveLoad() {}
32

33
  ~SaveLoad() { close(); }
34

35
  void close();
36

37
38
39
40
  static bool FileExists(const char *name, int i = -1);

  static bool PFileExists(const char *name);

41
protected:
42
  void open(const char *name, const char *mode, int i = -1);
43

44
  void openOnProc(int proc, const char *name, const char *mode, int i = -1);
45

46
  XDR *xdr() { return &xdrs; }
47
48
49
50
};

class Saver : public SaveLoad {
public:
51
  Saver() : SaveLoad() {}
52

53
  explicit Saver(const char *name) : SaveLoad() { open(name); }
54

55
  Saver(const char *name, int i) : SaveLoad() { open(name, i); }
56

57
  void open(const char *name) { SaveLoad::open(name, "w"); }
58

59
  void open(const char *name, int i) { SaveLoad::open(name, "w", i); }
60

61
  Saver &operator<<(bool);
62

63
  Saver &operator<<(short int);
64

65
  Saver &operator<<(int);
66

67
  Saver &operator<<(size_t);
68

69
  Saver &operator<<(double);
70

71
  Saver &operator<<(char);
72

73
  Saver &operator<<(std::complex<int> z);
74

75
  Saver &operator<<(std::complex<double>);
76
77
78

#ifdef BUILD_IA

79
80
81
  Saver &operator<<(IAInterval);

  Saver &operator<<(IACInterval);
82
83
84
85
86
87

#endif
};

class PSaver : public Saver {
public:
88
  explicit PSaver(const char *name);
89
90
91
92
93
};

// For use on master only
class MSaver : public Saver {
public:
94
  explicit MSaver(const char *name);
95
96
97
98
};

class Loader : public SaveLoad {
public:
99
100
  /// Checks if files exist on all procs
  static bool files_exist(const char *name);
101

102
  Loader() : SaveLoad() {}
103

104
  explicit Loader(const char *name) : SaveLoad() { open(name); }
105

106
  Loader(const char *name, int i) : SaveLoad() { open(name, i); }
107

108
  void open(const char *name) { SaveLoad::open(name, "r"); }
109

110
  void open(const char *name, int i) { SaveLoad::open(name, "r", i); }
111

112
  Loader &operator>>(bool &);
113

114
  Loader &operator>>(short int &);
115

116
  Loader &operator>>(int &);
117

118
  Loader &operator>>(size_t &);
119

120
  Loader &operator>>(double &);
121

122
  Loader &operator>>(char &);
123

124
  Loader &operator>>(std::complex<double> &);
125
126
127

#ifdef BUILD_IA

128
129
130
  Loader &operator>>(IAInterval &);

  Loader &operator>>(IACInterval &);
131
132
133
134
135
136

#endif
};

class PLoader : public Loader {
public:
137
  explicit PLoader(const char *name);
138
139
140
141
142
};

// For use on master only
class MLoader : public Loader {
public:
143
  explicit MLoader(const char *name);
144
145
146
};

#endif // of #ifndef _SAVELOAD_H_