📄 objects.html
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head> <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"> <title>C++/Tcl</title></head><body><table style="width: 100%; text-align: left;" border="0" cellpadding="2" cellspacing="20"> <tbody> <tr> <td style="vertical-align: top;"><font size="+3"><span style="font-family: helvetica,arial,sans-serif; font-weight: bold; color: rgb(0, 0, 255);">C++/Tcl</span></font><br> <span style="font-family: helvetica,arial,sans-serif; color: rgb(0, 0, 255); font-weight: bold;">AC++ library for interoperability between C++ and Tcl</span> </td> <td style="vertical-align: top; text-align: right;"><br> </td> </tr> </tbody></table><br>[<a href="classes.html">prev</a>][<a href="index.html">top</a>][<a href="callpolicies.html">next</a>]<br><h4>Objects and Lists</h4>In order to help with managing Tcl lists, there is an object wrapperthat allows to manipulate Tcl objects.<br>The class <code>object</code> provides the following members:<br><br>1. Constructors<br><br><div style="margin-left: 40px;"><code>object();<br>explicit object(bool b);</code><br><code>object(char const *buf, size_t size);</code><br><code>explicit object(double b);</code><br><code>explicit object(int i);</code><br><code></code><br><code>template <class InputIterator></code><br><code>object(InputIterator first, InputIterator last);</code><br><code></code><br><code>explicit object(long i);</code><br><code>explicit object(char const *s);</code><br><code>explicit object(std::string const &s);</code><br><code></code></div><br>The above constructors allow to create a Tcl object from common C++types.<br>The constructor accepting iterators is for the list creation. Theprovided iterator type should give either <code>object</code> or <code>Tcl_Obj*</code>type when dereferenced.<br><br>2. Copy constructors<br><br><div style="margin-left: 40px;"><code>explicit object(Tcl_Obj *o, boolshared = false);</code><br><code>object(object const &other, bool shared = false);<br></code><code></code></div><br>If the <code>shared</code> flag is set, the newly created objectwrapper will not duplicate the underlying Tcl object.<br><br>3. Assignment-related members<br><br><div style="margin-left: 40px;"><code>object & assign(bool b);</code><br><code>object & resize(size_tsize); // byte array resize</code><br><code>object & assign(char const *buf, size_t size); // byte arrayassignment</code><br><code>object & assign(double d);</code><br><code>object & assign(int i);</code><br><code></code><br><code> template <class InputIterator></code><br><code>object & assign(InputIterator first, InputIterator last);</code><br><code></code><br><code>object & assign(long l);</code><br><code>object & assign(char const *s);</code><br><code>object & assign(std::string const &s);</code><br><code>object & assign(object const &o);<br>object & assign(Tcl_Obj *o);<br></code><code></code><br><code>object & operator=(bool b);</code><br><code>object & operator=(double d);</code><br><code>object & operator=(int i);</code><br><code>object & operator=(long l);</code><br><code>object & operator=(char const *s);</code><br><code>object & operator=(std::string const &s);</code><br><code></code><br><code>object & operator=(object const &o);</code><br><code>object & swap(object &other);</code><br><code></code></div><br>The <code>assign</code> member function accepting iterators is for thelist assignment. The provided iterator type should give either <code>object</code>or <code>Tcl_Obj*</code> type when dereferenced.<br><br>4. Non-modifying accessors<br><br><div style="margin-left: 40px;"><code>template <typename T></code><br><code>T get(interpreter &i) const;</code><br><code></code><br><code>char const * get()const; // string get</code><br><code>char const * get(size_t &size) const; // byte array get</code><br><code></code><br><code>size_t length(interpreter &i) const; // returns listlength</code><br><code>object at(interpreter &i, size_t index) const;</code><br><code></code><br><code>Tcl_Obj * get_object() const { return obj_; }</code><br><code></code></div><br>The <code>get<T></code> template is specialized for thefollowing types:<br><ul> <li><code>bool</code></li> <li><code>vector<char></code> (for byte array queries)</li> <li><code>double</code></li> <li><code>int</code></li> <li><code>long</code></li> <li><code>char const *</code></li> <li><code>std::string</code><br> </li></ul>5. List-related modifiers<br><br><div style="margin-left: 40px;"><code>object & append(interpreter&i, object const &o);</code><br><code>object & append_list(interpreter &i, object const &o);</code><br><code></code><br><code>template <class InputIterator></code><br><code>object & replace(Interpreter &i, size_t index, size_tcount,</code><br><code> InputIterator first, InputIterator last);</code><br><code></code><br><code>object & replace(interpreter &i, size_t index, size_tcount, object const &o);</code><br><code>object & replace_list(interpreter &i, size_t index,size_tcount, object const &o);</code><br><code></code></div><br>6. Additional helpers<br><br><div style="margin-left: 40px;"><code>void set_interp(Tcl_Interp*interp);</code><br><code>Tcl_Interp * get_interp() const;</code><br><code></code></div><br>These functions may help to transmit the information about the"current" interpreter when the C++ function accepting <code>object</code>parameter is called from Tcl.<br>The <code>set_interp</code> function is automatically called by theunderlyingconversion logic, so that the C++ code can use the other function foraccessing the interpreter.<br>This may be useful when the C++ code needs to invoke other functionsthat may change the interpreter state.<br><br>Note: If there is any need to extract the interpreter from the existingobject, it may be helpful to wrap the resulting raw pointer into the <code>interpreter</code>object, which will not disrupt its normal lifetime:<br><br><div style="margin-left: 40px;"><code>interpreter i(o.get_interp(),false);</code><br></div><br>The second parameter given to the <code>interpreter</code> constructormeans that the newly created <code>i</code> object will not claimownership to the pointer received from <code>get_interp()</code>.<br>In other words, the destructor of the object <code>i</code> will notfree the actual interpreter.<br><br><br><span style="font-weight: bold;">Example:</span><br><br>The following complete program creates the list of numbers, sorts itusing the Tcl interpreter and prints the results on the console (note:this is not the most efficient way to sort numbers in C++!):<br><br><div style="margin-left: 40px;"><code>// example6.cc</code><br><code></code><br><code>#include "../cpptcl.h"</code><br><code>#include <iostream></code><br><code></code><br><code>using namespace std;</code><br><code>using namespace Tcl;</code><br><code></code><br><code>int main()</code><br><code>{</code><br><code> interpreter i;</code><br><code></code><br><code> int numbers[] = {5, 7, 1, 6, 3, 9, 7};</code><br><code> size_t elems = sizeof(numbers) /sizeof(int);</code><br><code></code><br><code> object tab;</code><br><code> for (size_t indx = 0; indx != elems;++indx)</code><br><code> {</code><br><code> tab.append(i, object(numbers[indx]));</code><br><code> }</code><br><code></code><br><code> object cmd("lsort -integer");</code><br><code> cmd.append(i, tab);</code><br><code></code><br><code> // here, cmd contains the following:</code><br><code> // lsort -integer {5 7 1 6 3 9 7}</code><br><code></code><br><code> object result = i.eval(cmd);</code><br><code></code><br><code> cout << "unsorted: ";</code><br><code> for (size_t indx = 0; indx != elems;++indx)</code><br><code> {</code><br><code> cout<< numbers[indx] << ' ';</code><br><code> }</code><br><code></code><br><code> cout << "\n sorted: ";</code><br><code> elems = result.length(i);</code><br><code> for (size_t indx = 0; indx != elems;++indx)</code><br><code> {</code><br><code> objectobj(result.at(i, indx));</code><br><code> int val =obj.get<int>(i);</code><br><code></code><br><code> cout<< val << ' ';</code><br><code> }</code><br><code> cout << '\n';</code><br><code>}</code><br><code></code></div><br>When this program is run, it gives the following output:<br><br><div style="margin-left: 40px;"><code>$ ./example6</code><br><code>unsorted: 5 7 1 6 3 9 7 </code><br><code> sorted: 1 3 5 6 7 7 9 </code><br><code>$ </code><br><code></code></div><br>In this example, an empty <code>tab</code> object is created and allnumbers are appended to it to form a Tcl list of numbers.<br>After that, the sorting command is composed and executed (as you see,the <code>object</code> can be passed for evaluation).<br>The result of the command is retrieved also in the form of objectwrapper, which is used to decompose the resulting list into itselements.<br><br><br>[<a href="classes.html">prev</a>][<a href="index.html">top</a>][<a href="callpolicies.html">next</a>]<br><br><hr style="width: 100%; height: 2px;">Copyright © 2004-2006,MaciejSobczak<br><br></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -