⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 objects.html

📁 cpptcl library for ns2
💻 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 &lt;class InputIterator&gt;</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 &amp;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 &amp;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 &amp; assign(bool b);</code><br><code>object &amp; resize(size_tsize);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// byte array resize</code><br><code>object &amp; assign(char const *buf, size_t size); // byte arrayassignment</code><br><code>object &amp; assign(double d);</code><br><code>object &amp; assign(int i);</code><br><code></code><br><code> template &lt;class InputIterator&gt;</code><br><code>object &amp; assign(InputIterator first, InputIterator last);</code><br><code></code><br><code>object &amp; assign(long l);</code><br><code>object &amp; assign(char const *s);</code><br><code>object &amp; assign(std::string const &amp;s);</code><br><code>object &amp; assign(object const &amp;o);<br>object &amp; assign(Tcl_Obj *o);<br></code><code></code><br><code>object &amp; operator=(bool b);</code><br><code>object &amp; operator=(double d);</code><br><code>object &amp; operator=(int i);</code><br><code>object &amp; operator=(long l);</code><br><code>object &amp; operator=(char const *s);</code><br><code>object &amp; operator=(std::string const &amp;s);</code><br><code></code><br><code>object &amp; operator=(object const &amp;o);</code><br><code>object &amp; swap(object &amp;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 &lt;typename T&gt;</code><br><code>T get(interpreter &amp;i) const;</code><br><code></code><br><code>char const * get()const;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// string get</code><br><code>char const * get(size_t &amp;size) const; // byte array get</code><br><code></code><br><code>size_t length(interpreter &amp;i) const;&nbsp; // returns listlength</code><br><code>object at(interpreter &amp;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&lt;T&gt;</code> template is specialized for thefollowing types:<br><ul>  <li><code>bool</code></li>  <li><code>vector&lt;char&gt;</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 &amp; append(interpreter&amp;i, object const &amp;o);</code><br><code>object &amp; append_list(interpreter &amp;i, object const &amp;o);</code><br><code></code><br><code>template &lt;class InputIterator&gt;</code><br><code>object &amp; replace(Interpreter &amp;i, size_t index, size_tcount,</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; InputIterator first, InputIterator last);</code><br><code></code><br><code>object &amp; replace(interpreter &amp;i, size_t index, size_tcount, object const &amp;o);</code><br><code>object &amp; replace_list(interpreter &amp;i, size_t index,size_tcount, object const &amp;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 &lt;iostream&gt;</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>&nbsp;&nbsp;&nbsp;&nbsp; interpreter i;</code><br><code></code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; int numbers[] = {5, 7, 1, 6, 3, 9, 7};</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; size_t elems = sizeof(numbers) /sizeof(int);</code><br><code></code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; object tab;</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; for (size_t indx = 0; indx != elems;++indx)</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; {</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tab.append(i, object(numbers[indx]));</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; }</code><br><code></code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; object cmd("lsort -integer");</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; cmd.append(i, tab);</code><br><code></code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; // here, cmd contains the following:</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; // lsort -integer {5 7 1 6 3 9 7}</code><br><code></code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; object result = i.eval(cmd);</code><br><code></code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; "unsorted: ";</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; for (size_t indx = 0; indx != elems;++indx)</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; {</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout&lt;&lt; numbers[indx] &lt;&lt; ' ';</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; }</code><br><code></code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; "\n&nbsp; sorted: ";</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; elems = result.length(i);</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; for (size_t indx = 0; indx != elems;++indx)</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; {</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; objectobj(result.at(i, indx));</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int val =obj.get&lt;int&gt;(i);</code><br><code></code><br><code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout&lt;&lt; val &lt;&lt; ' ';</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; }</code><br><code>&nbsp;&nbsp;&nbsp;&nbsp; cout &lt;&lt; '\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>&nbsp; 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 &copy; 2004-2006,MaciejSobczak<br><br></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -