45#ifndef __SERIALIZE_HH__
46#define __SERIALIZE_HH__
56#include <unordered_map>
87 bool find(
const std::string §ion,
const std::string &entry,
90 bool entryExists(
const std::string §ion,
const std::string &entry);
117 static std::string
setDir(
const std::string &base_name);
129 static std::string
dir();
312 std::ofstream &outstream);
315 static std::stack<std::string>
path;
363 warn_if(do_warn,
"optional parameter %s:%s not present",
386template <
class InputIterator>
389 InputIterator start, InputIterator end)
393 using Elem = std::remove_cv_t<std::remove_reference_t<
decltype(*it)>>;
407decltype(std::begin(std::declval<const T&>()),
408 std::end(std::declval<const T&>()), void())
422 const T *param,
unsigned size)
439template <
class T,
class InsertIterator>
442 InsertIterator inserter, ssize_t fixed_size=-1)
447 "Can't unserialize '%s:%s'.", section,
name);
452 fatal_if(fixed_size >= 0 && tokens.size() != fixed_size,
453 "Array size mismatch on %s:%s (Got %u, expected %u)'\n",
454 section,
name, tokens.size(), fixed_size);
456 for (
const auto &
token: tokens) {
459 "Could not parse \"%s\".", str);
468decltype(std::declval<T>().insert(std::declval<typename T::value_type>()),
474 cp,
name, std::inserter(param, param.begin()));
481decltype(std::declval<T>().push_back(std::declval<typename T::value_type>()),
495 T *param,
unsigned size)
500 T &operator *() {
return *
data++; }
517 const char*
const names[],
const T *param,
unsigned size)
520 for (
unsigned i = 0;
i < size; ++
i) {
531 const char*
const names[], T *param,
unsigned size)
534 std::unordered_map<std::string, size_t> name_to_index;
535 for (
size_t i = 0;
i < size;
i++) {
536 name_to_index[names[
i]] =
i;
538 for (
size_t i = 0;
i < size;
i++) {
539 auto& key = names[
i];
542 param[name_to_index[key]] = value;
547 [name_to_index](
const std::string& key,
const std::string&
val)
549 if (!name_to_index.count(key)) {
550 warn(
"unknown entry found in checkpoint: %s %s %s\n",
551 Serializable::currentSection(), key, val);
568#define SERIALIZE_SCALAR(scalar) paramOut(cp, #scalar, scalar)
575#define UNSERIALIZE_SCALAR(scalar) paramIn(cp, #scalar, scalar)
582#define UNSERIALIZE_OPT_SCALAR(scalar) optParamIn(cp, #scalar, scalar)
591#define SERIALIZE_ENUM(scalar) paramOut(cp, #scalar, (int)scalar)
598#define UNSERIALIZE_ENUM(scalar) \
601 ::gem5::paramIn(cp, #scalar, tmp); \
602 scalar = static_cast<decltype(scalar)>(tmp); \
610#define SERIALIZE_ARRAY(member, size) \
611 ::gem5::arrayParamOut(cp, #member, member, size)
618#define UNSERIALIZE_ARRAY(member, size) \
619 ::gem5::arrayParamIn(cp, #member, member, size)
626#define SERIALIZE_CONTAINER(member) \
627 ::gem5::arrayParamOut(cp, #member, member)
634#define UNSERIALIZE_CONTAINER(member) \
635 ::gem5::arrayParamIn(cp, #member, member)
648#define SERIALIZE_OBJ(obj) obj.serializeSection(cp, #obj)
655#define UNSERIALIZE_OBJ(obj) obj.unserializeSection(cp, #obj)
660#define SERIALIZE_MAPPING(member, names, size) \
661 ::gem5::mappingParamOut(cp, #member, names, member, size)
666#define UNSERIALIZE_MAPPING(member, names, size) \
667 ::gem5::mappingParamIn(cp, #member, names, member, size)
const std::string _cptDir
static std::string currentDirectory
CheckpointIn(const std::string &cpt_dir)
static const char * baseFilename
This class represents the contents of a ".ini" file.
std::function< void( const std::string &, const std::string &)> VisitSectionCallback
Visitor callback that receives key/value pairs.
ScopedCheckpointSection & operator=(const ScopedCheckpointSection &)=delete
void pushName(const char *name)
~ScopedCheckpointSection()
void nameOut(CheckpointIn &cp)
void nameOut(CheckpointOut &cp)
ScopedCheckpointSection & operator=(ScopedCheckpointSection &&)=delete
ScopedCheckpointSection()=delete
ScopedCheckpointSection(const ScopedCheckpointSection &)=delete
Basic support for object serialization.
static std::stack< std::string > path
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
bool entryExists(const std::string §ion, const std::string &entry)
virtual void unserialize(CheckpointIn &cp)=0
Unserialize an object.
bool optParamIn(CheckpointIn &cp, const std::string &name, T ¶m, bool do_warn=true)
This function is used for restoring optional parameters from the checkpoint.
ScopedCheckpointSection(CP &cp, const std::string &name)
bool find(const std::string §ion, const std::string &entry, std::string &value)
static std::string setDir(const std::string &base_name)
Set the current directory.
void serializeSection(CheckpointOut &cp, const char *name) const
Serialize an object into a new section.
static const std::string & currentSection()
Gets the fully-qualified name of the active section.
virtual void serialize(CheckpointOut &cp) const =0
Serialize an object.
const std::string getCptDir()
void serializeSection(CheckpointOut &cp, const std::string &name) const
void unserializeSection(CheckpointIn &cp, const std::string &name)
static std::string dir()
Get the current checkout directory name.
bool sectionExists(const std::string §ion)
void visitSection(const std::string §ion, IniFile::VisitSectionCallback cb)
ScopedCheckpointSection(CP &cp, const char *name)
This is the constructor for Scoped checkpoint section helper class.
static void generateCheckpointOut(const std::string &cpt_dir, std::ofstream &outstream)
Generate a checkpoint file so that the serialization can be routed to it.
void unserializeSection(CheckpointIn &cp, const char *name)
Unserialize an a child object.
Declaration of IniFile object.
#define warn_if(cond,...)
Conditional warning macro that checks the supplied condition and only prints a warning if the conditi...
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
void mappingParamOut(CheckpointOut &os, const char *sectionName, const char *const names[], const T *param, unsigned size)
Serialize a mapping represented as two arrays: one containing names and the other containing values.
void tokenize(std::vector< std::string > &v, const std::string &s, char token, bool ignore)
std::ostream CheckpointOut
void paramOut(CheckpointOut &cp, const std::string &name, ExtMachInst const &machInst)
void paramIn(CheckpointIn &cp, const std::string &name, ExtMachInst &machInst)
void arrayParamOut(CheckpointOut &cp, const std::string &name, const CircleBuf< T > ¶m)
bool paramInImpl(CheckpointIn &cp, const std::string &name, T ¶m)
void mappingParamIn(CheckpointIn &cp, const char *sectionName, const char *const names[], T *param, unsigned size)
Restore mappingParamOut.
void arrayParamIn(CheckpointIn &cp, const std::string &name, CircleBuf< T > ¶m)
static void show(std::ostream &os, const T &value)
const std::string & name()