2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
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.
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.
28 * Authors: Erik Hallnor
33 * Serialization Interface Declarations
36 #ifndef __SERIALIZE_HH__
37 #define __SERIALIZE_HH__
44 #include "sim/host.hh"
51 void paramOut(std::ostream &os, const std::string &name, const T ¶m);
54 void paramIn(Checkpoint *cp, const std::string §ion,
55 const std::string &name, T ¶m);
58 void arrayParamOut(std::ostream &os, const std::string &name,
59 const T *param, int size);
62 void arrayParamIn(Checkpoint *cp, const std::string §ion,
63 const std::string &name, T *param, int size);
66 objParamIn(Checkpoint *cp, const std::string §ion,
67 const std::string &name, Serializable * ¶m);
71 // These macros are streamlined to use in serialize/unserialize
72 // functions. It's assumed that serialize() has a parameter 'os' for
73 // the ostream, and unserialize() has parameters 'cp' and 'section'.
74 #define SERIALIZE_SCALAR(scalar) paramOut(os, #scalar, scalar)
76 #define UNSERIALIZE_SCALAR(scalar) paramIn(cp, section, #scalar, scalar)
78 // ENUMs are like SCALARs, but we cast them to ints on the way out
79 #define SERIALIZE_ENUM(scalar) paramOut(os, #scalar, (int)scalar)
81 #define UNSERIALIZE_ENUM(scalar) \
84 paramIn(cp, section, #scalar, tmp); \
85 scalar = (typeof(scalar))tmp; \
88 #define SERIALIZE_ARRAY(member, size) \
89 arrayParamOut(os, #member, member, size)
91 #define UNSERIALIZE_ARRAY(member, size) \
92 arrayParamIn(cp, section, #member, member, size)
94 #define SERIALIZE_OBJPTR(objptr) paramOut(os, #objptr, (objptr)->name())
96 #define UNSERIALIZE_OBJPTR(objptr) \
99 objParamIn(cp, section, #objptr, sptr); \
100 objptr = dynamic_cast<typeof(objptr)>(sptr); \
104 * Basic support for object serialization.
109 void nameOut(std::ostream &os);
110 void nameOut(std::ostream &os, const std::string &_name);
114 virtual ~Serializable() {}
116 // manditory virtual function, so objects must provide names
117 virtual const std::string name() const = 0;
119 virtual void serialize(std::ostream &os) {}
120 virtual void unserialize(Checkpoint *cp, const std::string §ion) {}
122 static Serializable *create(Checkpoint *cp,
123 const std::string §ion);
125 static int ckptCount;
126 static int ckptMaxCount;
127 static int ckptPrevCount;
128 static void serializeAll();
129 static void unserializeGlobals(Checkpoint *cp);
133 // A SerializableBuilder serves as an evaluation context for a set of
134 // parameters that describe a specific instance of a Serializable. This
135 // evaluation context corresponds to a section in the .ini file (as
136 // with the base ParamContext) plus an optional node in the
137 // configuration hierarchy (the configNode member) for resolving
138 // Serializable references. SerializableBuilder is an abstract superclass;
139 // derived classes specialize the class for particular subclasses of
140 // Serializable (e.g., BaseCache).
142 // For typical usage, see the definition of
143 // SerializableClass::createObject().
145 class SerializableBuilder
149 SerializableBuilder() {}
151 virtual ~SerializableBuilder() {}
153 // Create the actual Serializable corresponding to the parameter
154 // values in this context. This function is overridden in derived
155 // classes to call a specific constructor for a particular
156 // subclass of Serializable.
157 virtual Serializable *create() = 0;
161 // An instance of SerializableClass corresponds to a class derived from
162 // Serializable. The SerializableClass instance serves to bind the string
163 // name (found in the config file) to a function that creates an
164 // instance of the appropriate derived class.
166 // This would be much cleaner in Smalltalk or Objective-C, where types
167 // are first-class objects themselves.
169 class SerializableClass
173 // Type CreateFunc is a pointer to a function that creates a new
174 // simulation object builder based on a .ini-file parameter
175 // section (specified by the first string argument), a unique name
176 // for the object (specified by the second string argument), and
177 // an optional config hierarchy node (specified by the third
178 // argument). A pointer to the new SerializableBuilder is returned.
179 typedef Serializable *(*CreateFunc)(Checkpoint *cp,
180 const std::string §ion);
182 static std::map<std::string,CreateFunc> *classMap;
184 // Constructor. For example:
186 // SerializableClass baseCacheSerializableClass("BaseCacheSerializable",
187 // newBaseCacheSerializableBuilder);
189 SerializableClass(const std::string &className, CreateFunc createFunc);
191 // create Serializable given name of class and pointer to
192 // configuration hierarchy node
193 static Serializable *createObject(Checkpoint *cp,
194 const std::string §ion);
198 // Macros to encapsulate the magic of declaring & defining
199 // SerializableBuilder and SerializableClass objects
202 #define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS) \
203 SerializableClass the##OBJ_CLASS##Class(CLASS_NAME, \
204 OBJ_CLASS::createForUnserialize);
211 const std::string basePath;
212 std::map<std::string, Serializable*> objMap;
215 Checkpoint(const std::string &cpt_dir, const std::string &path);
217 const std::string cptDir;
219 bool find(const std::string §ion, const std::string &entry,
222 bool findObj(const std::string §ion, const std::string &entry,
223 Serializable *&value);
225 bool sectionExists(const std::string §ion);
227 // The following static functions have to do with checkpoint
228 // creation rather than restoration. This class makes a handy
229 // namespace for them though.
231 // Export current checkpoint directory name so other objects can
232 // derive filenames from it (e.g., memory). The return value is
233 // guaranteed to end in '/' so filenames can be directly appended.
234 // This function is only valid while a checkpoint is being created.
235 static std::string dir();
237 // Filename for base checkpoint file within directory.
238 static const char *baseFilename;
240 // Set up a checkpoint creation event or series of events.
241 static void setup(Tick when, Tick period = 0);
244 #endif // __SERIALIZE_HH__