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"
42 SimObjectBuilder::SimObjectBuilder(const string
&_configClass
,
43 const string
&_instanceName
,
44 ConfigNode
*_configNode
,
45 const string
&_simObjClassName
)
46 : ParamContext(_configClass
, NoAutoInit
),
47 instanceName(_instanceName
),
48 configNode(_configNode
),
49 simObjClassName(_simObjClassName
)
53 SimObjectBuilder::~SimObjectBuilder()
57 ///////////////////////////////////////////
59 // SimObjectBuilder member definitions
61 ///////////////////////////////////////////
63 // override ParamContext::parseParams() to check params based on
64 // instance name first. If not found, then check based on iniSection
65 // (as in default ParamContext implementation).
67 SimObjectBuilder::parseParams(IniFile
&iniFile
)
69 iniFilePtr
= &iniFile
; // set object member
71 ParamList::iterator i
;
73 for (i
= paramList
->begin(); i
!= paramList
->end(); ++i
) {
76 if (iniFile
.findDefault(instanceName
, (*i
)->name
, string_value
)) {
77 (*i
)->parse(string_value
);
79 else if (iniFile
.findDefault(iniSection
, (*i
)->name
, string_value
)) {
80 (*i
)->parse(string_value
);
87 SimObjectBuilder::printErrorProlog(ostream
&os
)
89 os
<< "Error creating object '" << getInstanceName()
90 << "' of type '" << simObjClassName
91 << "', section '" << iniSection
<< "':" << endl
;
95 ////////////////////////////////////////////////////////////////////////
97 // SimObjectClass member definitions
99 ////////////////////////////////////////////////////////////////////////
101 // Map of class names to SimObjectBuilder creation functions. Need to
102 // make this a pointer so we can force initialization on the first
103 // reference; otherwise, some SimObjectClass constructors may be invoked
104 // before the classMap constructor.
105 map
<string
,SimObjectClass::CreateFunc
> *SimObjectClass::classMap
= NULL
;
107 // SimObjectClass constructor: add mapping to classMap
108 SimObjectClass::SimObjectClass(const string
&className
, CreateFunc createFunc
)
110 if (classMap
== NULL
)
111 classMap
= new map
<string
,SimObjectClass::CreateFunc
>();
113 if ((*classMap
)[className
])
115 cerr
<< "Error: simulation object class " << className
<< " redefined"
120 // add className --> createFunc to class map
121 (*classMap
)[className
] = createFunc
;
128 SimObjectClass::createObject(IniFile
&configDB
,
129 const string
&configClassName
,
130 const string
&objName
,
131 ConfigNode
*configNode
)
133 // find simulation object class name from configuration class
134 // (specified by 'type=' parameter)
135 string simObjClassName
;
137 if (!configNode
->find("type", simObjClassName
)) {
138 cerr
<< "Configuration class '" << configClassName
<< "' not found."
143 // look up className to get appropriate createFunc
144 if (classMap
->find(simObjClassName
) == classMap
->end()) {
145 cerr
<< "Simulator object class '" << simObjClassName
<< "' not found."
150 CreateFunc createFunc
= (*classMap
)[simObjClassName
];
152 // call createFunc with config hierarchy node to get object
153 // builder instance (context with parameters for object creation)
154 SimObjectBuilder
*objectBuilder
= (*createFunc
)(configClassName
,
158 assert(objectBuilder
!= NULL
);
160 // parse all parameters in context to generate parameter values
161 objectBuilder
->parseParams(configDB
);
163 // now create the actual simulation object
164 SimObject
*object
= objectBuilder
->create();
166 assert(object
!= NULL
);
168 // echo object parameters to stats file (for documenting the
169 // config used to generate the associated stats)
170 *configStream
<< "[" << object
->name() << "]" << endl
;
171 *configStream
<< "type=" << simObjClassName
<< endl
;
172 objectBuilder
->showParams(*configStream
);
173 *configStream
<< endl
;
175 // done with the SimObjectBuilder now
176 delete objectBuilder
;
186 SimObjectClass::describeAllClasses(ostream
&os
)
188 map
<string
,CreateFunc
>::iterator iter
;
190 for (iter
= classMap
->begin(); iter
!= classMap
->end(); ++iter
) {
191 const string
&className
= iter
->first
;
192 CreateFunc createFunc
= iter
->second
;
194 os
<< "[" << className
<< "]\n";
196 // create dummy object builder just to instantiate parameters
197 SimObjectBuilder
*objectBuilder
= (*createFunc
)("", "", NULL
, "");
199 // now get the object builder to describe ite params
200 objectBuilder
->describeParams(os
);
204 // done with the object builder now
205 delete objectBuilder
;