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.
31 #include "sim_object.hh"
33 #include "configfile.hh"
37 #include "sim_stats.hh"
43 ////////////////////////////////////////////////////////////////////////
45 // SimObject member definitions
47 ////////////////////////////////////////////////////////////////////////
50 // static list of all SimObjects, used for initialization etc.
52 SimObject::SimObjectList
SimObject::simObjectList
;
55 // SimObject constructor: used to maintain static simObjectList
57 SimObject::SimObject(const string
&_name
)
58 : Serializeable(_name
)
60 simObjectList
.push_back(this);
64 // no default statistics, so nothing to do in base implementation
67 SimObject::reg_stats(struct stat_sdb_t
*sdb
)
77 SimObject::regFormulas()
82 // no default extra output
85 SimObject::printExtraOutput(ostream
&os
)
90 // static function: call reg_stats() on all SimObjects.
93 SimObject::regAllStats()
95 SimObjectList::iterator i
;
96 SimObjectList::iterator end
= simObjectList
.end();
99 * @todo change cprintfs to DPRINTFs
101 for (i
= simObjectList
.begin(); i
!= end
; ++i
) {
103 cprintf("registering stats for %s\n", (*i
)->name());
105 (*i
)->reg_stats(sim_sdb
);
109 for (i
= simObjectList
.begin(); i
!= end
; ++i
) {
111 cprintf("registering formulas for %s\n", (*i
)->name());
118 // static function: call printExtraOutput() on all SimObjects.
121 SimObject::printAllExtraOutput(ostream
&os
)
123 SimObjectList::iterator i
;
125 for (i
= simObjectList
.begin(); i
!= simObjectList
.end(); ++i
) {
126 (*i
)->printExtraOutput(os
);
130 ///////////////////////////////////////////
132 // SimObjectBuilder member definitions
134 ///////////////////////////////////////////
136 // override ParamContext::parseParams() to check params based on
137 // instance name first. If not found, then check based on iniSection
138 // (as in default ParamContext implementation).
140 SimObjectBuilder::parseParams(IniFile
&iniFile
)
142 iniFilePtr
= &iniFile
; // set object member
144 ParamList::iterator i
;
146 for (i
= paramList
->begin(); i
!= paramList
->end(); ++i
) {
149 if (iniFile
.findDefault(instanceName
, (*i
)->name
, string_value
)) {
150 (*i
)->parse(string_value
);
152 else if (iniFile
.findDefault(iniSection
, (*i
)->name
, string_value
)) {
153 (*i
)->parse(string_value
);
160 SimObjectBuilder::printErrorProlog(ostream
&os
)
162 os
<< "Error creating object '" << getInstanceName()
163 << "' of type '" << simObjClassName
164 << "', section '" << iniSection
<< "':" << endl
;
168 ////////////////////////////////////////////////////////////////////////
170 // SimObjectClass member definitions
172 ////////////////////////////////////////////////////////////////////////
174 // Map of class names to SimObjectBuilder creation functions. Need to
175 // make this a pointer so we can force initialization on the first
176 // reference; otherwise, some SimObjectClass constructors may be invoked
177 // before the classMap constructor.
178 map
<string
,SimObjectClass::CreateFunc
> *SimObjectClass::classMap
= NULL
;
180 // SimObjectClass constructor: add mapping to classMap
181 SimObjectClass::SimObjectClass(const string
&className
, CreateFunc createFunc
)
183 if (classMap
== NULL
)
184 classMap
= new map
<string
,SimObjectClass::CreateFunc
>();
186 if ((*classMap
)[className
])
188 cerr
<< "Error: simulation object class " << className
<< " redefined"
193 // add className --> createFunc to class map
194 (*classMap
)[className
] = createFunc
;
201 SimObjectClass::createObject(IniFile
&configDB
,
202 const string
&configClassName
,
203 const string
&objName
,
204 ConfigNode
*configNode
)
206 // find simulation object class name from configuration class
207 // (specified by 'type=' parameter)
208 string simObjClassName
;
210 if (!configDB
.findDefault(configClassName
, "type", simObjClassName
)) {
211 cerr
<< "Configuration class '" << configClassName
<< "' not found."
216 // look up className to get appropriate createFunc
217 if (classMap
->find(simObjClassName
) == classMap
->end()) {
218 cerr
<< "Simulator object class '" << simObjClassName
<< "' not found."
223 CreateFunc createFunc
= (*classMap
)[simObjClassName
];
225 // call createFunc with config hierarchy node to get object
226 // builder instance (context with parameters for object creation)
227 SimObjectBuilder
*objectBuilder
= (*createFunc
)(configClassName
,
231 assert(objectBuilder
!= NULL
);
233 // parse all parameters in context to generate parameter values
234 objectBuilder
->parseParams(configDB
);
236 // now create the actual simulation object
237 SimObject
*object
= objectBuilder
->create();
239 assert(object
!= NULL
);
241 // echo object parameters to stats file (for documenting the
242 // config used to generate the associated stats)
243 *statStream
<< "[" << object
->name() << "]" << endl
;
244 *statStream
<< "type=" << simObjClassName
<< endl
;
245 objectBuilder
->showParams(*statStream
);
248 // done with the SimObjectBuilder now
249 delete objectBuilder
;
259 SimObjectClass::describeAllClasses(ostream
&os
)
261 map
<string
,CreateFunc
>::iterator iter
;
263 for (iter
= classMap
->begin(); iter
!= classMap
->end(); ++iter
) {
264 const string
&className
= iter
->first
;
265 CreateFunc createFunc
= iter
->second
;
267 os
<< "[" << className
<< "]\n";
269 // create dummy object builder just to instantiate parameters
270 SimObjectBuilder
*objectBuilder
= (*createFunc
)("", "", NULL
, "");
272 // now get the object builder to describe ite params
273 objectBuilder
->describeParams(os
);
277 // done with the object builder now
278 delete objectBuilder
;