serialize.hh revision 2760
12SN/A/*
21762SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan
32SN/A * All rights reserved.
42SN/A *
52SN/A * Redistribution and use in source and binary forms, with or without
62SN/A * modification, are permitted provided that the following conditions are
72SN/A * met: redistributions of source code must retain the above copyright
82SN/A * notice, this list of conditions and the following disclaimer;
92SN/A * redistributions in binary form must reproduce the above copyright
102SN/A * notice, this list of conditions and the following disclaimer in the
112SN/A * documentation and/or other materials provided with the distribution;
122SN/A * neither the name of the copyright holders nor the names of its
132SN/A * contributors may be used to endorse or promote products derived from
142SN/A * this software without specific prior written permission.
152SN/A *
162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282760Sbinkertn@umich.edu * Authors: Nathan Binkert
292760Sbinkertn@umich.edu *          Erik Hallnor
302665Ssaidi@eecs.umich.edu *          Steve Reinhardt
312SN/A */
322SN/A
332SN/A/* @file
342SN/A * Serialization Interface Declarations
352SN/A */
362SN/A
372SN/A#ifndef __SERIALIZE_HH__
382SN/A#define __SERIALIZE_HH__
392SN/A
402SN/A
412SN/A#include <list>
422SN/A#include <iostream>
43217SN/A#include <map>
442SN/A
4556SN/A#include "sim/host.hh"
462SN/A
472738Sstever@eecs.umich.educlass IniFile;
48395SN/Aclass Serializable;
49237SN/Aclass Checkpoint;
502SN/A
51217SN/Atemplate <class T>
52502SN/Avoid paramOut(std::ostream &os, const std::string &name, const T &param);
53217SN/A
54217SN/Atemplate <class T>
55237SN/Avoid paramIn(Checkpoint *cp, const std::string &section,
56502SN/A             const std::string &name, T &param);
57217SN/A
58217SN/Atemplate <class T>
59217SN/Avoid arrayParamOut(std::ostream &os, const std::string &name,
60217SN/A                   const T *param, int size);
61217SN/A
62217SN/Atemplate <class T>
63237SN/Avoid arrayParamIn(Checkpoint *cp, const std::string &section,
64217SN/A                  const std::string &name, T *param, int size);
65217SN/A
66237SN/Avoid
67237SN/AobjParamIn(Checkpoint *cp, const std::string &section,
68395SN/A           const std::string &name, Serializable * &param);
69237SN/A
70237SN/A
71217SN/A//
72217SN/A// These macros are streamlined to use in serialize/unserialize
73217SN/A// functions.  It's assumed that serialize() has a parameter 'os' for
74237SN/A// the ostream, and unserialize() has parameters 'cp' and 'section'.
75222SN/A#define SERIALIZE_SCALAR(scalar)	paramOut(os, #scalar, scalar)
76217SN/A
77237SN/A#define UNSERIALIZE_SCALAR(scalar)	paramIn(cp, section, #scalar, scalar)
78217SN/A
79223SN/A// ENUMs are like SCALARs, but we cast them to ints on the way out
80223SN/A#define SERIALIZE_ENUM(scalar)		paramOut(os, #scalar, (int)scalar)
81223SN/A
82223SN/A#define UNSERIALIZE_ENUM(scalar)		\
83223SN/A do {						\
84223SN/A    int tmp;					\
85237SN/A    paramIn(cp, section, #scalar, tmp);		\
86223SN/A    scalar = (typeof(scalar))tmp;		\
87223SN/A  } while (0)
88223SN/A
89217SN/A#define SERIALIZE_ARRAY(member, size)	\
90217SN/A        arrayParamOut(os, #member, member, size)
91217SN/A
92217SN/A#define UNSERIALIZE_ARRAY(member, size)	\
93237SN/A        arrayParamIn(cp, section, #member, member, size)
94237SN/A
95237SN/A#define SERIALIZE_OBJPTR(objptr)	paramOut(os, #objptr, (objptr)->name())
96237SN/A
97237SN/A#define UNSERIALIZE_OBJPTR(objptr)			\
98237SN/A  do {							\
99395SN/A    Serializable *sptr;				\
100237SN/A    objParamIn(cp, section, #objptr, sptr);		\
101237SN/A    objptr = dynamic_cast<typeof(objptr)>(sptr);	\
102237SN/A  } while (0)
103217SN/A
1042SN/A/*
1052SN/A * Basic support for object serialization.
1062SN/A */
107395SN/Aclass Serializable
1082SN/A{
1092SN/A  protected:
110510SN/A    void nameOut(std::ostream &os);
111510SN/A    void nameOut(std::ostream &os, const std::string &_name);
1122SN/A
1132SN/A  public:
114395SN/A    Serializable() {}
115395SN/A    virtual ~Serializable() {}
1162SN/A
117265SN/A    // manditory virtual function, so objects must provide names
118512SN/A    virtual const std::string name() const = 0;
1192SN/A
120510SN/A    virtual void serialize(std::ostream &os) {}
121237SN/A    virtual void unserialize(Checkpoint *cp, const std::string &section) {}
122237SN/A
123395SN/A    static Serializable *create(Checkpoint *cp,
124237SN/A                                 const std::string &section);
1252SN/A
1262287SN/A    static int ckptCount;
1272287SN/A    static int ckptMaxCount;
1282287SN/A    static int ckptPrevCount;
129395SN/A    static void serializeAll();
130395SN/A    static void unserializeGlobals(Checkpoint *cp);
1312SN/A};
1322SN/A
1332SN/A//
134395SN/A// A SerializableBuilder serves as an evaluation context for a set of
135395SN/A// parameters that describe a specific instance of a Serializable.  This
1362SN/A// evaluation context corresponds to a section in the .ini file (as
1372SN/A// with the base ParamContext) plus an optional node in the
1382SN/A// configuration hierarchy (the configNode member) for resolving
139395SN/A// Serializable references.  SerializableBuilder is an abstract superclass;
1402SN/A// derived classes specialize the class for particular subclasses of
141395SN/A// Serializable (e.g., BaseCache).
1422SN/A//
1432SN/A// For typical usage, see the definition of
144395SN/A// SerializableClass::createObject().
1452SN/A//
146395SN/Aclass SerializableBuilder
1472SN/A{
1482SN/A  public:
1492SN/A
150395SN/A    SerializableBuilder() {}
1512SN/A
152395SN/A    virtual ~SerializableBuilder() {}
1532SN/A
154395SN/A    // Create the actual Serializable corresponding to the parameter
1552SN/A    // values in this context.  This function is overridden in derived
1562SN/A    // classes to call a specific constructor for a particular
157395SN/A    // subclass of Serializable.
158395SN/A    virtual Serializable *create() = 0;
1592SN/A};
1602SN/A
1612SN/A//
162395SN/A// An instance of SerializableClass corresponds to a class derived from
163395SN/A// Serializable.  The SerializableClass instance serves to bind the string
1642SN/A// name (found in the config file) to a function that creates an
1652SN/A// instance of the appropriate derived class.
1662SN/A//
1672SN/A// This would be much cleaner in Smalltalk or Objective-C, where types
1682SN/A// are first-class objects themselves.
1692SN/A//
170395SN/Aclass SerializableClass
1712SN/A{
1722SN/A  public:
1732SN/A
1742SN/A    // Type CreateFunc is a pointer to a function that creates a new
1752SN/A    // simulation object builder based on a .ini-file parameter
1762SN/A    // section (specified by the first string argument), a unique name
1772SN/A    // for the object (specified by the second string argument), and
1782SN/A    // an optional config hierarchy node (specified by the third
179395SN/A    // argument).  A pointer to the new SerializableBuilder is returned.
180395SN/A    typedef Serializable *(*CreateFunc)(Checkpoint *cp,
1812738Sstever@eecs.umich.edu                                        const std::string &section);
1822SN/A
1832SN/A    static std::map<std::string,CreateFunc> *classMap;
1842SN/A
1852SN/A    // Constructor.  For example:
1862SN/A    //
187395SN/A    // SerializableClass baseCacheSerializableClass("BaseCacheSerializable",
188395SN/A    //                         newBaseCacheSerializableBuilder);
1892SN/A    //
190395SN/A    SerializableClass(const std::string &className, CreateFunc createFunc);
1912SN/A
192395SN/A    // create Serializable given name of class and pointer to
1932SN/A    // configuration hierarchy node
194395SN/A    static Serializable *createObject(Checkpoint *cp,
1952738Sstever@eecs.umich.edu                                      const std::string &section);
1962SN/A};
1972SN/A
1982SN/A//
1992SN/A// Macros to encapsulate the magic of declaring & defining
200395SN/A// SerializableBuilder and SerializableClass objects
2012SN/A//
2022SN/A
203237SN/A#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS)			   \
204395SN/ASerializableClass the##OBJ_CLASS##Class(CLASS_NAME,			   \
205237SN/A                                         OBJ_CLASS::createForUnserialize);
2062SN/A
207237SN/Aclass Checkpoint
208237SN/A{
209237SN/A  private:
210237SN/A
211237SN/A    IniFile *db;
212237SN/A    const std::string basePath;
213395SN/A    std::map<std::string, Serializable*> objMap;
214237SN/A
215237SN/A  public:
2162738Sstever@eecs.umich.edu    Checkpoint(const std::string &cpt_dir, const std::string &path);
217237SN/A
218937SN/A    const std::string cptDir;
219937SN/A
220237SN/A    bool find(const std::string &section, const std::string &entry,
221237SN/A              std::string &value);
222237SN/A
223237SN/A    bool findObj(const std::string &section, const std::string &entry,
224395SN/A                 Serializable *&value);
225304SN/A
226304SN/A    bool sectionExists(const std::string &section);
227449SN/A
228449SN/A    // The following static functions have to do with checkpoint
229449SN/A    // creation rather than restoration.  This class makes a handy
230449SN/A    // namespace for them though.
231449SN/A
232449SN/A    // Export current checkpoint directory name so other objects can
233449SN/A    // derive filenames from it (e.g., memory).  The return value is
234449SN/A    // guaranteed to end in '/' so filenames can be directly appended.
235449SN/A    // This function is only valid while a checkpoint is being created.
236449SN/A    static std::string dir();
237449SN/A
238449SN/A    // Filename for base checkpoint file within directory.
239449SN/A    static const char *baseFilename;
240449SN/A
241449SN/A    // Set up a checkpoint creation event or series of events.
242449SN/A    static void setup(Tick when, Tick period = 0);
243237SN/A};
2442SN/A
2452SN/A#endif // __SERIALIZE_HH__
246