Mercurial > flash_v2
comparison host/libcdl/component.cxx @ 76:435cced73e2f ecos-v1_3_1-release
eCos v1.3.1 merged from eCos master repository on 2000-03-27-23:22:51-BST
| author | jlarmour |
|---|---|
| date | Tue, 28 Mar 2000 14:10:45 +0000 |
| parents | |
| children | 6736c52df507 |
comparison
equal
deleted
inserted
replaced
| 75:41bf073c0c32 | 76:435cced73e2f |
|---|---|
| 1 //{{{ Banner | |
| 2 | |
| 3 //============================================================================ | |
| 4 // | |
| 5 // component.cxx | |
| 6 // | |
| 7 // Implementation of the CdlComponent class | |
| 8 // | |
| 9 //============================================================================ | |
| 10 //####COPYRIGHTBEGIN#### | |
| 11 // | |
| 12 // ---------------------------------------------------------------------------- | |
| 13 // Copyright (C) 1999, 2000 Red Hat, Inc. | |
| 14 // | |
| 15 // This file is part of the eCos host tools. | |
| 16 // | |
| 17 // This program is free software; you can redistribute it and/or modify it | |
| 18 // under the terms of the GNU General Public License as published by the Free | |
| 19 // Software Foundation; either version 2 of the License, or (at your option) | |
| 20 // any later version. | |
| 21 // | |
| 22 // This program is distributed in the hope that it will be useful, but WITHOUT | |
| 23 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
| 24 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
| 25 // more details. | |
| 26 // | |
| 27 // You should have received a copy of the GNU General Public License along with | |
| 28 // this program; if not, write to the Free Software Foundation, Inc., | |
| 29 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
| 30 // | |
| 31 // ---------------------------------------------------------------------------- | |
| 32 // | |
| 33 //####COPYRIGHTEND#### | |
| 34 //============================================================================ | |
| 35 //#####DESCRIPTIONBEGIN#### | |
| 36 // | |
| 37 // Author(s): bartv | |
| 38 // Contact(s): bartv | |
| 39 // Date: 1999/03/01 | |
| 40 // Version: 0.02 | |
| 41 // | |
| 42 //####DESCRIPTIONEND#### | |
| 43 //============================================================================ | |
| 44 | |
| 45 //}}} | |
| 46 //{{{ #include's | |
| 47 | |
| 48 // ---------------------------------------------------------------------------- | |
| 49 #include "cdlconfig.h" | |
| 50 | |
| 51 // Get the infrastructure types, assertions, tracing and similar | |
| 52 // facilities. | |
| 53 #include <cyg/infra/cyg_ass.h> | |
| 54 #include <cyg/infra/cyg_trac.h> | |
| 55 | |
| 56 // <cdl.hxx> defines everything implemented in this module. | |
| 57 // It implicitly supplies <string>, <vector> and <map> because | |
| 58 // the class definitions rely on these headers. | |
| 59 #include <cdl.hxx> | |
| 60 | |
| 61 //}}} | |
| 62 | |
| 63 //{{{ Statics | |
| 64 | |
| 65 // ---------------------------------------------------------------------------- | |
| 66 CYGDBG_DEFINE_MEMLEAK_COUNTER(CdlComponentBody); | |
| 67 | |
| 68 //}}} | |
| 69 //{{{ Constructor | |
| 70 | |
| 71 // ---------------------------------------------------------------------------- | |
| 72 CdlComponentBody::CdlComponentBody(std::string name_arg) | |
| 73 : CdlNodeBody(name_arg), | |
| 74 CdlContainerBody(), | |
| 75 CdlUserVisibleBody(), | |
| 76 CdlValuableBody(), | |
| 77 CdlParentableBody(), | |
| 78 CdlBuildableBody(), | |
| 79 CdlDefinableBody() | |
| 80 { | |
| 81 CYG_REPORT_FUNCNAME("CdlComponentBody:: constructor"); | |
| 82 CYG_REPORT_FUNCARG1XV(this); | |
| 83 | |
| 84 cdlcomponentbody_cookie = CdlComponentBody_Magic; | |
| 85 CYGDBG_MEMLEAK_CONSTRUCTOR(); | |
| 86 | |
| 87 CYG_POSTCONDITION_THISC(); | |
| 88 CYG_REPORT_RETURN(); | |
| 89 } | |
| 90 | |
| 91 //}}} | |
| 92 //{{{ Destructor | |
| 93 | |
| 94 // ---------------------------------------------------------------------------- | |
| 95 | |
| 96 CdlComponentBody::~CdlComponentBody() | |
| 97 { | |
| 98 CYG_REPORT_FUNCNAME("CdlComponentBody:: destructor"); | |
| 99 CYG_REPORT_FUNCARG1XV(this); | |
| 100 CYG_PRECONDITION_THISC(); | |
| 101 | |
| 102 cdlcomponentbody_cookie = CdlComponentBody_Invalid; | |
| 103 CYGDBG_MEMLEAK_DESTRUCTOR(); | |
| 104 | |
| 105 CYG_REPORT_RETURN(); | |
| 106 } | |
| 107 | |
| 108 //}}} | |
| 109 //{{{ parse_component() | |
| 110 | |
| 111 // ---------------------------------------------------------------------------- | |
| 112 // Parsing a component definition. This routine gets invoked directly from the | |
| 113 // Tcl interpreter. | |
| 114 | |
| 115 int | |
| 116 CdlComponentBody::parse_component(CdlInterpreter interp, int argc, char** argv) | |
| 117 { | |
| 118 CYG_REPORT_FUNCNAMETYPE("CdlComponentBody::parse_component", "result %d"); | |
| 119 CYG_REPORT_FUNCARG1("argc %d", argc); | |
| 120 CYG_PRECONDITION_CLASSC(interp); | |
| 121 | |
| 122 const char* diag_argv0 = CdlParse::get_tcl_cmd_name(argv[0]); | |
| 123 | |
| 124 CdlLoadable loadable = interp->get_loadable(); | |
| 125 CdlPackage package = dynamic_cast<CdlPackage>(loadable); | |
| 126 CdlContainer parent = interp->get_container(); | |
| 127 CdlToplevel toplevel = interp->get_toplevel(); | |
| 128 std::string filename = interp->get_filename(); | |
| 129 CYG_ASSERT_CLASSC(loadable); // There should always be a loadable during parsing | |
| 130 CYG_ASSERT_CLASSC(package); // And packages are the only loadable for software CDL. | |
| 131 CYG_ASSERT_CLASSC(parent); | |
| 132 CYG_ASSERT_CLASSC(toplevel); | |
| 133 CYG_ASSERTC("" != filename); | |
| 134 | |
| 135 // The new component should be created and added to the package | |
| 136 // early on. If there is a parsing error it will get cleaned up | |
| 137 // automatically as a consequence of the package destructor. | |
| 138 // However it is necessary to validate the name first. Errors | |
| 139 // should be reported via CdlParse::report_error(), | |
| 140 // which may result in an exception. | |
| 141 CdlComponent new_component = 0; | |
| 142 bool ok = true; | |
| 143 int result = TCL_OK; | |
| 144 try { | |
| 145 | |
| 146 // Currently there are no options. This may change in future. | |
| 147 if (3 != argc) { | |
| 148 CdlParse::report_error(interp, std::string("Incorrect number of arguments to ") + diag_argv0 + | |
| 149 "\n Expecting name and properties list."); | |
| 150 ok = false; | |
| 151 goto done; | |
| 152 } | |
| 153 if (!Tcl_CommandComplete(argv[2])) { | |
| 154 CdlParse::report_error(interp, std::string("Invalid property list for cdl_component ") + argv[1]); | |
| 155 ok = false; | |
| 156 goto done; | |
| 157 } | |
| 158 | |
| 159 if (0 != toplevel->lookup(argv[1])) { | |
| 160 CdlParse::report_error(interp, std::string("Component ") + argv[1] + " cannot be loaded.\n" + | |
| 161 " The name is already in use."); | |
| 162 ok = false; | |
| 163 } else { | |
| 164 new_component = new CdlComponentBody(argv[1]); | |
| 165 toplevel->add_node(package, parent, new_component); | |
| 166 } | |
| 167 | |
| 168 done: | |
| 169 if (!ok) { | |
| 170 // Just because this component cannot be created, that is no | |
| 171 // reason to abort the whole parsing process. | |
| 172 CYG_REPORT_RETVAL(TCL_OK); | |
| 173 return TCL_OK; | |
| 174 } | |
| 175 } catch(std::bad_alloc e) { | |
| 176 interp->set_result(CdlParse::get_diagnostic_prefix(interp) + "Out of memory."); | |
| 177 result = TCL_ERROR; | |
| 178 } catch(CdlParseException e) { | |
| 179 interp->set_result(e.get_message()); | |
| 180 result = TCL_ERROR; | |
| 181 } catch(...) { | |
| 182 interp->set_result(CdlParse::get_diagnostic_prefix(interp) + "internal error, unexpected C++ exception."); | |
| 183 result = TCL_ERROR; | |
| 184 } | |
| 185 if (TCL_OK != result) { | |
| 186 CYG_REPORT_RETVAL(result); | |
| 187 return result; | |
| 188 } | |
| 189 | |
| 190 // At this stage new_component has been created and added to the hierarchy. | |
| 191 // The main work now is to add the properties. | |
| 192 | |
| 193 // Push the component as the current node early on. This aids | |
| 194 // diagnostics. Also make it the new container. | |
| 195 CdlNode old_node = interp->push_node(new_component); | |
| 196 CdlContainer old_container = interp->push_container(new_component); | |
| 197 std::string old_filename; | |
| 198 CYG_ASSERTC(parent == old_container); | |
| 199 | |
| 200 // Declare these outside the scope of the try statement, to allow | |
| 201 // goto calls for the error handling. | |
| 202 std::string tcl_result; | |
| 203 std::vector<CdlInterpreterCommandEntry> new_commands; | |
| 204 std::vector<CdlInterpreterCommandEntry>* old_commands = 0; | |
| 205 static CdlInterpreterCommandEntry commands[] = | |
| 206 { | |
| 207 CdlInterpreterCommandEntry("script", &CdlComponentBody::parse_script ), | |
| 208 CdlInterpreterCommandEntry("cdl_component", &CdlComponentBody::parse_component ), | |
| 209 CdlInterpreterCommandEntry("cdl_option", &CdlOptionBody::parse_option ), | |
| 210 CdlInterpreterCommandEntry("cdl_interface", &CdlInterfaceBody::parse_interface ), | |
| 211 CdlInterpreterCommandEntry("cdl_dialog", &CdlDialogBody::parse_dialog ), | |
| 212 CdlInterpreterCommandEntry("cdl_wizard", &CdlWizardBody::parse_wizard ), | |
| 213 CdlInterpreterCommandEntry("", 0 ) | |
| 214 }; | |
| 215 static CdlInterpreterCommandEntry script_commands[] = | |
| 216 { | |
| 217 CdlInterpreterCommandEntry("cdl_component", &CdlComponentBody::parse_component ), | |
| 218 CdlInterpreterCommandEntry("cdl_option", &CdlOptionBody::parse_option ), | |
| 219 CdlInterpreterCommandEntry("cdl_interface", &CdlInterfaceBody::parse_interface ), | |
| 220 CdlInterpreterCommandEntry("cdl_dialog", &CdlDialogBody::parse_dialog ), | |
| 221 CdlInterpreterCommandEntry("cdl_wizard", &CdlWizardBody::parse_wizard ), | |
| 222 CdlInterpreterCommandEntry("", 0 ), | |
| 223 }; | |
| 224 int i; | |
| 225 | |
| 226 // All parsing errors may result in an exception, under the control of | |
| 227 // application code. This exception must not pass through the Tcl interpreter. | |
| 228 try { | |
| 229 | |
| 230 for (i = 0; 0 != commands[i].command; i++) { | |
| 231 new_commands.push_back(commands[i]); | |
| 232 } | |
| 233 CdlBuildableBody::add_property_parsers(new_commands); | |
| 234 CdlDefinableBody::add_property_parsers(new_commands); | |
| 235 CdlParentableBody::add_property_parsers(new_commands); | |
| 236 CdlValuableBody::add_property_parsers(new_commands); | |
| 237 CdlUserVisibleBody::add_property_parsers(new_commands); | |
| 238 CdlNodeBody::add_property_parsers(new_commands); | |
| 239 | |
| 240 // Now evaluate the body. If an error occurs then typically | |
| 241 // this will be reported via CdlParse::report_error(), | |
| 242 // but any exceptions will have been intercepted and | |
| 243 // turned into a Tcl error. | |
| 244 old_commands = interp->push_commands(new_commands); | |
| 245 result = interp->eval(argv[2], tcl_result); | |
| 246 interp->pop_commands(old_commands); | |
| 247 | |
| 248 if (TCL_OK != result) { | |
| 249 // No point in taking any further action, just go with the flow | |
| 250 goto done2; | |
| 251 } | |
| 252 | |
| 253 // Even if there were errors, they were not fatal. There may | |
| 254 // now be a number of properties for this component, and some | |
| 255 // validation should take place. Start with the base classes. | |
| 256 new_component->CdlNodeBody::check_properties(interp); | |
| 257 new_component->CdlUserVisibleBody::check_properties(interp); | |
| 258 new_component->CdlValuableBody::check_properties(interp); | |
| 259 new_component->CdlParentableBody::check_properties(interp); | |
| 260 new_component->CdlBuildableBody::check_properties(interp); | |
| 261 new_component->CdlDefinableBody::check_properties(interp); | |
| 262 | |
| 263 // There should be at most one each of wizard and script. | |
| 264 if (new_component->count_properties(CdlPropertyId_Wizard) > 1) { | |
| 265 CdlParse::report_error(interp, "A component should have at most one `wizard' property."); | |
| 266 } | |
| 267 if (new_component->count_properties(CdlPropertyId_Script) > 1) { | |
| 268 CdlParse::report_error(interp, "A component should have at most one `script' property."); | |
| 269 } | |
| 270 | |
| 271 // If there is a script property, life gets more interesting. | |
| 272 if (new_component->has_property(CdlPropertyId_Script)) { | |
| 273 CdlProperty_String prop = dynamic_cast<CdlProperty_String>(new_component->get_property(CdlPropertyId_Script)); | |
| 274 CYG_PRECONDITION_CLASSC(prop); | |
| 275 std::string script_name = prop->get_string(); | |
| 276 | |
| 277 // Try to locate this script. | |
| 278 std::string script_filename = package->find_absolute_file(script_name, "cdl", false); | |
| 279 if ("" == script_filename) { | |
| 280 CdlParse::report_error(interp, "Unable to find script " + script_name); | |
| 281 } else { | |
| 282 // The script exists, so we need to try and execute it. | |
| 283 // The current container is still set correctly, but we need | |
| 284 // to change the filename and install a different set | |
| 285 // of commands. | |
| 286 old_filename = interp->push_filename(script_filename); | |
| 287 new_commands.clear(); | |
| 288 for (i = 0; 0 != script_commands[i].command; i++) { | |
| 289 new_commands.push_back(script_commands[i]); | |
| 290 } | |
| 291 old_commands = interp->push_commands(new_commands); | |
| 292 result = interp->eval_file(script_filename, tcl_result); | |
| 293 interp->pop_commands(old_commands); | |
| 294 interp->pop_filename(old_filename); | |
| 295 } | |
| 296 } | |
| 297 | |
| 298 done2: | |
| 299 // Dummy command just to keep the compiler happy | |
| 300 filename = ""; | |
| 301 | |
| 302 } catch (std::bad_alloc e) { | |
| 303 // Errors at this stage should be reported via Tcl, not via C++. | |
| 304 // However there is no point in continuing with the parsing operation, | |
| 305 // just give up. | |
| 306 interp->set_result(CdlParse::get_diagnostic_prefix(interp) + "Out of memory."); | |
| 307 result = TCL_ERROR; | |
| 308 } catch (CdlParseException e) { | |
| 309 interp->set_result(e.get_message()); | |
| 310 result = TCL_ERROR; | |
| 311 } catch(...) { | |
| 312 interp->set_result(CdlParse::get_diagnostic_prefix(interp) + "internal error, unexpected C++ exception."); | |
| 313 result = TCL_ERROR; | |
| 314 } | |
| 315 | |
| 316 // Restore the interpreter to its prior state. | |
| 317 interp->pop_node(old_node); | |
| 318 interp->pop_container(old_container); | |
| 319 if (0 != old_commands) { | |
| 320 interp->pop_commands(old_commands); | |
| 321 } | |
| 322 | |
| 323 CYG_REPORT_RETVAL(result); | |
| 324 return result; | |
| 325 } | |
| 326 | |
| 327 | |
| 328 // ---------------------------------------------------------------------------- | |
| 329 // Syntax: script <filename> | |
| 330 int | |
| 331 CdlComponentBody::parse_script(CdlInterpreter interp, int argc, char** argv) | |
| 332 { | |
| 333 CYG_REPORT_FUNCNAMETYPE("parse_script", "result %d"); | |
| 334 | |
| 335 int result = CdlParse::parse_string_property(interp, argc, argv, CdlPropertyId_Script, 0, 0); | |
| 336 | |
| 337 CYG_REPORT_RETVAL(result); | |
| 338 return result; | |
| 339 } | |
| 340 | |
| 341 //}}} | |
| 342 //{{{ Propagation support | |
| 343 | |
| 344 // ---------------------------------------------------------------------------- | |
| 345 void | |
| 346 CdlComponentBody::update(CdlTransaction transaction, CdlUpdate update) | |
| 347 { | |
| 348 CYG_REPORT_FUNCNAME("CdlComponent::update"); | |
| 349 | |
| 350 this->CdlValuableBody::update(transaction, update); | |
| 351 this->CdlContainerBody::update(transaction, update); | |
| 352 | |
| 353 CYG_REPORT_RETURN(); | |
| 354 } | |
| 355 | |
| 356 //}}} | |
| 357 //{{{ Persistence support | |
| 358 | |
| 359 // ---------------------------------------------------------------------------- | |
| 360 void | |
| 361 CdlComponentBody::initialize_savefile_support(CdlToplevel toplevel) | |
| 362 { | |
| 363 CYG_REPORT_FUNCNAME("CdlComponent::initialize_savefile_support"); | |
| 364 | |
| 365 toplevel->add_savefile_command("cdl_component", 0, &savefile_component_command); | |
| 366 CdlValuableBody::initialize_savefile_support(toplevel, "cdl_component"); | |
| 367 | |
| 368 CYG_REPORT_RETURN(); | |
| 369 } | |
| 370 | |
| 371 void | |
| 372 CdlComponentBody::save(CdlInterpreter interp, Tcl_Channel chan, int indentation, bool minimal) | |
| 373 throw(CdlInputOutputException, std::bad_alloc) | |
| 374 { | |
| 375 CYG_REPORT_FUNCNAME("CdlComponent::save"); | |
| 376 CYG_REPORT_FUNCARG5XV(this, interp, chan, indentation, minimal); | |
| 377 CYG_PRECONDITION_THISC(); | |
| 378 CYG_PRECONDITION_CLASSC(interp); | |
| 379 | |
| 380 if (!minimal || this->has_additional_savefile_information() || this->value_savefile_entry_needed()) { | |
| 381 // Start with the UserVisible data, which will result in a suitable set | |
| 382 // of comments before the package definition itself. | |
| 383 this->CdlUserVisibleBody::save(interp, chan, indentation, minimal); | |
| 384 | |
| 385 // Now output the line "cdl_component <name> {" | |
| 386 // The name is guaranteed to be a valid C preprocessor symbol, so it | |
| 387 // is not going to need any quoting. | |
| 388 std::string data = std::string(indentation, ' ') + "cdl_component " + get_name() + " {\n"; | |
| 389 interp->write_data(chan, data); | |
| 390 | |
| 391 // Deal with the value | |
| 392 bool modifiable = !(CdlValueFlavor_None == this->get_flavor()) && | |
| 393 !this->has_property(CdlPropertyId_Calculated); | |
| 394 this->CdlValuableBody::save(interp, chan, indentation + 4, modifiable, minimal); | |
| 395 | |
| 396 // And with any unrecognised data | |
| 397 this->CdlNodeBody::save(interp, chan, indentation + 4, minimal); | |
| 398 | |
| 399 // Close the cdl_component body. A blank line is added here. | |
| 400 interp->write_data(chan, "};\n\n"); | |
| 401 } | |
| 402 | |
| 403 // Packages are containers, so dump the contents as well. | |
| 404 this->CdlContainerBody::save(interp, chan, indentation, minimal); | |
| 405 | |
| 406 CYG_REPORT_RETURN(); | |
| 407 } | |
| 408 | |
| 409 int | |
| 410 CdlComponentBody::savefile_component_command(CdlInterpreter interp, int argc, char** argv) | |
| 411 { | |
| 412 CYG_REPORT_FUNCNAMETYPE("CdlComponent::savefile_component_command", "result %d"); | |
| 413 CYG_PRECONDITION_CLASSC(interp); | |
| 414 | |
| 415 int result = TCL_OK; | |
| 416 CdlToplevel toplevel = interp->get_toplevel(); | |
| 417 CYG_ASSERT_CLASSC(toplevel); | |
| 418 CdlConfiguration config = dynamic_cast<CdlConfiguration>(toplevel); | |
| 419 CYG_ASSERT_CLASSC(config); | |
| 420 | |
| 421 std::vector<CdlInterpreterCommandEntry> subcommands; | |
| 422 std::vector<CdlInterpreterCommandEntry>* toplevel_commands = 0; | |
| 423 CdlNode old_node = 0; | |
| 424 | |
| 425 try { | |
| 426 | |
| 427 if (3 != argc) { | |
| 428 CdlParse::report_error(interp, "Invalid cdl_component command in savefile, expecting two arguments."); | |
| 429 } else { | |
| 430 | |
| 431 CdlNode current_node = config->lookup(argv[1]); | |
| 432 if (0 == current_node) { | |
| 433 // FIXME: save value in limbo | |
| 434 CdlParse::report_error(interp, | |
| 435 std::string("The savefile contains a cdl_component command for an unknown component `") | |
| 436 + argv[1] + "'"); | |
| 437 } else { | |
| 438 config->get_savefile_subcommands("cdl_component", subcommands); | |
| 439 toplevel_commands = interp->push_commands(subcommands); | |
| 440 old_node = interp->push_node(current_node); | |
| 441 | |
| 442 std::string tcl_result; | |
| 443 result = interp->eval(argv[2], tcl_result); | |
| 444 | |
| 445 interp->pop_commands(toplevel_commands); | |
| 446 toplevel_commands = 0; | |
| 447 interp->pop_node(old_node); | |
| 448 old_node = 0; | |
| 449 } | |
| 450 } | |
| 451 } catch(...) { | |
| 452 if (0 != old_node) { | |
| 453 interp->pop_node(old_node); | |
| 454 } | |
| 455 if (0 != toplevel_commands) { | |
| 456 interp->pop_commands(toplevel_commands); | |
| 457 } | |
| 458 throw; | |
| 459 } | |
| 460 | |
| 461 CYG_REPORT_RETVAL(result); | |
| 462 return result; | |
| 463 } | |
| 464 | |
| 465 //}}} | |
| 466 //{{{ check_this() | |
| 467 | |
| 468 // ---------------------------------------------------------------------------- | |
| 469 | |
| 470 bool | |
| 471 CdlComponentBody::check_this(cyg_assert_class_zeal zeal) const | |
| 472 { | |
| 473 if (CdlComponentBody_Magic != cdlcomponentbody_cookie) { | |
| 474 return false; | |
| 475 } | |
| 476 CYGDBG_MEMLEAK_CHECKTHIS(); | |
| 477 | |
| 478 return CdlNodeBody::check_this(zeal) && | |
| 479 CdlContainerBody::check_this(zeal) && | |
| 480 CdlUserVisibleBody::check_this(zeal) && | |
| 481 CdlParentableBody::check_this(zeal) && | |
| 482 CdlValuableBody::check_this(zeal) && | |
| 483 CdlBuildableBody::check_this(zeal) && | |
| 484 CdlDefinableBody::check_this(zeal); | |
| 485 } | |
| 486 | |
| 487 //}}} | |
| 488 //{{{ Misc | |
| 489 | |
| 490 // ---------------------------------------------------------------------------- | |
| 491 | |
| 492 std::string | |
| 493 CdlComponentBody::get_class_name() const | |
| 494 { | |
| 495 CYG_REPORT_FUNCNAME("CdlComponent::get_class_name"); | |
| 496 CYG_PRECONDITION_THISC(); | |
| 497 CYG_REPORT_RETURN(); | |
| 498 return "component"; | |
| 499 } | |
| 500 | |
| 501 //}}} |
