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.
30 * Serialization Interface Declarations
33 #ifndef __SERIALIZE_HH__
34 #define __SERIALIZE_HH__
41 #include "sim/host.hh"
42 #include "sim/configfile.hh"
48 void paramOut(std::ostream &os, const std::string &name, const T ¶m);
51 void paramIn(Checkpoint *cp, const std::string §ion,
52 const std::string &name, T ¶m);
55 void arrayParamOut(std::ostream &os, const std::string &name,
56 const T *param, int size);
59 void arrayParamIn(Checkpoint *cp, const std::string §ion,
60 const std::string &name, T *param, int size);
63 objParamIn(Checkpoint *cp, const std::string §ion,
64 const std::string &name, Serializable * ¶m);
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)
73 #define UNSERIALIZE_SCALAR(scalar) paramIn(cp, section, #scalar, scalar)
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)
78 #define UNSERIALIZE_ENUM(scalar) \
81 paramIn(cp, section, #scalar, tmp); \
82 scalar = (typeof(scalar))tmp; \
85 #define SERIALIZE_ARRAY(member, size) \
86 arrayParamOut(os, #member, member, size)
88 #define UNSERIALIZE_ARRAY(member, size) \
89 arrayParamIn(cp, section, #member, member, size)
91 #define SERIALIZE_OBJPTR(objptr) paramOut(os, #objptr, (objptr)->name())
93 #define UNSERIALIZE_OBJPTR(objptr) \
96 objParamIn(cp, section, #objptr, sptr); \
97 objptr = dynamic_cast<typeof(objptr)>(sptr); \
101 * Basic support for object serialization.
106 void nameOut(std::ostream &os);
107 void nameOut(std::ostream &os, const std::string &_name);
111 virtual ~Serializable() {}
113 // manditory virtual function, so objects must provide names
114 virtual const std::string name() const = 0;
116 virtual void serialize(std::ostream &os) {}
117 virtual void unserialize(Checkpoint *cp, const std::string §ion) {}
119 static Serializable *create(Checkpoint *cp,
120 const std::string §ion);
124 static void serializeAll();
125 static void unserializeGlobals(Checkpoint *cp);
129 // A SerializableBuilder serves as an evaluation context for a set of
130 // parameters that describe a specific instance of a Serializable. This
131 // evaluation context corresponds to a section in the .ini file (as
132 // with the base ParamContext) plus an optional node in the
133 // configuration hierarchy (the configNode member) for resolving
134 // Serializable references. SerializableBuilder is an abstract superclass;
135 // derived classes specialize the class for particular subclasses of
136 // Serializable (e.g., BaseCache).
138 // For typical usage, see the definition of
139 // SerializableClass::createObject().
141 class SerializableBuilder
145 SerializableBuilder() {}
147 virtual ~SerializableBuilder() {}
149 // Create the actual Serializable corresponding to the parameter
150 // values in this context. This function is overridden in derived
151 // classes to call a specific constructor for a particular
152 // subclass of Serializable.
153 virtual Serializable *create() = 0;
157 // An instance of SerializableClass corresponds to a class derived from
158 // Serializable. The SerializableClass instance serves to bind the string
159 // name (found in the config file) to a function that creates an
160 // instance of the appropriate derived class.
162 // This would be much cleaner in Smalltalk or Objective-C, where types
163 // are first-class objects themselves.
165 class SerializableClass
169 // Type CreateFunc is a pointer to a function that creates a new
170 // simulation object builder based on a .ini-file parameter
171 // section (specified by the first string argument), a unique name
172 // for the object (specified by the second string argument), and
173 // an optional config hierarchy node (specified by the third
174 // argument). A pointer to the new SerializableBuilder is returned.
175 typedef Serializable *(*CreateFunc)(Checkpoint *cp,
176 const std::string §ion);
178 static std::map<std::string,CreateFunc> *classMap;
180 // Constructor. For example:
182 // SerializableClass baseCacheSerializableClass("BaseCacheSerializable",
183 // newBaseCacheSerializableBuilder);
185 SerializableClass(const std::string &className, CreateFunc createFunc);
187 // create Serializable given name of class and pointer to
188 // configuration hierarchy node
189 static Serializable *createObject(Checkpoint *cp,
190 const std::string §ion);
194 // Macros to encapsulate the magic of declaring & defining
195 // SerializableBuilder and SerializableClass objects
198 #define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS) \
199 SerializableClass the##OBJ_CLASS##Class(CLASS_NAME, \
200 OBJ_CLASS::createForUnserialize);
207 const std::string basePath;
208 const ConfigNode *configNode;
209 std::map<std::string, Serializable*> objMap;
212 Checkpoint(const std::string &cpt_dir, const std::string &path,
213 const ConfigNode *_configNode);
215 const std::string cptDir;
217 bool find(const std::string §ion, const std::string &entry,
220 bool findObj(const std::string §ion, const std::string &entry,
221 Serializable *&value);
223 bool sectionExists(const std::string §ion);
225 // The following static functions have to do with checkpoint
226 // creation rather than restoration. This class makes a handy
227 // namespace for them though.
229 // Export current checkpoint directory name so other objects can
230 // derive filenames from it (e.g., memory). The return value is
231 // guaranteed to end in '/' so filenames can be directly appended.
232 // This function is only valid while a checkpoint is being created.
233 static std::string dir();
235 // Filename for base checkpoint file within directory.
236 static const char *baseFilename;
238 // Set up a checkpoint creation event or series of events.
239 static void setup(Tick when, Tick period = 0);
242 #endif // __SERIALIZE_HH__