serialize.hh revision 8902:75b524b64c28
1/*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Nathan Binkert
29 *          Erik Hallnor
30 *          Steve Reinhardt
31 */
32
33/* @file
34 * Serialization Interface Declarations
35 */
36
37#ifndef __SERIALIZE_HH__
38#define __SERIALIZE_HH__
39
40
41#include <iostream>
42#include <list>
43#include <map>
44#include <vector>
45
46#include "base/types.hh"
47
48class IniFile;
49class Serializable;
50class Checkpoint;
51class SimObject;
52
53template <class T>
54void paramOut(std::ostream &os, const std::string &name, const T &param);
55
56template <class T>
57void paramIn(Checkpoint *cp, const std::string &section,
58             const std::string &name, T &param);
59
60template <class T>
61bool optParamIn(Checkpoint *cp, const std::string &section,
62             const std::string &name, T &param);
63
64template <class T>
65void arrayParamOut(std::ostream &os, const std::string &name,
66                   const T *param, unsigned size);
67
68template <class T>
69void arrayParamOut(std::ostream &os, const std::string &name,
70                   const std::vector<T> &param);
71
72template <class T>
73void arrayParamOut(std::ostream &os, const std::string &name,
74                   const std::list<T> &param);
75
76template <class T>
77void arrayParamIn(Checkpoint *cp, const std::string &section,
78                  const std::string &name, T *param, unsigned size);
79
80template <class T>
81void arrayParamIn(Checkpoint *cp, const std::string &section,
82                  const std::string &name, std::vector<T> &param);
83
84template <class T>
85void arrayParamIn(Checkpoint *cp, const std::string &section,
86                  const std::string &name, std::list<T> &param);
87
88void
89objParamIn(Checkpoint *cp, const std::string &section,
90           const std::string &name, SimObject * &param);
91
92template <typename T>
93void fromInt(T &t, int i)
94{
95    t = (T)i;
96}
97
98template <typename T>
99void fromSimObject(T &t, SimObject *s)
100{
101    t = dynamic_cast<T>(s);
102}
103
104//
105// These macros are streamlined to use in serialize/unserialize
106// functions.  It's assumed that serialize() has a parameter 'os' for
107// the ostream, and unserialize() has parameters 'cp' and 'section'.
108#define SERIALIZE_SCALAR(scalar)        paramOut(os, #scalar, scalar)
109
110#define UNSERIALIZE_SCALAR(scalar)      paramIn(cp, section, #scalar, scalar)
111#define UNSERIALIZE_OPT_SCALAR(scalar)      optParamIn(cp, section, #scalar, scalar)
112
113// ENUMs are like SCALARs, but we cast them to ints on the way out
114#define SERIALIZE_ENUM(scalar)          paramOut(os, #scalar, (int)scalar)
115
116#define UNSERIALIZE_ENUM(scalar)                \
117 do {                                           \
118    int tmp;                                    \
119    paramIn(cp, section, #scalar, tmp);         \
120    fromInt(scalar, tmp);                    \
121  } while (0)
122
123#define SERIALIZE_ARRAY(member, size)           \
124        arrayParamOut(os, #member, member, size)
125
126#define UNSERIALIZE_ARRAY(member, size)         \
127        arrayParamIn(cp, section, #member, member, size)
128
129#define SERIALIZE_OBJPTR(objptr)        paramOut(os, #objptr, (objptr)->name())
130
131#define UNSERIALIZE_OBJPTR(objptr)                      \
132  do {                                                  \
133    SimObject *sptr;                                    \
134    objParamIn(cp, section, #objptr, sptr);             \
135    fromSimObject(objptr, sptr);                        \
136  } while (0)
137
138/*
139 * Basic support for object serialization.
140 */
141class Serializable
142{
143  protected:
144    void nameOut(std::ostream &os);
145    void nameOut(std::ostream &os, const std::string &_name);
146
147  public:
148    Serializable();
149    virtual ~Serializable();
150
151    // manditory virtual function, so objects must provide names
152    virtual const std::string name() const = 0;
153
154    virtual void serialize(std::ostream &os);
155    virtual void unserialize(Checkpoint *cp, const std::string &section);
156
157    static Serializable *create(Checkpoint *cp, const std::string &section);
158
159    static int ckptCount;
160    static int ckptMaxCount;
161    static int ckptPrevCount;
162    static void serializeAll(const std::string &cpt_dir);
163    static void unserializeGlobals(Checkpoint *cp);
164};
165
166//
167// A SerializableBuilder serves as an evaluation context for a set of
168// parameters that describe a specific instance of a Serializable.  This
169// evaluation context corresponds to a section in the .ini file (as
170// with the base ParamContext) plus an optional node in the
171// configuration hierarchy (the configNode member) for resolving
172// Serializable references.  SerializableBuilder is an abstract superclass;
173// derived classes specialize the class for particular subclasses of
174// Serializable (e.g., BaseCache).
175//
176// For typical usage, see the definition of
177// SerializableClass::createObject().
178//
179class SerializableBuilder
180{
181  public:
182
183    SerializableBuilder() {}
184
185    virtual ~SerializableBuilder() {}
186
187    // Create the actual Serializable corresponding to the parameter
188    // values in this context.  This function is overridden in derived
189    // classes to call a specific constructor for a particular
190    // subclass of Serializable.
191    virtual Serializable *create() = 0;
192};
193
194//
195// An instance of SerializableClass corresponds to a class derived from
196// Serializable.  The SerializableClass instance serves to bind the string
197// name (found in the config file) to a function that creates an
198// instance of the appropriate derived class.
199//
200// This would be much cleaner in Smalltalk or Objective-C, where types
201// are first-class objects themselves.
202//
203class SerializableClass
204{
205  public:
206
207    // Type CreateFunc is a pointer to a function that creates a new
208    // simulation object builder based on a .ini-file parameter
209    // section (specified by the first string argument), a unique name
210    // for the object (specified by the second string argument), and
211    // an optional config hierarchy node (specified by the third
212    // argument).  A pointer to the new SerializableBuilder is returned.
213    typedef Serializable *(*CreateFunc)(Checkpoint *cp,
214                                        const std::string &section);
215
216    static std::map<std::string,CreateFunc> *classMap;
217
218    // Constructor.  For example:
219    //
220    // SerializableClass baseCacheSerializableClass("BaseCacheSerializable",
221    //                         newBaseCacheSerializableBuilder);
222    //
223    SerializableClass(const std::string &className, CreateFunc createFunc);
224
225    // create Serializable given name of class and pointer to
226    // configuration hierarchy node
227    static Serializable *createObject(Checkpoint *cp,
228                                      const std::string &section);
229};
230
231//
232// Macros to encapsulate the magic of declaring & defining
233// SerializableBuilder and SerializableClass objects
234//
235
236#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS)                      \
237SerializableClass the##OBJ_CLASS##Class(CLASS_NAME,                        \
238                                         OBJ_CLASS::createForUnserialize);
239
240class Checkpoint
241{
242  private:
243
244    IniFile *db;
245
246  public:
247    Checkpoint(const std::string &cpt_dir);
248
249    const std::string cptDir;
250
251    bool find(const std::string &section, const std::string &entry,
252              std::string &value);
253
254    bool findObj(const std::string &section, const std::string &entry,
255                 SimObject *&value);
256
257    bool sectionExists(const std::string &section);
258
259    // The following static functions have to do with checkpoint
260    // creation rather than restoration.  This class makes a handy
261    // namespace for them though.  Currently no Checkpoint object is
262    // created on serialization (only unserialization) so we track the
263    // directory name as a global.  It would be nice to change this
264    // someday
265
266  private:
267    // current directory we're serializing into.
268    static std::string currentDirectory;
269
270  public:
271    // Set the current directory.  This function takes care of
272    // inserting curTick() if there's a '%d' in the argument, and
273    // appends a '/' if necessary.  The final name is returned.
274    static std::string setDir(const std::string &base_name);
275
276    // Export current checkpoint directory name so other objects can
277    // derive filenames from it (e.g., memory).  The return value is
278    // guaranteed to end in '/' so filenames can be directly appended.
279    // This function is only valid while a checkpoint is being created.
280    static std::string dir();
281
282    // Filename for base checkpoint file within directory.
283    static const char *baseFilename;
284};
285
286#endif // __SERIALIZE_HH__
287