#include "TclUtils/ExampleTclModule.hh" ExampleTclModule::ExampleTclModule(const char* const theName, const char* const theDescription, const char* const className) : AppModule( theName, theDescription ), TclModule( theName, theDescription, className ), _scriptPeriod(3), _mean(0.0), _width(1.0) { init_tcl_parameter(_debug, true); init_tcl_parameter(_eventCounter, 0); setup_tcl_parameter(_mean); setup_tcl_parameter(_width); init_tcl_parameter(_someString, "a string"); setParameterDescription("someString", "An example string parameter"); init_tcl_parameter(_periodicallyExecutedScript, "puts {This is the periodic script}"); setup_tcl_parameter(_scriptPeriod); setup_tcl_parameter(_vectorOfInts); setup_tcl_parameter(_cuts); } ExampleTclModule::~ExampleTclModule() { } int ExampleTclModule::parseCommand(Tcl_Interp * const interp, int argc, char *argv[]) { // This function implements an option called "hello". It also // augments "methods" and "help" options so that there is help // for the "hello" option. It also introduces some arbitrary // help topic named here "exampleHelpTopic". if (argc > 0) { if (strcmp(argv[0], "hello") == 0) { if (argc == 1) { Tcl_AppendResult(interp, "Hello from ", name(), "!", 0); return TCL_OK; } else { Tcl_SetResult(interp, "wrong # of arguments", TCL_VOLATILE); return TCL_ERROR; } } else if (strcmp(argv[0], "help") == 0) { if (argc == 1) { Tcl_AppendElement(interp, "hello"); Tcl_AppendElement(interp, "exampleHelpTopic"); } else if (argc == 2) { if (strcmp(argv[1], "hello") == 0) { Tcl_AppendResult( interp, "\n* Usage: ", getTclHandle(), " ", argv[1], "\nReturns a greeting message.\n", 0); return TCL_OK; } else if (strcmp(argv[1], "exampleHelpTopic") == 0) { Tcl_AppendResult( interp, "\n* Usage: ", getTclHandle(), " ", argv[1], "\nReturns this help message.\n", 0); return TCL_OK; } } } else if (strcmp(argv[0], "methods") == 0 && argc == 1) { Tcl_AppendElement(interp, "hello"); } } return TclModule::parseCommand(interp, argc, argv); } AppResult ExampleTclModule::beginJob(AbsEvent* aJob) { // Parse the Tcl list into an array if (parseTclList(modInterp(), _vectorOfInts.c_str(), &_someArray) != TCL_OK) { std::cerr << "ERROR in " << name() << ": failed to parse string \"" << _vectorOfInts << "\": " #if (TCL_MAJOR_VERSION > 7) << Tcl_GetStringResult(modInterp()) #else << modInterp()->result #endif << std::endl; } else { if (_someArray.size() > 0) { std::cout << name() << ": printing _someArray values:"; for (int i=0; i<_someArray.size(); ++i) std::cout << "\n_someArray[" << i << "] = " << _someArray[i]; std::cout << std::endl; } } if (_debug) { // Print all parameter values std::cout << '\n' << name() << " beginJob parameter values:" << std::endl; printParameterTable(std::cout); } else { // Check that all non-array parameters have been defined std::cout << std::endl; checkConfigCount(std::cout, name()); } return AppResult::OK; } AppResult ExampleTclModule::event(AbsEvent* anEvent) { ++_eventCounter; if (_scriptPeriod > 0) { if (_eventCounter % _scriptPeriod == 0) { if (Tcl_Eval(modInterp(), const_cast( _periodicallyExecutedScript.c_str())) != TCL_OK) { std::cerr << "ERROR in periodicallyExecutedScript: " #if (TCL_MAJOR_VERSION > 7) << Tcl_GetStringResult(modInterp()) #else << modInterp()->result #endif << std::endl; return AppResult::ERROR; } } } return AppResult::OK; } AppResult ExampleTclModule::endJob(AbsEvent* aJob) { if (_debug) { // Print all parameter values std::cout << name() << " endJob parameter values:" << std::endl; printParameterTable(std::cout); } return AppResult::OK; }