12.07.2015 Views

The ns Manual (formerly ns Notes and Documentation)1 - NM Lab at ...

The ns Manual (formerly ns Notes and Documentation)1 - NM Lab at ...

The ns Manual (formerly ns Notes and Documentation)1 - NM Lab at ...

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

• tcl.result(co<strong>ns</strong>t char* s)Pass the result string s back to the interpreter.• tcl.resultf(co<strong>ns</strong>t char* fmt, . . . )varargs(3) variant of above to form<strong>at</strong> the result using vsprintf(3), pass the result string back to the interpreter.if (strcmp(argv[1], "now") == 0) {tcl.resultf("%.17g", clock());return TCL_OK;}tcl.result("Invalid oper<strong>at</strong>ion specified");return TCL_ERROR;Likewise, when a C++ method invokes an OTcl comm<strong>and</strong>, the interpreter retur<strong>ns</strong> the result in tcl_->result.• tcl.result(void) must be used to retrieve the result. Note th<strong>at</strong> the result is a string, th<strong>at</strong> must be converted into aninternal form<strong>at</strong> appropri<strong>at</strong>e to the type of result.tcl.evalc("Simul<strong>at</strong>or set NumberInterfaces_");char* ni = tcl.result();if (<strong>at</strong>oi(ni) != 1)tcl.evalc("Simul<strong>at</strong>or set NumberInterfaces_ 1");3.3.4 Error Reporting <strong>and</strong> ExitThis method provides a uniform way to report errors in the compiled code.• tcl.error(co<strong>ns</strong>t char* s) performs the following functio<strong>ns</strong>: write s to stdout; write tcl_->result to stdout; exitwith error code 1.tcl.resultf("cmd = %s", cmd);tcl.error("invalid comm<strong>and</strong> specified");/*NOTREACHED*/Note th<strong>at</strong> there are minor differences between returning TCL_ERROR as we did in the previous subsection (Section 3.3.3),<strong>and</strong> calling Tcl::error(). <strong>The</strong> former gener<strong>at</strong>es an exception within the interpreter; the user can trap the exception <strong>and</strong>possibly recover from the error. If the user has not specified any traps, the interpreter will print a stack trace <strong>and</strong> exit. However,if the code invokes error(), then the simul<strong>at</strong>ion user cannot trap the error; in addition, <strong>ns</strong> will not print any stack trace.3.3.5 Hash Functio<strong>ns</strong> within the Interpreter<strong>ns</strong> stores a reference to every TclObject in the compiled hierarchy in a hash table; this permits quick access to the objects.<strong>The</strong> hash table is internal to the interpreter. <strong>ns</strong> uses the name of the TclObject as the key to enter, lookup, or delete theTclObject in the hash table.22

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!