From da5f62af7bdc4a0a6c8da1eee61b0ecb1a7212b4 Mon Sep 17 00:00:00 2001 From: Ali Saidi Date: Thu, 2 Aug 2007 14:43:27 -0400 Subject: Serialization: Provide array serialization methods that work on std::vector --HG-- extra : convert_revision : aecdf1a7e50edbb12921991cc81df1b431ce8b38 --- src/sim/serialize.cc | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 2 deletions(-) (limited to 'src/sim/serialize.cc') diff --git a/src/sim/serialize.cc b/src/sim/serialize.cc index a01e053b9..a7f81d5fb 100644 --- a/src/sim/serialize.cc +++ b/src/sim/serialize.cc @@ -40,6 +40,7 @@ #include #include +#include "base/annotate.hh" #include "base/inifile.hh" #include "base/misc.hh" #include "base/output.hh" @@ -178,6 +179,22 @@ paramOut(ostream &os, const std::string &name, const T ¶m) os << "\n"; } +template +void +arrayParamOut(ostream &os, const std::string &name, + const std::vector ¶m) +{ + int size = param.size(); + os << name << "="; + if (size > 0) + showParam(os, param[0]); + for (int i = 1; i < size; ++i) { + os << " "; + showParam(os, param[i]); + } + os << "\n"; +} + template void @@ -251,6 +268,49 @@ arrayParamIn(Checkpoint *cp, const std::string §ion, } } +template +void +arrayParamIn(Checkpoint *cp, const std::string §ion, + const std::string &name, std::vector ¶m) +{ + std::string str; + if (!cp->find(section, name, str)) { + fatal("Can't unserialize '%s:%s'\n", section, name); + } + + // code below stolen from VectorParam::parse(). + // it would be nice to unify these somehow... + + vector tokens; + + tokenize(tokens, str, ' '); + + // Need this if we were doing a vector + // value.resize(tokens.size()); + + param.resize(tokens.size()); + + for (int i = 0; i < tokens.size(); i++) { + // need to parse into local variable to handle vector, + // for which operator[] returns a special reference class + // that's not the same as 'bool&', (since it's a packed + // vector) + T scalar_value; + if (!parseParam(tokens[i], scalar_value)) { + string err("could not parse \""); + + err += str; + err += "\""; + + fatal(err); + } + + // assign parsed value to vector + param[i] = scalar_value; + } +} + + void objParamIn(Checkpoint *cp, const std::string §ion, @@ -273,7 +333,13 @@ arrayParamOut(ostream &os, const std::string &name, \ type const *param, int size); \ template void \ arrayParamIn(Checkpoint *cp, const std::string §ion, \ - const std::string &name, type *param, int size); + const std::string &name, type *param, int size); \ +template void \ +arrayParamOut(ostream &os, const std::string &name, \ + const std::vector ¶m); \ +template void \ +arrayParamIn(Checkpoint *cp, const std::string §ion, \ + const std::string &name, std::vector ¶m); INSTANTIATE_PARAM_TEMPLATES(signed char) INSTANTIATE_PARAM_TEMPLATES(unsigned char) @@ -343,6 +409,7 @@ Serializable::serializeAll(const std::string &cpt_dir) outstream << "// checkpoint generated: " << ctime(&t); globals.serialize(outstream); + Annotate::annotations.serialize(outstream); SimObject::serializeAll(outstream); } @@ -358,7 +425,7 @@ Serializable::unserializeAll(const std::string &cpt_dir) dir); Checkpoint *cp = new Checkpoint(dir, section); unserializeGlobals(cp); - + Annotate::annotations.unserialize(cp); SimObject::unserializeAll(cp); } -- cgit v1.2.3