1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
// ------------------------------------------------------------
//
// Author: Tony Darugar, tdarugar@binevolve.com
//
// $Id$
// -------------------------------------------------------------
#include "tcl.h"
#include "connection_manager.h"
// -------------------------------------------------------------
// Our sample command.
// Notice how the ClientData is used to pass around the reference
// to our 'counter' object.
//
SampleCmd(ClientData clientData, Tcl_Interp *interp, int argc, char **argv)
{
// Get a pointer to the sample object from the clientData:
Sample *counter = (Sample *)clientData;
// Call the counter:
int c = counter->getNext();
// Return the result of the command:
char returnValue[10];
sprintf(returnValue, "%d", c);
// The TCL_VOLATILE means the memory for our returnValue was allocated
// from the stack. See Tcl_SetResult for details.
Tcl_SetResult(interp, returnValue, TCL_VOLATILE);
return TCL_OK;
}
// -------------------------------------------------------------
// It's necessary to declare the Init procedure as extern C to make
// tcl happy.
//
extern "C" {
Sample_Init(Tcl_Interp *interp);
}
// -------------------------------------------------------------
// The initialization function. Tcl calls this function when you
// load the package. Its name must be the name of the package, with
// the first letter capitalized, the rest lower, and '_Init' appended
// to the end of it.
//
Sample_Init(Tcl_Interp *interp) {
Sample *counter = new Sample();
// Create a new tcl command called 'counter'
// Notes:
// the first argument is the interpreter, as passed in. Ignore it.
// the 2nd arg is the name of your command
// the 3rd arg is the name of the function to call when the new command
// is used
// the 4th arg is data that your command can use as it wants to.
// In this case we pass a pointer to the 'counter' object.
// This pointer will be passed to our 'SampleCmd' command whenever
// it's called.
// the 5th argument is a function to delete your command. It can be NULL.
Tcl_CreateCommand (interp, "counter", SampleCmd ,(ClientData) counter,
(Tcl_CmdDeleteProc*) NULL);
// Provide a package called Sample
if (Tcl_PkgProvide(interp, "Sample", "1.0") == TCL_ERROR)
return TCL_ERROR;
return TCL_OK;
}
|