2 * Copyright (c) 2003-2005 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/root.hh"
42 SimObjectBuilder::SimObjectBuilder(ConfigNode
*_configNode
)
43 : ParamContext(_configNode
->getPath(), NoAutoInit
),
44 configNode(_configNode
)
48 SimObjectBuilder::~SimObjectBuilder()
52 ///////////////////////////////////////////
54 // SimObjectBuilder member definitions
56 ///////////////////////////////////////////
58 // override ParamContext::parseParams() to check params based on
59 // instance name first. If not found, then check based on iniSection
60 // (as in default ParamContext implementation).
62 SimObjectBuilder::parseParams(IniFile
&iniFile
)
64 iniFilePtr
= &iniFile
; // set object member
66 ParamList::iterator i
;
68 for (i
= paramList
->begin(); i
!= paramList
->end(); ++i
) {
70 if (iniFile
.find(iniSection
, (*i
)->name
, string_value
))
71 (*i
)->parse(string_value
);
77 SimObjectBuilder::printErrorProlog(ostream
&os
)
79 ccprintf(os
, "Error creating object '%s' of type '%s':\n",
80 iniSection
, configNode
->getType());
84 ////////////////////////////////////////////////////////////////////////
86 // SimObjectClass member definitions
88 ////////////////////////////////////////////////////////////////////////
90 // Map of class names to SimObjectBuilder creation functions. Need to
91 // make this a pointer so we can force initialization on the first
92 // reference; otherwise, some SimObjectClass constructors may be invoked
93 // before the classMap constructor.
94 map
<string
,SimObjectClass::CreateFunc
> *SimObjectClass::classMap
= NULL
;
96 // SimObjectClass constructor: add mapping to classMap
97 SimObjectClass::SimObjectClass(const string
&className
, CreateFunc createFunc
)
100 classMap
= new map
<string
,SimObjectClass::CreateFunc
>();
102 if ((*classMap
)[className
])
103 panic("Error: simulation object class '%s' redefined\n", className
);
105 // add className --> createFunc to class map
106 (*classMap
)[className
] = createFunc
;
113 SimObjectClass::createObject(IniFile
&configDB
, ConfigNode
*configNode
)
115 const string
&type
= configNode
->getType();
117 // look up className to get appropriate createFunc
118 if (classMap
->find(type
) == classMap
->end())
119 panic("Simulator object type '%s' not found.\n", type
);
122 CreateFunc createFunc
= (*classMap
)[type
];
124 // call createFunc with config hierarchy node to get object
125 // builder instance (context with parameters for object creation)
126 SimObjectBuilder
*objectBuilder
= (*createFunc
)(configNode
);
128 assert(objectBuilder
!= NULL
);
130 // parse all parameters in context to generate parameter values
131 objectBuilder
->parseParams(configDB
);
133 // now create the actual simulation object
134 SimObject
*object
= objectBuilder
->create();
136 assert(object
!= NULL
);
138 // echo object parameters to stats file (for documenting the
139 // config used to generate the associated stats)
140 ccprintf(*configStream
, "[%s]\n", object
->name());
141 ccprintf(*configStream
, "type=%s\n", type
);
142 objectBuilder
->showParams(*configStream
);
143 ccprintf(*configStream
, "\n");
145 // done with the SimObjectBuilder now
146 delete objectBuilder
;
156 SimObjectClass::describeAllClasses(ostream
&os
)
158 map
<string
,CreateFunc
>::iterator iter
;
160 for (iter
= classMap
->begin(); iter
!= classMap
->end(); ++iter
) {
161 const string
&className
= iter
->first
;
162 CreateFunc createFunc
= iter
->second
;
164 os
<< "[" << className
<< "]\n";
166 // create dummy object builder just to instantiate parameters
167 SimObjectBuilder
*objectBuilder
= (*createFunc
)(NULL
);
169 // now get the object builder to describe ite params
170 objectBuilder
->describeParams(os
);
174 // done with the object builder now
175 delete objectBuilder
;