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/sim_stats.hh"
41 ///////////////////////////////////////////
43 // SimObjectBuilder member definitions
45 ///////////////////////////////////////////
47 // override ParamContext::parseParams() to check params based on
48 // instance name first. If not found, then check based on iniSection
49 // (as in default ParamContext implementation).
51 SimObjectBuilder::parseParams(IniFile
&iniFile
)
53 iniFilePtr
= &iniFile
; // set object member
55 ParamList::iterator i
;
57 for (i
= paramList
->begin(); i
!= paramList
->end(); ++i
) {
60 if (iniFile
.findDefault(instanceName
, (*i
)->name
, string_value
)) {
61 (*i
)->parse(string_value
);
63 else if (iniFile
.findDefault(iniSection
, (*i
)->name
, string_value
)) {
64 (*i
)->parse(string_value
);
71 SimObjectBuilder::printErrorProlog(ostream
&os
)
73 os
<< "Error creating object '" << getInstanceName()
74 << "' of type '" << simObjClassName
75 << "', section '" << iniSection
<< "':" << endl
;
79 ////////////////////////////////////////////////////////////////////////
81 // SimObjectClass member definitions
83 ////////////////////////////////////////////////////////////////////////
85 // Map of class names to SimObjectBuilder creation functions. Need to
86 // make this a pointer so we can force initialization on the first
87 // reference; otherwise, some SimObjectClass constructors may be invoked
88 // before the classMap constructor.
89 map
<string
,SimObjectClass::CreateFunc
> *SimObjectClass::classMap
= NULL
;
91 // SimObjectClass constructor: add mapping to classMap
92 SimObjectClass::SimObjectClass(const string
&className
, CreateFunc createFunc
)
95 classMap
= new map
<string
,SimObjectClass::CreateFunc
>();
97 if ((*classMap
)[className
])
99 cerr
<< "Error: simulation object class " << className
<< " redefined"
104 // add className --> createFunc to class map
105 (*classMap
)[className
] = createFunc
;
112 SimObjectClass::createObject(IniFile
&configDB
,
113 const string
&configClassName
,
114 const string
&objName
,
115 ConfigNode
*configNode
)
117 // find simulation object class name from configuration class
118 // (specified by 'type=' parameter)
119 string simObjClassName
;
121 if (!configDB
.findDefault(configClassName
, "type", simObjClassName
)) {
122 cerr
<< "Configuration class '" << configClassName
<< "' not found."
127 // look up className to get appropriate createFunc
128 if (classMap
->find(simObjClassName
) == classMap
->end()) {
129 cerr
<< "Simulator object class '" << simObjClassName
<< "' not found."
134 CreateFunc createFunc
= (*classMap
)[simObjClassName
];
136 // call createFunc with config hierarchy node to get object
137 // builder instance (context with parameters for object creation)
138 SimObjectBuilder
*objectBuilder
= (*createFunc
)(configClassName
,
142 assert(objectBuilder
!= NULL
);
144 // parse all parameters in context to generate parameter values
145 objectBuilder
->parseParams(configDB
);
147 // now create the actual simulation object
148 SimObject
*object
= objectBuilder
->create();
150 assert(object
!= NULL
);
152 // echo object parameters to stats file (for documenting the
153 // config used to generate the associated stats)
154 *statStream
<< "[" << object
->name() << "]" << endl
;
155 *statStream
<< "type=" << simObjClassName
<< endl
;
156 objectBuilder
->showParams(*statStream
);
159 // done with the SimObjectBuilder now
160 delete objectBuilder
;
170 SimObjectClass::describeAllClasses(ostream
&os
)
172 map
<string
,CreateFunc
>::iterator iter
;
174 for (iter
= classMap
->begin(); iter
!= classMap
->end(); ++iter
) {
175 const string
&className
= iter
->first
;
176 CreateFunc createFunc
= iter
->second
;
178 os
<< "[" << className
<< "]\n";
180 // create dummy object builder just to instantiate parameters
181 SimObjectBuilder
*objectBuilder
= (*createFunc
)("", "", NULL
, "");
183 // now get the object builder to describe ite params
184 objectBuilder
->describeParams(os
);
188 // done with the object builder now
189 delete objectBuilder
;