1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
|
/*
* checkpoint.h
*
* Created on: Jun 12, 2014
* Author: minh
*/
#ifndef CHECKPOINT_H_
#define CHECKPOINT_H_
#include <stdio.h>
#include <map>
#include <string>
#include <sstream>
#include <cassert>
#include <vector>
#include <typeinfo>
using namespace std;
// several useful declaration to save to or restore from a checkpoint
#define CKP_SAVE(var) checkpoint->put(#var, var)
#define CKP_ARRAY_SAVE(num, arr) checkpoint->putArray(#arr, num, arr)
#define CKP_VECTOR_SAVE(arr) checkpoint->putVector(#arr, arr)
#define CKP_RESTORE(var) checkpoint->get(#var, var)
#define CKP_RESTORE_STRING(var) checkpoint->getString(#var, var)
#define CKP_ARRAY_RESTORE(num, arr) checkpoint->getArray(#arr, num, arr)
#define CKP_VECTOR_RESTORE(arr) checkpoint->getVector(#arr, arr)
/** checkpoint stream */
class CkpStream : public stringstream {
public:
explicit CkpStream (ios_base::openmode which = ios_base::in | ios_base::out) : stringstream(which) {}
explicit CkpStream (const string& str, ios_base::openmode which = ios_base::in | ios_base::out) :
stringstream(str, which) {}
};
/* overload operators */
//ostream& operator<<(ostream& os, const T& obj) {
// return os;
//}
//
//std::istream& operator>>(std::istream& is, T& obj) {
// return is;
//}
/**
* Checkpoint as map from key strings to value strings
*/
class Checkpoint : public map<string, string> {
public:
/** constructor */
Checkpoint();
/** destructor */
virtual ~Checkpoint();
/**
* @param filename file name
*/
void setFileName(string filename);
/**
set compression for checkpoint file
@param compression true to compress checkpoint file, or false: no compression
*/
void setCompression(bool compression);
/**
set the header line to overwrite the default header
@param header header line
*/
void setHeader(string header);
/**
* load checkpoint information from an input stram
* @param in input stream
*/
void load(istream &in);
/**
* load checkpoint information from file
*/
void load();
/**
* dump checkpoint information into an output stream
* @param out output stream
*/
void dump(ostream &out);
/**
* dump checkpoint information into file
* @param force TRUE to dump no matter if time interval exceeded or not
*/
void dump(bool force = false);
/**
set dumping interval in seconds
@param interval dumping interval
*/
void setDumpInterval(double interval);
/**
* @return true if checkpoint contains the key
* @param key key to search for
*/
bool hasKey(string key);
/*-------------------------------------------------------------
* series of get function to get value of a key
*-------------------------------------------------------------*/
/**
@param key key name
@param[out] value value for key
@return true if key exists, false otherwise
*/
template<class T>
bool get(string key, T& value) {
if (key.empty())
key = struct_name.substr(0, struct_name.length()-1);
else
key = struct_name + key;
iterator it = find(key);
if (it == end())
return false;
CkpStream ss(it->second);
ss >> value;
return true;
}
/**
@param key key name
@param[out] value entire string
@return true if key exists, false otherwise
*/
bool getString(string key, string &value) {
if (key.empty())
key = struct_name.substr(0, struct_name.length()-1);
else
key = struct_name + key;
iterator it = find(key);
if (it == end())
return false;
value = it->second;
return true;
}
/**
get an array from checkpoint
@param key key name
@param num number of elements
@param[out] value value
*/
template<class T>
bool getArray(string key, int maxnum, T* value) {
if (key.empty())
key = struct_name.substr(0, struct_name.length()-1);
else
key = struct_name + key;
iterator it = find(key);
if (it == end())
return false;
size_t pos = 0, next_pos;
for (int i = 0; i < maxnum; i++) {
next_pos = it->second.find(", ", pos);
CkpStream ss(it->second.substr(pos, next_pos-pos));
if (!(ss >> value[i]))
break;
if (next_pos == string::npos) break;
pos = next_pos+2;
}
return true;
}
/**
get an array from checkpoint
@param key key name
@param num number of elements
@param[out] value value
*/
template<class T>
bool getVector(string key, vector<T> &value) {
if (key.empty())
key = struct_name.substr(0, struct_name.length()-1);
else
key = struct_name + key;
iterator it = find(key);
if (it == end())
return false;
size_t pos = 0, next_pos;
value.clear();
for (int i = 0; ; i++) {
next_pos = it->second.find(", ", pos);
CkpStream ss(it->second.substr(pos, next_pos-pos));
T val;
if (ss >> val)
value.push_back(val);
else
break;
if (next_pos == string::npos) break;
pos = next_pos+2;
}
return true;
}
/**
@param key key name
@return bool value for key
*/
bool getBool(string key, bool &ret);
bool getBool(string key);
// /**
// @param key key name
// @return double value for key
// */
// double getDouble(string key);
//
// /**
// @param key key name
// @return int value for key
// */
// int getInt(string key);
/*-------------------------------------------------------------
* series of put function to put pair of (key,value)
*-------------------------------------------------------------*/
/**
put pair of (key,value) to checkpoint
@param key key name
@param value value
*/
template<class T>
void put(string key, T value) {
if (key.empty())
key = struct_name.substr(0, struct_name.length()-1);
else
key = struct_name + key;
CkpStream ss;
if (typeid(T) == typeid(double))
ss.precision(10);
ss << value;
(*this)[key] = ss.str();
}
/**
@param key key name
@param value
*/
void putBool(string key, bool value);
/**
put an array to checkpoint
@param key key name
@param num number of elements
@param value value
*/
template<class T>
void putArray(string key, int num, T* value) {
if (key.empty())
key = struct_name.substr(0, struct_name.length()-1);
else
key = struct_name + key;
CkpStream ss;
if (typeid(T) == typeid(double))
ss.precision(10);
for (int i = 0; i < num; i++) {
if (i > 0) ss << ", ";
ss << value[i];
}
(*this)[key] = ss.str();
}
/**
put an STL vector to checkpoint
@param key key name
@param num number of elements
@param value value
*/
template<class T>
void putVector(string key, vector<T> &value) {
if (key.empty())
key = struct_name.substr(0, struct_name.length()-1);
else
key = struct_name + key;
CkpStream ss;
if (typeid(T) == typeid(double))
ss.precision(10);
for (int i = 0; i < value.size(); i++) {
if (i > 0) ss << ", ";
ss << value[i];
}
(*this)[key] = ss.str();
}
/*-------------------------------------------------------------
* helper functions
*-------------------------------------------------------------*/
/**
start a new struct
*/
void startStruct(string name);
/**
end the current struct
*/
void endStruct();
/**
start a new list in the current scope
@param nelem number of elements
*/
void startList(int nelem);
/**
set the starting list element, should only be called right after startList
@param id element ID
*/
void setListElement(int id);
/**
add an element to the current list
*/
void addListElement();
/**
end the current list
*/
void endList();
/**
get a subset of checkpoint where the key string contains a given substring
@param[out] target checkpoint
@param sub_key key substring to search for
*/
void getSubCheckpoint(Checkpoint *target, string sub_key);
protected:
/** filename to write checkpoint */
string filename;
/** previous dump time in seconds */
double prev_dump_time;
/** dumping time interval */
double dump_interval;
/** true (default) to compress checkpoint file, false: no compression */
bool compression;
/** header line of checkpoint file */
string header;
private:
/** name of the current nested key */
string struct_name;
/** current list element ID */
vector<int> list_element;
/** width to element ID for prefixing with '0' */
vector<int> list_element_precision;
};
/**
Root class handling all checkpoint facilities. Inherit this class
if you want a class to be checkpointed.
*/
class CheckpointFactory {
public:
/** constructor */
CheckpointFactory();
virtual ~CheckpointFactory() {}
/**
set checkpoint object
@param checkpoint
*/
virtual void setCheckpoint(Checkpoint *checkpoint);
/**
get checkpoint object
@return checkpoint
*/
Checkpoint *getCheckpoint();
/**
save object into the checkpoint
*/
virtual void saveCheckpoint();
/**
restore object from the checkpoint
*/
virtual void restoreCheckpoint();
protected:
Checkpoint *checkpoint;
};
#endif /* CHECKPOINT_H_ */
|