[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[ns] Re: accessing a c++ function from Tcl code (fwd)




thought i should share this with the community. please let me know if
there's some flaw.

There are two ways that i figured out to do this:
1) in this procedure, you don't need to create an instance of the class to
access its function:

lets say i want to add a method called mymethod() to class LL (ll.cc,ll.h)
in ll.cc add the following:

void LLClass::bind()
{
        TclClass::bind();
        add_method("method");
}

int LLClass::method(int ac, const char*const* av)
{
	Tcl& tcl = Tcl::instance();
        int argc = ac - 2;

        const char*const* argv = av + 2;
	if (av=3)  // assuming u have passed one parameter called
		   //"myparam" to the call
	{
		mymethod(myparam); // actual call to the function
				   //mymethod()
	}
	return TclClass::method(ac, av);
}

Then in the tcl file u call the function as follows:

LL method 23;
# 23 is the parameter 'myparam'

2) in this manner u will have to initiate the class first.

just add the call to the already defined command() function:

int LL::command(int argc, const char*const* argv)
{
	..........
	if (argc==3)
	{
		if (strcmp(argv[1], "method") == 0) {
			mymethod(atol(argv[2]));
			return(TCL_OK);
		}
	}
	..........
	.........
}

now in tcl script first create an instance of the class:

set ll [new LL...]  # i don't remember how to create a new LL object
$ll method 23;


thanks,
Shailesh Sheoran
M.ASc.
Department of Electrical Engineering
University of British Columbia