2 * Copyright (c) 2003 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__
40 #include "sim/host.hh"
41 #include "sim/configfile.hh"
46 * Basic support for object serialization.
51 // To allow other classes to do some of the serialization work.
56 // Make it so only Serializables can construct one of these.
57 Proxy(Serializeable *o) : obj(o) {};
59 friend class Serializeable;
63 void paramOut(const std::string &name, const T& param) const {
64 obj->paramOut(name, param);
68 friend class Serializer;
75 const Proxy &getProxy() { return(proxy); };
77 // object name: should be unique
81 static Serializer *serializer;
85 void nameOut(const std::string &_name);
86 void childOut(const std::string &name, Serializeable *child);
88 void paramOut(const std::string &name, const T& param);
90 std::ostream &out() const;
93 Serializeable(const std::string &n);
94 virtual ~Serializeable();
96 void setName(const std::string &name);
99 const std::string &name() const { return objName; }
101 virtual void nameChildren() {}
102 virtual void serialize() {}
103 virtual void unserialize(IniFile &db, const std::string &category,
104 ConfigNode *node = NULL)
106 std::cout << name() << " is being unserialized" << std::endl;
112 friend class Serializeable;
115 typedef std::list<Serializeable *> serlist_t;
118 std::ostream *output;
119 std::ostream &out() const;
123 virtual ~Serializer();
126 void add_object(Serializeable *obj);
130 void serialize(const std::string &file);
131 const std::string &filename() const { return file; }
136 Serializeable::paramOut(const std::string &name, const T& param)
138 out() << name << "=" << param << "\n";
142 Serializeable::paramOut(const std::string &name, const uint64_t& param);
146 // A SerializeableBuilder serves as an evaluation context for a set of
147 // parameters that describe a specific instance of a Serializeable. This
148 // evaluation context corresponds to a section in the .ini file (as
149 // with the base ParamContext) plus an optional node in the
150 // configuration hierarchy (the configNode member) for resolving
151 // Serializeable references. SerializeableBuilder is an abstract superclass;
152 // derived classes specialize the class for particular subclasses of
153 // Serializeable (e.g., BaseCache).
155 // For typical usage, see the definition of
156 // SerializeableClass::createObject().
158 class SerializeableBuilder
162 SerializeableBuilder() {}
164 virtual ~SerializeableBuilder() {}
166 // Create the actual Serializeable corresponding to the parameter
167 // values in this context. This function is overridden in derived
168 // classes to call a specific constructor for a particular
169 // subclass of Serializeable.
170 virtual Serializeable *create() = 0;
174 // An instance of SerializeableClass corresponds to a class derived from
175 // Serializeable. The SerializeableClass instance serves to bind the string
176 // name (found in the config file) to a function that creates an
177 // instance of the appropriate derived class.
179 // This would be much cleaner in Smalltalk or Objective-C, where types
180 // are first-class objects themselves.
182 class SerializeableClass
186 // Type CreateFunc is a pointer to a function that creates a new
187 // simulation object builder based on a .ini-file parameter
188 // section (specified by the first string argument), a unique name
189 // for the object (specified by the second string argument), and
190 // an optional config hierarchy node (specified by the third
191 // argument). A pointer to the new SerializeableBuilder is returned.
192 typedef SerializeableBuilder *(*CreateFunc)();
194 static std::map<std::string,CreateFunc> *classMap;
196 // Constructor. For example:
198 // SerializeableClass baseCacheSerializeableClass("BaseCacheSerializeable",
199 // newBaseCacheSerializeableBuilder);
201 SerializeableClass(const std::string &className, CreateFunc createFunc);
203 // create Serializeable given name of class and pointer to
204 // configuration hierarchy node
205 static Serializeable *createObject(IniFile &configDB,
206 const std::string &configClassName);
211 // Macros to encapsulate the magic of declaring & defining
212 // SerializeableBuilder and SerializeableClass objects
215 #define CREATE_SERIALIZEABLE(OBJ_CLASS) \
216 OBJ_CLASS *OBJ_CLASS##Builder::create()
218 #define REGISTER_SERIALIZEABLE(CLASS_NAME, OBJ_CLASS) \
219 class OBJ_CLASS##Builder : public SerializeableBuilder \
223 OBJ_CLASS##Builder() {} \
224 virtual ~OBJ_CLASS##Builder() {} \
226 OBJ_CLASS *create(); \
230 SerializeableBuilder * \
231 new##OBJ_CLASS##Builder() \
233 return new OBJ_CLASS##Builder(); \
236 SerializeableClass the##OBJ_CLASS##Class(CLASS_NAME, \
237 new##OBJ_CLASS##Builder);
241 #endif // __SERIALIZE_HH__