📄 checkpoint.hh
字号:
#ifndef checkpoint_hh_included#define checkpoint_hh_included#include <stdio.h>#include "error.hh"#include "inttypes.hh"#include "object.hh"#include "simarg.hh"#include "serial.hh"// The actual checkpoint class. It exists mainly to allow use of C++-like// insertion and extraction operators.class Checkpoint : public Object{private: const char *name_str; // file name FILE *fp; // underlying C file stream. int ln; // current line number. // Functions used to implement integer extractors. int extract_int(int max, bool sign); long extract_long(long max, bool sign);public: // An I/O error or a premature end of file. class EOFError : public FileError { private: Checkpoint &cp; public: EOFError(Checkpoint &c) : FileError(c.name()), cp(c) { } const char *what() const; }; friend class EOFError; // A checkpoint file syntax error. class SyntaxError : public Error { public: SyntaxError(Checkpoint &cp); SyntaxError(Checkpoint &cp, const char *cause, ...); }; // Constructors, etc. Checkpoint(const char *ns) : name_str(ns), fp(0), ln(1) { } // Attribute access. const char *name() const { return name_str; } int lineno() const { return ln; } // Inserters for basic types. Checkpoint& operator<<(bool b) { putc('0' + b, fp); return *this; } Checkpoint& operator<<(char ch) { putc(ch, fp); if (ch == '\n') ++ln; return *this; } Checkpoint& operator<<(Int8 x) { fprintf(fp, "%d", (int)x); return *this; } Checkpoint& operator<<(Int16 x) { fprintf(fp, "%d", (int)x); return *this; } Checkpoint& operator<<(Int32 x) { fprintf(fp, "%ld", (long)x); return *this; } Checkpoint& operator<<(UInt8 x) { fprintf(fp, "%u", (unsigned int)x); return *this; } Checkpoint& operator<<(UInt16 x) { fprintf(fp, "%u", (unsigned int)x); return *this; } Checkpoint& operator<<(UInt32 x) { fprintf(fp, "%lu", (unsigned long)x); return *this; } // The remaining inserters are non-trivial. Checkpoint& operator<<(const char *s); Checkpoint& operator<<(Int64 x); Checkpoint& operator<<(UInt64 x); // Extractors for basic types. Checkpoint& operator>>(char& ch) { int c = getc(fp); if (c == EOF) throw EOFError(*this); if (c == '\n') ++ln; ch = c; return *this; } Checkpoint& operator>>(Int8& x) { x = extract_int(0x7f + 1, true); return *this; } Checkpoint& operator>>(UInt8& x) { x = extract_int(0xff + 1, false); return *this; } // The remaining extractors are non-trivial. Checkpoint& operator>>(bool& b); Checkpoint& operator>>(char *&s); Checkpoint& operator>>(Int16& x); Checkpoint& operator>>(Int32& x); Checkpoint& operator>>(UInt16& x); Checkpoint& operator>>(UInt32& x); Checkpoint& operator>>(Int64& x); Checkpoint& operator>>(UInt64& x); // Two pattern-matching extractors. Checkpoint& operator>>(char ch) { int c = getc(fp); if (c != ch) { if (c == EOF) throw EOFError(*this); else { throw SyntaxError(*this, "'%#c' expected", ch); } } if (ch == '\n') ++ln; return *this; } Checkpoint& operator>>(const char *s); // Inserters for SerialType objects. Checkpoint& operator<<(BasicSerialType &type) { return *this << type.name(); } // Checkpoint interfaces. static SimArg create(const SimArgs &args); static SimArg restore(const SimArgs &args);};// Sometimes more traditional extractor interfaces are easier to use.inline char *extract_string(Checkpoint &cp, char *delim){ char *s = delim; cp >> s; return s;}inline char *extract_word(Checkpoint &cp){ return extract_string(cp, " \n\t");}inline char *extract_line(Checkpoint &cp){ return extract_string(cp, "\n");}template <typename T>inline T extract(Checkpoint &cp){ T x; cp >> x; return x;}#endif // checkpoint_hh_included
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -