C++/Tcl A C++ library for interoperability between C++ and Tcl |
object
provides the following members:object();
explicit object(bool b);
object(char const *buf, size_t size);
explicit object(double b);
explicit object(int i);
template <class InputIterator>
object(InputIterator first, InputIterator last);
explicit object(long i);
explicit object(char const *s);
explicit object(std::string const &s);
object
or Tcl_Obj*
type when dereferenced.explicit object(Tcl_Obj *o, bool
shared = false);
object(object const &other, bool shared = false);
shared
flag is set, the newly created object
wrapper will not duplicate the underlying Tcl object.object & assign(bool b);
object & resize(size_t
size);
// byte array resize
object & assign(char const *buf, size_t size); // byte array
assignment
object & assign(double d);
object & assign(int i);
template <class InputIterator>
object & assign(InputIterator first, InputIterator last);
object & assign(long l);
object & assign(char const *s);
object & assign(std::string const &s);
object & assign(object const &o);
object & assign(Tcl_Obj *o);
object & operator=(bool b);
object & operator=(double d);
object & operator=(int i);
object & operator=(long l);
object & operator=(char const *s);
object & operator=(std::string const &s);
object & operator=(object const &o);
object & swap(object &other);
assign
member function accepting iterators is for the
list assignment. The provided iterator type should give either object
or Tcl_Obj*
type when dereferenced.template <typename T>
T get(interpreter &i) const;
char const * get()
const;
// string get
char const * get(size_t &size) const; // byte array get
size_t length(interpreter &i) const; // returns list
length
object at(interpreter &i, size_t index) const;
Tcl_Obj * get_object() const { return obj_; }
get<T>
template is specialized for the
following types:bool
vector<char>
(for byte array queries)double
int
long
char const *
std::string
object & append(interpreter
&i, object const &o);
object & append_list(interpreter &i, object const &o);
template <class InputIterator>
object & replace(Interpreter &i, size_t index, size_t
count,
InputIterator first, InputIterator last);
object & replace(interpreter &i, size_t index, size_t
count, object const &o);
object & replace_list(interpreter &i, size_t index,
size_t
count, object const &o);
void set_interp(Tcl_Interp
*interp);
Tcl_Interp * get_interp() const;
object
parameter is called from Tcl.set_interp
function is automatically called by the
underlying
conversion logic, so that the C++ code can use the other function for
accessing the interpreter.interpreter
object, which will not disrupt its normal lifetime:interpreter i(o.get_interp(),
false);
interpreter
constructor
means that the newly created i
object will not claim
ownership to the pointer received from get_interp()
.i
will not
free the actual interpreter.// example6.cc
#include "../cpptcl.h"
#include <iostream>
using namespace std;
using namespace Tcl;
int main()
{
interpreter i;
int numbers[] = {5, 7, 1, 6, 3, 9, 7};
size_t elems = sizeof(numbers) /
sizeof(int);
object tab;
for (size_t indx = 0; indx != elems;
++indx)
{
tab.append(i, object(numbers[indx]));
}
object cmd("lsort -integer");
cmd.append(i, tab);
// here, cmd contains the following:
// lsort -integer {5 7 1 6 3 9 7}
object result = i.eval(cmd);
cout << "unsorted: ";
for (size_t indx = 0; indx != elems;
++indx)
{
cout
<< numbers[indx] << ' ';
}
cout << "\n sorted: ";
elems = result.length(i);
for (size_t indx = 0; indx != elems;
++indx)
{
object
obj(result.at(i, indx));
int val =
obj.get<int>(i);
cout
<< val << ' ';
}
cout << '\n';
}
$ ./example6
unsorted: 5 7 1 6 3 9 7
sorted: 1 3 5 6 7 7 9
$
tab
object is created and all
numbers are appended to it to form a Tcl list of numbers.object
can be passed for evaluation).