serialize.hh (6214:1ec0ec8933ae) serialize.hh (6227:a17798f2a52c)
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 <list>
42#include <vector>
43#include <iostream>
44#include <map>
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>
61void arrayParamOut(std::ostream &os, const std::string &name,
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 <list>
42#include <vector>
43#include <iostream>
44#include <map>
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>
61void arrayParamOut(std::ostream &os, const std::string &name,
62 const T *param, int size);
62 const T *param, unsigned size);
63
64template <class T>
65void arrayParamOut(std::ostream &os, const std::string &name,
66 const std::vector<T> &param);
67
68template <class T>
69void arrayParamIn(Checkpoint *cp, const std::string &section,
63
64template <class T>
65void arrayParamOut(std::ostream &os, const std::string &name,
66 const std::vector<T> &param);
67
68template <class T>
69void arrayParamIn(Checkpoint *cp, const std::string &section,
70 const std::string &name, T *param, int size);
70 const std::string &name, T *param, unsigned size);
71
72template <class T>
73void arrayParamIn(Checkpoint *cp, const std::string &section,
74 const std::string &name, std::vector<T> &param);
75
76void
77objParamIn(Checkpoint *cp, const std::string &section,
78 const std::string &name, SimObject * &param);
79
80
81//
82// These macros are streamlined to use in serialize/unserialize
83// functions. It's assumed that serialize() has a parameter 'os' for
84// the ostream, and unserialize() has parameters 'cp' and 'section'.
85#define SERIALIZE_SCALAR(scalar) paramOut(os, #scalar, scalar)
86
87#define UNSERIALIZE_SCALAR(scalar) paramIn(cp, section, #scalar, scalar)
88
89// ENUMs are like SCALARs, but we cast them to ints on the way out
90#define SERIALIZE_ENUM(scalar) paramOut(os, #scalar, (int)scalar)
91
92#define UNSERIALIZE_ENUM(scalar) \
93 do { \
94 int tmp; \
95 paramIn(cp, section, #scalar, tmp); \
96 scalar = (typeof(scalar))tmp; \
97 } while (0)
98
99#define SERIALIZE_ARRAY(member, size) \
100 arrayParamOut(os, #member, member, size)
101
102#define UNSERIALIZE_ARRAY(member, size) \
103 arrayParamIn(cp, section, #member, member, size)
104
105#define SERIALIZE_OBJPTR(objptr) paramOut(os, #objptr, (objptr)->name())
106
107#define UNSERIALIZE_OBJPTR(objptr) \
108 do { \
109 SimObject *sptr; \
110 objParamIn(cp, section, #objptr, sptr); \
111 objptr = dynamic_cast<typeof(objptr)>(sptr); \
112 } while (0)
113
114/*
115 * Basic support for object serialization.
116 */
117class Serializable
118{
119 protected:
120 void nameOut(std::ostream &os);
121 void nameOut(std::ostream &os, const std::string &_name);
122
123 public:
124 Serializable();
125 virtual ~Serializable();
126
127 // manditory virtual function, so objects must provide names
128 virtual const std::string name() const = 0;
129
130 virtual void serialize(std::ostream &os);
131 virtual void unserialize(Checkpoint *cp, const std::string &section);
132
133 static Serializable *create(Checkpoint *cp, const std::string &section);
134
135 static int ckptCount;
136 static int ckptMaxCount;
137 static int ckptPrevCount;
138 static void serializeAll(const std::string &cpt_dir);
139 static void unserializeAll(const std::string &cpt_dir);
140 static void unserializeGlobals(Checkpoint *cp);
141};
142
143//
144// A SerializableBuilder serves as an evaluation context for a set of
145// parameters that describe a specific instance of a Serializable. This
146// evaluation context corresponds to a section in the .ini file (as
147// with the base ParamContext) plus an optional node in the
148// configuration hierarchy (the configNode member) for resolving
149// Serializable references. SerializableBuilder is an abstract superclass;
150// derived classes specialize the class for particular subclasses of
151// Serializable (e.g., BaseCache).
152//
153// For typical usage, see the definition of
154// SerializableClass::createObject().
155//
156class SerializableBuilder
157{
158 public:
159
160 SerializableBuilder() {}
161
162 virtual ~SerializableBuilder() {}
163
164 // Create the actual Serializable corresponding to the parameter
165 // values in this context. This function is overridden in derived
166 // classes to call a specific constructor for a particular
167 // subclass of Serializable.
168 virtual Serializable *create() = 0;
169};
170
171//
172// An instance of SerializableClass corresponds to a class derived from
173// Serializable. The SerializableClass instance serves to bind the string
174// name (found in the config file) to a function that creates an
175// instance of the appropriate derived class.
176//
177// This would be much cleaner in Smalltalk or Objective-C, where types
178// are first-class objects themselves.
179//
180class SerializableClass
181{
182 public:
183
184 // Type CreateFunc is a pointer to a function that creates a new
185 // simulation object builder based on a .ini-file parameter
186 // section (specified by the first string argument), a unique name
187 // for the object (specified by the second string argument), and
188 // an optional config hierarchy node (specified by the third
189 // argument). A pointer to the new SerializableBuilder is returned.
190 typedef Serializable *(*CreateFunc)(Checkpoint *cp,
191 const std::string &section);
192
193 static std::map<std::string,CreateFunc> *classMap;
194
195 // Constructor. For example:
196 //
197 // SerializableClass baseCacheSerializableClass("BaseCacheSerializable",
198 // newBaseCacheSerializableBuilder);
199 //
200 SerializableClass(const std::string &className, CreateFunc createFunc);
201
202 // create Serializable given name of class and pointer to
203 // configuration hierarchy node
204 static Serializable *createObject(Checkpoint *cp,
205 const std::string &section);
206};
207
208//
209// Macros to encapsulate the magic of declaring & defining
210// SerializableBuilder and SerializableClass objects
211//
212
213#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS) \
214SerializableClass the##OBJ_CLASS##Class(CLASS_NAME, \
215 OBJ_CLASS::createForUnserialize);
216
217void
218setCheckpointDir(const std::string &name);
219
220class Checkpoint
221{
222 private:
223
224 IniFile *db;
225 const std::string basePath;
226 std::map<std::string, Serializable*> objMap;
227
228 public:
229 Checkpoint(const std::string &cpt_dir, const std::string &path);
230
231 const std::string cptDir;
232
233 bool find(const std::string &section, const std::string &entry,
234 std::string &value);
235
236 bool findObj(const std::string &section, const std::string &entry,
237 SimObject *&value);
238
239 bool sectionExists(const std::string &section);
240
241 // The following static functions have to do with checkpoint
242 // creation rather than restoration. This class makes a handy
243 // namespace for them though.
244
245 // Export current checkpoint directory name so other objects can
246 // derive filenames from it (e.g., memory). The return value is
247 // guaranteed to end in '/' so filenames can be directly appended.
248 // This function is only valid while a checkpoint is being created.
249 static std::string dir();
250
251 // Filename for base checkpoint file within directory.
252 static const char *baseFilename;
253};
254
255#endif // __SERIALIZE_HH__
71
72template <class T>
73void arrayParamIn(Checkpoint *cp, const std::string &section,
74 const std::string &name, std::vector<T> &param);
75
76void
77objParamIn(Checkpoint *cp, const std::string &section,
78 const std::string &name, SimObject * &param);
79
80
81//
82// These macros are streamlined to use in serialize/unserialize
83// functions. It's assumed that serialize() has a parameter 'os' for
84// the ostream, and unserialize() has parameters 'cp' and 'section'.
85#define SERIALIZE_SCALAR(scalar) paramOut(os, #scalar, scalar)
86
87#define UNSERIALIZE_SCALAR(scalar) paramIn(cp, section, #scalar, scalar)
88
89// ENUMs are like SCALARs, but we cast them to ints on the way out
90#define SERIALIZE_ENUM(scalar) paramOut(os, #scalar, (int)scalar)
91
92#define UNSERIALIZE_ENUM(scalar) \
93 do { \
94 int tmp; \
95 paramIn(cp, section, #scalar, tmp); \
96 scalar = (typeof(scalar))tmp; \
97 } while (0)
98
99#define SERIALIZE_ARRAY(member, size) \
100 arrayParamOut(os, #member, member, size)
101
102#define UNSERIALIZE_ARRAY(member, size) \
103 arrayParamIn(cp, section, #member, member, size)
104
105#define SERIALIZE_OBJPTR(objptr) paramOut(os, #objptr, (objptr)->name())
106
107#define UNSERIALIZE_OBJPTR(objptr) \
108 do { \
109 SimObject *sptr; \
110 objParamIn(cp, section, #objptr, sptr); \
111 objptr = dynamic_cast<typeof(objptr)>(sptr); \
112 } while (0)
113
114/*
115 * Basic support for object serialization.
116 */
117class Serializable
118{
119 protected:
120 void nameOut(std::ostream &os);
121 void nameOut(std::ostream &os, const std::string &_name);
122
123 public:
124 Serializable();
125 virtual ~Serializable();
126
127 // manditory virtual function, so objects must provide names
128 virtual const std::string name() const = 0;
129
130 virtual void serialize(std::ostream &os);
131 virtual void unserialize(Checkpoint *cp, const std::string &section);
132
133 static Serializable *create(Checkpoint *cp, const std::string &section);
134
135 static int ckptCount;
136 static int ckptMaxCount;
137 static int ckptPrevCount;
138 static void serializeAll(const std::string &cpt_dir);
139 static void unserializeAll(const std::string &cpt_dir);
140 static void unserializeGlobals(Checkpoint *cp);
141};
142
143//
144// A SerializableBuilder serves as an evaluation context for a set of
145// parameters that describe a specific instance of a Serializable. This
146// evaluation context corresponds to a section in the .ini file (as
147// with the base ParamContext) plus an optional node in the
148// configuration hierarchy (the configNode member) for resolving
149// Serializable references. SerializableBuilder is an abstract superclass;
150// derived classes specialize the class for particular subclasses of
151// Serializable (e.g., BaseCache).
152//
153// For typical usage, see the definition of
154// SerializableClass::createObject().
155//
156class SerializableBuilder
157{
158 public:
159
160 SerializableBuilder() {}
161
162 virtual ~SerializableBuilder() {}
163
164 // Create the actual Serializable corresponding to the parameter
165 // values in this context. This function is overridden in derived
166 // classes to call a specific constructor for a particular
167 // subclass of Serializable.
168 virtual Serializable *create() = 0;
169};
170
171//
172// An instance of SerializableClass corresponds to a class derived from
173// Serializable. The SerializableClass instance serves to bind the string
174// name (found in the config file) to a function that creates an
175// instance of the appropriate derived class.
176//
177// This would be much cleaner in Smalltalk or Objective-C, where types
178// are first-class objects themselves.
179//
180class SerializableClass
181{
182 public:
183
184 // Type CreateFunc is a pointer to a function that creates a new
185 // simulation object builder based on a .ini-file parameter
186 // section (specified by the first string argument), a unique name
187 // for the object (specified by the second string argument), and
188 // an optional config hierarchy node (specified by the third
189 // argument). A pointer to the new SerializableBuilder is returned.
190 typedef Serializable *(*CreateFunc)(Checkpoint *cp,
191 const std::string &section);
192
193 static std::map<std::string,CreateFunc> *classMap;
194
195 // Constructor. For example:
196 //
197 // SerializableClass baseCacheSerializableClass("BaseCacheSerializable",
198 // newBaseCacheSerializableBuilder);
199 //
200 SerializableClass(const std::string &className, CreateFunc createFunc);
201
202 // create Serializable given name of class and pointer to
203 // configuration hierarchy node
204 static Serializable *createObject(Checkpoint *cp,
205 const std::string &section);
206};
207
208//
209// Macros to encapsulate the magic of declaring & defining
210// SerializableBuilder and SerializableClass objects
211//
212
213#define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS) \
214SerializableClass the##OBJ_CLASS##Class(CLASS_NAME, \
215 OBJ_CLASS::createForUnserialize);
216
217void
218setCheckpointDir(const std::string &name);
219
220class Checkpoint
221{
222 private:
223
224 IniFile *db;
225 const std::string basePath;
226 std::map<std::string, Serializable*> objMap;
227
228 public:
229 Checkpoint(const std::string &cpt_dir, const std::string &path);
230
231 const std::string cptDir;
232
233 bool find(const std::string &section, const std::string &entry,
234 std::string &value);
235
236 bool findObj(const std::string &section, const std::string &entry,
237 SimObject *&value);
238
239 bool sectionExists(const std::string &section);
240
241 // The following static functions have to do with checkpoint
242 // creation rather than restoration. This class makes a handy
243 // namespace for them though.
244
245 // Export current checkpoint directory name so other objects can
246 // derive filenames from it (e.g., memory). The return value is
247 // guaranteed to end in '/' so filenames can be directly appended.
248 // This function is only valid while a checkpoint is being created.
249 static std::string dir();
250
251 // Filename for base checkpoint file within directory.
252 static const char *baseFilename;
253};
254
255#endif // __SERIALIZE_HH__