36e40c2a9a3698766f7c28bed88dccb240faa756
[gem5.git] / sim / builder.hh
1 /*
2 * Copyright (c) 2003-2004 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 #ifndef __BUILDER_HH__
30 #define __BUILDER_HH__
31
32 #include <iosfwd>
33 #include <list>
34 #include <map>
35 #include <vector>
36
37 #include "sim/param.hh"
38
39 class SimObject;
40
41 //
42 // A SimObjectBuilder serves as an evaluation context for a set of
43 // parameters that describe a specific instance of a SimObject. This
44 // evaluation context corresponds to a section in the .ini file (as
45 // with the base ParamContext) plus an optional node in the
46 // configuration hierarchy (the configNode member) for resolving
47 // SimObject references. SimObjectBuilder is an abstract superclass;
48 // derived classes specialize the class for particular subclasses of
49 // SimObject (e.g., BaseCache).
50 //
51 // For typical usage, see the definition of
52 // SimObjectClass::createObject().
53 //
54 class SimObjectBuilder : public ParamContext
55 {
56 private:
57 // name of the instance we are creating
58 std::string instanceName;
59
60 // The corresponding node in the configuration hierarchy.
61 // (optional: may be null if the created object is not in the
62 // hierarchy)
63 ConfigNode *configNode;
64
65 // The external SimObject class name (for error messages)
66 std::string simObjClassName;
67
68 public:
69 SimObjectBuilder(const std::string &_configClass,
70 const std::string &_instanceName,
71 ConfigNode *_configNode,
72 const std::string &_simObjClassName);
73
74 virtual ~SimObjectBuilder();
75
76 // call parse() on all params in this context to convert string
77 // representations to parameter values
78 virtual void parseParams(IniFile &iniFile);
79
80 // parameter error prolog (override of ParamContext)
81 virtual void printErrorProlog(std::ostream &);
82
83 // generate the name for this SimObject instance (derived from the
84 // configuration hierarchy node label and position)
85 virtual const std::string &getInstanceName() { return instanceName; }
86
87 // return the configuration hierarchy node for this context.
88 virtual ConfigNode *getConfigNode() { return configNode; }
89
90 // Create the actual SimObject corresponding to the parameter
91 // values in this context. This function is overridden in derived
92 // classes to call a specific constructor for a particular
93 // subclass of SimObject.
94 virtual SimObject *create() = 0;
95 };
96
97
98 //
99 // Handy macros for initializing parameter members of classes derived
100 // from SimObjectBuilder. Assumes that the name of the parameter
101 // member object is the same as the textual parameter name seen by the
102 // user. (Note that '#p' is expanded by the preprocessor to '"p"'.)
103 //
104 #define INIT_PARAM(p, desc) p(this, #p, desc)
105 #define INIT_PARAM_DFLT(p, desc, dflt) p(this, #p, desc, dflt)
106
107 //
108 // Initialize an enumeration variable... assumes that 'map' is the
109 // name of an array of mappings (char * for SimpleEnumParam, or
110 // EnumParamMap for MappedEnumParam).
111 //
112 #define INIT_ENUM_PARAM(p, desc, map) \
113 p(this, #p, desc, map, sizeof(map)/sizeof(map[0]))
114 #define INIT_ENUM_PARAM_DFLT(p, desc, map, dflt) \
115 p(this, #p, desc, map, sizeof(map)/sizeof(map[0]), dflt)
116
117 //
118 // An instance of SimObjectClass corresponds to a class derived from
119 // SimObject. The SimObjectClass instance serves to bind the string
120 // name (found in the config file) to a function that creates an
121 // instance of the appropriate derived class.
122 //
123 // This would be much cleaner in Smalltalk or Objective-C, where types
124 // are first-class objects themselves.
125 //
126 class SimObjectClass
127 {
128 public:
129 // Type CreateFunc is a pointer to a function that creates a new
130 // simulation object builder based on a .ini-file parameter
131 // section (specified by the first string argument), a unique name
132 // for the object (specified by the second string argument), and
133 // an optional config hierarchy node (specified by the third
134 // argument). A pointer to the new SimObjectBuilder is returned.
135 typedef SimObjectBuilder *(*CreateFunc)(const std::string &configClassName,
136 const std::string &objName,
137 ConfigNode *configNode,
138 const std::string &simObjClassName);
139
140 static std::map<std::string,CreateFunc> *classMap;
141
142 // Constructor. For example:
143 //
144 // SimObjectClass baseCacheClass("BaseCache", newBaseCacheBuilder);
145 //
146 SimObjectClass(const std::string &className, CreateFunc createFunc);
147
148 // create SimObject given name of class and pointer to
149 // configuration hierarchy node
150 static SimObject *createObject(IniFile &configDB,
151 const std::string &configClassName,
152 const std::string &objName,
153 ConfigNode *configNode);
154
155 // print descriptions of all parameters registered with all
156 // SimObject classes
157 static void describeAllClasses(std::ostream &os);
158 };
159
160 //
161 // Macros to encapsulate the magic of declaring & defining
162 // SimObjectBuilder and SimObjectClass objects
163 //
164
165 #define BEGIN_DECLARE_SIM_OBJECT_PARAMS(OBJ_CLASS) \
166 class OBJ_CLASS##Builder : public SimObjectBuilder \
167 { \
168 public:
169
170 #define END_DECLARE_SIM_OBJECT_PARAMS(OBJ_CLASS) \
171 \
172 OBJ_CLASS##Builder(const std::string &configClass, \
173 const std::string &instanceName, \
174 ConfigNode *configNode, \
175 const std::string &simObjClassName); \
176 virtual ~OBJ_CLASS##Builder() {} \
177 \
178 OBJ_CLASS *create(); \
179 };
180
181 #define BEGIN_INIT_SIM_OBJECT_PARAMS(OBJ_CLASS) \
182 OBJ_CLASS##Builder::OBJ_CLASS##Builder(const std::string &configClass, \
183 const std::string &instanceName, \
184 ConfigNode *configNode, \
185 const std::string &simObjClassName) \
186 : SimObjectBuilder(configClass, instanceName, \
187 configNode, simObjClassName),
188
189
190 #define END_INIT_SIM_OBJECT_PARAMS(OBJ_CLASS) \
191 { \
192 }
193
194 #define CREATE_SIM_OBJECT(OBJ_CLASS) \
195 OBJ_CLASS *OBJ_CLASS##Builder::create()
196
197 #define REGISTER_SIM_OBJECT(CLASS_NAME, OBJ_CLASS) \
198 SimObjectBuilder * \
199 new##OBJ_CLASS##Builder(const std::string &configClass, \
200 const std::string &instanceName, \
201 ConfigNode *configNode, \
202 const std::string &simObjClassName) \
203 { \
204 return new OBJ_CLASS##Builder(configClass, instanceName, \
205 configNode, simObjClassName); \
206 } \
207 \
208 SimObjectClass the##OBJ_CLASS##Class(CLASS_NAME, \
209 new##OBJ_CLASS##Builder); \
210 \
211 /* see param.hh */ \
212 DEFINE_SIM_OBJECT_CLASS_NAME(CLASS_NAME, OBJ_CLASS)
213
214
215 #endif // __BUILDER_HH__