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 "base/inifile.hh"
32 #include "base/misc.hh"
33 #include "sim/builder.hh"
34 #include "sim/configfile.hh"
35 #include "sim/host.hh"
36 #include "sim/sim_object.hh"
37 #include "sim/universe.hh"
46 static ostream
*stream
= NULL
;
49 if (!outputDirectory
.empty()) {
50 string filename
= outputDirectory
+ "builder.txt";
51 file
.open(filename
.c_str());
54 stream
= outputStream
;
61 SimObjectBuilder::SimObjectBuilder(const string
&_configClass
,
62 const string
&_instanceName
,
63 ConfigNode
*_configNode
,
64 const string
&_simObjClassName
)
65 : ParamContext(_configClass
, true),
66 instanceName(_instanceName
),
67 configNode(_configNode
),
68 simObjClassName(_simObjClassName
)
72 SimObjectBuilder::~SimObjectBuilder()
76 ///////////////////////////////////////////
78 // SimObjectBuilder member definitions
80 ///////////////////////////////////////////
82 // override ParamContext::parseParams() to check params based on
83 // instance name first. If not found, then check based on iniSection
84 // (as in default ParamContext implementation).
86 SimObjectBuilder::parseParams(IniFile
&iniFile
)
88 iniFilePtr
= &iniFile
; // set object member
90 ParamList::iterator i
;
92 for (i
= paramList
->begin(); i
!= paramList
->end(); ++i
) {
95 if (iniFile
.findDefault(instanceName
, (*i
)->name
, string_value
)) {
96 (*i
)->parse(string_value
);
98 else if (iniFile
.findDefault(iniSection
, (*i
)->name
, string_value
)) {
99 (*i
)->parse(string_value
);
106 SimObjectBuilder::printErrorProlog(ostream
&os
)
108 os
<< "Error creating object '" << getInstanceName()
109 << "' of type '" << simObjClassName
110 << "', section '" << iniSection
<< "':" << endl
;
114 ////////////////////////////////////////////////////////////////////////
116 // SimObjectClass member definitions
118 ////////////////////////////////////////////////////////////////////////
120 // Map of class names to SimObjectBuilder creation functions. Need to
121 // make this a pointer so we can force initialization on the first
122 // reference; otherwise, some SimObjectClass constructors may be invoked
123 // before the classMap constructor.
124 map
<string
,SimObjectClass::CreateFunc
> *SimObjectClass::classMap
= NULL
;
126 // SimObjectClass constructor: add mapping to classMap
127 SimObjectClass::SimObjectClass(const string
&className
, CreateFunc createFunc
)
129 if (classMap
== NULL
)
130 classMap
= new map
<string
,SimObjectClass::CreateFunc
>();
132 if ((*classMap
)[className
])
134 cerr
<< "Error: simulation object class " << className
<< " redefined"
139 // add className --> createFunc to class map
140 (*classMap
)[className
] = createFunc
;
147 SimObjectClass::createObject(IniFile
&configDB
,
148 const string
&configClassName
,
149 const string
&objName
,
150 ConfigNode
*configNode
)
152 // find simulation object class name from configuration class
153 // (specified by 'type=' parameter)
154 string simObjClassName
;
156 if (!configDB
.findDefault(configClassName
, "type", simObjClassName
)) {
157 cerr
<< "Configuration class '" << configClassName
<< "' not found."
162 // look up className to get appropriate createFunc
163 if (classMap
->find(simObjClassName
) == classMap
->end()) {
164 cerr
<< "Simulator object class '" << simObjClassName
<< "' not found."
169 CreateFunc createFunc
= (*classMap
)[simObjClassName
];
171 // call createFunc with config hierarchy node to get object
172 // builder instance (context with parameters for object creation)
173 SimObjectBuilder
*objectBuilder
= (*createFunc
)(configClassName
,
177 assert(objectBuilder
!= NULL
);
179 // parse all parameters in context to generate parameter values
180 objectBuilder
->parseParams(configDB
);
182 // now create the actual simulation object
183 SimObject
*object
= objectBuilder
->create();
185 assert(object
!= NULL
);
187 // echo object parameters to stats file (for documenting the
188 // config used to generate the associated stats)
189 builderStream() << "[" << object
->name() << "]" << endl
;
190 builderStream() << "type=" << simObjClassName
<< endl
;
191 objectBuilder
->showParams(builderStream());
192 builderStream() << endl
;
194 // done with the SimObjectBuilder now
195 delete objectBuilder
;
205 SimObjectClass::describeAllClasses(ostream
&os
)
207 map
<string
,CreateFunc
>::iterator iter
;
209 for (iter
= classMap
->begin(); iter
!= classMap
->end(); ++iter
) {
210 const string
&className
= iter
->first
;
211 CreateFunc createFunc
= iter
->second
;
213 os
<< "[" << className
<< "]\n";
215 // create dummy object builder just to instantiate parameters
216 SimObjectBuilder
*objectBuilder
= (*createFunc
)("", "", NULL
, "");
218 // now get the object builder to describe ite params
219 objectBuilder
->describeParams(os
);
223 // done with the object builder now
224 delete objectBuilder
;