3 //============================================================================
7 // Implementation of the CdlInterface class
9 //============================================================================
10 //####COPYRIGHTBEGIN####
12 // ----------------------------------------------------------------------------
13 // Copyright (C) 2002 Bart Veer
14 // Copyright (C) 1999, 2000 Red Hat, Inc.
16 // This file is part of the eCos host tools.
18 // This program is free software; you can redistribute it and/or modify it
19 // under the terms of the GNU General Public License as published by the Free
20 // Software Foundation; either version 2 of the License, or (at your option)
23 // This program is distributed in the hope that it will be useful, but WITHOUT
24 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
25 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
28 // You should have received a copy of the GNU General Public License along with
29 // this program; if not, write to the Free Software Foundation, Inc.,
30 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
32 // ----------------------------------------------------------------------------
34 //####COPYRIGHTEND####
35 //============================================================================
36 //#####DESCRIPTIONBEGIN####
43 //####DESCRIPTIONEND####
44 //============================================================================
49 // ----------------------------------------------------------------------------
50 #include "cdlconfig.h"
52 // Get the infrastructure types, assertions, tracing and similar
54 #include <cyg/infra/cyg_ass.h>
55 #include <cyg/infra/cyg_trac.h>
57 // <cdlcore.hxx> defines everything implemented in this module.
58 // It implicitly supplies <string>, <vector> and <map> because
59 // the class definitions rely on these headers.
60 #include <cdlcore.hxx>
66 // ----------------------------------------------------------------------------
67 CYGDBG_DEFINE_MEMLEAK_COUNTER(CdlInterfaceBody);
72 // ----------------------------------------------------------------------------
73 CdlInterfaceBody::CdlInterfaceBody(std::string name_arg, bool generated_arg)
74 : CdlNodeBody(name_arg),
76 CdlValuableBody(CdlValueFlavor_Data),
81 CYG_REPORT_FUNCNAME("CdlInterfaceBody:: constructor");
82 CYG_REPORT_FUNCARG1XV(this);
84 generated = generated_arg;
85 cdlinterfacebody_cookie = CdlInterfaceBody_Magic;
86 CYGDBG_MEMLEAK_CONSTRUCTOR();
88 CYG_POSTCONDITION_THISC();
95 // ----------------------------------------------------------------------------
96 CdlInterfaceBody::~CdlInterfaceBody()
98 CYG_REPORT_FUNCNAME("CdlInterfaceBody:: destructor");
99 CYG_REPORT_FUNCARG1XV(this);
100 CYG_PRECONDITION_THISC();
102 cdlinterfacebody_cookie = CdlInterfaceBody_Invalid;
103 CYGDBG_MEMLEAK_DESTRUCTOR();
109 //{{{ parse_interface()
111 // ----------------------------------------------------------------------------
112 // Parsing an interface definition. This is basically the same as parsing
113 // an option, component, or package.
116 CdlInterfaceBody::parse_interface(CdlInterpreter interp, int argc, const char* argv[])
118 CYG_REPORT_FUNCNAMETYPE("CdlInterface::parse_interface", "result %d");
119 CYG_REPORT_FUNCARG1("argc %d", argc);
120 CYG_PRECONDITION_CLASSC(interp);
122 std::string diag_argv0 = CdlParse::get_tcl_cmd_name(argv[0]);
124 CdlLoadable loadable = interp->get_loadable();
125 CdlContainer parent = interp->get_container();
126 CdlToplevel toplevel = interp->get_toplevel();
127 CYG_ASSERT_CLASSC(loadable); // There should always be a loadable during parsing
128 CYG_ASSERT_CLASSC(parent);
129 CYG_ASSERT_CLASSC(toplevel);
131 // The new interface should be created and added to the loadable.
132 // early on. If there is a parsing error it will get cleaned up
133 // automatically as a consequence of the loadable destructor.
134 // However it is necessary to validate the name first. Errors
135 // should be reported via CdlParse::report_error(), which
136 // may result in an exception.
137 CdlInterface new_interface = 0;
142 // Currently there are no command-line options. This may change in future.
144 CdlParse::report_error(interp, "", std::string("Incorrect number of arguments to `") + diag_argv0 +
145 "'\nExpecting name and properties list.");
147 } else if (!Tcl_CommandComplete(CDL_TCL_CONST_CAST(char*, argv[2]))) {
148 CdlParse::report_error(interp, "", std::string("Invalid property list for cdl_interface `") + argv[1] + "'.");
150 } else if (0 != toplevel->lookup(argv[1])) {
151 // FIXME: interfaces can be generated implicitly because of an
152 // unresolved implements property. This code should look for
153 // an existing auto-generated interface object and replace it
155 CdlParse::report_error(interp, "", std::string("Interface `") + argv[1] +
156 "' cannot be loaded.\nThe name is already in use.");
159 new_interface = new CdlInterfaceBody(argv[1], false);
160 toplevel->add_node(loadable, parent, new_interface);
164 // Just because this component cannot be created, that is no
165 // reason to abort the whole parsing process.
166 CYG_REPORT_RETVAL(TCL_OK);
169 } catch(std::bad_alloc e) {
170 interp->set_result(CdlParse::construct_diagnostic(interp, "internal error", "", "Out of memory"));
172 } catch(CdlParseException e) {
173 interp->set_result(e.get_message());
176 interp->set_result(CdlParse::construct_diagnostic(interp, "internal error", "", "Unexpected C++ exception"));
179 if (TCL_OK != result) {
180 CYG_REPORT_RETVAL(result);
184 // At this stage new_interface has been created and added to the
185 // hierarchy. The main work now is to add the properties.
187 // Push the option as the current node early on. This aids
189 CdlNode old_node = interp->push_node(new_interface);
191 // Declare these outside the scope of the try statement, to allow
192 // goto calls for the error handling.
193 std::string tcl_result;
194 std::vector<CdlInterpreterCommandEntry> new_commands;
195 std::vector<CdlInterpreterCommandEntry>* old_commands = 0;
196 static CdlInterpreterCommandEntry commands[] =
198 CdlInterpreterCommandEntry("", 0)
202 // All parsing errors may result in an exception, under the control of
203 // application code. This exception must not pass through the Tcl interpreter.
206 for (i = 0; 0 != commands[i].command; i++) {
207 new_commands.push_back(commands[i]);
209 CdlDefinableBody::add_property_parsers(new_commands);
210 CdlBuildableBody::add_property_parsers(new_commands);
211 CdlParentableBody::add_property_parsers(new_commands);
212 CdlValuableBody::add_property_parsers(new_commands);
213 CdlUserVisibleBody::add_property_parsers(new_commands);
214 CdlNodeBody::add_property_parsers(new_commands);
216 // Now evaluate the body. If an error occurs then typically
217 // this will be reported via CdlParse::report_error(),
218 // but any exceptions will have been intercepted and
219 // turned into a Tcl error.
220 old_commands = interp->push_commands(new_commands);
221 result = interp->eval(argv[2], tcl_result);
222 interp->pop_commands(old_commands);
224 if (TCL_OK == result) {
225 // Even if there were errors, they were not fatal. There may
226 // now be a number of properties for this option, and some
227 // validation should take place. Start with the base classes.
228 new_interface->CdlNodeBody::check_properties(interp);
229 new_interface->CdlUserVisibleBody::check_properties(interp);
230 new_interface->CdlValuableBody::check_properties(interp);
231 new_interface->CdlParentableBody::check_properties(interp);
232 new_interface->CdlBuildableBody::check_properties(interp);
233 new_interface->CdlDefinableBody::check_properties(interp);
235 // The flavor "none" makes no sense for interfaces.
236 // The flavor "bool" makes very little sense, but may be useful
237 // in weird cases. Both booldata and data make sense.
238 // The default flavor is "data", because interfaces are
239 // essentially just counters.
240 if (new_interface->has_property(CdlPropertyId_Flavor)) {
241 if (CdlValueFlavor_None == new_interface->get_flavor()) {
242 CdlParse::report_error(interp, "", "An interface should not have the `none' flavor.");
246 // Interfaces cannot be modified directly by the user, so
247 // there is no point in entry_proc, check_proc, dialog or
249 if (new_interface->has_property(CdlPropertyId_EntryProc)) {
250 CdlParse::report_error(interp, "", "An interface should not have an `entry_proc' property.");
252 if (new_interface->has_property(CdlPropertyId_CheckProc)) {
253 CdlParse::report_error(interp, "", "An interface should not have a `check_proc' property.");
255 if (new_interface->has_property(CdlPropertyId_Dialog)) {
256 CdlParse::report_error(interp, "", "An interface should not have a `dialog' property.");
258 if (new_interface->has_property(CdlPropertyId_Wizard)) {
259 CdlParse::report_error(interp, "", "An interface should not have a `wizard' property.");
261 // Calculated does not make sense, an interface is implicitly calculated
262 // Nor does default_value.
263 if (new_interface->has_property(CdlPropertyId_Calculated)) {
264 CdlParse::report_error(interp, "", "An interface should not have a `calculated' property.");
266 if (new_interface->has_property(CdlPropertyId_DefaultValue)) {
267 CdlParse::report_error(interp, "", "An interface should not have a `default_value' property.");
269 // active_if might make sense, as a way of controlling
270 // whether or not a #define will be generated.
272 // legal_values, requires and implements are all sensible
273 // properties for an interface.
275 // group may or may not make sense, allow it for now.
277 // For the uservisible base class, allow all of display,
278 // doc and description. At worst these are harmless
280 // For the parentable base class, allow parent. Again this
283 // Also allow all of the definable and buildable
286 } catch (std::bad_alloc e) {
287 // Errors at this stage should be reported via Tcl, not via C++.
288 // However there is no point in continuing with the parsing operation,
290 interp->set_result(CdlParse::construct_diagnostic(interp, "internal error", "", "Out of memory"));
292 } catch (CdlParseException e) {
293 interp->set_result(e.get_message());
296 interp->set_result(CdlParse::construct_diagnostic(interp, "internal error", "", "Unexpected C++ exception"));
300 // Restore the interpreter to its prior state.
301 interp->pop_node(old_node);
302 if (0 != old_commands) {
303 interp->pop_commands(old_commands);
306 CYG_REPORT_RETVAL(result);
311 //{{{ Persistence support
313 // ----------------------------------------------------------------------------
316 CdlInterfaceBody::initialize_savefile_support(CdlToplevel toplevel)
318 CYG_REPORT_FUNCNAME("CdlInterface::initialize_savefile_support");
320 toplevel->add_savefile_command("cdl_interface", 0, &savefile_interface_command);
321 CdlValuableBody::initialize_savefile_support(toplevel, "cdl_interface");
327 CdlInterfaceBody::save(CdlInterpreter interp, Tcl_Channel chan, int indentation, bool minimal)
329 CYG_REPORT_FUNCNAME("CdlInterface::save");
330 CYG_REPORT_FUNCARG5XV(this, interp, chan, indentation, minimal);
331 CYG_PRECONDITION_THISC();
332 CYG_PRECONDITION_CLASSC(interp);
334 // Interfaces contain only calculated data, so for a minimal save
335 // there is no point in storing any of the data.
336 if (!minimal || this->has_additional_savefile_information()) {
337 // Start with the UserVisible data, which will result in a suitable set
338 // of comments before the package definition itself.
339 this->CdlUserVisibleBody::save(interp, chan, indentation, minimal);
341 // Now output the line "cdl_interface <name> {"
342 // The name is guaranteed to be a valid C preprocessor symbol, so it
343 // is not going to need any quoting.
344 std::string data = std::string(indentation, ' ') + "cdl_interface " + get_name() + " {\n";
346 // Start with details of everything that implements this interface.
348 const std::vector<CdlReferrer>& referrers = this->get_referrers();
349 std::vector<CdlReferrer>::const_iterator ref_i;
350 int real_referrers = 0;
351 for (ref_i = referrers.begin(); ref_i != referrers.end(); ref_i++) {
352 CdlNode node = ref_i->get_source();
353 CdlProperty prop = ref_i->get_source_property();
355 CdlValuable valuable = dynamic_cast<CdlValuable>(node);
356 if ((0 != valuable) && (CdlPropertyId_Implements == prop->get_property_name())) {
358 data += std::string(indentation, ' ') + " # Implemented by " + valuable->get_name() + ", " +
359 (valuable->is_active() ? "active" : "inactive") + ", " +
360 (valuable->is_enabled() ? "enabled" : "disabled") + '\n';
363 if (0 == real_referrers) {
364 data += std::string(indentation, ' ') + " # No options implement this inferface\n";
367 interp->write_data(chan, data);
369 // Deal with the value
370 this->CdlValuableBody::_save(interp, chan, indentation + 4, false, minimal);
372 // Close the cdl_interface body. A blank line is added here.
374 interp->write_data(chan, data);
381 CdlInterfaceBody::savefile_interface_command(CdlInterpreter interp, int argc, const char* argv[])
383 CYG_REPORT_FUNCNAMETYPE("CdlInterface::savefile_interface_command", "result %d");
384 CYG_PRECONDITION_CLASSC(interp);
387 CdlToplevel toplevel = interp->get_toplevel();
388 CYG_ASSERT_CLASSC(toplevel);
390 std::vector<CdlInterpreterCommandEntry> subcommands;
391 std::vector<CdlInterpreterCommandEntry>* toplevel_commands = 0;
392 CdlNode old_node = 0;
397 CdlParse::report_error(interp, "", "Invalid cdl_interface command in savefile, expecting two arguments.");
400 CdlNode current_node = toplevel->lookup(argv[1]);
401 if (0 == current_node) {
402 // FIXME: save value in limbo
403 CdlParse::report_error(interp, "",
404 std::string("The savefile contains a cdl_interface command for an unknown interface `")
407 toplevel->get_savefile_subcommands("cdl_interface", subcommands);
408 toplevel_commands = interp->push_commands(subcommands);
409 old_node = interp->push_node(current_node);
411 std::string tcl_result;
412 result = interp->eval(argv[2], tcl_result);
414 interp->pop_commands(toplevel_commands);
415 toplevel_commands = 0;
416 interp->pop_node(old_node);
422 interp->pop_node(old_node);
424 if (0 != toplevel_commands) {
425 interp->pop_commands(toplevel_commands);
430 CYG_REPORT_RETVAL(result);
437 // ----------------------------------------------------------------------------
440 CdlInterfaceBody::check_this(cyg_assert_class_zeal zeal) const
442 if (CdlInterfaceBody_Magic != cdlinterfacebody_cookie) {
445 CYGDBG_MEMLEAK_CHECKTHIS();
446 return CdlNodeBody::check_this(zeal) &&
447 CdlUserVisibleBody::check_this(zeal) &&
448 CdlValuableBody::check_this(zeal) &&
449 CdlParentableBody::check_this(zeal) &&
450 CdlBuildableBody::check_this(zeal) &&
451 CdlDefinableBody::check_this(zeal);
457 // ----------------------------------------------------------------------------
458 // There has been a change in the configuration which may affect the value
459 // of an interface. This can happen for a variety of reasons. For simplicity
460 // the entire value is just recalculated, with no attempt at optimisation.
461 // This may have to change in future.
464 CdlInterfaceBody::recalculate(CdlTransaction transaction)
466 CYG_REPORT_FUNCNAME("CdlInterface::recalculate");
468 const CdlValue& old_value = transaction->get_whole_value(this);
471 std::vector<CdlValuable> implementers;
472 std::vector<CdlValuable>::const_iterator valuable_i;
473 this->get_implementers(implementers);
474 for (valuable_i = implementers.begin(); valuable_i != implementers.end(); valuable_i++) {
475 if (transaction->is_active(*valuable_i)) {
476 const CdlValue& implementer_value = transaction->get_whole_value(*valuable_i);
477 if (implementer_value.is_enabled()) {
483 // What to do with the count depends on the flavor.
484 switch(this->get_flavor()) {
485 case CdlValueFlavor_Bool :
487 bool new_bool = (count > 0);
488 if (new_bool != old_value.is_enabled()) {
489 CdlValue new_value = old_value;
490 new_value.set_enabled(new_bool, CdlValueSource_Default);
491 transaction->set_whole_value(this, old_value, new_value);
495 case CdlValueFlavor_BoolData:
497 // The only thing that actually needs checking is the count value.
498 // Iff that has changed then the boolean part may need changing as well.
499 if (count != old_value.get_integer_value()) {
500 CdlValue new_value = old_value;
501 new_value.set_enabled_and_value(count > 0, count, CdlValueSource_Default);
502 transaction->set_whole_value(this, old_value, new_value);
507 case CdlValueFlavor_Data:
509 if (count != old_value.get_integer_value()) {
510 CdlValue new_value = old_value;
511 new_value.set_integer_value(count, CdlValueSource_Default);
512 transaction->set_whole_value(this, old_value, new_value);
527 // ----------------------------------------------------------------------------
530 CdlInterfaceBody::is_modifiable() const
532 CYG_REPORT_FUNCNAME("CdlInterface::is_modifiable (false)");
533 CYG_PRECONDITION_THISC();
539 CdlInterfaceBody::get_class_name() const
541 CYG_REPORT_FUNCNAME("CdlInterface::get_class_name");
542 CYG_PRECONDITION_THISC();
548 CdlInterfaceBody::get_implementers(std::vector<CdlValuable>& implementers) const
550 CYG_REPORT_FUNCNAME("CdlInterface::get_implementers");
551 CYG_PRECONDITION_THISC();
553 const std::vector<CdlReferrer>& referrers = this->get_referrers();
554 std::vector<CdlReferrer>::const_iterator ref_i;
555 for (ref_i = referrers.begin(); ref_i != referrers.end(); ref_i++) {
556 CdlNode node = ref_i->get_source();
557 CdlProperty prop = ref_i->get_source_property();
559 CdlValuable valuable = dynamic_cast<CdlValuable>(node);
560 if ((0 != valuable) && (CdlPropertyId_Implements == prop->get_property_name())) {
561 implementers.push_back(valuable);
568 // Interfaces are somewhat peculiar, in that they can be defined implicitly
569 // simply by occurring in an "implements" property, or explicitly inside
570 // a CDL script. Worse, they can switch between these two states when
571 // loadables are added or removed.
574 CdlInterfaceBody::was_generated() const
576 CYG_REPORT_FUNCNAMETYPE("CdlInterface::was_generated", "result %d");
577 CYG_REPORT_FUNCARG1XV(this);
578 CYG_PRECONDITION_THISC();
580 bool result = this->generated;
581 CYG_REPORT_RETVAL(result);