From b90f810575679442d78c0e9cd0c98a057ba4871b Mon Sep 17 00:00:00 2001 From: Steve Reinhardt Date: Tue, 28 Oct 2003 12:55:12 -0800 Subject: Revamp serialization to make it easier. --HG-- extra : convert_revision : c57a538d7cf606dbdf5fa244f92da46bd830e335 --- sim/serialize.hh | 80 ++++++++++++++++++++++++-------------------------------- 1 file changed, 34 insertions(+), 46 deletions(-) (limited to 'sim/serialize.hh') diff --git a/sim/serialize.hh b/sim/serialize.hh index ffcbbcdc2..5ebbfaba5 100644 --- a/sim/serialize.hh +++ b/sim/serialize.hh @@ -36,44 +36,52 @@ #include #include +#include #include "sim/host.hh" #include "sim/configfile.hh" class IniFile; +template +void paramOut(std::ostream &os, const std::string &name, const T& param); + +template +void paramIn(IniFile &db, const std::string §ion, + const std::string &name, T& param); + +template +void arrayParamOut(std::ostream &os, const std::string &name, + const T *param, int size); + +template +void arrayParamIn(IniFile &db, const std::string §ion, + const std::string &name, T *param, int size); + +// +// These macros are streamlined to use in serialize/unserialize +// functions. It's assumed that serialize() has a parameter 'os' for +// the ostream, and unserialize() has parameters 'db' and 'section'. +#define SERIALIZE_MEMBER(member) paramOut(os, #member, member) + +#define UNSERIALIZE_MEMBER(member) paramIn(db, section, #member, member) + +#define SERIALIZE_ARRAY(member, size) \ + arrayParamOut(os, #member, member, size) + +#define UNSERIALIZE_ARRAY(member, size) \ + arrayParamIn(db, section, #member, member, size) + /* * Basic support for object serialization. */ class Serializeable { public: - // To allow other classes to do some of the serialization work. - class Proxy { - private: - Serializeable *obj; - - // Make it so only Serializables can construct one of these. - Proxy(Serializeable *o) : obj(o) {}; - - friend class Serializeable; - - public: - template - void paramOut(const std::string &name, const T& param) const { - obj->paramOut(name, param); - }; - }; friend class Serializer; - friend class Proxy; - - private: - Proxy proxy; protected: - const Proxy &getProxy() { return(proxy); }; - // object name: should be unique std::string objName; @@ -81,13 +89,8 @@ class Serializeable static Serializer *serializer; void mark(); - void nameOut(); - void nameOut(const std::string &_name); - void childOut(const std::string &name, Serializeable *child); - template - void paramOut(const std::string &name, const T& param); - - std::ostream &out() const; + void nameOut(std::ostream& os); + void nameOut(std::ostream& os, const std::string &_name); public: Serializeable(const std::string &n); @@ -99,12 +102,8 @@ class Serializeable const std::string &name() const { return objName; } virtual void nameChildren() {} - virtual void serialize() {} - virtual void unserialize(IniFile &db, const std::string &category, - ConfigNode *node = NULL) - { - std::cout << name() << " is being unserialized" << std::endl; - } + virtual void serialize(std::ostream& os) {} + virtual void unserialize(IniFile &db, const std::string §ion) {} }; class Serializer @@ -131,17 +130,6 @@ class Serializer const std::string &filename() const { return file; } }; -template -inline void -Serializeable::paramOut(const std::string &name, const T& param) -{ - out() << name << "=" << param << "\n"; -} - -template <> void -Serializeable::paramOut(const std::string &name, const uint64_t& param); - - // // A SerializeableBuilder serves as an evaluation context for a set of // parameters that describe a specific instance of a Serializeable. This -- cgit v1.2.3