Orxonox  0.0.5 Codename: Arcturus
Classes | Namespaces
TclBind.h File Reference

Declaration of the TclBind class. More...

#include "core/CorePrereqs.h"
#include <cassert>
#include <string>
#include "util/Singleton.h"

Go to the source code of this file.

Classes

class  orxonox::TclBind
 A wrapper class for a Tcl interpreter. More...
 

Namespaces

 orxonox
 Die Wagnis Klasse hat die folgenden Aufgaben:
 

Detailed Description

Declaration of the TclBind class.

orxonox::TclBind is a wrapper class for a Tcl interpreter. It is implemented as singleton, so it can be accessed by everyone, but all share the same static Tcl interpreter. If you need a Tcl interpreter at your own, see orxonox::TclThreadManager for more information.

orxonox::TclBind is used by orxonox::CommandExecutor to execute Tcl commands. It can also be used to execute Tcl commands from different sources, but note that they may interfer with the ingame console if used improperly. By no means execute blocking commands such as endless loops or the tcl command vwait. Use orxonox::TclThreadManager and execute these commands in a multithreaded Tcl interpreter instead.

TclBind also defines different callback functions to return commands from the Tcl interpreter back to Orxonox. Because of that it's possible to send a mixture of Orxonox- and Tcl-commands to TclBind::eval() and still get the desired behavior.

Example:

TclBind::eval("puts \"Hello World\""); // calls the tcl command "puts", prints "Hello World" to the console
TclBind::eval("log Hello World"); // calls the orxonox command "log", prints "Hello World" to the console
TclBind::eval("log [expr 1+1]"); // prints "2" to the console (which is the result of the tcl command "expr")
TclBind::eval("puts -nonewline Hello; log World"); // prints "HelloWorld" to the console
TclBind::eval("for {set i 0} {$i < 10} {incr i} {log test: $i}"); // prints "test: 0", ..., "test: 9" to the console

Note that puts and log behave slightly different, even though both can print text to the console. puts needs quotation marks around multi-word output, while log doesn't. puts on the other hand supports the flag -nonewline.

TclBind::eval() can also be used to obtain the return-value of a Tcl command:

std::string result = TclBind::eval("expr 1+1"); // result == "2"