C++/Tcl A C++ library for interoperability between C++ and Tcl |
class Person
{
public:
void setName(string const &n) { name
= n; }
string getName() { return name; }
private:
string name;
};
CPPTCL_MODULE(Mymodule, i)
{
i.class_<Person>("Person")
.def("setName", &Person::setName)
.def("getName", &Person::getName);
}
CPPTCL_MODULE
macro is a
name that you
later use to refer to the interpreter. You can choose whatever name you
like. The rest of the code is the same as if the following object was
declared in the C++ program:interpreter i;
class_
member function template, called on the interpreter object (it could
not be named "class", since that is a keyword in C++).class_
function is a name that will be visible to the Tcl scripts - again, it
does not have to be the name of the C++ class that you use in code.class_
function returns an object that can
be used to define class member functions, in the same expression and in
a chained way.i.class_<Person>("Person")
.def("setName",
&Person::setName)
.def("getName", &Person::getName);
mymodule.so
):% load ./mymodule.so
% set p [Person]
p0x807b790
% $p setName "Maciej"
% $p getName
Maciej
%
Person
command that creates and
returns a new object. This is simply a constructor of our class. It
returns a name of the new object (here it is p0x807b790 -
it
may be different on your machine or when you run the same program
twice; you should not attach any external meaning to this name) and
this name is immediately available as a new command, to use with member
functions.% $p setName "Maciej"
setName
with the parameter "Maciej"
,
on the object whose name is stored in the variable p
.% $p getName
getName
without any parameters
and it returns the string
result of that call.Person
has no
constructors. Or, to be strict, it has a default constructor without
parameters.Person
class could be exposed also in the following
way:i.class_<Person>("Person", init<>())
.def("setName", &Person::setName)
.def("getName", &Person::getName);
init
object provided. It
can carry information about the expected parameters that are needed for
constructor, as in the following full example:// example4.cc
#include "cpptcl.h"
#include <string>
using namespace std;
using namespace Tcl;
class Person
{
public:
Person(string
const &n) : name(n) {}
void setName(string const &n) { name
= n; }
string getName() { return name; }
private:
string name;
};
CPPTCL_MODULE(Mymodule, i)
{
i.class_<Person>("Person", init<string const &>())
.def("setName", &Person::setName)
.def("getName", &Person::getName);
}
init
object can bring with it information about the
number and the types of parameters needed by the constructor.Person
needs 1 parameter of type string const &
.% set p [Person]
Too few arguments.
%
% set p [Person "Maciej"]
p0x807b7c0
% $p getName
Maciej
%
i.class_<Person>("Person", no_init)
.def("setName", &Person::setName)
.def("getName", &Person::getName);
This basically means that the exposed class has no
constructors at all (or, to be strict, we do not want them to be
visible from Tcl).% $p -delete
% $p getName
invalid command name "p0x807b790"
%