A small, type-safe, API is provided for writing c++ extensions is provided.
Before invoking a function, the API will:
- Check all arguments have been provided
- Construct the arguments into c++ types (string, int, long and double supported by default)
- Raise a standard error message if an incorrect type has been provided
Also provided by the API:
- Return results by providing the native type
- Throw standard TCL errors within
- Simple to add more supported types
int Repeat(TCL_Interpreter& interp, string phrase, int times) {
stringstream result;
for ( int i =0; i< times; i++ ) {
result << phrase << endl;
}
interp.SetResult(result.str());
return TCL_OK;
}
extern "C"
{
// Standard TCL hook for pkg_mkIndex
int Tcltest_Init(Tcl_Interp* theInterpreter) {
TCL_Interpreter interp(theInterpreter);
// Register the function with the API
interp.AddCommand("Repeat",Repeat);
// Declare the package we're providing
return interp.PackageProvide("Tcltest");
}
}
>>> package require Tcltest
>>> Repeat "Hello World!" 4
Hello World!
Hello World!
Hello World!
Hello World!
Standard TCL errors are raised for missing or incorrect arguments
>>> Repeat "Hello World!" three
expected integer but got "three"
while executing
"Repeat "Hello World!" three"
(file "./pluginTest.tcl" line 177)
Lets amend the command with some boundrary case testing
int Repeat(TCL_Interpreter& interp, string phrase, int times) {
stringstream result;
if ( times < 0 ) {
interp.RaiseError("repeat must be a positive number!");
}
for ( int i =0; i< times; i++ ) {
result << phrase << endl;
}
interp.SetResult(result.str());
return TCL_OK;
}
>>> Repeat "Hello World!" -1
repeat must be a positive number!
while executing
"Repeat "Hello World" -1"
(file "./pluginTest.tcl" line 177)
An example plugin which integrates with the readline library will be provided...