Serialization: Provide array serialization methods that work on std::vector

--HG--
extra : convert_revision : aecdf1a7e50edbb12921991cc81df1b431ce8b38
This commit is contained in:
Ali Saidi 2007-08-02 14:43:27 -04:00
parent c4e026daf4
commit da5f62af7b
2 changed files with 78 additions and 2 deletions

View file

@ -40,6 +40,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "base/annotate.hh"
#include "base/inifile.hh" #include "base/inifile.hh"
#include "base/misc.hh" #include "base/misc.hh"
#include "base/output.hh" #include "base/output.hh"
@ -178,6 +179,22 @@ paramOut(ostream &os, const std::string &name, const T &param)
os << "\n"; os << "\n";
} }
template <class T>
void
arrayParamOut(ostream &os, const std::string &name,
const std::vector<T> &param)
{
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 <class T> template <class T>
void void
@ -251,6 +268,49 @@ arrayParamIn(Checkpoint *cp, const std::string &section,
} }
} }
template <class T>
void
arrayParamIn(Checkpoint *cp, const std::string &section,
const std::string &name, std::vector<T> &param)
{
std::string str;
if (!cp->find(section, name, str)) {
fatal("Can't unserialize '%s:%s'\n", section, name);
}
// code below stolen from VectorParam<T>::parse().
// it would be nice to unify these somehow...
vector<string> 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<bool>,
// 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 void
objParamIn(Checkpoint *cp, const std::string &section, objParamIn(Checkpoint *cp, const std::string &section,
@ -273,7 +333,13 @@ arrayParamOut(ostream &os, const std::string &name, \
type const *param, int size); \ type const *param, int size); \
template void \ template void \
arrayParamIn(Checkpoint *cp, const std::string &section, \ arrayParamIn(Checkpoint *cp, const std::string &section, \
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<type> &param); \
template void \
arrayParamIn(Checkpoint *cp, const std::string &section, \
const std::string &name, std::vector<type> &param);
INSTANTIATE_PARAM_TEMPLATES(signed char) INSTANTIATE_PARAM_TEMPLATES(signed char)
INSTANTIATE_PARAM_TEMPLATES(unsigned char) INSTANTIATE_PARAM_TEMPLATES(unsigned char)
@ -343,6 +409,7 @@ Serializable::serializeAll(const std::string &cpt_dir)
outstream << "// checkpoint generated: " << ctime(&t); outstream << "// checkpoint generated: " << ctime(&t);
globals.serialize(outstream); globals.serialize(outstream);
Annotate::annotations.serialize(outstream);
SimObject::serializeAll(outstream); SimObject::serializeAll(outstream);
} }
@ -358,7 +425,7 @@ Serializable::unserializeAll(const std::string &cpt_dir)
dir); dir);
Checkpoint *cp = new Checkpoint(dir, section); Checkpoint *cp = new Checkpoint(dir, section);
unserializeGlobals(cp); unserializeGlobals(cp);
Annotate::annotations.unserialize(cp);
SimObject::unserializeAll(cp); SimObject::unserializeAll(cp);
} }

View file

@ -39,6 +39,7 @@
#include <list> #include <list>
#include <vector>
#include <iostream> #include <iostream>
#include <map> #include <map>
@ -60,10 +61,18 @@ template <class T>
void arrayParamOut(std::ostream &os, const std::string &name, void arrayParamOut(std::ostream &os, const std::string &name,
const T *param, int size); const T *param, int size);
template <class T>
void arrayParamOut(std::ostream &os, const std::string &name,
const std::vector<T> &param);
template <class T> template <class T>
void arrayParamIn(Checkpoint *cp, const std::string &section, void arrayParamIn(Checkpoint *cp, const std::string &section,
const std::string &name, T *param, int size); const std::string &name, T *param, int size);
template <class T>
void arrayParamIn(Checkpoint *cp, const std::string &section,
const std::string &name, std::vector<T> &param);
void void
objParamIn(Checkpoint *cp, const std::string &section, objParamIn(Checkpoint *cp, const std::string &section,
const std::string &name, SimObject * &param); const std::string &name, SimObject * &param);