📄 serial.hh
字号:
#ifndef serial_hh_included#define serial_hh_included#include "assert.hh"#include "inttypes.hh"#include "object.hh"#include "simarg.hh"// Serializability of an object refers to the ability to construct it from the// information stored in a checkpoint file. To make objects of type T// serializable, you must do all of the following://// 1. Derive T from Serializable (directly or indirectly)//// 2. Include a public declaration `static SerialType<T> type;' in T. The// `type' member should be constructed from a pair of strings, where the first// string is the name to which T is referred to in scripts and checkpoint// files (and cannot contain spaces), and the second is a free-form one-line// description of the simulated module//// 3. Provide a constructor T::T(Checkpoint &) that constructs the object from// a checkpoint stream.//// 4. Implement T::checkpoint(Checkpoint &) that stores the object data to a// checkpoint stream.class SerialTypeIterator;class Serializable;class Checkpoint;typedef UInt32 SerialID;class BasicSerialType : public Object{ friend class SerialTypeIterator;private: // The type name and description. const char *name_str, *descr_str; // The list of all serialized object types. static BasicSerialType *first; BasicSerialType *next;public: // The constructor. BasicSerialType(const char *n, const char *d) : name_str(n), descr_str(d), next(first) { first = this; } // Attribute access. const char *name() const { return name_str; } const char *descr() const { return descr_str; } // Object construction used primarly by Module::install_module(). virtual Serializable *create(const SimArgs &args) const; // Object retrieval from the checkpoint. virtual Serializable *restore(Checkpoint &fp) const = 0;};template <typename T> class SerialType : public BasicSerialType{public: SerialType(const char *name, const char *descr = "") : BasicSerialType(name, descr) { } Serializable *create(const SimArgs& a) const { return new T(a); } Serializable *restore(Checkpoint &cp) const { return new T(cp); }};class SerialTypeIterator : public Object{private: BasicSerialType *value;public: SerialTypeIterator() : value(BasicSerialType::first) { } SerialTypeIterator &operator++() { value = value->next; return *this; } SerialTypeIterator operator++(int) { SerialTypeIterator t = *this; value = value->next; return t; } operator bool() const { return value != 0; } BasicSerialType *operator->() const { return value; } BasicSerialType &operator*() const { return *value; }};class Serializable : public Object{ friend class ObjectIterator;public: // Object ID; sort of like pointer, but slow and persistent ;) const SerialID id;private: static Serializable sentinel_object; Serializable *prev_object, *next_object; // Unique object ID generator. static SerialID id_generator; // Constructor for the sentinel. Serializable(int) : id(0), prev_object(this), next_object(this) { }public: // Constructors, etc. Serializable() : id(++id_generator) { next_object = &sentinel_object; prev_object = sentinel_object.prev_object; next_object->prev_object = this; prev_object->next_object = this; } explicit Serializable(Checkpoint &cp); virtual ~Serializable() { prev_object->next_object = next_object; next_object->prev_object = prev_object; } // Checkpointing interface. virtual void checkpoint(Checkpoint &cp, bool parent = false) const;};class ObjectIterator : public Object{private: Serializable *value;public: ObjectIterator() : value(Serializable::sentinel_object.next_object) { } ObjectIterator &operator++() { value = value->next_object; return *this; } ObjectIterator operator++(int) { ObjectIterator t = *this; value = value->next_object; return t; } operator bool() const { return value != &(Serializable::sentinel_object); } Serializable *operator->() const { return value; } Serializable &operator*() const { return *value; }};// Find a type with the specified name.BasicSerialType &find_type(const char *name);// List all installed module IDs.SimArg list_known_modules(const SimArgs &args);// Type extractor.Checkpoint &operator>>(Checkpoint &cp, BasicSerialType *&type);// Find an object with the given ID.Serializable *find_object(SerialID id);#endif // serial_hh_included
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -