Debian Codegraph @

embedded/man/files/critcl_cproc.n

raw
1 '\"
2 '\" Generated from file 'critcl_cproc\&.man' by tcllib/doctools with format 'nroff'
3 '\" Copyright (c) Jean-Claude Wippler
4 '\" Copyright (c) Steve Landers
5 '\" Copyright (c) 2011-2024 Andreas Kupries
6 '\"
7 .TH "critcl_cproc_types" n 3\&.3\&.1 doc "C Runtime In Tcl (CriTcl)"
8 .\" The -*- nroff -*- definitions below are for supplemental macros used
9 .\" in Tcl/Tk manual entries.
10 .\"
11 .\" .AP type name in/out ?indent?
12 .\" Start paragraph describing an argument to a library procedure.
13 .\" type is type of argument (int, etc.), in/out is either "in", "out",
14 .\" or "in/out" to describe whether procedure reads or modifies arg,
15 .\" and indent is equivalent to second arg of .IP (shouldn't ever be
16 .\" needed; use .AS below instead)
17 .\"
18 .\" .AS ?type? ?name?
19 .\" Give maximum sizes of arguments for setting tab stops. Type and
20 .\" name are examples of largest possible arguments that will be passed
21 .\" to .AP later. If args are omitted, default tab stops are used.
22 .\"
23 .\" .BS
24 .\" Start box enclosure. From here until next .BE, everything will be
25 .\" enclosed in one large box.
26 .\"
27 .\" .BE
28 .\" End of box enclosure.
29 .\"
30 .\" .CS
31 .\" Begin code excerpt.
32 .\"
33 .\" .CE
34 .\" End code excerpt.
35 .\"
36 .\" .VS ?version? ?br?
37 .\" Begin vertical sidebar, for use in marking newly-changed parts
38 .\" of man pages. The first argument is ignored and used for recording
39 .\" the version when the .VS was added, so that the sidebars can be
40 .\" found and removed when they reach a certain age. If another argument
41 .\" is present, then a line break is forced before starting the sidebar.
42 .\"
43 .\" .VE
44 .\" End of vertical sidebar.
45 .\"
46 .\" .DS
47 .\" Begin an indented unfilled display.
48 .\"
49 .\" .DE
50 .\" End of indented unfilled display.
51 .\"
52 .\" .SO ?manpage?
53 .\" Start of list of standard options for a Tk widget. The manpage
54 .\" argument defines where to look up the standard options; if
55 .\" omitted, defaults to "options". The options follow on successive
56 .\" lines, in three columns separated by tabs.
57 .\"
58 .\" .SE
59 .\" End of list of standard options for a Tk widget.
60 .\"
61 .\" .OP cmdName dbName dbClass
62 .\" Start of description of a specific option. cmdName gives the
63 .\" option's name as specified in the class command, dbName gives
64 .\" the option's name in the option database, and dbClass gives
65 .\" the option's class in the option database.
66 .\"
67 .\" .UL arg1 arg2
68 .\" Print arg1 underlined, then print arg2 normally.
69 .\"
70 .\" .QW arg1 ?arg2?
71 .\" Print arg1 in quotes, then arg2 normally (for trailing punctuation).
72 .\"
73 .\" .PQ arg1 ?arg2?
74 .\" Print an open parenthesis, arg1 in quotes, then arg2 normally
75 .\" (for trailing punctuation) and then a closing parenthesis.
76 .\"
77 .\" # Set up traps and other miscellaneous stuff for Tcl/Tk man pages.
78 .if t .wh -1.3i ^B
79 .nr ^l \n(.l
80 .ad b
81 .\" # Start an argument description
82 .de AP
83 .ie !"\\$4"" .TP \\$4
84 .el \{\
85 . ie !"\\$2"" .TP \\n()Cu
86 . el .TP 15
87 .\}
88 .ta \\n()Au \\n()Bu
89 .ie !"\\$3"" \{\
90 \&\\$1 \\fI\\$2\\fP (\\$3)
91 .\".b
92 .\}
93 .el \{\
94 .br
95 .ie !"\\$2"" \{\
96 \&\\$1 \\fI\\$2\\fP
97 .\}
98 .el \{\
99 \&\\fI\\$1\\fP
100 .\}
101 .\}
102 ..
103 .\" # define tabbing values for .AP
104 .de AS
105 .nr )A 10n
106 .if !"\\$1"" .nr )A \\w'\\$1'u+3n
107 .nr )B \\n()Au+15n
108 .\"
109 .if !"\\$2"" .nr )B \\w'\\$2'u+\\n()Au+3n
110 .nr )C \\n()Bu+\\w'(in/out)'u+2n
111 ..
112 .AS Tcl_Interp Tcl_CreateInterp in/out
113 .\" # BS - start boxed text
114 .\" # ^y = starting y location
115 .\" # ^b = 1
116 .de BS
117 .br
118 .mk ^y
119 .nr ^b 1u
120 .if n .nf
121 .if n .ti 0
122 .if n \l'\\n(.lu\(ul'
123 .if n .fi
124 ..
125 .\" # BE - end boxed text (draw box now)
126 .de BE
127 .nf
128 .ti 0
129 .mk ^t
130 .ie n \l'\\n(^lu\(ul'
131 .el \{\
132 .\" Draw four-sided box normally, but don't draw top of
133 .\" box if the box started on an earlier page.
134 .ie !\\n(^b-1 \{\
135 \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
136 .\}
137 .el \}\
138 \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
139 .\}
140 .\}
141 .fi
142 .br
143 .nr ^b 0
144 ..
145 .\" # VS - start vertical sidebar
146 .\" # ^Y = starting y location
147 .\" # ^v = 1 (for troff; for nroff this doesn't matter)
148 .de VS
149 .if !"\\$2"" .br
150 .mk ^Y
151 .ie n 'mc \s12\(br\s0
152 .el .nr ^v 1u
153 ..
154 .\" # VE - end of vertical sidebar
155 .de VE
156 .ie n 'mc
157 .el \{\
158 .ev 2
159 .nf
160 .ti 0
161 .mk ^t
162 \h'|\\n(^lu+3n'\L'|\\n(^Yu-1v\(bv'\v'\\n(^tu+1v-\\n(^Yu'\h'-|\\n(^lu+3n'
163 .sp -1
164 .fi
165 .ev
166 .\}
167 .nr ^v 0
168 ..
169 .\" # Special macro to handle page bottom: finish off current
170 .\" # box/sidebar if in box/sidebar mode, then invoked standard
171 .\" # page bottom macro.
172 .de ^B
173 .ev 2
174 'ti 0
175 'nf
176 .mk ^t
177 .if \\n(^b \{\
178 .\" Draw three-sided box if this is the box's first page,
179 .\" draw two sides but no top otherwise.
180 .ie !\\n(^b-1 \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
181 .el \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
182 .\}
183 .if \\n(^v \{\
184 .nr ^x \\n(^tu+1v-\\n(^Yu
185 \kx\h'-\\nxu'\h'|\\n(^lu+3n'\ky\L'-\\n(^xu'\v'\\n(^xu'\h'|0u'\c
186 .\}
187 .bp
188 'fi
189 .ev
190 .if \\n(^b \{\
191 .mk ^y
192 .nr ^b 2
193 .\}
194 .if \\n(^v \{\
195 .mk ^Y
196 .\}
197 ..
198 .\" # DS - begin display
199 .de DS
200 .RS
201 .nf
202 .sp
203 ..
204 .\" # DE - end display
205 .de DE
206 .fi
207 .RE
208 .sp
209 ..
210 .\" # SO - start of list of standard options
211 .de SO
212 'ie '\\$1'' .ds So \\fBoptions\\fR
213 'el .ds So \\fB\\$1\\fR
214 .SH "STANDARD OPTIONS"
215 .LP
216 .nf
217 .ta 5.5c 11c
218 .ft B
219 ..
220 .\" # SE - end of list of standard options
221 .de SE
222 .fi
223 .ft R
224 .LP
225 See the \\*(So manual entry for details on the standard options.
226 ..
227 .\" # OP - start of full description for a single option
228 .de OP
229 .LP
230 .nf
231 .ta 4c
232 Command-Line Name: \\fB\\$1\\fR
233 Database Name: \\fB\\$2\\fR
234 Database Class: \\fB\\$3\\fR
235 .fi
236 .IP
237 ..
238 .\" # CS - begin code excerpt
239 .de CS
240 .RS
241 .nf
242 .ta .25i .5i .75i 1i
243 ..
244 .\" # CE - end code excerpt
245 .de CE
246 .fi
247 .RE
248 ..
249 .\" # UL - underline word
250 .de UL
251 \\$1\l'|0\(ul'\\$2
252 ..
253 .\" # QW - apply quotation marks to word
254 .de QW
255 .ie '\\*(lq'"' ``\\$1''\\$2
256 .\"" fix emacs highlighting
257 .el \\*(lq\\$1\\*(rq\\$2
258 ..
259 .\" # PQ - apply parens and quotation marks to word
260 .de PQ
261 .ie '\\*(lq'"' (``\\$1''\\$2)\\$3
262 .\"" fix emacs highlighting
263 .el (\\*(lq\\$1\\*(rq\\$2)\\$3
264 ..
265 .\" # QR - quoted range
266 .de QR
267 .ie '\\*(lq'"' ``\\$1''\\-``\\$2''\\$3
268 .\"" fix emacs highlighting
269 .el \\*(lq\\$1\\*(rq\\-\\*(lq\\$2\\*(rq\\$3
270 ..
271 .\" # MT - "empty" string
272 .de MT
273 .QW ""
274 ..
275 .BS
276 .SH NAME
277 critcl_cproc_types \- CriTcl cproc Type Reference
278 .SH SYNOPSIS
279 package require \fBTcl 8\&.6\fR
280 .sp
281 package require \fBcritcl ?3\&.3\&.1?\fR
282 .sp
283 \fB::critcl::has-resulttype\fR \fIname\fR
284 .sp
285 \fB::critcl::resulttype\fR \fIname\fR \fIbody\fR ?\fIctype\fR?
286 .sp
287 \fB::critcl::resulttype\fR \fIname\fR \fB=\fR \fIorigname\fR
288 .sp
289 \fB::critcl::has-argtype\fR \fIname\fR
290 .sp
291 \fB::critcl::argtype\fR \fIname\fR \fIbody\fR ?\fIctype\fR? ?\fIctypefun\fR?
292 .sp
293 \fB::critcl::argtype\fR \fIname\fR \fB=\fR \fIorigname\fR
294 .sp
295 \fB::critcl::argtypesupport\fR \fIname\fR \fIcode\fR ?\fIguard\fR?
296 .sp
297 \fB::critcl::argtyperelease\fR \fIname\fR \fIcode\fR
298 .sp
299 .BE
300 .SH DESCRIPTION
301 .PP
302 Be welcome to the \fIC Runtime In Tcl\fR (short: \fICriTcl\fR), a system for embedding and using C
303 code from within \fITcl\fR [http://core\&.tcl-lang\&.org/tcl] scripts\&.
304 .PP
305 This document is a breakout of the descriptions for the predefined argument- and result-types usable
306 with the \fBcritcl::cproc\fR command, as detailed in the reference manpage for the \fBcritcl\fR
307 package, plus the information on how to extend the predefined set with custom types\&. The breakout
308 was made to make this information easier to find (toplevel document vs\&. having to search the large
309 main reference)\&.
310 .PP
311 Its intended audience are developers wishing to write Tcl packages with embedded C code\&.
312 .SH "STANDARD ARGUMENT TYPES"
313 Before going into the details first a quick overview:
314 .CS
315
316
317 CriTcl type | C type | Tcl type | Notes
318 ---------------- | -------------- | --------- | ------------------------------
319 Tcl_Interp* | Tcl_Interp* | n/a | \fISpecial\fR, only first
320 ---------------- | -------------- | --------- | ------------------------------
321 Tcl_Obj* | Tcl_Obj* | Any | \fIRead-only\fR
322 object | | | Alias of \fBTcl_Obj*\fR above
323 list | critcl_list | List | \fIRead-only\fR
324 [], [*] | | | Alias of \fBlist\fR above
325 ---------------- | -------------- | --------- | ------------------------------
326 [N] | | | Restricted \fBlist\fR-types\&.
327 type[], type[N] | | | Length-limited ([\&.\&.]), expected
328 []type, [N]type | | | element type, or both\&.
329 | | |
330 | | | Element types can be all known argument
331 | | | types, except for any kind of list\&.
332 | | | IOW multi-dimensional lists are not
333 | | | supported\&.
334 ---------------- | -------------- | --------- | ------------------------------
335 char* | const char* | Any | \fIRead-only\fR, \fIstring rep\fR
336 pstring | critcl_pstring | Any | \fIRead-only\fR
337 bytes | critcl_bytes | ByteArray | \fIRead-only\fR
338 ---------------- | -------------- | --------- | ------------------------------
339 int | int | Int |
340 long | long | Long |
341 wideint | Tcl_WideInt | WideInt |
342 double | double | Double |
343 float | float | Double |
344 ---------------- | -------------- | --------- | ------------------------------
345 X > N | | | For X in \fBint\fR \&.\&.\&. \fBfloat\fR above\&.
346 X >= N | | | The C types are as per the base type X\&.
347 X < N | | | N, A, B are expected to be constant integer
348 X <= N | | | numbers for types \fBint\fR, \fBlong\fR,
349 X > A < B | | | and \fBwideint\fR\&. For types \fBdouble\fR
350 etc\&. | | | and \fBfloat\fR the N, A, and B can be floating
351 | | | point numbers\&. Multiple restrictions are
352 | | | fused as much as possible to yield at most
353 | | | both upper and lower limits\&.
354 ---------------- | -------------- | --------- | ------------------------------
355 boolean | int | Boolean |
356 bool | | | Alias of \fBboolean\fR above
357 ---------------- | -------------- | --------- | ------------------------------
358 channel | Tcl_Channel | String | Assumed to be registered
359 unshared-channel | Tcl_Channel | String | As above, limited to current interpreter
360 take-channel | Tcl_Channel | String | As above, C code takes ownership
361
362 .CE
363 And now the details:
364 .TP
365 Tcl_Interp*
366 \fIAttention\fR: This is a \fIspecial\fR argument type\&. It can
367 \fIonly\fR be used by the \fIfirst\fR argument of a function\&.
368 Any other argument using it will cause critcl to throw an error\&.
369 .sp
370 When used, the argument will contain a reference to the current
371 interpreter that the function body may use\&. Furthermore the argument
372 will \fInot\fR be an argument of the Tcl command for the function\&.
373 .sp
374 This is useful when the function has to do more than simply
375 returning a value\&. Examples would be setting up error messages on
376 failure, or querying the interpreter for variables and other data\&.
377 .TP
378 Tcl_Obj*
379 .TP
380 object
381 The function takes an argument of type \fBTcl_Obj*\fR\&.
382 No argument checking is done\&.
383 The Tcl level word is passed to the argument as-is\&.
384 Note that this value must be treated as \fIread-only\fR (except for
385 hidden changes to its intrep, i\&.e\&. \fIshimmering\fR)\&.
386 .TP
387 pstring
388 The function takes an argument of type \fBcritcl_pstring\fR
389 containing the original \fBTcl_Obj*\fR reference of the Tcl argument,
390 plus the length of the string and a pointer to the character array\&.
391 .CS
392
393
394 typedef struct critcl_pstring {
395 Tcl_Obj* o;
396 const char* s;
397 int len;
398 } critcl_pstring;
399
400 .CE
401 .IP
402 Note the \fIconst\fR\&. The string is \fIread-only\fR\&. Any
403 modification can have arbitrary effects, from pulling out the rug
404 under the script because of string value and internal representation
405 not matching anymore, up to crashes anytime later\&.
406 .TP
407 list
408 .TP
409 []
410 .TP
411 [*]
412 The function takes an argument of type \fBcritcl_list\fR containing the original \fBTcl_Obj*\fR
413 reference of the Tcl argument, plus the length of the Tcl list and a pointer to the array of the
414 list elements\&.
415 .CS
416
417
418 typedef struct critcl_list {
419 Tcl_Obj* o;
420 Tcl_Obj* const* v;
421 int c;
422 } critcl_list;
423
424 .CE
425 .IP
426 The Tcl argument must be convertible to \fBList\fR, an error is thrown otherwise\&.
427 .sp
428 Note the \fIconst\fR\&. The list is \fIread-only\fR\&. Any modification can have arbitrary
429 effects, from pulling out the rug under the script because of string value and internal
430 representation not matching anymore, up to crashes anytime later\&.
431 .sp
432 Further note that the system understands a number of more complex syntactical forms which all
433 translate into forms of lists under the hood, as described by the following points\&.
434 .TP
435 [N]
436 A \fIlist\fR type with additional checks limiting the length to \fBN\fR, an integer
437 number greater than zero\&.
438 .TP
439 []type
440 .TP
441 type[]
442 A \fIlist\fR type whose elements all have to be convertible for \fItype\fR\&. All known
443 types, including user-defined, are allowed, except for \fBlist\fR and derivates\&. In other
444 words, multi-dimensional lists are not supported\&.
445 .sp
446 The function will take a structure argument of the general form
447 .CS
448
449
450 typedef struct critcl_list_\&.\&.\&. {
451 Tcl_Obj* o;
452 int c;
453 (Ctype)* v;
454 } critcl_list_\&.\&.\&.;
455
456 .CE
457 .IP
458 where \fB(Ctype)\fR represents the C type for values of type \fBtype\fR\&.
459 .TP
460 [N]type
461 .TP
462 type[N]
463 These are \fBlist\fR types combining the elements of
464 .CS
465
466 [N]
467 .CE
468 .IP and
469 .CS
470
471 []type
472 .CE
473 .IP\&.
474 .sp
475 As an example, the specification of
476 .CS
477
478 int[3] a
479 .CE
480 .IP describes argument \fIa\fR
481 as a list of exactly 3 elements, all of which have to be of type \fBint\fR\&.
482 .sp
483 Note that this example can also be written in the more C-like form of
484 .CS
485
486 int a[3]
487 .CE
488 .IP\&. The system will translate this internally to the first shown form\&.
489 .TP
490 bytes
491 This is the \fInew\fR and usable \fBByteArray\fR type\&.
492 .sp
493 The function takes an argument of type \fBcritcl_bytes\fR
494 containing the original \fBTcl_Obj*\fR reference of the Tcl argument,
495 plus the length of the byte array and a pointer to the byte data\&.
496 .CS
497
498
499 typedef struct critcl_bytes {
500 Tcl_Obj* o;
501 const unsigned char* s;
502 int len;
503 } critcl_list;
504
505 .CE
506 .IP
507 The Tcl argument must be convertible to \fBByteArray\fR, an error is
508 thrown otherwise\&.
509 .sp
510 Note the \fIconst\fR\&. The bytes are \fIread-only\fR\&. Any
511 modification can have arbitrary effects, from pulling out the rug
512 under the script because of string value and internal representation
513 not matching anymore, up to crashes anytime later\&.
514 .TP
515 char*
516 The function takes an argument of type \fBconst char*\fR\&.
517 The string representation of the Tcl argument is passed in\&.
518 .sp
519 Note the \fIconst\fR\&. The string is \fIread-only\fR\&. Any
520 modification can have arbitrary effects, from pulling out the rug
521 under the script because of string value and internal representation
522 not matching anymore, up to crashes anytime later\&.
523 .TP
524 double
525 The function takes an argument of type \fBdouble\fR\&.
526 The Tcl argument must be convertible to \fBDouble\fR, an error is thrown otherwise\&.
527 .TP
528 double > N
529 .TP
530 double >= N
531 .TP
532 double < N
533 .TP
534 double <= N
535 These are variants of \fIdouble\fR above, restricting the argument value to the shown relation\&.
536 An error is thrown for Tcl arguments outside of the specified range\&.
537 .sp
538 The limiter \fIN\fR has to be a constant floating point value\&.
539 .sp
540 It is possible to use multiple limiters\&.
541 For example \fIdouble > A > B <= C\fR\&.
542 The system will fuse them to a single upper/lower limit (or both)\&.
543 .sp
544 The system will reject limits describing an empty range of values, or a range containing only
545 a single value\&.
546 .TP
547 float
548 The function takes an argument of type \fBfloat\fR\&.
549 The Tcl argument must be convertible to \fBDouble\fR, an error is thrown otherwise\&.
550 .TP
551 float > N
552 .TP
553 float >= N
554 .TP
555 float < N
556 .TP
557 float <= N
558 These are variants of \fIfloat\fR above, restricting the argument value to the shown relation\&.
559 An error is thrown for Tcl arguments outside of the specified range\&.
560 .sp
561 The limiter \fIN\fR has to be a constant floating point value\&.
562 .sp
563 It is possible to use multiple limiters\&.
564 For example \fIfloat > A > B <= C\fR\&.
565 The system will fuse them to a single upper/lower limit (or both)\&.
566 .sp
567 The system will reject limits describing an empty range of values, or a range containing only
568 a single value\&.
569 .TP
570 boolean
571 .TP
572 bool
573 The function takes an argument of type \fBint\fR\&.
574 The Tcl argument must be convertible to \fBBoolean\fR, an error is
575 thrown otherwise\&.
576 .TP
577 channel
578 The function takes an argument of type \fBTcl_Channel\fR\&.
579 The Tcl argument must be convertible to type \fBChannel\fR, an error
580 is thrown otherwise\&.
581 The channel is further assumed to be \fIalready registered\fR
582 with the interpreter\&.
583 .TP
584 unshared-channel
585 This type is an extension of \fBchannel\fR above\&.
586 All of the information above applies\&.
587 .sp
588 Beyond that the channel must not be shared by multiple
589 interpreters, an error is thrown otherwise\&.
590 .TP
591 take-channel
592 This type is an extension of \fBunshared-channel\fR above\&.
593 All of the information above applies\&.
594 .sp
595 Beyond that the code removes the channel from the current
596 interpreter without closing it, and disables all pre-existing event
597 handling for it\&.
598 .sp
599 With this the function takes full ownership of the channel in
600 question, taking it away from the interpreter invoking it\&. It is then
601 responsible for the lifecycle of the channel, up to and including
602 closing it\&.
603 .sp
604 Should the system the function is a part of wish to return
605 control of the channel back to the interpeter it then has to use the
606 result type \fBreturn-channel\fR\&. This will undo the registration
607 changes made by this argument type\&.
608 \fINote\fR however that the removal of pre-existing event handling
609 done here cannot be undone\&.
610 .sp
611 \fIAttention\fR Removal from the interpreter without closing
612 the channel is effected by incrementing the channel's reference count
613 without providing an interpreter, before decrementing the same for the
614 current interpreter\&. This leaves the overall reference count intact
615 without causing Tcl to close it when it is removed from the
616 interpreter structures\&. At this point the channel is effectively a
617 globally-owned part of the system not associated with any interpreter\&.
618 .sp
619 The complementary result type then runs this sequence in
620 reverse\&. And if the channel is never returned to Tcl either the
621 function or the system it is a part of have to unregister the global
622 reference when they are done with it\&.
623 .TP
624 int
625 The function takes an argument of type \fBint\fR\&.
626 The Tcl argument must be convertible to \fBInt\fR, an error is thrown otherwise\&.
627 .TP
628 int > N
629 .TP
630 int >= N
631 .TP
632 int < N
633 .TP
634 int <= N
635 These are variants of \fIint\fR above, restricting the argument value to the shown
636 relation\&.
637 An error is thrown for Tcl arguments outside of the specified range\&.
638 .sp
639 The limiter \fIN\fR has to be a constant integer value\&.
640 .sp
641 It is possible to use multiple limiters\&.
642 For example \fIint > A > B <= C\fR\&.
643 The system will fuse them to a single upper/lower limit (or both)\&.
644 .sp
645 The system will reject limits describing an empty range of values, or a range
646 containing only a single value\&.
647 .TP
648 long
649 The function takes an argument of type \fBlong int\fR\&.
650 The Tcl argument must be convertible to \fBLong\fR, an error is thrown otherwise\&.
651 .TP
652 long > N
653 .TP
654 long >= N
655 .TP
656 long < N
657 .TP
658 long <= N
659 These are variants of \fIlong\fR above, restricting the argument value to the shown
660 relation\&.
661 An error is thrown for Tcl arguments outside of the specified range\&.
662 .sp
663 The limiter \fIN\fR has to be a constant integer value\&.
664 .sp
665 It is possible to use multiple limiters\&.
666 For example \fIlong > A > B <= C\fR\&.
667 The system will fuse them to a single upper/lower limit (or both)\&.
668 .sp
669 The system will reject limits describing an empty range of values, or a range
670 containing only a single value\&.
671 .TP
672 wideint
673 The function takes an argument of type \fBTcl_WideInt\fR\&.
674 The Tcl argument must be convertible to \fBWideInt\fR, an error is thrown otherwise\&.
675 .TP
676 wideint > N
677 .TP
678 wideint >= N
679 .TP
680 wideint < N
681 .TP
682 wideint <= N
683 These are variants of \fIwideint\fR above, restricting the argument value to the shown
684 relation\&.
685 An error is thrown for Tcl arguments outside of the specified range\&.
686 .sp
687 The limiter \fIN\fR has to be a constant integer value\&.
688 .sp
689 It is possible to use multiple limiters\&.
690 For example \fIwideint > A > B <= C\fR\&.
691 The system will fuse them to a single upper/lower limit (or both)\&.
692 .sp
693 The system will reject limits describing an empty range of values, or a range
694 containing only a single value\&.
695 .TP
696 void*
697 .PP
698 .SH "STANDARD RESULT TYPES"
699 Before going into the details first a quick overview:
700 .CS
701
702
703 CriTcl type | C type | Tcl type | Notes
704 -------------- | -------------- | --------- | ------------------------------
705 void | n/a | n/a | Always OK\&. Body sets result
706 ok | int | n/a | Result code\&. Body sets result
707 -------------- | -------------- | --------- | ------------------------------
708 int | int | Int |
709 boolean | | | Alias of \fBint\fR above
710 bool | | | Alias of \fBint\fR above
711 long | long | Long |
712 wideint | Tcl_WideInt | WideInt |
713 double | double | Double |
714 float | float | Double |
715 -------------- | -------------- | --------- | ------------------------------
716 char* | char* | String | \fIMakes a copy\fR
717 vstring | | | Alias of \fBchar*\fR above
718 const char* | const char* | | Behavior of \fBchar*\fR above
719 -------------- | -------------- | --------- | ------------------------------
720 string | | String | Freeable string set directly
721 | | | \fINo copy is made\fR
722 dstring | | | Alias of \fBstring\fR above
723 -------------- | -------------- | --------- | ------------------------------
724 | | | For all below: Null is ERROR
725 | | | Body has to set any message
726 Tcl_Obj* | Tcl_Obj* | Any | \fIrefcount --\fR
727 object | | | Alias of \fBTcl_Obj*\fR above
728 Tcl_Obj*0 | | Any | \fIrefcount unchanged\fR
729 object0 | | | Alias of \fBTcl_Obj*0\fR above
730 -------------- | -------------- | --------- | ------------------------------
731 known-channel | Tcl_Channel | String | Assumes to already be registered
732 new-channel | Tcl_Channel | String | New channel, will be registered
733 return-channel | Tcl_Channel | String | Inversion of take-channel
734
735 .CE
736 And now the details:
737 .TP
738 Tcl_Obj*
739 .TP
740 object
741 If the returned \fBTcl_Obj*\fR is \fBNULL\fR, the Tcl return code
742 is \fBTCL_ERROR\fR and the function should \fIset an error mesage\fR [https://www\&.tcl-lang\&.org/man/tcl/TclLib/SetResult\&.htm]
743 as the interpreter result\&. Otherwise, the returned \fBTcl_Obj*\fR is
744 set as the interpreter result\&.
745 .sp
746 Note that setting an error message requires the function body
747 to have access to the interpreter the function is running in\&. See the
748 argument type \fBTcl_Interp*\fR for the details on how to make that
749 happen\&.
750 .sp
751 Note further that the returned \fBTcl_Obj*\fR should have a
752 reference count greater than \fB0\fR\&. This is because the converter
753 decrements the reference count to release possession after setting the
754 interpreter result\&. It assumes that the function incremented the
755 reference count of the returned \fBTcl_Obj*\fR\&.
756 If a \fBTcl_Obj*\fR with a reference count of \fB0\fR were
757 returned, the reference count would become \fB1\fR when set as the
758 interpreter result, and immediately thereafter be decremented to
759 \fB0\fR again, causing the memory to be freed\&. The system is then
760 likely to crash at some point after the return due to reuse of the
761 freed memory\&.
762 .TP
763 Tcl_Obj*0
764 .TP
765 object0
766 Like \fBTcl_Obj*\fR except that this conversion assumes that the
767 returned value has a reference count of \fB0\fR and
768 \fIdoes not\fR decrement it\&. Returning a value whose reference
769 count is greater than \fB0\fR is therefore likely to cause a memory
770 leak\&.
771 .sp
772 Note that setting an error message requires the function body
773 to have access to the interpreter the function is running in\&. See the
774 argument type \fBTcl_Interp*\fR for the details on how to make that
775 happen\&.
776 .TP
777 new-channel
778 A \fBString\fR Tcl_Obj holding the name of the returned
779 \fBTcl_Channel\fR is set as the interpreter result\&.
780 The channel is further assumed to be \fInew\fR, and therefore
781 registered with the interpreter to make it known\&.
782 .TP
783 known-channel
784 A \fBString\fR Tcl_Obj holding the name of the returned
785 \fBTcl_Channel\fR is set as the interpreter result\&.
786 The channel is further assumed to be \fIalready registered\fR
787 with the interpreter\&.
788 .TP
789 return-channel
790 This type is a variant of \fBnew-channel\fR above\&.
791 It varies slightly from it in the registration sequence to be properly
792 complementary to the argument type \fBtake-channel\fR\&.
793 A \fBString\fR Tcl_Obj holding the name of the returned
794 \fBTcl_Channel\fR is set as the interpreter result\&.
795 The channel is further assumed to be \fInew\fR, and therefore
796 registered with the interpreter to make it known\&.
797 .TP
798 char*
799 .TP
800 vstring
801 A \fBString\fR Tcl_Obj holding a \fIcopy\fR of the returned
802 \fBchar*\fR is set as the interpreter result\&. If the value is
803 allocated then the function itself and the extension it is a part of
804 are responsible for releasing the memory when the data is not in use
805 any longer\&.
806 .TP
807 const char*
808 Like \fBchar*\fR above, except that the returned string is
809 \fBconst\fR-qualified\&.
810 .TP
811 string
812 .TP
813 dstring
814 The returned \fBchar*\fR is directly set as the interpreter result
815 \fIwithout making a copy\fR\&. Therefore it must be dynamically
816 allocated via \fBTcl_Alloc\fR\&. Release happens automatically when the
817 Interpreter finds that the value is not required any longer\&.
818 .TP
819 double
820 .TP
821 float
822 The returned \fBdouble\fR or \fBfloat\fR is converted to a \fBDouble\fR
823 Tcl_Obj and set as the interpreter result\&.
824 .TP
825 boolean
826 .TP
827 bool
828 The returned \fBint\fR value is converted to an \fBInt\fR Tcl_Obj and set as
829 the interpreter result\&.
830 .TP
831 int
832 The returned \fBint\fR value is converted to an \fBInt\fR Tcl_Obj and set as
833 the interpreter result\&.
834 .TP
835 long
836 The returned \fBlong int\fR value is converted to a \fBLong\fR Tcl_Obj and
837 set as the interpreter result\&.
838 .TP
839 wideint
840 The returned \fBTcl_WideInt\fR value is converted to a \fBWideInt\fR Tcl_Obj
841 and set as the interpreter result\&.
842 .TP
843 ok
844 The returned \fBint\fR value becomes the Tcl return code\&.
845 The interpreter result is left untouched and can be set by the
846 function if desired\&. Note that doing this requires the function body
847 to have access to the interpreter the function is running in\&. See the
848 argument type \fBTcl_Interp*\fR for the details on how to make that
849 happen\&.
850 .TP
851 void
852 The function does not return a value\&.
853 The interpreter result is left untouched and can be set by the function if
854 desired\&.
855 .PP
856 .SH "ADVANCED: ADDING TYPES"
857 While the \fBcritcl::cproc\fR command understands the most common C
858 types (as per the previous 2 sections), sometimes this is not enough\&.
859 .PP
860 To get around this limitation the commands in this section
861 enable users of \fBcritcl\fR to extend the set of argument and
862 result types understood by \fBcritcl::cproc\fR\&. In other words, they
863 allow them to define their own, custom, types\&.
864 .TP
865 \fB::critcl::has-resulttype\fR \fIname\fR
866 This command tests if the named result-type is known or not\&.
867 It returns a boolean value, \fBtrue\fR if the type is known and
868 \fBfalse\fR otherwise\&.
869 .TP
870 \fB::critcl::resulttype\fR \fIname\fR \fIbody\fR ?\fIctype\fR?
871 This command defines the result type \fIname\fR, and associates it
872 with the C code doing the conversion (\fIbody\fR) from C to Tcl\&.
873 The C return type of the associated function, also the C type of the
874 result variable, is \fIctype\fR\&. This type defaults to \fIname\fR if
875 it is not specified\&.
876 .sp
877 If \fIname\fR is already declared an error is thrown\&.
878 \fIAttention!\fR The standard result type \fBvoid\fR is special as
879 it has no accompanying result variable\&. This cannot be expressed
880 by this extension command\&.
881 .sp
882 The \fIbody\fR's responsibility is the conversion of the
883 functions result into a Tcl result and a Tcl status\&. The first has to
884 be set into the interpreter we are in, and the second has to be
885 returned\&.
886 .sp
887 The C code of \fIbody\fR is guaranteed to be called last in the
888 wrapper around the actual implementation of the \fBcproc\fR in
889 question and has access to the following environment:
890 .RS
891 .TP
892 \fBinterp\fR
893 A Tcl_Interp* typed C variable referencing the
894 interpreter the result has to be stored into\&.
895 .TP
896 \fBrv\fR
897 The C variable holding the result to convert, of type
898 \fIctype\fR\&.
899 .RE
900 .IP
901 As examples here are the definitions of two standard result types:
902 .CS
903
904
905 resulttype int {
906 Tcl_SetObjResult(interp, Tcl_NewIntObj(rv));
907 return TCL_OK;
908 }
909
910 resulttype ok {
911 /* interp result must be set by cproc body */
912 return rv;
913 } int
914
915 .CE
916 .TP
917 \fB::critcl::resulttype\fR \fIname\fR \fB=\fR \fIorigname\fR
918 This form of the \fBresulttype\fR command declares \fIname\fR as an
919 alias of result type \fIorigname\fR, which has to be defined
920 already\&. If this is not the case an error is thrown\&.
921 .TP
922 \fB::critcl::has-argtype\fR \fIname\fR
923 This command tests if the named argument-type is known or not\&.
924 It returns a boolean value, \fBtrue\fR if the type is known and
925 \fBfalse\fR otherwise\&.
926 .TP
927 \fB::critcl::argtype\fR \fIname\fR \fIbody\fR ?\fIctype\fR? ?\fIctypefun\fR?
928 This command defines the argument type \fIname\fR, and associates it
929 with the C code doing the conversion (\fIbody\fR) from Tcl to C\&.
930 \fIctype\fR is the C type of the variable to hold the conversion result
931 and \fIctypefun\fR is the type of the function argument itself\&.
932 Both types default to \fIname\fR if they are the empty string or are not
933 provided\&.
934 .sp
935 If \fIname\fR is already declared an error is thrown\&.
936 .sp
937 \fIbody\fR is a C code fragment that converts a Tcl_Obj* into a
938 C value which is stored in a helper variable in the underlying function\&.
939 .sp
940 \fIbody\fR is called inside its own code block to isolate local
941 variables, and the following items are in scope:
942 .RS
943 .TP
944 \fBinterp\fR
945 A variable of type \fBTcl_Interp*\fR which is the
946 interpreter the code is running in\&.
947 .TP
948 \fB@@\fR
949 A placeholder for an expression that evaluates to the
950 \fBTcl_Obj*\fR to convert\&.
951 .TP
952 \fB@A\fR
953 A placeholder for the name of the variable to store the
954 converted argument into\&.
955 .RE
956 .IP
957 As examples, here are the definitions of two standard argument types:
958 .CS
959
960
961 argtype int {
962 if (Tcl_GetIntFromObj(interp, @@, &@A) != TCL_OK) return TCL_ERROR;
963 }
964
965 argtype float {
966 double t;
967 if (Tcl_GetDoubleFromObj(interp, @@, &t) != TCL_OK) return TCL_ERROR;
968 @A = (float) t;
969 }
970
971 .CE
972 .TP
973 \fB::critcl::argtype\fR \fIname\fR \fB=\fR \fIorigname\fR
974 This form of the \fBargtype\fR command declares \fIname\fR as an alias
975 of argument type \fIorigname\fR, which has to be defined already\&. If
976 this is not the case an error is thrown\&.
977 .TP
978 \fB::critcl::argtypesupport\fR \fIname\fR \fIcode\fR ?\fIguard\fR?
979 This command defines a C code fragment for the already defined
980 argument type \fIname\fR which is inserted before all functions
981 using that type\&. Its purpose is the definition of any supporting C
982 types needed by the argument type\&.
983 If the type is used by many functions the system ensures that only the
984 first of the multiple insertions of the code fragment is active, and
985 the others disabled\&.
986 The guard identifier is normally derived from \fIname\fR, but can also
987 be set explicitly, via \fIguard\fR\&. This latter allows different
988 custom types to share a common support structure without having to
989 perform their own guarding\&.
990 .TP
991 \fB::critcl::argtyperelease\fR \fIname\fR \fIcode\fR
992 This command defines a C code fragment for the already defined
993 argument type \fIname\fR which is inserted whenever the worker
994 function of a \fBcritcl::cproc\fR returns to the shim\&. It is the
995 responsibility of this fragment to unconditionally release any
996 resources the \fBcritcl::argtype\fR conversion code allocated\&.
997 An example of this are the \fIvariadic\fR types for the support of
998 the special, variadic \fIargs\fR argument to \fBcritcl::cproc\fR's\&.
999 They allocate a C array for the collected arguments which has to be
1000 released when the worker returns\&. This command defines the C code
1001 for doing that\&.
1002 .PP
1003 .SH EXAMPLES
1004 The examples shown here have been drawn from the section "Embedding C" in the document about
1005 \fIUsing CriTcl\fR\&. Please see that document for many more examples\&.
1006 .SS "A SIMPLE PROCEDURE"
1007 Starting simple, let us assume that the Tcl code in question is
1008 something like
1009 .CS
1010
1011
1012 proc math {x y z} {
1013 return [expr {(sin($x)*rand())/$y**log($z)}]
1014 }
1015
1016 .CE
1017 with the expression pretending to be something very complex and
1018 slow\&. Converting this to C we get:
1019 .CS
1020
1021
1022 critcl::cproc math {double x double y double z} double {
1023 double up = rand () * sin (x);
1024 double down = pow(y, log (z));
1025 return up/down;
1026 }
1027
1028 .CE
1029 Notable about this translation:
1030 .IP [1]
1031 All the arguments got type information added to them, here
1032 "double"\&. Like in C the type precedes the argument name\&. Other
1033 than that it is pretty much a Tcl dictionary, with keys and
1034 values swapped\&.
1035 .IP [2]
1036 We now also have to declare the type of the result, here
1037 "double", again\&.
1038 .IP [3]
1039 The reference manpage lists all the legal C types supported as
1040 arguments and results\&.
1041 .PP
1042 .PP
1043 While the above example was based on type \fBdouble\fR for
1044 both arguments and result we have a number of additional types in the
1045 same category, i\&.e\&. simple types\&. These are:
1046 .CS
1047
1048
1049 CriTcl type | C type | Tcl type | Notes
1050 ----------- | -------------- | --------- | ------------------------------
1051 bool | | | Alias of \fBboolean\fR below
1052 boolean | int | Boolean |
1053 double | double | Double |
1054 float | float | Double |
1055 int | int | Int |
1056 long | long | Long |
1057 wideint | Tcl_WideInt | WideInt |
1058
1059 .CE
1060 .PP
1061 A slightly advanced form of these simple types are a limited
1062 set of constraints on the argument value\&. Note that \fBbool\fR and
1063 alias do not support this\&.
1064 .CS
1065
1066
1067 critcl::cproc sqrt {{double >= 0} x} double {
1068 return sqrt(x);
1069 }
1070
1071 .CE
1072 .PP
1073 In the example above CriTcl's argument handling will reject
1074 calling the command with a negative number, without ever invoking the
1075 C code\&.
1076 .PP
1077 These constraints are called \fIlimited\fR because only
1078 \fB0\fR and \fB1\fR can be used as the borders, although all the
1079 operators \fB<\fR, \fB<=\fR, \fB>\fR, and \fB>=\fR are
1080 possible\&. It is also not possible to combine restrictions\&.
1081 .SS "MORE BUILTIN TYPES: STRINGS"
1082 .PP
1083 Given that "Everything is a String" is a slogan of Tcl the ability of \fBcproc\fRs
1084 to receive strings as arguments, and return them as results is quite important\&.
1085 .PP
1086 We actually have a variety of builtin string types, all alike, yet different\&.
1087 .PP
1088 For arguments we have:
1089 .CS
1090
1091
1092 CriTcl type | C type | Tcl type | Notes
1093 ----------- | -------------- | --------- | ------------------------------
1094 char* | const char* | Any | \fIRead-only\fR, \fIstring rep\fR
1095 pstring | critcl_pstring | Any | \fIRead-only\fR
1096 bytes | critcl_bytes | ByteArray | \fIRead-only\fR
1097
1098 .CE
1099 In C
1100 .CS
1101
1102
1103 critcl::cproc takeStrings {
1104 char* cstring
1105 pstring pstring
1106 bytes barray
1107 } void {
1108 printf ("len %d = %s\\n", strlen(cstring), cstring);
1109 printf ("len %d = %s\\n", pstring\&.len, pstring\&.s);
1110 printf ("len %d = %s\\n", barray\&.len, barray\&.s);
1111 return; // void result, no result
1112 }
1113
1114 .CE
1115 Notable about the above:
1116 .IP [1]
1117 The \fBcstring\fR is a plain \fBconst char*\fR\&. It \fIpoints directly\fR
1118 into the \fBTcl_Obj*\fR holding the argument in the script\&.
1119 .IP [2]
1120 The \fBpstring\fR is a slight extension to that\&. The value is actually a structure
1121 containing the string pointer like \fBcstring\fR (field \fB\&.s\fR), the length of the
1122 string (field \fB\&.len\fR), and a pointer to the \fBTcl_Obj*\fR these came from\&.
1123 .IP [3]
1124 The last, \fBbarray\fR is like \fBpstring\fR, however it has ensured that the
1125 \fBTcl_Obj*\fR is a Tcl ByteArray, i\&.e\&. binary data\&.
1126 .PP
1127 .PP
1128 Treat all of them as \fIRead Only\fR\&. Do not modify ever\&.
1129 .PP
1130 On the other side, string results, we have:
1131 .CS
1132
1133
1134 CriTcl type | C type | Tcl type | Notes
1135 ------------- | -------------- | --------- | ------------------------------
1136 char* | char* | String | \fIMakes a copy\fR
1137 vstring | | | Alias of \fBchar*\fR above
1138 const char* | const char* | | Behavior of \fBchar*\fR above
1139 ------------- | -------------- | --------- | ------------------------------
1140 string | char* | String | Freeable string set directly
1141 | | | \fINo copy is made\fR
1142 dstring | | | Alias of \fBstring\fR above
1143
1144 .CE
1145 .CS
1146
1147
1148 critcl::cproc returnCString {} char* {
1149 return "a string";
1150 }
1151 critcl::cproc returnString {} string {
1152 char* str = Tcl_Alloc (200);
1153 sprintf (str, "hello world");
1154 return str;
1155 }
1156
1157 .CE
1158 Notable about the above:
1159 .IP [1]
1160 The type \fBchar*\fR is best used for static strings, or strings in some kind
1161 fixed buffer\&.
1162 .sp
1163 CriTcl's translation layer makes a copy of it for the result of the command\&. While
1164 it is possible to return heap-allocated strings it is the C code who is responsible for
1165 freeing such at some point\&. If that is not done they will leak\&.
1166 .IP [2]
1167 The type \fBstring\fR on the other hand is exactly for returning strings allocated
1168 with \fBTcl_Alloc\fR and associates\&.
1169 .sp
1170 For these the translation layer makes no copy at all, and sets them directly as the
1171 result of the command\&. A \fIvery important effect\fR of this is that the ownership of
1172 the string pointer moves from the function to Tcl\&.
1173 .sp
1174 \fITcl\fR will release the allocated memory when it does not need it any
1175 longer\&. The C code has no say in that\&.
1176 .PP
1177 .SS "CUSTOM TYPES, INTRODUCTION"
1178 When writing bindings to external libraries \fBcritcl::cproc\fR is
1179 usually the most convenient way of writing the lower layers\&. This is
1180 however hampered by the fact that critcl on its own only supports a
1181 few standard (arguably the most import) standard types, whereas the
1182 functions we wish to bind most certainly will use much more, specific
1183 to the library's function\&.
1184 .PP
1185 The critcl commands \fBargtype\fR, \fBresulttype\fR and their
1186 adjuncts are provided to help here, by allowing a developer to extend
1187 critcl's type system with custom conversions\&.
1188 .PP
1189 This and the three following sections will demonstrate this,
1190 from trivial to complex\&.
1191 .PP
1192 The most trivial use is to create types which are aliases of
1193 existing types, standard or other\&. As an alias it simply copies and
1194 uses the conversion code from the referenced types\&.
1195 .PP
1196 Our example is pulled from an incomplete project of mine, a
1197 binding to \fIJeffrey Kegler\fR's \fIlibmarpa\fR library managing
1198 Earley parsers\&. Several custom types simply reflect the typedef's done
1199 by the library, to make the \fBcritcl::cproc\fRs as self-documenting
1200 as the underlying library functions themselves\&.
1201 .CS
1202
1203
1204 critcl::argtype Marpa_Symbol_ID = int
1205 critcl::argtype Marpa_Rule_ID = int
1206 critcl::argtype Marpa_Rule_Int = int
1207 critcl::argtype Marpa_Rank = int
1208 critcl::argtype Marpa_Earleme = int
1209 critcl::argtype Marpa_Earley_Set_ID = int
1210
1211 \&.\&.\&.
1212
1213 method sym-rank: proc {
1214 Marpa_Symbol_ID sym
1215 Marpa_Rank rank
1216 } Marpa_Rank {
1217 return marpa_g_symbol_rank_set (instance->grammar, sym, rank);
1218 }
1219
1220 \&.\&.\&.
1221
1222 .CE
1223 .SS "CUSTOM TYPES, SEMI-TRIVIAL"
1224 A more involved custom argument type would be to map from Tcl strings
1225 to some internal representation, like an integer code\&.
1226 .PP
1227 The first example is taken from the \fBtclyaml\fR package,
1228 a binding to the \fBlibyaml\fR library\&. In a few places we have to
1229 map readable names for block styles, scalar styles, etc\&. to the
1230 internal enumeration\&.
1231 .CS
1232
1233
1234 critcl::argtype yaml_sequence_style_t {
1235 if (!encode_sequence_style (interp, @@, &@A)) return TCL_ERROR;
1236 }
1237
1238 \&.\&.\&.
1239
1240 critcl::ccode {
1241 static const char* ty_block_style_names [] = {
1242 "any", "block", "flow", NULL
1243 };
1244
1245 static int
1246 encode_sequence_style (Tcl_Interp* interp, Tcl_Obj* style,
1247 yaml_sequence_style_t* estyle)
1248 {
1249 int value;
1250 if (Tcl_GetIndexFromObj (interp, style, ty_block_style_names,
1251 "sequence style", 0, &value) != TCL_OK) {
1252 return 0;
1253 }
1254 *estyle = value;
1255 return 1;
1256 }
1257 }
1258
1259 \&.\&.\&.
1260
1261 method sequence_start proc {
1262 pstring anchor
1263 pstring tag
1264 int implicit
1265 yaml_sequence_style_t style
1266 } ok {
1267 /* Syntax: <instance> seq_start <anchor> <tag> <implicit> <style> */
1268 \&.\&.\&.
1269 }
1270
1271 \&.\&.\&.
1272
1273 .CE
1274 It should be noted that this code precedes the advent of the
1275 supporting generator package \fBcritcl::emap\fR\&. using the
1276 generator the definition of the mapping becomes much simpler:
1277 .CS
1278
1279
1280 critcl::emap::def yaml_sequence_style_t {
1281 any 0
1282 block 1
1283 flow 2
1284 }
1285
1286 .CE
1287 Note that the generator will not only provide the conversions, but
1288 also define the argument and result types needed for their use by
1289 \fBcritcl::cproc\fR\&.
1290 Another example of such a semi-trivial argument type can be found in
1291 the \fBCRIMP\fR package, which defines a \fBTcl_ObjType\fR for
1292 \fIimage\fR values\&. This not only provides a basic argument type for
1293 any image, but also derived types which check that the image has a
1294 specific format\&. Here we see for the first time non-integer arguments,
1295 and the need to define the C types used for variables holding the C
1296 level value, and the type of function parameters (Due to C promotion
1297 rules we may need different types)\&.
1298 .CS
1299
1300
1301 critcl::argtype image {
1302 if (crimp_get_image_from_obj (interp, @@, &@A) != TCL_OK) {
1303 return TCL_ERROR;
1304 }
1305 } crimp_image* crimp_image*
1306
1307 \&.\&.\&.
1308
1309 set map [list <<type>> $type]
1310 critcl::argtype image_$type [string map $map {
1311 if (crimp_get_image_from_obj (interp, @@, &@A) != TCL_OK) {
1312 return TCL_ERROR;
1313 }
1314 if (@A->itype != crimp_imagetype_find ("crimp::image::<<type>>")) {
1315 Tcl_SetObjResult (interp,
1316 Tcl_NewStringObj ("expected image type <<type>>",
1317 -1));
1318 return TCL_ERROR;
1319 }
1320 }] crimp_image* crimp_image*
1321
1322 \&.\&.\&.
1323
1324 .CE
1325 .SS "CUSTOM TYPES, SUPPORT STRUCTURES"
1326 The adjunct command \fBcritcl::argtypesupport\fR is for when the
1327 conversion needs additional definitions, for example a helper
1328 structure\&.
1329 .PP
1330 An example of this can be found among the standard types of
1331 critcl itself, the \fBpstring\fR type\&. This type provides the C
1332 function with not only the string pointer, but also the string length,
1333 and the \fBTcl_Obj*\fR this data came from\&. As \fBcritcl::cproc\fR's
1334 calling conventions allow us only one argument for the data of the
1335 parameter a structure is needed to convey these three pieces of
1336 information\&.
1337 .PP
1338 Thus the argument type is defined as
1339 .CS
1340
1341
1342 critcl::argtype pstring {
1343 @A\&.s = Tcl_GetStringFromObj(@@, &(@A\&.len));
1344 @A\&.o = @@;
1345 } critcl_pstring critcl_pstring
1346
1347 critcl::argtypesupport pstring {
1348 typedef struct critcl_pstring {
1349 Tcl_Obj* o;
1350 const char* s;
1351 int len;
1352 } critcl_pstring;
1353 }
1354
1355 .CE
1356 .PP
1357 In the case of such a structure being large we may wish to
1358 allocate it on the heap instead of having it taking space on the
1359 stack\&. If we do that we need another adjunct command,
1360 \fBcritcl::argtyperelease\fR\&. This command specifies the code required
1361 to release dynamically allocated resources when the worker function
1362 returns, before the shim returns to the caller in Tcl\&.
1363 To keep things simple our example is synthetic, a modification of
1364 \fBpstring\fR above, to demonstrate the technique\&. An actual, but
1365 more complex example is the code to support the variadic \fIargs\fR
1366 argument of \fBcritcl::cproc\fR\&.
1367 .CS
1368
1369
1370 critcl::argtype pstring {
1371 @A = (critcl_pstring*) ckalloc(sizeof(critcl_pstring));
1372 @A->s = Tcl_GetStringFromObj(@@, &(@A->len));
1373 @A->o = @@;
1374 } critcl_pstring* critcl_pstring*
1375
1376 critcl::argtypesupport pstring {
1377 typedef struct critcl_pstring {
1378 Tcl_Obj* o;
1379 const char* s;
1380 int len;
1381 } critcl_pstring;
1382 }
1383
1384 critcl::argtyperelease pstring {
1385 ckfree ((char*)) @A);
1386 }
1387
1388 .CE
1389 Note, the above example shows only the most simple case of an
1390 allocated argument, with a conversion that cannot fail (namely, string
1391 retrieval)\&. If the conversion can fail then either the allocation has
1392 to be defered to happen only on successful conversion, or the
1393 conversion code has to release the allocated memory itself in the
1394 failure path, because it will never reach the code defined via
1395 \fBcritcl::argtyperelease\fR in that case\&.
1396 .SS "CUSTOM TYPES, RESULTS"
1397 All of the previous sections dealt with argument conversions,
1398 i\&.e\&. going from Tcl into C\&.
1399 Custom result types are for the reverse direction, from C to Tcl\&.
1400 This is usually easier, as most of the time errors should not be
1401 possible\&. Supporting structures, or allocating them on the heap are
1402 not really required and therefore not supported\&.
1403 .PP
1404 The example of a result type shown below was pulled from
1405 \fBKineTcl\fR\&. It is a variant of the builtin result type
1406 \fBTcl_Obj*\fR, aka \fBobject\fR\&. The builtin conversion assumes
1407 that the object returned by the function has a refcount of 1 (or
1408 higher), with the function having held the reference, and releases
1409 that reference after placing the value into the interp result\&. The
1410 conversion below on the other hand assumes that the value has a
1411 refcount of 0 and thus that decrementing it is forbidden, lest it be
1412 released much to early, and crashing the system\&.
1413 .CS
1414
1415
1416 critcl::resulttype KTcl_Obj* {
1417 if (rv == NULL) { return TCL_ERROR; }
1418 Tcl_SetObjResult(interp, rv);
1419 /* No refcount adjustment */
1420 return TCL_OK;
1421 } Tcl_Obj*
1422
1423 .CE
1424 This type of definition is also found in \fBMarpa\fR and recent
1425 hacking hacking on \fBCRIMP\fR introduced it there as well\&. Which
1426 is why this definition became a builtin type starting with version
1427 3\&.1\&.16, under the names \fBTcl_Obj*0\fR and \fBobject0\fR\&.
1428 .PP
1429 Going back to errors and their handling, of course, if a
1430 function we are wrapping signals them in-band, then the conversion of
1431 such results has to deal with that\&. This happens for example in
1432 \fBKineTcl\fR, where we find
1433 .CS
1434
1435
1436 critcl::resulttype XnStatus {
1437 if (rv != XN_STATUS_OK) {
1438 Tcl_AppendResult (interp, xnGetStatusString (rv), NULL);
1439 return TCL_ERROR;
1440 }
1441 return TCL_OK;
1442 }
1443
1444 critcl::resulttype XnDepthPixel {
1445 if (rv == ((XnDepthPixel) -1)) {
1446 Tcl_AppendResult (interp,
1447 "Inheritance error: Not a depth generator",
1448 NULL);
1449 return TCL_ERROR;
1450 }
1451 Tcl_SetObjResult (interp, Tcl_NewIntObj (rv));
1452 return TCL_OK;
1453 }
1454
1455 .CE
1456 .SH AUTHORS
1457 Jean Claude Wippler, Steve Landers, Andreas Kupries
1458 .SH "BUGS, IDEAS, FEEDBACK"
1459 This document, and the package it describes, will undoubtedly contain
1460 bugs and other problems\&.
1461 Please report them at \fIhttps://github\&.com/andreas-kupries/critcl/issues\fR\&.
1462 Ideas for enhancements you may have for either package, application,
1463 and/or the documentation are also very welcome and should be reported
1464 at \fIhttps://github\&.com/andreas-kupries/critcl/issues\fR as well\&.
1465 .SH KEYWORDS
1466 C code, Embedded C Code, calling C code from Tcl, code generator, compile & run, compiler, dynamic code generation, dynamic compilation, generate package, linker, on demand compilation, on-the-fly compilation
1467 .SH CATEGORY
1468 Glueing/Embedded C code
1469 .SH COPYRIGHT
1470 .nf
1471 Copyright (c) Jean-Claude Wippler
1472 Copyright (c) Steve Landers
1473 Copyright (c) 2011-2024 Andreas Kupries
1474
1475 .fi