Debian Codegraph @

embedded/www/files/critcl_callback.html

raw
1 <!DOCTYPE html><html><head>
2 <meta charset="UTF-8">
3 <meta name="viewport" content="width=device-width, initial-scale=1">
4 <title>critcl::callback - C Runtime In Tcl (CriTcl)</title>
5 <style type="text/css"><!--
6 HTML {
7 background: #FFFFFF;
8 color: black;
9 }
10 BODY {
11 background: #FFFFFF;
12 color: black;
13 }
14 DIV.doctools {
15 margin-left: 10%;
16 margin-right: 10%;
17 }
18 DIV.doctools H1,DIV.doctools H2 {
19 margin-left: -5%;
20 }
21 H1, H2, H3, H4 {
22 margin-top: 1em;
23 font-family: sans-serif;
24 font-size: large;
25 color: #005A9C;
26 background: transparent;
27 text-align: left;
28 }
29 H1.doctools_title {
30 text-align: center;
31 }
32 UL,OL {
33 margin-right: 0em;
34 margin-top: 3pt;
35 margin-bottom: 3pt;
36 }
37 UL LI {
38 list-style: disc;
39 }
40 OL LI {
41 list-style: decimal;
42 }
43 DT {
44 padding-top: 1ex;
45 }
46 UL.doctools_toc,UL.doctools_toc UL, UL.doctools_toc UL UL {
47 font: normal 12pt/14pt sans-serif;
48 list-style: none;
49 }
50 LI.doctools_section, LI.doctools_subsection {
51 list-style: none;
52 margin-left: 0em;
53 text-indent: 0em;
54 padding: 0em;
55 }
56 PRE {
57 display: block;
58 font-family: monospace;
59 white-space: pre;
60 margin: 0%;
61 padding-top: 0.5ex;
62 padding-bottom: 0.5ex;
63 padding-left: 1ex;
64 padding-right: 1ex;
65 width: 100%;
66 }
67 PRE.doctools_example {
68 color: black;
69 background: #f5dcb3;
70 border: 1px solid black;
71 }
72 UL.doctools_requirements LI, UL.doctools_syntax LI {
73 list-style: none;
74 margin-left: 0em;
75 text-indent: 0em;
76 padding: 0em;
77 }
78 DIV.doctools_synopsis {
79 color: black;
80 background: #80ffff;
81 border: 1px solid black;
82 font-family: serif;
83 margin-top: 1em;
84 margin-bottom: 1em;
85 }
86 UL.doctools_syntax {
87 margin-top: 1em;
88 border-top: 1px solid black;
89 }
90 UL.doctools_requirements {
91 margin-bottom: 1em;
92 border-bottom: 1px solid black;
93 }
94 --></style>
95 </head>
96 <!-- Generated from file 'critcl_callback.man' by tcllib/doctools with format 'html'
97 -->
98 <!-- Copyright &amp;copy; 2011-2024 Andreas Kupries
99 -->
100 <!-- critcl::callback.n
101 -->
102 <body><hr> [
103 <a href="../toc.html">Table Of Contents</a>
104 &#124; <a href="../index.html">Keyword Index</a>
105 ] <hr>
106 <div class="doctools">
107 <h1 class="doctools_title">critcl::callback(n) 1.1 doc &quot;C Runtime In Tcl (CriTcl)&quot;</h1>
108 <div id="name" class="doctools_section"><h2><a name="name">Name</a></h2>
109 <p>critcl::callback - CriTcl - C-level Callback Utilities</p>
110 </div>
111 <div id="toc" class="doctools_section"><h2><a name="toc">Table Of Contents</a></h2>
112 <ul class="doctools_toc">
113 <li class="doctools_section"><a href="#toc">Table Of Contents</a></li>
114 <li class="doctools_section"><a href="#synopsis">Synopsis</a></li>
115 <li class="doctools_section"><a href="#section1">Description</a></li>
116 <li class="doctools_section"><a href="#section2">API</a></li>
117 <li class="doctools_section"><a href="#section3">Examples</a>
118 <ul>
119 <li class="doctools_subsection"><a href="#subsection1">Simple callback</a></li>
120 <li class="doctools_subsection"><a href="#subsection2">Multiple methods</a></li>
121 </ul>
122 </li>
123 <li class="doctools_section"><a href="#section4">Authors</a></li>
124 <li class="doctools_section"><a href="#section5">Bugs, Ideas, Feedback</a></li>
125 <li class="doctools_section"><a href="#keywords">Keywords</a></li>
126 <li class="doctools_section"><a href="#category">Category</a></li>
127 <li class="doctools_section"><a href="#copyright">Copyright</a></li>
128 </ul>
129 </div>
130 <div id="synopsis" class="doctools_section"><h2><a name="synopsis">Synopsis</a></h2>
131 <div class="doctools_synopsis">
132 <ul class="doctools_requirements">
133 <li>package require <b class="pkgname">Tcl 8.6</b></li>
134 <li>package require <b class="pkgname">critcl <span class="opt">?3.2?</span></b></li>
135 <li>package require <b class="pkgname">critcl::callback <span class="opt">?1.1?</span></b></li>
136 </ul>
137 <ul class="doctools_syntax">
138 <li><a href="#1"><b class="type">critcl_callback_p</b> <b class="function">critcl_callback_new</b> <i class="arg">interp</i> <i class="arg">objc</i> <i class="arg">objv</i> <i class="arg">nargs</i></a></li>
139 <li><a href="#2"><b class="type">void</b> <b class="function">critcl_callback_extend</b> <i class="arg">callback</i> <i class="arg">argument</i></a></li>
140 <li><a href="#3"><b class="type">void</b> <b class="function">critcl_callback_destroy</b> <i class="arg">callback</i></a></li>
141 <li><a href="#4"><b class="type">int</b> <b class="function">critcl_callback_invoke</b> <i class="arg">callback</i> <i class="arg">objc</i> <i class="arg">objv</i></a></li>
142 </ul>
143 </div>
144 </div>
145 <div id="section1" class="doctools_section"><h2><a name="section1">Description</a></h2>
146 <p>Be welcome to the <i class="term">C Runtime In Tcl</i> (short: <i class="term"><a href="critcl.html">CriTcl</a></i>), a system for embedding and using C
147 code from within <a href="http://core.tcl-lang.org/tcl">Tcl</a> scripts.</p>
148 <p>This document is the reference manpage for the
149 <b class="package">critcl::callback</b> package.
150 This package provides, via a stubs API table, data structures and
151 functions to manage callbacks from C to Tcl. The package has no
152 Tcl-level facilities.
153 Its intended audience are mainly developers wishing to write Tcl
154 packages with embedded C code who have to invoke user-specified
155 command (prefixes) in Tcl.</p>
156 <p>This package resides in the Support Package Layer of CriTcl.</p>
157 <p><img alt="arch_support" src="../image/arch_support.png"></p>
158 </div>
159 <div id="section2" class="doctools_section"><h2><a name="section2">API</a></h2>
160 <p>The package API consist of one opaque data structure
161 (<b class="type">critcl_callback_p</b>) and four functions operating on the same.
162 These functions are</p>
163 <dl class="doctools_definitions">
164 <dt><a name="1"><b class="type">critcl_callback_p</b> <b class="function">critcl_callback_new</b> <i class="arg">interp</i> <i class="arg">objc</i> <i class="arg">objv</i> <i class="arg">nargs</i></a></dt>
165 <dd><p>This function creates a new callback (manager) and returns it as its result.</p>
166 <p>The callback is initialized with the Tcl_Interp* <i class="arg">interp</i>
167 specifying where to run the callback, the fixed part of the command to
168 run in standard <i class="arg">objc</i>/<i class="arg">objv</i> notation, plus the number of
169 free arguments to expect after the fixed part.</p>
170 <p>The fixed part is the essentially the command prefix of the callback.</p>
171 <p>All <b class="type">Tcl_Obj*</b> elements of <i class="arg">objv</i> are protected against early
172 release by incrementing their reference counts. The callback
173 effectively takes ownership of these objects.</p></dd>
174 <dt><a name="2"><b class="type">void</b> <b class="function">critcl_callback_extend</b> <i class="arg">callback</i> <i class="arg">argument</i></a></dt>
175 <dd><p>This function takes a <i class="arg">callback</i> of type <b class="type">critcl_callback_p</b>
176 and extends its fixed part with the <i class="arg">argument</i>, taking the first
177 free slot for arguments to do so.
178 This means that after the application of this function the specified
179 callback has one free argument less.</p>
180 <p>With assertions active attempting to extend beyond the number of free
181 arguments will cause a panic. Without assertions active expect a crash
182 at some point.</p>
183 <p>This allows the user to extend the fixed part of the callback with
184 semi-fixed elements, like method names (See <span class="sectref"><a href="#subsection2">Multiple methods</a></span>).</p>
185 <p>The <i class="arg">argument</i> is protected against early release by incrementing
186 its reference count. The callback effectively takes ownership of this
187 object.</p></dd>
188 <dt><a name="3"><b class="type">void</b> <b class="function">critcl_callback_destroy</b> <i class="arg">callback</i></a></dt>
189 <dd><p>This function takes a <i class="arg">callback</i> of type <b class="type">critcl_callback_p</b>
190 and releases all memory associated with it.
191 After application of this function the callback cannot be used anymore.</p>
192 <p>All fixed elements of the callback (owned by it) are released by
193 decrementing their reference counts.</p></dd>
194 <dt><a name="4"><b class="type">int</b> <b class="function">critcl_callback_invoke</b> <i class="arg">callback</i> <i class="arg">objc</i> <i class="arg">objv</i></a></dt>
195 <dd><p>This function invokes the callback in the Tcl interpreter specified at
196 the time of construction, in the global level and namespace, with the
197 free arguments filled by the <b class="type">Tcl_Obj*</b> objects specified via
198 <i class="arg">objc</i>/<i class="arg">objv</i>.</p>
199 <p>It returns the Tcl status of the invoked command as its result.
200 Any further results or error messages will be found in the result area
201 of the Tcl interpreter in question. The exact nature of such is
202 dependent on the callback itself.</p>
203 <p>With assertions active attempting to use more arguments than available
204 will cause a panic. Without assertions active expect a crash at some
205 point.</p>
206 <p>While the callback is running all <b class="type">Tcl_Obj*</b> elements of the
207 command, fixed and arguments, are protected against early release by
208 temporarily incrementing their reference counts.</p></dd>
209 </dl>
210 </div>
211 <div id="section3" class="doctools_section"><h2><a name="section3">Examples</a></h2>
212 <div id="subsection1" class="doctools_subsection"><h3><a name="subsection1">Simple callback</a></h3>
213 <p>The example here shows the important parts of using the functions of
214 this package for a simple callback which is invoked with a single
215 argument, some kind of data to hand to the Tcl level.</p>
216 <pre class="doctools_example">
217 // Create the callback with interpreter and command prefix in
218 // oc/ov, plus space for the argument
219 critcl_callback_p cb = critcl_callback_new (interp, oc, ov, 1);
220 // Invoke the callback somewhere in the C package using this one,
221 // with Tcl_Obj* data holding the information to pass up.
222 critcl_callback_invoke (cb, 1, &amp;data);
223 // At the end of the lifetime, release the callback.
224 critcl_callback_destroy (cb);
225 </pre>
226 <p>Note that the functions of this package are designed for the case
227 where the created callback (<b class="const">cb</b> above) is kept around for a
228 long time, and many different invokations.</p>
229 <p>Using the sequence above as is, creating and destroying the callback
230 each time it is invoked will yield very poor performance and lots of
231 undesirable memory churn.</p>
232 </div>
233 <div id="subsection2" class="doctools_subsection"><h3><a name="subsection2">Multiple methods</a></h3>
234 <p>While we can use the methodology of the previous section when a single
235 (Tcl-level) callback is invoked from different places in C, with
236 different methods, simply having another argument slot and filling it
237 an invokation time with the method object, a second methodology is
238 open to us due to <b class="function">critcl_callback_extend</b>.</p>
239 <pre class="doctools_example">
240 // Create one callback manager per different method the callback
241 // will be used with. Fill the first of the two declared arguments
242 // with the different methods.
243 critcl_callback_p cb_a = critcl_callback_new (interp, oc, ov, 2);
244 critcl_callback_p cb_b = critcl_callback_new (interp, oc, ov, 2);
245 critcl_callback_extend (cb_a, Tcl_NewStringObj (&quot;method1&quot;, -1));
246 critcl_callback_extend (cb_b, Tcl_NewStringObj (&quot;method2&quot;, -1));
247 // After the extension we have one free argument left, for use in
248 // the invokations.
249 critcl_callback_invoke (cb_a, 1, &amp;dataX);
250 critcl_callback_invoke (cb_b, 1, &amp;dataY);
251 // At the end release both managers again
252 critcl_callback_destroy (cb_a);
253 critcl_callback_destroy (cb_b);
254 </pre>
255 <p>The nice thing here is that the method objects are allocated only once
256 and automatically shared by all the calls. No memory churn to
257 repeatedly allocate the same string objects over and over again.</p>
258 </div>
259 </div>
260 <div id="section4" class="doctools_section"><h2><a name="section4">Authors</a></h2>
261 <p>Andreas Kupries</p>
262 </div>
263 <div id="section5" class="doctools_section"><h2><a name="section5">Bugs, Ideas, Feedback</a></h2>
264 <p>This document, and the package it describes, will undoubtedly contain
265 bugs and other problems.
266 Please report such at <a href="https://github.com/andreas-kupries/critcl">https://github.com/andreas-kupries/critcl</a>.
267 Please also report any ideas for enhancements you may have for either
268 package and/or documentation.</p>
269 </div>
270 <div id="keywords" class="doctools_section"><h2><a name="keywords">Keywords</a></h2>
271 <p><a href="../index.html#c_code">C code</a>, <a href="../index.html#embedded_c_code">Embedded C Code</a>, <a href="../index.html#code_generator">code generator</a>, <a href="../index.html#compile_run">compile &amp; run</a>, <a href="../index.html#compiler">compiler</a>, <a href="../index.html#dynamic_code_generation">dynamic code generation</a>, <a href="../index.html#dynamic_compilation">dynamic compilation</a>, <a href="../index.html#generate_package">generate package</a>, <a href="../index.html#linker">linker</a>, <a href="../index.html#on_demand_compilation">on demand compilation</a>, <a href="../index.html#on_the_fly_compilation">on-the-fly compilation</a></p>
272 </div>
273 <div id="category" class="doctools_section"><h2><a name="category">Category</a></h2>
274 <p>Glueing/Embedded C code</p>
275 </div>
276 <div id="copyright" class="doctools_section"><h2><a name="copyright">Copyright</a></h2>
277 <p>Copyright &copy; 2011-2024 Andreas Kupries</p>
278 </div>
279 </div></body></html>