| 1 |
<!DOCTYPE html><html><head> |
| 2 |
<meta charset="UTF-8"> |
| 3 |
<meta name="viewport" content="width=device-width, initial-scale=1"> |
| 4 |
<title>critcl_cproc_types - 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_cproc.man' by tcllib/doctools with format 'html' |
| 97 |
--> |
| 98 |
<!-- Copyright &copy; Jean-Claude Wippler -- Copyright &copy; Steve Landers -- Copyright &copy; 2011-2024 Andreas Kupries |
| 99 |
--> |
| 100 |
<!-- critcl_cproc_types.n |
| 101 |
--> |
| 102 |
<body><hr> [ |
| 103 |
<a href="../toc.html">Table Of Contents</a> |
| 104 |
| <a href="../index.html">Keyword Index</a> |
| 105 |
] <hr> |
| 106 |
<div class="doctools"> |
| 107 |
<h1 class="doctools_title">critcl_cproc_types(n) 3.3.1 doc "C Runtime In Tcl (CriTcl)"</h1> |
| 108 |
<div id="name" class="doctools_section"><h2><a name="name">Name</a></h2> |
| 109 |
<p>critcl_cproc_types - CriTcl cproc Type Reference</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">Standard argument types</a></li> |
| 117 |
<li class="doctools_section"><a href="#section3">Standard result types</a></li> |
| 118 |
<li class="doctools_section"><a href="#section4">Advanced: Adding types</a></li> |
| 119 |
<li class="doctools_section"><a href="#section5">Examples</a> |
| 120 |
<ul> |
| 121 |
<li class="doctools_subsection"><a href="#subsection1">A Simple Procedure</a></li> |
| 122 |
<li class="doctools_subsection"><a href="#subsection2">More Builtin Types: Strings</a></li> |
| 123 |
<li class="doctools_subsection"><a href="#subsection3">Custom Types, Introduction</a></li> |
| 124 |
<li class="doctools_subsection"><a href="#subsection4">Custom Types, Semi-trivial</a></li> |
| 125 |
<li class="doctools_subsection"><a href="#subsection5">Custom Types, Support structures</a></li> |
| 126 |
<li class="doctools_subsection"><a href="#subsection6">Custom Types, Results</a></li> |
| 127 |
</ul> |
| 128 |
</li> |
| 129 |
<li class="doctools_section"><a href="#section6">Authors</a></li> |
| 130 |
<li class="doctools_section"><a href="#section7">Bugs, Ideas, Feedback</a></li> |
| 131 |
<li class="doctools_section"><a href="#keywords">Keywords</a></li> |
| 132 |
<li class="doctools_section"><a href="#category">Category</a></li> |
| 133 |
<li class="doctools_section"><a href="#copyright">Copyright</a></li> |
| 134 |
</ul> |
| 135 |
</div> |
| 136 |
<div id="synopsis" class="doctools_section"><h2><a name="synopsis">Synopsis</a></h2> |
| 137 |
<div class="doctools_synopsis"> |
| 138 |
<ul class="doctools_requirements"> |
| 139 |
<li>package require <b class="pkgname">Tcl 8.6</b></li> |
| 140 |
<li>package require <b class="pkgname">critcl <span class="opt">?3.3.1?</span></b></li> |
| 141 |
</ul> |
| 142 |
<ul class="doctools_syntax"> |
| 143 |
<li><a href="#1"><b class="cmd">::critcl::has-resulttype</b> <i class="arg">name</i></a></li> |
| 144 |
<li><a href="#2"><b class="cmd">::critcl::resulttype</b> <i class="arg">name</i> <i class="arg">body</i> <span class="opt">?<i class="arg">ctype</i>?</span></a></li> |
| 145 |
<li><a href="#3"><b class="cmd">::critcl::resulttype</b> <i class="arg">name</i> <b class="method">=</b> <i class="arg">origname</i></a></li> |
| 146 |
<li><a href="#4"><b class="cmd">::critcl::has-argtype</b> <i class="arg">name</i></a></li> |
| 147 |
<li><a href="#5"><b class="cmd">::critcl::argtype</b> <i class="arg">name</i> <i class="arg">body</i> <span class="opt">?<i class="arg">ctype</i>?</span> <span class="opt">?<i class="arg">ctypefun</i>?</span></a></li> |
| 148 |
<li><a href="#6"><b class="cmd">::critcl::argtype</b> <i class="arg">name</i> <b class="method">=</b> <i class="arg">origname</i></a></li> |
| 149 |
<li><a href="#7"><b class="cmd">::critcl::argtypesupport</b> <i class="arg">name</i> <i class="arg">code</i> <span class="opt">?<i class="arg">guard</i>?</span></a></li> |
| 150 |
<li><a href="#8"><b class="cmd">::critcl::argtyperelease</b> <i class="arg">name</i> <i class="arg">code</i></a></li> |
| 151 |
</ul> |
| 152 |
</div> |
| 153 |
</div> |
| 154 |
<div id="section1" class="doctools_section"><h2><a name="section1">Description</a></h2> |
| 155 |
<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 |
| 156 |
code from within <a href="http://core.tcl-lang.org/tcl">Tcl</a> scripts.</p> |
| 157 |
<p>This document is a breakout of the descriptions for the predefined argument- and result-types usable |
| 158 |
with the <b class="cmd">critcl::cproc</b> command, as detailed in the reference manpage for the <b class="package"><a href="critcl.html">critcl</a></b> |
| 159 |
package, plus the information on how to extend the predefined set with custom types. The breakout |
| 160 |
was made to make this information easier to find (toplevel document vs. having to search the large |
| 161 |
main reference).</p> |
| 162 |
<p>Its intended audience are developers wishing to write Tcl packages with embedded C code.</p> |
| 163 |
</div> |
| 164 |
<div id="section2" class="doctools_section"><h2><a name="section2">Standard argument types</a></h2> |
| 165 |
<p>Before going into the details first a quick overview:</p> |
| 166 |
<pre class="doctools_example"> |
| 167 |
CriTcl type | C type | Tcl type | Notes |
| 168 |
---------------- | -------------- | --------- | ------------------------------ |
| 169 |
Tcl_Interp* | Tcl_Interp* | n/a | <em>Special</em>, only first |
| 170 |
---------------- | -------------- | --------- | ------------------------------ |
| 171 |
Tcl_Obj* | Tcl_Obj* | Any | <em>Read-only</em> |
| 172 |
object | | | Alias of <b class="type">Tcl_Obj*</b> above |
| 173 |
list | critcl_list | List | <em>Read-only</em> |
| 174 |
[], [*] | | | Alias of <b class="type">list</b> above |
| 175 |
---------------- | -------------- | --------- | ------------------------------ |
| 176 |
[N] | | | Restricted <b class="type">list</b>-types. |
| 177 |
type[], type[N] | | | Length-limited ([..]), expected |
| 178 |
[]type, [N]type | | | element type, or both. |
| 179 |
| | | |
| 180 |
| | | Element types can be all known argument |
| 181 |
| | | types, except for any kind of list. |
| 182 |
| | | IOW multi-dimensional lists are not |
| 183 |
| | | supported. |
| 184 |
---------------- | -------------- | --------- | ------------------------------ |
| 185 |
char* | const char* | Any | <em>Read-only</em>, <em>string rep</em> |
| 186 |
pstring | critcl_pstring | Any | <em>Read-only</em> |
| 187 |
bytes | critcl_bytes | ByteArray | <em>Read-only</em> |
| 188 |
---------------- | -------------- | --------- | ------------------------------ |
| 189 |
int | int | Int | |
| 190 |
long | long | Long | |
| 191 |
wideint | Tcl_WideInt | WideInt | |
| 192 |
double | double | Double | |
| 193 |
float | float | Double | |
| 194 |
---------------- | -------------- | --------- | ------------------------------ |
| 195 |
X > N | | | For X in <b class="type">int</b> ... <b class="type">float</b> above. |
| 196 |
X >= N | | | The C types are as per the base type X. |
| 197 |
X < N | | | N, A, B are expected to be constant integer |
| 198 |
X <= N | | | numbers for types <b class="type">int</b>, <b class="type">long</b>, |
| 199 |
X > A < B | | | and <b class="type">wideint</b>. For types <b class="type">double</b> |
| 200 |
etc. | | | and <b class="type">float</b> the N, A, and B can be floating |
| 201 |
| | | point numbers. Multiple restrictions are |
| 202 |
| | | fused as much as possible to yield at most |
| 203 |
| | | both upper and lower limits. |
| 204 |
---------------- | -------------- | --------- | ------------------------------ |
| 205 |
boolean | int | Boolean | |
| 206 |
bool | | | Alias of <b class="type">boolean</b> above |
| 207 |
---------------- | -------------- | --------- | ------------------------------ |
| 208 |
channel | Tcl_Channel | String | Assumed to be registered |
| 209 |
unshared-channel | Tcl_Channel | String | As above, limited to current interpreter |
| 210 |
take-channel | Tcl_Channel | String | As above, C code takes ownership |
| 211 |
</pre> |
| 212 |
<p>And now the details:</p> |
| 213 |
<dl class="doctools_definitions"> |
| 214 |
<dt>Tcl_Interp*</dt> |
| 215 |
<dd><p><em>Attention</em>: This is a <em>special</em> argument type. It can |
| 216 |
<em>only</em> be used by the <em>first</em> argument of a function. |
| 217 |
Any other argument using it will cause critcl to throw an error.</p> |
| 218 |
<p>When used, the argument will contain a reference to the current |
| 219 |
interpreter that the function body may use. Furthermore the argument |
| 220 |
will <em>not</em> be an argument of the Tcl command for the function.</p> |
| 221 |
<p>This is useful when the function has to do more than simply |
| 222 |
returning a value. Examples would be setting up error messages on |
| 223 |
failure, or querying the interpreter for variables and other data.</p></dd> |
| 224 |
<dt>Tcl_Obj*</dt> |
| 225 |
<dd></dd> |
| 226 |
<dt>object</dt> |
| 227 |
<dd><p>The function takes an argument of type <b class="type">Tcl_Obj*</b>. |
| 228 |
No argument checking is done. |
| 229 |
The Tcl level word is passed to the argument as-is. |
| 230 |
Note that this value must be treated as <em>read-only</em> (except for |
| 231 |
hidden changes to its intrep, i.e. <i class="term">shimmering</i>).</p></dd> |
| 232 |
<dt>pstring</dt> |
| 233 |
<dd><p>The function takes an argument of type <b class="type">critcl_pstring</b> |
| 234 |
containing the original <b class="type">Tcl_Obj*</b> reference of the Tcl argument, |
| 235 |
plus the length of the string and a pointer to the character array.</p> |
| 236 |
<pre class="doctools_example"> |
| 237 |
typedef struct critcl_pstring { |
| 238 |
Tcl_Obj* o; |
| 239 |
const char* s; |
| 240 |
int len; |
| 241 |
} critcl_pstring; |
| 242 |
</pre> |
| 243 |
<p>Note the <em>const</em>. The string is <em>read-only</em>. Any |
| 244 |
modification can have arbitrary effects, from pulling out the rug |
| 245 |
under the script because of string value and internal representation |
| 246 |
not matching anymore, up to crashes anytime later.</p></dd> |
| 247 |
<dt>list</dt> |
| 248 |
<dd></dd> |
| 249 |
<dt>[]</dt> |
| 250 |
<dd></dd> |
| 251 |
<dt>[*]</dt> |
| 252 |
<dd><p>The function takes an argument of type <b class="type">critcl_list</b> containing the original <b class="type">Tcl_Obj*</b> |
| 253 |
reference of the Tcl argument, plus the length of the Tcl list and a pointer to the array of the |
| 254 |
list elements.</p> |
| 255 |
<pre class="doctools_example"> |
| 256 |
typedef struct critcl_list { |
| 257 |
Tcl_Obj* o; |
| 258 |
Tcl_Obj* const* v; |
| 259 |
int c; |
| 260 |
} critcl_list; |
| 261 |
</pre> |
| 262 |
<p>The Tcl argument must be convertible to <b class="type">List</b>, an error is thrown otherwise.</p> |
| 263 |
<p>Note the <em>const</em>. The list is <em>read-only</em>. Any modification can have arbitrary |
| 264 |
effects, from pulling out the rug under the script because of string value and internal |
| 265 |
representation not matching anymore, up to crashes anytime later.</p> |
| 266 |
<p>Further note that the system understands a number of more complex syntactical forms which all |
| 267 |
translate into forms of lists under the hood, as described by the following points.</p></dd> |
| 268 |
<dt>[N]</dt> |
| 269 |
<dd><p>A <i class="term">list</i> type with additional checks limiting the length to <b class="const">N</b>, an integer |
| 270 |
number greater than zero.</p></dd> |
| 271 |
<dt>[]type</dt> |
| 272 |
<dd></dd> |
| 273 |
<dt>type[]</dt> |
| 274 |
<dd><p>A <i class="term">list</i> type whose elements all have to be convertible for <i class="term">type</i>. All known |
| 275 |
types, including user-defined, are allowed, except for <b class="type">list</b> and derivates. In other |
| 276 |
words, multi-dimensional lists are not supported.</p> |
| 277 |
<p>The function will take a structure argument of the general form</p> |
| 278 |
<pre class="doctools_example"> |
| 279 |
typedef struct critcl_list_... { |
| 280 |
Tcl_Obj* o; |
| 281 |
int c; |
| 282 |
(Ctype)* v; |
| 283 |
} critcl_list_...; |
| 284 |
</pre> |
| 285 |
<p>where <b class="const">(Ctype)</b> represents the C type for values of type <b class="type">type</b>.</p></dd> |
| 286 |
<dt>[N]type</dt> |
| 287 |
<dd></dd> |
| 288 |
<dt>type[N]</dt> |
| 289 |
<dd><p>These are <b class="type">list</b> types combining the elements of</p> |
| 290 |
<pre class="doctools_example">[N]</pre> |
| 291 |
<p>and</p> |
| 292 |
<pre class="doctools_example">[]type</pre> |
| 293 |
<p>.</p> |
| 294 |
<p>As an example, the specification of</p> |
| 295 |
<pre class="doctools_example">int[3] a</pre> |
| 296 |
<p>describes argument <i class="arg">a</i> |
| 297 |
as a list of exactly 3 elements, all of which have to be of type <b class="type">int</b>.</p> |
| 298 |
<p>Note that this example can also be written in the more C-like form of</p> |
| 299 |
<pre class="doctools_example">int a[3]</pre> |
| 300 |
<p>. The system will translate this internally to the first shown form.</p></dd> |
| 301 |
<dt>bytes</dt> |
| 302 |
<dd><p>This is the <em>new</em> and usable <b class="type">ByteArray</b> type.</p> |
| 303 |
<p>The function takes an argument of type <b class="type">critcl_bytes</b> |
| 304 |
containing the original <b class="type">Tcl_Obj*</b> reference of the Tcl argument, |
| 305 |
plus the length of the byte array and a pointer to the byte data.</p> |
| 306 |
<pre class="doctools_example"> |
| 307 |
typedef struct critcl_bytes { |
| 308 |
Tcl_Obj* o; |
| 309 |
const unsigned char* s; |
| 310 |
int len; |
| 311 |
} critcl_list; |
| 312 |
</pre> |
| 313 |
<p>The Tcl argument must be convertible to <b class="type">ByteArray</b>, an error is |
| 314 |
thrown otherwise.</p> |
| 315 |
<p>Note the <em>const</em>. The bytes are <em>read-only</em>. Any |
| 316 |
modification can have arbitrary effects, from pulling out the rug |
| 317 |
under the script because of string value and internal representation |
| 318 |
not matching anymore, up to crashes anytime later.</p></dd> |
| 319 |
<dt>char*</dt> |
| 320 |
<dd><p>The function takes an argument of type <b class="type">const char*</b>. |
| 321 |
The string representation of the Tcl argument is passed in.</p> |
| 322 |
<p>Note the <em>const</em>. The string is <em>read-only</em>. Any |
| 323 |
modification can have arbitrary effects, from pulling out the rug |
| 324 |
under the script because of string value and internal representation |
| 325 |
not matching anymore, up to crashes anytime later.</p></dd> |
| 326 |
<dt>double</dt> |
| 327 |
<dd><p>The function takes an argument of type <b class="type">double</b>. |
| 328 |
The Tcl argument must be convertible to <b class="type">Double</b>, an error is thrown otherwise.</p></dd> |
| 329 |
<dt>double > N</dt> |
| 330 |
<dd></dd> |
| 331 |
<dt>double >= N</dt> |
| 332 |
<dd></dd> |
| 333 |
<dt>double < N</dt> |
| 334 |
<dd></dd> |
| 335 |
<dt>double <= N</dt> |
| 336 |
<dd><p>These are variants of <i class="term">double</i> above, restricting the argument value to the shown relation. |
| 337 |
An error is thrown for Tcl arguments outside of the specified range.</p> |
| 338 |
<p>The limiter <i class="arg">N</i> has to be a constant floating point value.</p> |
| 339 |
<p>It is possible to use multiple limiters. |
| 340 |
For example <i class="term">double > A > B <= C</i>. |
| 341 |
The system will fuse them to a single upper/lower limit (or both).</p> |
| 342 |
<p>The system will reject limits describing an empty range of values, or a range containing only |
| 343 |
a single value.</p></dd> |
| 344 |
<dt>float</dt> |
| 345 |
<dd><p>The function takes an argument of type <b class="type">float</b>. |
| 346 |
The Tcl argument must be convertible to <b class="type">Double</b>, an error is thrown otherwise.</p></dd> |
| 347 |
<dt>float > N</dt> |
| 348 |
<dd></dd> |
| 349 |
<dt>float >= N</dt> |
| 350 |
<dd></dd> |
| 351 |
<dt>float < N</dt> |
| 352 |
<dd></dd> |
| 353 |
<dt>float <= N</dt> |
| 354 |
<dd><p>These are variants of <i class="term">float</i> above, restricting the argument value to the shown relation. |
| 355 |
An error is thrown for Tcl arguments outside of the specified range.</p> |
| 356 |
<p>The limiter <i class="arg">N</i> has to be a constant floating point value.</p> |
| 357 |
<p>It is possible to use multiple limiters. |
| 358 |
For example <i class="term">float > A > B <= C</i>. |
| 359 |
The system will fuse them to a single upper/lower limit (or both).</p> |
| 360 |
<p>The system will reject limits describing an empty range of values, or a range containing only |
| 361 |
a single value.</p></dd> |
| 362 |
<dt>boolean</dt> |
| 363 |
<dd></dd> |
| 364 |
<dt>bool</dt> |
| 365 |
<dd><p>The function takes an argument of type <b class="type">int</b>. |
| 366 |
The Tcl argument must be convertible to <b class="type">Boolean</b>, an error is |
| 367 |
thrown otherwise.</p></dd> |
| 368 |
<dt>channel</dt> |
| 369 |
<dd><p>The function takes an argument of type <b class="type">Tcl_Channel</b>. |
| 370 |
The Tcl argument must be convertible to type <b class="type">Channel</b>, an error |
| 371 |
is thrown otherwise. |
| 372 |
The channel is further assumed to be <em>already registered</em> |
| 373 |
with the interpreter.</p></dd> |
| 374 |
<dt>unshared-channel</dt> |
| 375 |
<dd><p>This type is an extension of <b class="type">channel</b> above. |
| 376 |
All of the information above applies.</p> |
| 377 |
<p>Beyond that the channel must not be shared by multiple |
| 378 |
interpreters, an error is thrown otherwise.</p></dd> |
| 379 |
<dt>take-channel</dt> |
| 380 |
<dd><p>This type is an extension of <b class="type">unshared-channel</b> above. |
| 381 |
All of the information above applies.</p> |
| 382 |
<p>Beyond that the code removes the channel from the current |
| 383 |
interpreter without closing it, and disables all pre-existing event |
| 384 |
handling for it.</p> |
| 385 |
<p>With this the function takes full ownership of the channel in |
| 386 |
question, taking it away from the interpreter invoking it. It is then |
| 387 |
responsible for the lifecycle of the channel, up to and including |
| 388 |
closing it.</p> |
| 389 |
<p>Should the system the function is a part of wish to return |
| 390 |
control of the channel back to the interpeter it then has to use the |
| 391 |
result type <b class="type">return-channel</b>. This will undo the registration |
| 392 |
changes made by this argument type. |
| 393 |
<em>Note</em> however that the removal of pre-existing event handling |
| 394 |
done here cannot be undone.</p> |
| 395 |
<p><em>Attention</em> Removal from the interpreter without closing |
| 396 |
the channel is effected by incrementing the channel's reference count |
| 397 |
without providing an interpreter, before decrementing the same for the |
| 398 |
current interpreter. This leaves the overall reference count intact |
| 399 |
without causing Tcl to close it when it is removed from the |
| 400 |
interpreter structures. At this point the channel is effectively a |
| 401 |
globally-owned part of the system not associated with any interpreter.</p> |
| 402 |
<p>The complementary result type then runs this sequence in |
| 403 |
reverse. And if the channel is never returned to Tcl either the |
| 404 |
function or the system it is a part of have to unregister the global |
| 405 |
reference when they are done with it.</p></dd> |
| 406 |
<dt>int</dt> |
| 407 |
<dd><p>The function takes an argument of type <b class="type">int</b>. |
| 408 |
The Tcl argument must be convertible to <b class="type">Int</b>, an error is thrown otherwise.</p></dd> |
| 409 |
<dt>int > N</dt> |
| 410 |
<dd></dd> |
| 411 |
<dt>int >= N</dt> |
| 412 |
<dd></dd> |
| 413 |
<dt>int < N</dt> |
| 414 |
<dd></dd> |
| 415 |
<dt>int <= N</dt> |
| 416 |
<dd><p>These are variants of <i class="term">int</i> above, restricting the argument value to the shown |
| 417 |
relation. |
| 418 |
An error is thrown for Tcl arguments outside of the specified range.</p> |
| 419 |
<p>The limiter <i class="arg">N</i> has to be a constant integer value.</p> |
| 420 |
<p>It is possible to use multiple limiters. |
| 421 |
For example <i class="term">int > A > B <= C</i>. |
| 422 |
The system will fuse them to a single upper/lower limit (or both).</p> |
| 423 |
<p>The system will reject limits describing an empty range of values, or a range |
| 424 |
containing only a single value.</p></dd> |
| 425 |
<dt>long</dt> |
| 426 |
<dd><p>The function takes an argument of type <b class="type">long int</b>. |
| 427 |
The Tcl argument must be convertible to <b class="type">Long</b>, an error is thrown otherwise.</p></dd> |
| 428 |
<dt>long > N</dt> |
| 429 |
<dd></dd> |
| 430 |
<dt>long >= N</dt> |
| 431 |
<dd></dd> |
| 432 |
<dt>long < N</dt> |
| 433 |
<dd></dd> |
| 434 |
<dt>long <= N</dt> |
| 435 |
<dd><p>These are variants of <i class="term">long</i> above, restricting the argument value to the shown |
| 436 |
relation. |
| 437 |
An error is thrown for Tcl arguments outside of the specified range.</p> |
| 438 |
<p>The limiter <i class="arg">N</i> has to be a constant integer value.</p> |
| 439 |
<p>It is possible to use multiple limiters. |
| 440 |
For example <i class="term">long > A > B <= C</i>. |
| 441 |
The system will fuse them to a single upper/lower limit (or both).</p> |
| 442 |
<p>The system will reject limits describing an empty range of values, or a range |
| 443 |
containing only a single value.</p></dd> |
| 444 |
<dt>wideint</dt> |
| 445 |
<dd><p>The function takes an argument of type <b class="type">Tcl_WideInt</b>. |
| 446 |
The Tcl argument must be convertible to <b class="type">WideInt</b>, an error is thrown otherwise.</p></dd> |
| 447 |
<dt>wideint > N</dt> |
| 448 |
<dd></dd> |
| 449 |
<dt>wideint >= N</dt> |
| 450 |
<dd></dd> |
| 451 |
<dt>wideint < N</dt> |
| 452 |
<dd></dd> |
| 453 |
<dt>wideint <= N</dt> |
| 454 |
<dd><p>These are variants of <i class="term">wideint</i> above, restricting the argument value to the shown |
| 455 |
relation. |
| 456 |
An error is thrown for Tcl arguments outside of the specified range.</p> |
| 457 |
<p>The limiter <i class="arg">N</i> has to be a constant integer value.</p> |
| 458 |
<p>It is possible to use multiple limiters. |
| 459 |
For example <i class="term">wideint > A > B <= C</i>. |
| 460 |
The system will fuse them to a single upper/lower limit (or both).</p> |
| 461 |
<p>The system will reject limits describing an empty range of values, or a range |
| 462 |
containing only a single value.</p></dd> |
| 463 |
<dt>void*</dt> |
| 464 |
<dd></dd> |
| 465 |
</dl> |
| 466 |
</div> |
| 467 |
<div id="section3" class="doctools_section"><h2><a name="section3">Standard result types</a></h2> |
| 468 |
<p>Before going into the details first a quick overview:</p> |
| 469 |
<pre class="doctools_example"> |
| 470 |
CriTcl type | C type | Tcl type | Notes |
| 471 |
-------------- | -------------- | --------- | ------------------------------ |
| 472 |
void | n/a | n/a | Always OK. Body sets result |
| 473 |
ok | int | n/a | Result code. Body sets result |
| 474 |
-------------- | -------------- | --------- | ------------------------------ |
| 475 |
int | int | Int | |
| 476 |
boolean | | | Alias of <b class="type">int</b> above |
| 477 |
bool | | | Alias of <b class="type">int</b> above |
| 478 |
long | long | Long | |
| 479 |
wideint | Tcl_WideInt | WideInt | |
| 480 |
double | double | Double | |
| 481 |
float | float | Double | |
| 482 |
-------------- | -------------- | --------- | ------------------------------ |
| 483 |
char* | char* | String | <em>Makes a copy</em> |
| 484 |
vstring | | | Alias of <b class="type">char*</b> above |
| 485 |
const char* | const char* | | Behavior of <b class="type">char*</b> above |
| 486 |
-------------- | -------------- | --------- | ------------------------------ |
| 487 |
string | | String | Freeable string set directly |
| 488 |
| | | <em>No copy is made</em> |
| 489 |
dstring | | | Alias of <b class="type">string</b> above |
| 490 |
-------------- | -------------- | --------- | ------------------------------ |
| 491 |
| | | For all below: Null is ERROR |
| 492 |
| | | Body has to set any message |
| 493 |
Tcl_Obj* | Tcl_Obj* | Any | <em>refcount --</em> |
| 494 |
object | | | Alias of <b class="type">Tcl_Obj*</b> above |
| 495 |
Tcl_Obj*0 | | Any | <em>refcount unchanged</em> |
| 496 |
object0 | | | Alias of <b class="type">Tcl_Obj*0</b> above |
| 497 |
-------------- | -------------- | --------- | ------------------------------ |
| 498 |
known-channel | Tcl_Channel | String | Assumes to already be registered |
| 499 |
new-channel | Tcl_Channel | String | New channel, will be registered |
| 500 |
return-channel | Tcl_Channel | String | Inversion of take-channel |
| 501 |
</pre> |
| 502 |
<p>And now the details:</p> |
| 503 |
<dl class="doctools_definitions"> |
| 504 |
<dt>Tcl_Obj*</dt> |
| 505 |
<dd></dd> |
| 506 |
<dt>object</dt> |
| 507 |
<dd><p>If the returned <b class="type">Tcl_Obj*</b> is <b class="const">NULL</b>, the Tcl return code |
| 508 |
is <b class="const">TCL_ERROR</b> and the function should <a href="https://www.tcl-lang.org/man/tcl/TclLib/SetResult.htm">set an error mesage</a> |
| 509 |
as the interpreter result. Otherwise, the returned <b class="type">Tcl_Obj*</b> is |
| 510 |
set as the interpreter result.</p> |
| 511 |
<p>Note that setting an error message requires the function body |
| 512 |
to have access to the interpreter the function is running in. See the |
| 513 |
argument type <b class="type">Tcl_Interp*</b> for the details on how to make that |
| 514 |
happen.</p> |
| 515 |
<p>Note further that the returned <b class="type">Tcl_Obj*</b> should have a |
| 516 |
reference count greater than <b class="const">0</b>. This is because the converter |
| 517 |
decrements the reference count to release possession after setting the |
| 518 |
interpreter result. It assumes that the function incremented the |
| 519 |
reference count of the returned <b class="type">Tcl_Obj*</b>. |
| 520 |
If a <b class="type">Tcl_Obj*</b> with a reference count of <b class="const">0</b> were |
| 521 |
returned, the reference count would become <b class="const">1</b> when set as the |
| 522 |
interpreter result, and immediately thereafter be decremented to |
| 523 |
<b class="const">0</b> again, causing the memory to be freed. The system is then |
| 524 |
likely to crash at some point after the return due to reuse of the |
| 525 |
freed memory.</p></dd> |
| 526 |
<dt>Tcl_Obj*0</dt> |
| 527 |
<dd></dd> |
| 528 |
<dt>object0</dt> |
| 529 |
<dd><p>Like <b class="const">Tcl_Obj*</b> except that this conversion assumes that the |
| 530 |
returned value has a reference count of <b class="const">0</b> and |
| 531 |
<em>does not</em> decrement it. Returning a value whose reference |
| 532 |
count is greater than <b class="const">0</b> is therefore likely to cause a memory |
| 533 |
leak.</p> |
| 534 |
<p>Note that setting an error message requires the function body |
| 535 |
to have access to the interpreter the function is running in. See the |
| 536 |
argument type <b class="type">Tcl_Interp*</b> for the details on how to make that |
| 537 |
happen.</p></dd> |
| 538 |
<dt>new-channel</dt> |
| 539 |
<dd><p>A <b class="type">String</b> Tcl_Obj holding the name of the returned |
| 540 |
<b class="type">Tcl_Channel</b> is set as the interpreter result. |
| 541 |
The channel is further assumed to be <em>new</em>, and therefore |
| 542 |
registered with the interpreter to make it known.</p></dd> |
| 543 |
<dt>known-channel</dt> |
| 544 |
<dd><p>A <b class="type">String</b> Tcl_Obj holding the name of the returned |
| 545 |
<b class="type">Tcl_Channel</b> is set as the interpreter result. |
| 546 |
The channel is further assumed to be <em>already registered</em> |
| 547 |
with the interpreter.</p></dd> |
| 548 |
<dt>return-channel</dt> |
| 549 |
<dd><p>This type is a variant of <b class="type">new-channel</b> above. |
| 550 |
It varies slightly from it in the registration sequence to be properly |
| 551 |
complementary to the argument type <b class="type">take-channel</b>. |
| 552 |
A <b class="type">String</b> Tcl_Obj holding the name of the returned |
| 553 |
<b class="type">Tcl_Channel</b> is set as the interpreter result. |
| 554 |
The channel is further assumed to be <em>new</em>, and therefore |
| 555 |
registered with the interpreter to make it known.</p></dd> |
| 556 |
<dt>char*</dt> |
| 557 |
<dd></dd> |
| 558 |
<dt>vstring</dt> |
| 559 |
<dd><p>A <b class="type">String</b> Tcl_Obj holding a <em>copy</em> of the returned |
| 560 |
<b class="type">char*</b> is set as the interpreter result. If the value is |
| 561 |
allocated then the function itself and the extension it is a part of |
| 562 |
are responsible for releasing the memory when the data is not in use |
| 563 |
any longer.</p></dd> |
| 564 |
<dt>const char*</dt> |
| 565 |
<dd><p>Like <b class="const">char*</b> above, except that the returned string is |
| 566 |
<b class="const">const</b>-qualified.</p></dd> |
| 567 |
<dt>string</dt> |
| 568 |
<dd></dd> |
| 569 |
<dt>dstring</dt> |
| 570 |
<dd><p>The returned <b class="type">char*</b> is directly set as the interpreter result |
| 571 |
<em>without making a copy</em>. Therefore it must be dynamically |
| 572 |
allocated via <b class="function">Tcl_Alloc</b>. Release happens automatically when the |
| 573 |
Interpreter finds that the value is not required any longer.</p></dd> |
| 574 |
<dt>double</dt> |
| 575 |
<dd></dd> |
| 576 |
<dt>float</dt> |
| 577 |
<dd><p>The returned <b class="type">double</b> or <b class="type">float</b> is converted to a <b class="type">Double</b> |
| 578 |
Tcl_Obj and set as the interpreter result.</p></dd> |
| 579 |
<dt>boolean</dt> |
| 580 |
<dd></dd> |
| 581 |
<dt>bool</dt> |
| 582 |
<dd><p>The returned <b class="type">int</b> value is converted to an <b class="type">Int</b> Tcl_Obj and set as |
| 583 |
the interpreter result.</p></dd> |
| 584 |
<dt>int</dt> |
| 585 |
<dd><p>The returned <b class="type">int</b> value is converted to an <b class="type">Int</b> Tcl_Obj and set as |
| 586 |
the interpreter result.</p></dd> |
| 587 |
<dt>long</dt> |
| 588 |
<dd><p>The returned <b class="type">long int</b> value is converted to a <b class="type">Long</b> Tcl_Obj and |
| 589 |
set as the interpreter result.</p></dd> |
| 590 |
<dt>wideint</dt> |
| 591 |
<dd><p>The returned <b class="type">Tcl_WideInt</b> value is converted to a <b class="type">WideInt</b> Tcl_Obj |
| 592 |
and set as the interpreter result.</p></dd> |
| 593 |
<dt>ok</dt> |
| 594 |
<dd><p>The returned <b class="type">int</b> value becomes the Tcl return code. |
| 595 |
The interpreter result is left untouched and can be set by the |
| 596 |
function if desired. Note that doing this requires the function body |
| 597 |
to have access to the interpreter the function is running in. See the |
| 598 |
argument type <b class="type">Tcl_Interp*</b> for the details on how to make that |
| 599 |
happen.</p></dd> |
| 600 |
<dt>void</dt> |
| 601 |
<dd><p>The function does not return a value. |
| 602 |
The interpreter result is left untouched and can be set by the function if |
| 603 |
desired.</p></dd> |
| 604 |
</dl> |
| 605 |
</div> |
| 606 |
<div id="section4" class="doctools_section"><h2><a name="section4">Advanced: Adding types</a></h2> |
| 607 |
<p>While the <b class="cmd">critcl::cproc</b> command understands the most common C |
| 608 |
types (as per the previous 2 sections), sometimes this is not enough.</p> |
| 609 |
<p>To get around this limitation the commands in this section |
| 610 |
enable users of <b class="package"><a href="critcl.html">critcl</a></b> to extend the set of argument and |
| 611 |
result types understood by <b class="cmd">critcl::cproc</b>. In other words, they |
| 612 |
allow them to define their own, custom, types.</p> |
| 613 |
<dl class="doctools_definitions"> |
| 614 |
<dt><a name="1"><b class="cmd">::critcl::has-resulttype</b> <i class="arg">name</i></a></dt> |
| 615 |
<dd><p>This command tests if the named result-type is known or not. |
| 616 |
It returns a boolean value, <b class="const">true</b> if the type is known and |
| 617 |
<b class="const">false</b> otherwise.</p></dd> |
| 618 |
<dt><a name="2"><b class="cmd">::critcl::resulttype</b> <i class="arg">name</i> <i class="arg">body</i> <span class="opt">?<i class="arg">ctype</i>?</span></a></dt> |
| 619 |
<dd><p>This command defines the result type <i class="arg">name</i>, and associates it |
| 620 |
with the C code doing the conversion (<i class="arg">body</i>) from C to Tcl. |
| 621 |
The C return type of the associated function, also the C type of the |
| 622 |
result variable, is <i class="arg">ctype</i>. This type defaults to <i class="arg">name</i> if |
| 623 |
it is not specified.</p> |
| 624 |
<p>If <i class="arg">name</i> is already declared an error is thrown. |
| 625 |
<em>Attention!</em> The standard result type <b class="const">void</b> is special as |
| 626 |
it has no accompanying result variable. This cannot be expressed |
| 627 |
by this extension command.</p> |
| 628 |
<p>The <i class="arg">body</i>'s responsibility is the conversion of the |
| 629 |
functions result into a Tcl result and a Tcl status. The first has to |
| 630 |
be set into the interpreter we are in, and the second has to be |
| 631 |
returned.</p> |
| 632 |
<p>The C code of <i class="arg">body</i> is guaranteed to be called last in the |
| 633 |
wrapper around the actual implementation of the <b class="cmd">cproc</b> in |
| 634 |
question and has access to the following environment:</p> |
| 635 |
<dl class="doctools_definitions"> |
| 636 |
<dt><b class="variable">interp</b></dt> |
| 637 |
<dd><p>A Tcl_Interp* typed C variable referencing the |
| 638 |
interpreter the result has to be stored into.</p></dd> |
| 639 |
<dt><b class="variable">rv</b></dt> |
| 640 |
<dd><p>The C variable holding the result to convert, of type |
| 641 |
<i class="arg">ctype</i>.</p></dd> |
| 642 |
</dl> |
| 643 |
<p>As examples here are the definitions of two standard result types:</p> |
| 644 |
<pre class="doctools_example"> |
| 645 |
resulttype int { |
| 646 |
Tcl_SetObjResult(interp, Tcl_NewIntObj(rv)); |
| 647 |
return TCL_OK; |
| 648 |
} |
| 649 |
resulttype ok { |
| 650 |
/* interp result must be set by cproc body */ |
| 651 |
return rv; |
| 652 |
} int |
| 653 |
</pre> |
| 654 |
</dd> |
| 655 |
<dt><a name="3"><b class="cmd">::critcl::resulttype</b> <i class="arg">name</i> <b class="method">=</b> <i class="arg">origname</i></a></dt> |
| 656 |
<dd><p>This form of the <b class="cmd">resulttype</b> command declares <i class="arg">name</i> as an |
| 657 |
alias of result type <i class="arg">origname</i>, which has to be defined |
| 658 |
already. If this is not the case an error is thrown.</p></dd> |
| 659 |
<dt><a name="4"><b class="cmd">::critcl::has-argtype</b> <i class="arg">name</i></a></dt> |
| 660 |
<dd><p>This command tests if the named argument-type is known or not. |
| 661 |
It returns a boolean value, <b class="const">true</b> if the type is known and |
| 662 |
<b class="const">false</b> otherwise.</p></dd> |
| 663 |
<dt><a name="5"><b class="cmd">::critcl::argtype</b> <i class="arg">name</i> <i class="arg">body</i> <span class="opt">?<i class="arg">ctype</i>?</span> <span class="opt">?<i class="arg">ctypefun</i>?</span></a></dt> |
| 664 |
<dd><p>This command defines the argument type <i class="arg">name</i>, and associates it |
| 665 |
with the C code doing the conversion (<i class="arg">body</i>) from Tcl to C. |
| 666 |
<i class="arg">ctype</i> is the C type of the variable to hold the conversion result |
| 667 |
and <i class="arg">ctypefun</i> is the type of the function argument itself. |
| 668 |
Both types default to <i class="arg">name</i> if they are the empty string or are not |
| 669 |
provided.</p> |
| 670 |
<p>If <i class="arg">name</i> is already declared an error is thrown.</p> |
| 671 |
<p><i class="arg">body</i> is a C code fragment that converts a Tcl_Obj* into a |
| 672 |
C value which is stored in a helper variable in the underlying function.</p> |
| 673 |
<p><i class="arg">body</i> is called inside its own code block to isolate local |
| 674 |
variables, and the following items are in scope:</p> |
| 675 |
<dl class="doctools_definitions"> |
| 676 |
<dt><b class="variable">interp</b></dt> |
| 677 |
<dd><p>A variable of type <b class="const">Tcl_Interp*</b> which is the |
| 678 |
interpreter the code is running in.</p></dd> |
| 679 |
<dt><b class="const">@@</b></dt> |
| 680 |
<dd><p>A placeholder for an expression that evaluates to the |
| 681 |
<b class="const">Tcl_Obj*</b> to convert.</p></dd> |
| 682 |
<dt><b class="const">@A</b></dt> |
| 683 |
<dd><p>A placeholder for the name of the variable to store the |
| 684 |
converted argument into.</p></dd> |
| 685 |
</dl> |
| 686 |
<p>As examples, here are the definitions of two standard argument types:</p> |
| 687 |
<pre class="doctools_example"> |
| 688 |
argtype int { |
| 689 |
if (Tcl_GetIntFromObj(interp, @@, &@A) != TCL_OK) return TCL_ERROR; |
| 690 |
} |
| 691 |
argtype float { |
| 692 |
double t; |
| 693 |
if (Tcl_GetDoubleFromObj(interp, @@, &t) != TCL_OK) return TCL_ERROR; |
| 694 |
@A = (float) t; |
| 695 |
} |
| 696 |
</pre> |
| 697 |
</dd> |
| 698 |
<dt><a name="6"><b class="cmd">::critcl::argtype</b> <i class="arg">name</i> <b class="method">=</b> <i class="arg">origname</i></a></dt> |
| 699 |
<dd><p>This form of the <b class="cmd">argtype</b> command declares <i class="arg">name</i> as an alias |
| 700 |
of argument type <i class="arg">origname</i>, which has to be defined already. If |
| 701 |
this is not the case an error is thrown.</p></dd> |
| 702 |
<dt><a name="7"><b class="cmd">::critcl::argtypesupport</b> <i class="arg">name</i> <i class="arg">code</i> <span class="opt">?<i class="arg">guard</i>?</span></a></dt> |
| 703 |
<dd><p>This command defines a C code fragment for the already defined |
| 704 |
argument type <i class="arg">name</i> which is inserted before all functions |
| 705 |
using that type. Its purpose is the definition of any supporting C |
| 706 |
types needed by the argument type. |
| 707 |
If the type is used by many functions the system ensures that only the |
| 708 |
first of the multiple insertions of the code fragment is active, and |
| 709 |
the others disabled. |
| 710 |
The guard identifier is normally derived from <i class="arg">name</i>, but can also |
| 711 |
be set explicitly, via <i class="arg">guard</i>. This latter allows different |
| 712 |
custom types to share a common support structure without having to |
| 713 |
perform their own guarding.</p></dd> |
| 714 |
<dt><a name="8"><b class="cmd">::critcl::argtyperelease</b> <i class="arg">name</i> <i class="arg">code</i></a></dt> |
| 715 |
<dd><p>This command defines a C code fragment for the already defined |
| 716 |
argument type <i class="arg">name</i> which is inserted whenever the worker |
| 717 |
function of a <b class="cmd">critcl::cproc</b> returns to the shim. It is the |
| 718 |
responsibility of this fragment to unconditionally release any |
| 719 |
resources the <b class="cmd">critcl::argtype</b> conversion code allocated. |
| 720 |
An example of this are the <em>variadic</em> types for the support of |
| 721 |
the special, variadic <i class="arg">args</i> argument to <b class="cmd">critcl::cproc</b>'s. |
| 722 |
They allocate a C array for the collected arguments which has to be |
| 723 |
released when the worker returns. This command defines the C code |
| 724 |
for doing that.</p></dd> |
| 725 |
</dl> |
| 726 |
</div> |
| 727 |
<div id="section5" class="doctools_section"><h2><a name="section5">Examples</a></h2> |
| 728 |
<p>The examples shown here have been drawn from the section "Embedding C" in the document about |
| 729 |
<i class="term">Using CriTcl</i>. Please see that document for many more examples.</p> |
| 730 |
<div id="subsection1" class="doctools_subsection"><h3><a name="subsection1">A Simple Procedure</a></h3> |
| 731 |
<p>Starting simple, let us assume that the Tcl code in question is |
| 732 |
something like</p> |
| 733 |
<pre class="doctools_example"> |
| 734 |
proc math {x y z} { |
| 735 |
return [expr {(sin($x)*rand())/$y**log($z)}] |
| 736 |
} |
| 737 |
</pre> |
| 738 |
<p>with the expression pretending to be something very complex and |
| 739 |
slow. Converting this to C we get:</p> |
| 740 |
<pre class="doctools_example"> |
| 741 |
critcl::cproc math {double x double y double z} double { |
| 742 |
double up = rand () * sin (x); |
| 743 |
double down = pow(y, log (z)); |
| 744 |
return up/down; |
| 745 |
} |
| 746 |
</pre> |
| 747 |
<p>Notable about this translation:</p> |
| 748 |
<ol class="doctools_enumerated"> |
| 749 |
<li><p>All the arguments got type information added to them, here |
| 750 |
"double". Like in C the type precedes the argument name. Other |
| 751 |
than that it is pretty much a Tcl dictionary, with keys and |
| 752 |
values swapped.</p></li> |
| 753 |
<li><p>We now also have to declare the type of the result, here |
| 754 |
"double", again.</p></li> |
| 755 |
<li><p>The reference manpage lists all the legal C types supported as |
| 756 |
arguments and results.</p></li> |
| 757 |
</ol> |
| 758 |
<p>While the above example was based on type <b class="type">double</b> for |
| 759 |
both arguments and result we have a number of additional types in the |
| 760 |
same category, i.e. simple types. These are:</p> |
| 761 |
<pre class="doctools_example"> |
| 762 |
CriTcl type | C type | Tcl type | Notes |
| 763 |
----------- | -------------- | --------- | ------------------------------ |
| 764 |
bool | | | Alias of <b class="type">boolean</b> below |
| 765 |
boolean | int | Boolean | |
| 766 |
double | double | Double | |
| 767 |
float | float | Double | |
| 768 |
int | int | Int | |
| 769 |
long | long | Long | |
| 770 |
wideint | Tcl_WideInt | WideInt | |
| 771 |
</pre> |
| 772 |
<p>A slightly advanced form of these simple types are a limited |
| 773 |
set of constraints on the argument value. Note that <b class="type">bool</b> and |
| 774 |
alias do not support this.</p> |
| 775 |
<pre class="doctools_example"> |
| 776 |
critcl::cproc sqrt {{double >= 0} x} double { |
| 777 |
return sqrt(x); |
| 778 |
} |
| 779 |
</pre> |
| 780 |
<p>In the example above CriTcl's argument handling will reject |
| 781 |
calling the command with a negative number, without ever invoking the |
| 782 |
C code.</p> |
| 783 |
<p>These constraints are called <em>limited</em> because only |
| 784 |
<b class="const">0</b> and <b class="const">1</b> can be used as the borders, although all the |
| 785 |
operators <b class="const"><</b>, <b class="const"><=</b>, <b class="const">></b>, and <b class="const">>=</b> are |
| 786 |
possible. It is also not possible to combine restrictions.</p> |
| 787 |
</div> |
| 788 |
<div id="subsection2" class="doctools_subsection"><h3><a name="subsection2">More Builtin Types: Strings</a></h3> |
| 789 |
<p>Given that "Everything is a String" is a slogan of Tcl the ability of <b class="cmd">cproc</b>s |
| 790 |
to receive strings as arguments, and return them as results is quite important.</p> |
| 791 |
<p>We actually have a variety of builtin string types, all alike, yet different.</p> |
| 792 |
<p>For arguments we have:</p> |
| 793 |
<pre class="doctools_example"> |
| 794 |
CriTcl type | C type | Tcl type | Notes |
| 795 |
----------- | -------------- | --------- | ------------------------------ |
| 796 |
char* | const char* | Any | <em>Read-only</em>, <em>string rep</em> |
| 797 |
pstring | critcl_pstring | Any | <em>Read-only</em> |
| 798 |
bytes | critcl_bytes | ByteArray | <em>Read-only</em> |
| 799 |
</pre> |
| 800 |
<p>In C</p> |
| 801 |
<pre class="doctools_example"> |
| 802 |
critcl::cproc takeStrings { |
| 803 |
char* cstring |
| 804 |
pstring pstring |
| 805 |
bytes barray |
| 806 |
} void { |
| 807 |
printf ("len %d = %s\n", strlen(cstring), cstring); |
| 808 |
printf ("len %d = %s\n", pstring.len, pstring.s); |
| 809 |
printf ("len %d = %s\n", barray.len, barray.s); |
| 810 |
return; // void result, no result |
| 811 |
} |
| 812 |
</pre> |
| 813 |
<p>Notable about the above:</p> |
| 814 |
<ol class="doctools_enumerated"> |
| 815 |
<li><p>The <b class="variable">cstring</b> is a plain <b class="type">const char*</b>. It <em>points directly</em> |
| 816 |
into the <b class="type">Tcl_Obj*</b> holding the argument in the script.</p></li> |
| 817 |
<li><p>The <b class="variable">pstring</b> is a slight extension to that. The value is actually a structure |
| 818 |
containing the string pointer like <b class="variable">cstring</b> (field <b class="const">.s</b>), the length of the |
| 819 |
string (field <b class="const">.len</b>), and a pointer to the <b class="type">Tcl_Obj*</b> these came from.</p></li> |
| 820 |
<li><p>The last, <b class="variable">barray</b> is like <b class="variable">pstring</b>, however it has ensured that the |
| 821 |
<b class="type">Tcl_Obj*</b> is a Tcl ByteArray, i.e. binary data.</p></li> |
| 822 |
</ol> |
| 823 |
<p>Treat all of them as <em>Read Only</em>. Do not modify ever.</p> |
| 824 |
<p>On the other side, string results, we have:</p> |
| 825 |
<pre class="doctools_example"> |
| 826 |
CriTcl type | C type | Tcl type | Notes |
| 827 |
------------- | -------------- | --------- | ------------------------------ |
| 828 |
char* | char* | String | <em>Makes a copy</em> |
| 829 |
vstring | | | Alias of <b class="type">char*</b> above |
| 830 |
const char* | const char* | | Behavior of <b class="type">char*</b> above |
| 831 |
------------- | -------------- | --------- | ------------------------------ |
| 832 |
string | char* | String | Freeable string set directly |
| 833 |
| | | <em>No copy is made</em> |
| 834 |
dstring | | | Alias of <b class="type">string</b> above |
| 835 |
</pre> |
| 836 |
<pre class="doctools_example"> |
| 837 |
critcl::cproc returnCString {} char* { |
| 838 |
return "a string"; |
| 839 |
} |
| 840 |
critcl::cproc returnString {} string { |
| 841 |
char* str = Tcl_Alloc (200); |
| 842 |
sprintf (str, "hello world"); |
| 843 |
return str; |
| 844 |
} |
| 845 |
</pre> |
| 846 |
<p>Notable about the above:</p> |
| 847 |
<ol class="doctools_enumerated"> |
| 848 |
<li><p>The type <b class="type">char*</b> is best used for static strings, or strings in some kind |
| 849 |
fixed buffer.</p> |
| 850 |
<p>CriTcl's translation layer makes a copy of it for the result of the command. While |
| 851 |
it is possible to return heap-allocated strings it is the C code who is responsible for |
| 852 |
freeing such at some point. If that is not done they will leak.</p></li> |
| 853 |
<li><p>The type <b class="type">string</b> on the other hand is exactly for returning strings allocated |
| 854 |
with <b class="function">Tcl_Alloc</b> and associates.</p> |
| 855 |
<p>For these the translation layer makes no copy at all, and sets them directly as the |
| 856 |
result of the command. A <em>very important effect</em> of this is that the ownership of |
| 857 |
the string pointer moves from the function to Tcl.</p> |
| 858 |
<p><em>Tcl</em> will release the allocated memory when it does not need it any |
| 859 |
longer. The C code has no say in that.</p></li> |
| 860 |
</ol> |
| 861 |
</div> |
| 862 |
<div id="subsection3" class="doctools_subsection"><h3><a name="subsection3">Custom Types, Introduction</a></h3> |
| 863 |
<p>When writing bindings to external libraries <b class="cmd">critcl::cproc</b> is |
| 864 |
usually the most convenient way of writing the lower layers. This is |
| 865 |
however hampered by the fact that critcl on its own only supports a |
| 866 |
few standard (arguably the most import) standard types, whereas the |
| 867 |
functions we wish to bind most certainly will use much more, specific |
| 868 |
to the library's function.</p> |
| 869 |
<p>The critcl commands <b class="cmd">argtype</b>, <b class="cmd">resulttype</b> and their |
| 870 |
adjuncts are provided to help here, by allowing a developer to extend |
| 871 |
critcl's type system with custom conversions.</p> |
| 872 |
<p>This and the three following sections will demonstrate this, |
| 873 |
from trivial to complex.</p> |
| 874 |
<p>The most trivial use is to create types which are aliases of |
| 875 |
existing types, standard or other. As an alias it simply copies and |
| 876 |
uses the conversion code from the referenced types.</p> |
| 877 |
<p>Our example is pulled from an incomplete project of mine, a |
| 878 |
binding to <i class="term">Jeffrey Kegler</i>'s <i class="term">libmarpa</i> library managing |
| 879 |
Earley parsers. Several custom types simply reflect the typedef's done |
| 880 |
by the library, to make the <b class="cmd">critcl::cproc</b>s as self-documenting |
| 881 |
as the underlying library functions themselves.</p> |
| 882 |
<pre class="doctools_example"> |
| 883 |
critcl::argtype Marpa_Symbol_ID = int |
| 884 |
critcl::argtype Marpa_Rule_ID = int |
| 885 |
critcl::argtype Marpa_Rule_Int = int |
| 886 |
critcl::argtype Marpa_Rank = int |
| 887 |
critcl::argtype Marpa_Earleme = int |
| 888 |
critcl::argtype Marpa_Earley_Set_ID = int |
| 889 |
... |
| 890 |
method sym-rank: proc { |
| 891 |
Marpa_Symbol_ID sym |
| 892 |
Marpa_Rank rank |
| 893 |
} Marpa_Rank { |
| 894 |
return marpa_g_symbol_rank_set (instance->grammar, sym, rank); |
| 895 |
} |
| 896 |
... |
| 897 |
</pre> |
| 898 |
</div> |
| 899 |
<div id="subsection4" class="doctools_subsection"><h3><a name="subsection4">Custom Types, Semi-trivial</a></h3> |
| 900 |
<p>A more involved custom argument type would be to map from Tcl strings |
| 901 |
to some internal representation, like an integer code.</p> |
| 902 |
<p>The first example is taken from the <b class="package">tclyaml</b> package, |
| 903 |
a binding to the <b class="package">libyaml</b> library. In a few places we have to |
| 904 |
map readable names for block styles, scalar styles, etc. to the |
| 905 |
internal enumeration.</p> |
| 906 |
<pre class="doctools_example"> |
| 907 |
critcl::argtype yaml_sequence_style_t { |
| 908 |
if (!encode_sequence_style (interp, @@, &@A)) return TCL_ERROR; |
| 909 |
} |
| 910 |
... |
| 911 |
critcl::ccode { |
| 912 |
static const char* ty_block_style_names [] = { |
| 913 |
"any", "block", "flow", NULL |
| 914 |
}; |
| 915 |
static int |
| 916 |
encode_sequence_style (Tcl_Interp* interp, Tcl_Obj* style, |
| 917 |
yaml_sequence_style_t* estyle) |
| 918 |
{ |
| 919 |
int value; |
| 920 |
if (Tcl_GetIndexFromObj (interp, style, ty_block_style_names, |
| 921 |
"sequence style", 0, &value) != TCL_OK) { |
| 922 |
return 0; |
| 923 |
} |
| 924 |
*estyle = value; |
| 925 |
return 1; |
| 926 |
} |
| 927 |
} |
| 928 |
... |
| 929 |
method sequence_start proc { |
| 930 |
pstring anchor |
| 931 |
pstring tag |
| 932 |
int implicit |
| 933 |
yaml_sequence_style_t style |
| 934 |
} ok { |
| 935 |
/* Syntax: <instance> seq_start <anchor> <tag> <implicit> <style> */ |
| 936 |
... |
| 937 |
} |
| 938 |
... |
| 939 |
</pre> |
| 940 |
<p>It should be noted that this code precedes the advent of the |
| 941 |
supporting generator package <b class="package"><a href="critcl_emap.html">critcl::emap</a></b>. using the |
| 942 |
generator the definition of the mapping becomes much simpler:</p> |
| 943 |
<pre class="doctools_example"> |
| 944 |
critcl::emap::def yaml_sequence_style_t { |
| 945 |
any 0 |
| 946 |
block 1 |
| 947 |
flow 2 |
| 948 |
} |
| 949 |
</pre> |
| 950 |
<p>Note that the generator will not only provide the conversions, but |
| 951 |
also define the argument and result types needed for their use by |
| 952 |
<b class="cmd">critcl::cproc</b>. |
| 953 |
Another example of such a semi-trivial argument type can be found in |
| 954 |
the <b class="package">CRIMP</b> package, which defines a <b class="type">Tcl_ObjType</b> for |
| 955 |
<i class="term">image</i> values. This not only provides a basic argument type for |
| 956 |
any image, but also derived types which check that the image has a |
| 957 |
specific format. Here we see for the first time non-integer arguments, |
| 958 |
and the need to define the C types used for variables holding the C |
| 959 |
level value, and the type of function parameters (Due to C promotion |
| 960 |
rules we may need different types).</p> |
| 961 |
<pre class="doctools_example"> |
| 962 |
critcl::argtype image { |
| 963 |
if (crimp_get_image_from_obj (interp, @@, &@A) != TCL_OK) { |
| 964 |
return TCL_ERROR; |
| 965 |
} |
| 966 |
} crimp_image* crimp_image* |
| 967 |
... |
| 968 |
set map [list <<type>> $type] |
| 969 |
critcl::argtype image_$type [string map $map { |
| 970 |
if (crimp_get_image_from_obj (interp, @@, &@A) != TCL_OK) { |
| 971 |
return TCL_ERROR; |
| 972 |
} |
| 973 |
if (@A->itype != crimp_imagetype_find ("crimp::image::<<type>>")) { |
| 974 |
Tcl_SetObjResult (interp, |
| 975 |
Tcl_NewStringObj ("expected image type <<type>>", |
| 976 |
-1)); |
| 977 |
return TCL_ERROR; |
| 978 |
} |
| 979 |
}] crimp_image* crimp_image* |
| 980 |
... |
| 981 |
</pre> |
| 982 |
</div> |
| 983 |
<div id="subsection5" class="doctools_subsection"><h3><a name="subsection5">Custom Types, Support structures</a></h3> |
| 984 |
<p>The adjunct command <b class="cmd">critcl::argtypesupport</b> is for when the |
| 985 |
conversion needs additional definitions, for example a helper |
| 986 |
structure.</p> |
| 987 |
<p>An example of this can be found among the standard types of |
| 988 |
critcl itself, the <b class="type">pstring</b> type. This type provides the C |
| 989 |
function with not only the string pointer, but also the string length, |
| 990 |
and the <b class="type">Tcl_Obj*</b> this data came from. As <b class="cmd">critcl::cproc</b>'s |
| 991 |
calling conventions allow us only one argument for the data of the |
| 992 |
parameter a structure is needed to convey these three pieces of |
| 993 |
information.</p> |
| 994 |
<p>Thus the argument type is defined as</p> |
| 995 |
<pre class="doctools_example"> |
| 996 |
critcl::argtype pstring { |
| 997 |
@A.s = Tcl_GetStringFromObj(@@, &(@A.len)); |
| 998 |
@A.o = @@; |
| 999 |
} critcl_pstring critcl_pstring |
| 1000 |
critcl::argtypesupport pstring { |
| 1001 |
typedef struct critcl_pstring { |
| 1002 |
Tcl_Obj* o; |
| 1003 |
const char* s; |
| 1004 |
int len; |
| 1005 |
} critcl_pstring; |
| 1006 |
} |
| 1007 |
</pre> |
| 1008 |
<p>In the case of such a structure being large we may wish to |
| 1009 |
allocate it on the heap instead of having it taking space on the |
| 1010 |
stack. If we do that we need another adjunct command, |
| 1011 |
<b class="cmd">critcl::argtyperelease</b>. This command specifies the code required |
| 1012 |
to release dynamically allocated resources when the worker function |
| 1013 |
returns, before the shim returns to the caller in Tcl. |
| 1014 |
To keep things simple our example is synthetic, a modification of |
| 1015 |
<b class="const">pstring</b> above, to demonstrate the technique. An actual, but |
| 1016 |
more complex example is the code to support the variadic <i class="arg">args</i> |
| 1017 |
argument of <b class="cmd">critcl::cproc</b>.</p> |
| 1018 |
<pre class="doctools_example"> |
| 1019 |
critcl::argtype pstring { |
| 1020 |
@A = (critcl_pstring*) ckalloc(sizeof(critcl_pstring)); |
| 1021 |
@A->s = Tcl_GetStringFromObj(@@, &(@A->len)); |
| 1022 |
@A->o = @@; |
| 1023 |
} critcl_pstring* critcl_pstring* |
| 1024 |
critcl::argtypesupport pstring { |
| 1025 |
typedef struct critcl_pstring { |
| 1026 |
Tcl_Obj* o; |
| 1027 |
const char* s; |
| 1028 |
int len; |
| 1029 |
} critcl_pstring; |
| 1030 |
} |
| 1031 |
critcl::argtyperelease pstring { |
| 1032 |
ckfree ((char*)) @A); |
| 1033 |
} |
| 1034 |
</pre> |
| 1035 |
<p>Note, the above example shows only the most simple case of an |
| 1036 |
allocated argument, with a conversion that cannot fail (namely, string |
| 1037 |
retrieval). If the conversion can fail then either the allocation has |
| 1038 |
to be defered to happen only on successful conversion, or the |
| 1039 |
conversion code has to release the allocated memory itself in the |
| 1040 |
failure path, because it will never reach the code defined via |
| 1041 |
<b class="cmd">critcl::argtyperelease</b> in that case.</p> |
| 1042 |
</div> |
| 1043 |
<div id="subsection6" class="doctools_subsection"><h3><a name="subsection6">Custom Types, Results</a></h3> |
| 1044 |
<p>All of the previous sections dealt with argument conversions, |
| 1045 |
i.e. going from Tcl into C. |
| 1046 |
Custom result types are for the reverse direction, from C to Tcl. |
| 1047 |
This is usually easier, as most of the time errors should not be |
| 1048 |
possible. Supporting structures, or allocating them on the heap are |
| 1049 |
not really required and therefore not supported.</p> |
| 1050 |
<p>The example of a result type shown below was pulled from |
| 1051 |
<b class="package">KineTcl</b>. It is a variant of the builtin result type |
| 1052 |
<b class="type">Tcl_Obj*</b>, aka <b class="type">object</b>. The builtin conversion assumes |
| 1053 |
that the object returned by the function has a refcount of 1 (or |
| 1054 |
higher), with the function having held the reference, and releases |
| 1055 |
that reference after placing the value into the interp result. The |
| 1056 |
conversion below on the other hand assumes that the value has a |
| 1057 |
refcount of 0 and thus that decrementing it is forbidden, lest it be |
| 1058 |
released much to early, and crashing the system.</p> |
| 1059 |
<pre class="doctools_example"> |
| 1060 |
critcl::resulttype KTcl_Obj* { |
| 1061 |
if (rv == NULL) { return TCL_ERROR; } |
| 1062 |
Tcl_SetObjResult(interp, rv); |
| 1063 |
/* No refcount adjustment */ |
| 1064 |
return TCL_OK; |
| 1065 |
} Tcl_Obj* |
| 1066 |
</pre> |
| 1067 |
<p>This type of definition is also found in <b class="package">Marpa</b> and recent |
| 1068 |
hacking hacking on <b class="package">CRIMP</b> introduced it there as well. Which |
| 1069 |
is why this definition became a builtin type starting with version |
| 1070 |
3.1.16, under the names <b class="type">Tcl_Obj*0</b> and <b class="type">object0</b>.</p> |
| 1071 |
<p>Going back to errors and their handling, of course, if a |
| 1072 |
function we are wrapping signals them in-band, then the conversion of |
| 1073 |
such results has to deal with that. This happens for example in |
| 1074 |
<b class="package">KineTcl</b>, where we find</p> |
| 1075 |
<pre class="doctools_example"> |
| 1076 |
critcl::resulttype XnStatus { |
| 1077 |
if (rv != XN_STATUS_OK) { |
| 1078 |
Tcl_AppendResult (interp, xnGetStatusString (rv), NULL); |
| 1079 |
return TCL_ERROR; |
| 1080 |
} |
| 1081 |
return TCL_OK; |
| 1082 |
} |
| 1083 |
critcl::resulttype XnDepthPixel { |
| 1084 |
if (rv == ((XnDepthPixel) -1)) { |
| 1085 |
Tcl_AppendResult (interp, |
| 1086 |
"Inheritance error: Not a depth generator", |
| 1087 |
NULL); |
| 1088 |
return TCL_ERROR; |
| 1089 |
} |
| 1090 |
Tcl_SetObjResult (interp, Tcl_NewIntObj (rv)); |
| 1091 |
return TCL_OK; |
| 1092 |
} |
| 1093 |
</pre> |
| 1094 |
</div> |
| 1095 |
</div> |
| 1096 |
<div id="section6" class="doctools_section"><h2><a name="section6">Authors</a></h2> |
| 1097 |
<p>Jean Claude Wippler, Steve Landers, Andreas Kupries</p> |
| 1098 |
</div> |
| 1099 |
<div id="section7" class="doctools_section"><h2><a name="section7">Bugs, Ideas, Feedback</a></h2> |
| 1100 |
<p>This document, and the package it describes, will undoubtedly contain |
| 1101 |
bugs and other problems. |
| 1102 |
Please report them at <a href="https://github.com/andreas-kupries/critcl/issues">https://github.com/andreas-kupries/critcl/issues</a>. |
| 1103 |
Ideas for enhancements you may have for either package, application, |
| 1104 |
and/or the documentation are also very welcome and should be reported |
| 1105 |
at <a href="https://github.com/andreas-kupries/critcl/issues">https://github.com/andreas-kupries/critcl/issues</a> as well.</p> |
| 1106 |
</div> |
| 1107 |
<div id="keywords" class="doctools_section"><h2><a name="keywords">Keywords</a></h2> |
| 1108 |
<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#calling_c_code_from_tcl">calling C code from Tcl</a>, <a href="../index.html#code_generator">code generator</a>, <a href="../index.html#compile_run">compile & 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> |
| 1109 |
</div> |
| 1110 |
<div id="category" class="doctools_section"><h2><a name="category">Category</a></h2> |
| 1111 |
<p>Glueing/Embedded C code</p> |
| 1112 |
</div> |
| 1113 |
<div id="copyright" class="doctools_section"><h2><a name="copyright">Copyright</a></h2> |
| 1114 |
<p>Copyright © Jean-Claude Wippler<br> |
| 1115 |
Copyright © Steve Landers<br> |
| 1116 |
Copyright © 2011-2024 Andreas Kupries</p> |
| 1117 |
</div> |
| 1118 |
</div></body></html> |