53e5cf3d82919d685caf6b9307351a773804283f
2 * Copyright (c) 2003-2004 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/config_node.hh"
36 #include "sim/host.hh"
37 #include "sim/sim_object.hh"
38 #include "sim/universe.hh"
47 static ostream
*stream
= NULL
;
50 if (!outputDirectory
.empty()) {
51 string filename
= outputDirectory
+ "builder.txt";
52 file
.open(filename
.c_str());
55 stream
= outputStream
;
62 SimObjectBuilder::SimObjectBuilder(const string
&_configClass
,
63 const string
&_instanceName
,
64 ConfigNode
*_configNode
,
65 const string
&_simObjClassName
)
66 : ParamContext(_configClass
, true),
67 instanceName(_instanceName
),
68 configNode(_configNode
),
69 simObjClassName(_simObjClassName
)
73 SimObjectBuilder::~SimObjectBuilder()
77 ///////////////////////////////////////////
79 // SimObjectBuilder member definitions
81 ///////////////////////////////////////////
83 // override ParamContext::parseParams() to check params based on
84 // instance name first. If not found, then check based on iniSection
85 // (as in default ParamContext implementation).
87 SimObjectBuilder::parseParams(IniFile
&iniFile
)
89 iniFilePtr
= &iniFile
; // set object member
91 ParamList::iterator i
;
93 for (i
= paramList
->begin(); i
!= paramList
->end(); ++i
) {
96 if (iniFile
.findDefault(instanceName
, (*i
)->name
, string_value
)) {
97 (*i
)->parse(string_value
);
99 else if (iniFile
.findDefault(iniSection
, (*i
)->name
, string_value
)) {
100 (*i
)->parse(string_value
);
107 SimObjectBuilder::printErrorProlog(ostream
&os
)
109 os
<< "Error creating object '" << getInstanceName()
110 << "' of type '" << simObjClassName
111 << "', section '" << iniSection
<< "':" << endl
;
115 ////////////////////////////////////////////////////////////////////////
117 // SimObjectClass member definitions
119 ////////////////////////////////////////////////////////////////////////
121 // Map of class names to SimObjectBuilder creation functions. Need to
122 // make this a pointer so we can force initialization on the first
123 // reference; otherwise, some SimObjectClass constructors may be invoked
124 // before the classMap constructor.
125 map
<string
,SimObjectClass::CreateFunc
> *SimObjectClass::classMap
= NULL
;
127 // SimObjectClass constructor: add mapping to classMap
128 SimObjectClass::SimObjectClass(const string
&className
, CreateFunc createFunc
)
130 if (classMap
== NULL
)
131 classMap
= new map
<string
,SimObjectClass::CreateFunc
>();
133 if ((*classMap
)[className
])
135 cerr
<< "Error: simulation object class " << className
<< " redefined"
140 // add className --> createFunc to class map
141 (*classMap
)[className
] = createFunc
;
148 SimObjectClass::createObject(IniFile
&configDB
,
149 const string
&configClassName
,
150 const string
&objName
,
151 ConfigNode
*configNode
)
153 // find simulation object class name from configuration class
154 // (specified by 'type=' parameter)
155 string simObjClassName
;
157 if (!configNode
->find("type", simObjClassName
)) {
158 cerr
<< "Configuration class '" << configClassName
<< "' not found."
163 // look up className to get appropriate createFunc
164 if (classMap
->find(simObjClassName
) == classMap
->end()) {
165 cerr
<< "Simulator object class '" << simObjClassName
<< "' not found."
170 CreateFunc createFunc
= (*classMap
)[simObjClassName
];
172 // call createFunc with config hierarchy node to get object
173 // builder instance (context with parameters for object creation)
174 SimObjectBuilder
*objectBuilder
= (*createFunc
)(configClassName
,
178 assert(objectBuilder
!= NULL
);
180 // parse all parameters in context to generate parameter values
181 objectBuilder
->parseParams(configDB
);
183 // now create the actual simulation object
184 SimObject
*object
= objectBuilder
->create();
186 assert(object
!= NULL
);
188 // echo object parameters to stats file (for documenting the
189 // config used to generate the associated stats)
190 builderStream() << "[" << object
->name() << "]" << endl
;
191 builderStream() << "type=" << simObjClassName
<< endl
;
192 objectBuilder
->showParams(builderStream());
193 builderStream() << endl
;
195 // done with the SimObjectBuilder now
196 delete objectBuilder
;
206 SimObjectClass::describeAllClasses(ostream
&os
)
208 map
<string
,CreateFunc
>::iterator iter
;
210 for (iter
= classMap
->begin(); iter
!= classMap
->end(); ++iter
) {
211 const string
&className
= iter
->first
;
212 CreateFunc createFunc
= iter
->second
;
214 os
<< "[" << className
<< "]\n";
216 // create dummy object builder just to instantiate parameters
217 SimObjectBuilder
*objectBuilder
= (*createFunc
)("", "", NULL
, "");
219 // now get the object builder to describe ite params
220 objectBuilder
->describeParams(os
);
224 // done with the object builder now
225 delete objectBuilder
;