serialize.hh revision 237
1/*
2 * Copyright (c) 2003 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
29/* @file
30 * Serialization Interface Declarations
31 */
32
33#ifndef __SERIALIZE_HH__
34#define __SERIALIZE_HH__
35
36
37#include <list>
38#include <iostream>
39#include <map>
40
41#include "sim/host.hh"
42#include "sim/configfile.hh"
43
44class Serializeable;
45class Checkpoint;
46
47template <class T>
48void paramOut(std::ostream &os, const std::string &name, const T& param);
49
50template <class T>
51void paramIn(Checkpoint *cp, const std::string &section,
52             const std::string &name, T& param);
53
54template <class T>
55void arrayParamOut(std::ostream &os, const std::string &name,
56                   const T *param, int size);
57
58template <class T>
59void arrayParamIn(Checkpoint *cp, const std::string &section,
60                  const std::string &name, T *param, int size);
61
62void
63objParamIn(Checkpoint *cp, const std::string &section,
64           const std::string &name, Serializeable * &param);
65
66
67//
68// These macros are streamlined to use in serialize/unserialize
69// functions.  It's assumed that serialize() has a parameter 'os' for
70// the ostream, and unserialize() has parameters 'cp' and 'section'.
71#define SERIALIZE_SCALAR(scalar)	paramOut(os, #scalar, scalar)
72
73#define UNSERIALIZE_SCALAR(scalar)	paramIn(cp, section, #scalar, scalar)
74
75// ENUMs are like SCALARs, but we cast them to ints on the way out
76#define SERIALIZE_ENUM(scalar)		paramOut(os, #scalar, (int)scalar)
77
78#define UNSERIALIZE_ENUM(scalar)		\
79 do {						\
80    int tmp;					\
81    paramIn(cp, section, #scalar, tmp);		\
82    scalar = (typeof(scalar))tmp;		\
83  } while (0)
84
85#define SERIALIZE_ARRAY(member, size)	\
86        arrayParamOut(os, #member, member, size)
87
88#define UNSERIALIZE_ARRAY(member, size)	\
89        arrayParamIn(cp, section, #member, member, size)
90
91#define SERIALIZE_OBJPTR(objptr)	paramOut(os, #objptr, (objptr)->name())
92
93#define UNSERIALIZE_OBJPTR(objptr)			\
94  do {							\
95    Serializeable *sptr;				\
96    objParamIn(cp, section, #objptr, sptr);		\
97    objptr = dynamic_cast<typeof(objptr)>(sptr);	\
98  } while (0)
99
100/*
101 * Basic support for object serialization.
102 */
103class Serializeable
104{
105  public:
106
107    friend class Serializer;
108
109  protected:
110    // object name: should be unique
111    std::string objName;
112
113    bool serialized;
114    static Serializer *serializer;
115
116    void mark();
117    void nameOut(std::ostream& os);
118    void nameOut(std::ostream& os, const std::string &_name);
119
120  public:
121    Serializeable(const std::string &n);
122    virtual ~Serializeable();
123
124    void setName(const std::string &name);
125
126    // return name
127    const std::string &name() const { return objName; }
128
129    virtual void nameChildren() {}
130    virtual void serialize(std::ostream& os) {}
131    virtual void unserialize(Checkpoint *cp, const std::string &section) {}
132
133    static Serializeable *create(Checkpoint *cp,
134                                 const std::string &section);
135};
136
137class Serializer
138{
139    friend class Serializeable;
140
141  protected:
142    typedef std::list<Serializeable *> serlist_t;
143    serlist_t objects;
144    std::string file;
145    std::ostream *output;
146    std::ostream &out() const;
147
148  public:
149    Serializer();
150    virtual ~Serializer();
151
152  private:
153    void add_object(Serializeable *obj);
154    void add_objects();
155
156  public:
157    void serialize(const std::string &file);
158    const std::string &filename() const { return file; }
159};
160
161//
162// A SerializeableBuilder serves as an evaluation context for a set of
163// parameters that describe a specific instance of a Serializeable.  This
164// evaluation context corresponds to a section in the .ini file (as
165// with the base ParamContext) plus an optional node in the
166// configuration hierarchy (the configNode member) for resolving
167// Serializeable references.  SerializeableBuilder is an abstract superclass;
168// derived classes specialize the class for particular subclasses of
169// Serializeable (e.g., BaseCache).
170//
171// For typical usage, see the definition of
172// SerializeableClass::createObject().
173//
174class SerializeableBuilder
175{
176  public:
177
178    SerializeableBuilder() {}
179
180    virtual ~SerializeableBuilder() {}
181
182    // Create the actual Serializeable corresponding to the parameter
183    // values in this context.  This function is overridden in derived
184    // classes to call a specific constructor for a particular
185    // subclass of Serializeable.
186    virtual Serializeable *create() = 0;
187};
188
189//
190// An instance of SerializeableClass corresponds to a class derived from
191// Serializeable.  The SerializeableClass instance serves to bind the string
192// name (found in the config file) to a function that creates an
193// instance of the appropriate derived class.
194//
195// This would be much cleaner in Smalltalk or Objective-C, where types
196// are first-class objects themselves.
197//
198class SerializeableClass
199{
200  public:
201
202    // Type CreateFunc is a pointer to a function that creates a new
203    // simulation object builder based on a .ini-file parameter
204    // section (specified by the first string argument), a unique name
205    // for the object (specified by the second string argument), and
206    // an optional config hierarchy node (specified by the third
207    // argument).  A pointer to the new SerializeableBuilder is returned.
208    typedef Serializeable *(*CreateFunc)(Checkpoint *cp,
209                                         const std::string &section);
210
211    static std::map<std::string,CreateFunc> *classMap;
212
213    // Constructor.  For example:
214    //
215    // SerializeableClass baseCacheSerializeableClass("BaseCacheSerializeable",
216    //                         newBaseCacheSerializeableBuilder);
217    //
218    SerializeableClass(const std::string &className, CreateFunc createFunc);
219
220    // create Serializeable given name of class and pointer to
221    // configuration hierarchy node
222    static Serializeable *createObject(Checkpoint *cp,
223                                       const std::string &section);
224};
225
226//
227// Macros to encapsulate the magic of declaring & defining
228// SerializeableBuilder and SerializeableClass objects
229//
230
231#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS)			   \
232SerializeableClass the##OBJ_CLASS##Class(CLASS_NAME,			   \
233                                         OBJ_CLASS::createForUnserialize);
234
235class Checkpoint
236{
237  private:
238
239    IniFile *db;
240    const std::string basePath;
241    const ConfigNode *configNode;
242    std::map<std::string, Serializeable*> objMap;
243
244  public:
245    Checkpoint(const std::string &filename, const std::string &path,
246               const ConfigNode *_configNode);
247
248    bool find(const std::string &section, const std::string &entry,
249              std::string &value);
250
251    bool findObj(const std::string &section, const std::string &entry,
252                 Serializeable *&value);
253};
254
255
256//
257// Export checkpoint filename param so other objects can derive
258// filenames from it (e.g., memory).
259//
260extern std::string serializeFilename;
261
262#endif // __SERIALIZE_HH__
263