| 1 |
'\" |
| 2 |
'\" Generated from file 'critcl_callback\&.man' by tcllib/doctools with format 'nroff' |
| 3 |
'\" Copyright (c) 2011-2024 Andreas Kupries |
| 4 |
'\" |
| 5 |
.TH "critcl::callback" n 1\&.1 doc "C Runtime In Tcl (CriTcl)" |
| 6 |
.\" The -*- nroff -*- definitions below are for supplemental macros used |
| 7 |
.\" in Tcl/Tk manual entries. |
| 8 |
.\" |
| 9 |
.\" .AP type name in/out ?indent? |
| 10 |
.\" Start paragraph describing an argument to a library procedure. |
| 11 |
.\" type is type of argument (int, etc.), in/out is either "in", "out", |
| 12 |
.\" or "in/out" to describe whether procedure reads or modifies arg, |
| 13 |
.\" and indent is equivalent to second arg of .IP (shouldn't ever be |
| 14 |
.\" needed; use .AS below instead) |
| 15 |
.\" |
| 16 |
.\" .AS ?type? ?name? |
| 17 |
.\" Give maximum sizes of arguments for setting tab stops. Type and |
| 18 |
.\" name are examples of largest possible arguments that will be passed |
| 19 |
.\" to .AP later. If args are omitted, default tab stops are used. |
| 20 |
.\" |
| 21 |
.\" .BS |
| 22 |
.\" Start box enclosure. From here until next .BE, everything will be |
| 23 |
.\" enclosed in one large box. |
| 24 |
.\" |
| 25 |
.\" .BE |
| 26 |
.\" End of box enclosure. |
| 27 |
.\" |
| 28 |
.\" .CS |
| 29 |
.\" Begin code excerpt. |
| 30 |
.\" |
| 31 |
.\" .CE |
| 32 |
.\" End code excerpt. |
| 33 |
.\" |
| 34 |
.\" .VS ?version? ?br? |
| 35 |
.\" Begin vertical sidebar, for use in marking newly-changed parts |
| 36 |
.\" of man pages. The first argument is ignored and used for recording |
| 37 |
.\" the version when the .VS was added, so that the sidebars can be |
| 38 |
.\" found and removed when they reach a certain age. If another argument |
| 39 |
.\" is present, then a line break is forced before starting the sidebar. |
| 40 |
.\" |
| 41 |
.\" .VE |
| 42 |
.\" End of vertical sidebar. |
| 43 |
.\" |
| 44 |
.\" .DS |
| 45 |
.\" Begin an indented unfilled display. |
| 46 |
.\" |
| 47 |
.\" .DE |
| 48 |
.\" End of indented unfilled display. |
| 49 |
.\" |
| 50 |
.\" .SO ?manpage? |
| 51 |
.\" Start of list of standard options for a Tk widget. The manpage |
| 52 |
.\" argument defines where to look up the standard options; if |
| 53 |
.\" omitted, defaults to "options". The options follow on successive |
| 54 |
.\" lines, in three columns separated by tabs. |
| 55 |
.\" |
| 56 |
.\" .SE |
| 57 |
.\" End of list of standard options for a Tk widget. |
| 58 |
.\" |
| 59 |
.\" .OP cmdName dbName dbClass |
| 60 |
.\" Start of description of a specific option. cmdName gives the |
| 61 |
.\" option's name as specified in the class command, dbName gives |
| 62 |
.\" the option's name in the option database, and dbClass gives |
| 63 |
.\" the option's class in the option database. |
| 64 |
.\" |
| 65 |
.\" .UL arg1 arg2 |
| 66 |
.\" Print arg1 underlined, then print arg2 normally. |
| 67 |
.\" |
| 68 |
.\" .QW arg1 ?arg2? |
| 69 |
.\" Print arg1 in quotes, then arg2 normally (for trailing punctuation). |
| 70 |
.\" |
| 71 |
.\" .PQ arg1 ?arg2? |
| 72 |
.\" Print an open parenthesis, arg1 in quotes, then arg2 normally |
| 73 |
.\" (for trailing punctuation) and then a closing parenthesis. |
| 74 |
.\" |
| 75 |
.\" # Set up traps and other miscellaneous stuff for Tcl/Tk man pages. |
| 76 |
.if t .wh -1.3i ^B |
| 77 |
.nr ^l \n(.l |
| 78 |
.ad b |
| 79 |
.\" # Start an argument description |
| 80 |
.de AP |
| 81 |
.ie !"\\$4"" .TP \\$4 |
| 82 |
.el \{\ |
| 83 |
. ie !"\\$2"" .TP \\n()Cu |
| 84 |
. el .TP 15 |
| 85 |
.\} |
| 86 |
.ta \\n()Au \\n()Bu |
| 87 |
.ie !"\\$3"" \{\ |
| 88 |
\&\\$1 \\fI\\$2\\fP (\\$3) |
| 89 |
.\".b |
| 90 |
.\} |
| 91 |
.el \{\ |
| 92 |
.br |
| 93 |
.ie !"\\$2"" \{\ |
| 94 |
\&\\$1 \\fI\\$2\\fP |
| 95 |
.\} |
| 96 |
.el \{\ |
| 97 |
\&\\fI\\$1\\fP |
| 98 |
.\} |
| 99 |
.\} |
| 100 |
.. |
| 101 |
.\" # define tabbing values for .AP |
| 102 |
.de AS |
| 103 |
.nr )A 10n |
| 104 |
.if !"\\$1"" .nr )A \\w'\\$1'u+3n |
| 105 |
.nr )B \\n()Au+15n |
| 106 |
.\" |
| 107 |
.if !"\\$2"" .nr )B \\w'\\$2'u+\\n()Au+3n |
| 108 |
.nr )C \\n()Bu+\\w'(in/out)'u+2n |
| 109 |
.. |
| 110 |
.AS Tcl_Interp Tcl_CreateInterp in/out |
| 111 |
.\" # BS - start boxed text |
| 112 |
.\" # ^y = starting y location |
| 113 |
.\" # ^b = 1 |
| 114 |
.de BS |
| 115 |
.br |
| 116 |
.mk ^y |
| 117 |
.nr ^b 1u |
| 118 |
.if n .nf |
| 119 |
.if n .ti 0 |
| 120 |
.if n \l'\\n(.lu\(ul' |
| 121 |
.if n .fi |
| 122 |
.. |
| 123 |
.\" # BE - end boxed text (draw box now) |
| 124 |
.de BE |
| 125 |
.nf |
| 126 |
.ti 0 |
| 127 |
.mk ^t |
| 128 |
.ie n \l'\\n(^lu\(ul' |
| 129 |
.el \{\ |
| 130 |
.\" Draw four-sided box normally, but don't draw top of |
| 131 |
.\" box if the box started on an earlier page. |
| 132 |
.ie !\\n(^b-1 \{\ |
| 133 |
\h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul' |
| 134 |
.\} |
| 135 |
.el \}\ |
| 136 |
\h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul' |
| 137 |
.\} |
| 138 |
.\} |
| 139 |
.fi |
| 140 |
.br |
| 141 |
.nr ^b 0 |
| 142 |
.. |
| 143 |
.\" # VS - start vertical sidebar |
| 144 |
.\" # ^Y = starting y location |
| 145 |
.\" # ^v = 1 (for troff; for nroff this doesn't matter) |
| 146 |
.de VS |
| 147 |
.if !"\\$2"" .br |
| 148 |
.mk ^Y |
| 149 |
.ie n 'mc \s12\(br\s0 |
| 150 |
.el .nr ^v 1u |
| 151 |
.. |
| 152 |
.\" # VE - end of vertical sidebar |
| 153 |
.de VE |
| 154 |
.ie n 'mc |
| 155 |
.el \{\ |
| 156 |
.ev 2 |
| 157 |
.nf |
| 158 |
.ti 0 |
| 159 |
.mk ^t |
| 160 |
\h'|\\n(^lu+3n'\L'|\\n(^Yu-1v\(bv'\v'\\n(^tu+1v-\\n(^Yu'\h'-|\\n(^lu+3n' |
| 161 |
.sp -1 |
| 162 |
.fi |
| 163 |
.ev |
| 164 |
.\} |
| 165 |
.nr ^v 0 |
| 166 |
.. |
| 167 |
.\" # Special macro to handle page bottom: finish off current |
| 168 |
.\" # box/sidebar if in box/sidebar mode, then invoked standard |
| 169 |
.\" # page bottom macro. |
| 170 |
.de ^B |
| 171 |
.ev 2 |
| 172 |
'ti 0 |
| 173 |
'nf |
| 174 |
.mk ^t |
| 175 |
.if \\n(^b \{\ |
| 176 |
.\" Draw three-sided box if this is the box's first page, |
| 177 |
.\" draw two sides but no top otherwise. |
| 178 |
.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 |
| 179 |
.el \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c |
| 180 |
.\} |
| 181 |
.if \\n(^v \{\ |
| 182 |
.nr ^x \\n(^tu+1v-\\n(^Yu |
| 183 |
\kx\h'-\\nxu'\h'|\\n(^lu+3n'\ky\L'-\\n(^xu'\v'\\n(^xu'\h'|0u'\c |
| 184 |
.\} |
| 185 |
.bp |
| 186 |
'fi |
| 187 |
.ev |
| 188 |
.if \\n(^b \{\ |
| 189 |
.mk ^y |
| 190 |
.nr ^b 2 |
| 191 |
.\} |
| 192 |
.if \\n(^v \{\ |
| 193 |
.mk ^Y |
| 194 |
.\} |
| 195 |
.. |
| 196 |
.\" # DS - begin display |
| 197 |
.de DS |
| 198 |
.RS |
| 199 |
.nf |
| 200 |
.sp |
| 201 |
.. |
| 202 |
.\" # DE - end display |
| 203 |
.de DE |
| 204 |
.fi |
| 205 |
.RE |
| 206 |
.sp |
| 207 |
.. |
| 208 |
.\" # SO - start of list of standard options |
| 209 |
.de SO |
| 210 |
'ie '\\$1'' .ds So \\fBoptions\\fR |
| 211 |
'el .ds So \\fB\\$1\\fR |
| 212 |
.SH "STANDARD OPTIONS" |
| 213 |
.LP |
| 214 |
.nf |
| 215 |
.ta 5.5c 11c |
| 216 |
.ft B |
| 217 |
.. |
| 218 |
.\" # SE - end of list of standard options |
| 219 |
.de SE |
| 220 |
.fi |
| 221 |
.ft R |
| 222 |
.LP |
| 223 |
See the \\*(So manual entry for details on the standard options. |
| 224 |
.. |
| 225 |
.\" # OP - start of full description for a single option |
| 226 |
.de OP |
| 227 |
.LP |
| 228 |
.nf |
| 229 |
.ta 4c |
| 230 |
Command-Line Name: \\fB\\$1\\fR |
| 231 |
Database Name: \\fB\\$2\\fR |
| 232 |
Database Class: \\fB\\$3\\fR |
| 233 |
.fi |
| 234 |
.IP |
| 235 |
.. |
| 236 |
.\" # CS - begin code excerpt |
| 237 |
.de CS |
| 238 |
.RS |
| 239 |
.nf |
| 240 |
.ta .25i .5i .75i 1i |
| 241 |
.. |
| 242 |
.\" # CE - end code excerpt |
| 243 |
.de CE |
| 244 |
.fi |
| 245 |
.RE |
| 246 |
.. |
| 247 |
.\" # UL - underline word |
| 248 |
.de UL |
| 249 |
\\$1\l'|0\(ul'\\$2 |
| 250 |
.. |
| 251 |
.\" # QW - apply quotation marks to word |
| 252 |
.de QW |
| 253 |
.ie '\\*(lq'"' ``\\$1''\\$2 |
| 254 |
.\"" fix emacs highlighting |
| 255 |
.el \\*(lq\\$1\\*(rq\\$2 |
| 256 |
.. |
| 257 |
.\" # PQ - apply parens and quotation marks to word |
| 258 |
.de PQ |
| 259 |
.ie '\\*(lq'"' (``\\$1''\\$2)\\$3 |
| 260 |
.\"" fix emacs highlighting |
| 261 |
.el (\\*(lq\\$1\\*(rq\\$2)\\$3 |
| 262 |
.. |
| 263 |
.\" # QR - quoted range |
| 264 |
.de QR |
| 265 |
.ie '\\*(lq'"' ``\\$1''\\-``\\$2''\\$3 |
| 266 |
.\"" fix emacs highlighting |
| 267 |
.el \\*(lq\\$1\\*(rq\\-\\*(lq\\$2\\*(rq\\$3 |
| 268 |
.. |
| 269 |
.\" # MT - "empty" string |
| 270 |
.de MT |
| 271 |
.QW "" |
| 272 |
.. |
| 273 |
.BS |
| 274 |
.SH NAME |
| 275 |
critcl::callback \- CriTcl - C-level Callback Utilities |
| 276 |
.SH SYNOPSIS |
| 277 |
package require \fBTcl 8\&.6\fR |
| 278 |
.sp |
| 279 |
package require \fBcritcl ?3\&.2?\fR |
| 280 |
.sp |
| 281 |
package require \fBcritcl::callback ?1\&.1?\fR |
| 282 |
.sp |
| 283 |
\fBcritcl_callback_p\fR \fBcritcl_callback_new\fR \fIinterp\fR \fIobjc\fR \fIobjv\fR \fInargs\fR |
| 284 |
.sp |
| 285 |
\fBvoid\fR \fBcritcl_callback_extend\fR \fIcallback\fR \fIargument\fR |
| 286 |
.sp |
| 287 |
\fBvoid\fR \fBcritcl_callback_destroy\fR \fIcallback\fR |
| 288 |
.sp |
| 289 |
\fBint\fR \fBcritcl_callback_invoke\fR \fIcallback\fR \fIobjc\fR \fIobjv\fR |
| 290 |
.sp |
| 291 |
.BE |
| 292 |
.SH DESCRIPTION |
| 293 |
.PP |
| 294 |
Be welcome to the \fIC Runtime In Tcl\fR (short: \fICriTcl\fR), a system for embedding and using C |
| 295 |
code from within \fITcl\fR [http://core\&.tcl-lang\&.org/tcl] scripts\&. |
| 296 |
.PP |
| 297 |
This document is the reference manpage for the |
| 298 |
\fBcritcl::callback\fR package\&. |
| 299 |
This package provides, via a stubs API table, data structures and |
| 300 |
functions to manage callbacks from C to Tcl\&. The package has no |
| 301 |
Tcl-level facilities\&. |
| 302 |
Its intended audience are mainly developers wishing to write Tcl |
| 303 |
packages with embedded C code who have to invoke user-specified |
| 304 |
command (prefixes) in Tcl\&. |
| 305 |
.PP |
| 306 |
This package resides in the Support Package Layer of CriTcl\&. |
| 307 |
.PP |
| 308 |
.PS |
| 309 |
.nf |
| 310 |
+----------------+ |
| 311 |
|Applications | |
| 312 |
| critcl | |
| 313 |
| critcl::app | |
| 314 |
+----------------+ |
| 315 |
|
| 316 |
+----------------+ |
| 317 |
|Core Packages | |
| 318 |
| critcl | |
| 319 |
| critcl::util | |
| 320 |
+----------------+ |
| 321 |
|
| 322 |
*================* |
| 323 |
|Support Packages| |
| 324 |
| stubs::* | |
| 325 |
| md5, platform | |
| 326 |
| \&.\&.\&. | |
| 327 |
*================* |
| 328 |
|
| 329 |
.fi |
| 330 |
.PE |
| 331 |
.PP |
| 332 |
.SH API |
| 333 |
The package API consist of one opaque data structure |
| 334 |
(\fBcritcl_callback_p\fR) and four functions operating on the same\&. |
| 335 |
These functions are |
| 336 |
.TP |
| 337 |
\fBcritcl_callback_p\fR \fBcritcl_callback_new\fR \fIinterp\fR \fIobjc\fR \fIobjv\fR \fInargs\fR |
| 338 |
This function creates a new callback (manager) and returns it as its result\&. |
| 339 |
.sp |
| 340 |
The callback is initialized with the Tcl_Interp* \fIinterp\fR |
| 341 |
specifying where to run the callback, the fixed part of the command to |
| 342 |
run in standard \fIobjc\fR/\fIobjv\fR notation, plus the number of |
| 343 |
free arguments to expect after the fixed part\&. |
| 344 |
.sp |
| 345 |
The fixed part is the essentially the command prefix of the callback\&. |
| 346 |
.sp |
| 347 |
All \fBTcl_Obj*\fR elements of \fIobjv\fR are protected against early |
| 348 |
release by incrementing their reference counts\&. The callback |
| 349 |
effectively takes ownership of these objects\&. |
| 350 |
.TP |
| 351 |
\fBvoid\fR \fBcritcl_callback_extend\fR \fIcallback\fR \fIargument\fR |
| 352 |
This function takes a \fIcallback\fR of type \fBcritcl_callback_p\fR |
| 353 |
and extends its fixed part with the \fIargument\fR, taking the first |
| 354 |
free slot for arguments to do so\&. |
| 355 |
This means that after the application of this function the specified |
| 356 |
callback has one free argument less\&. |
| 357 |
.sp |
| 358 |
With assertions active attempting to extend beyond the number of free |
| 359 |
arguments will cause a panic\&. Without assertions active expect a crash |
| 360 |
at some point\&. |
| 361 |
.sp |
| 362 |
This allows the user to extend the fixed part of the callback with |
| 363 |
semi-fixed elements, like method names (See \fBMultiple methods\fR)\&. |
| 364 |
.sp |
| 365 |
The \fIargument\fR is protected against early release by incrementing |
| 366 |
its reference count\&. The callback effectively takes ownership of this |
| 367 |
object\&. |
| 368 |
.TP |
| 369 |
\fBvoid\fR \fBcritcl_callback_destroy\fR \fIcallback\fR |
| 370 |
This function takes a \fIcallback\fR of type \fBcritcl_callback_p\fR |
| 371 |
and releases all memory associated with it\&. |
| 372 |
After application of this function the callback cannot be used anymore\&. |
| 373 |
.sp |
| 374 |
All fixed elements of the callback (owned by it) are released by |
| 375 |
decrementing their reference counts\&. |
| 376 |
.TP |
| 377 |
\fBint\fR \fBcritcl_callback_invoke\fR \fIcallback\fR \fIobjc\fR \fIobjv\fR |
| 378 |
This function invokes the callback in the Tcl interpreter specified at |
| 379 |
the time of construction, in the global level and namespace, with the |
| 380 |
free arguments filled by the \fBTcl_Obj*\fR objects specified via |
| 381 |
\fIobjc\fR/\fIobjv\fR\&. |
| 382 |
.sp |
| 383 |
It returns the Tcl status of the invoked command as its result\&. |
| 384 |
Any further results or error messages will be found in the result area |
| 385 |
of the Tcl interpreter in question\&. The exact nature of such is |
| 386 |
dependent on the callback itself\&. |
| 387 |
.sp |
| 388 |
With assertions active attempting to use more arguments than available |
| 389 |
will cause a panic\&. Without assertions active expect a crash at some |
| 390 |
point\&. |
| 391 |
.sp |
| 392 |
While the callback is running all \fBTcl_Obj*\fR elements of the |
| 393 |
command, fixed and arguments, are protected against early release by |
| 394 |
temporarily incrementing their reference counts\&. |
| 395 |
.PP |
| 396 |
.SH EXAMPLES |
| 397 |
.SS "SIMPLE CALLBACK" |
| 398 |
The example here shows the important parts of using the functions of |
| 399 |
this package for a simple callback which is invoked with a single |
| 400 |
argument, some kind of data to hand to the Tcl level\&. |
| 401 |
.CS |
| 402 |
|
| 403 |
|
| 404 |
// Create the callback with interpreter and command prefix in |
| 405 |
// oc/ov, plus space for the argument |
| 406 |
critcl_callback_p cb = critcl_callback_new (interp, oc, ov, 1); |
| 407 |
|
| 408 |
// Invoke the callback somewhere in the C package using this one, |
| 409 |
// with Tcl_Obj* data holding the information to pass up\&. |
| 410 |
critcl_callback_invoke (cb, 1, &data); |
| 411 |
|
| 412 |
// At the end of the lifetime, release the callback\&. |
| 413 |
critcl_callback_destroy (cb); |
| 414 |
|
| 415 |
.CE |
| 416 |
Note that the functions of this package are designed for the case |
| 417 |
where the created callback (\fBcb\fR above) is kept around for a |
| 418 |
long time, and many different invokations\&. |
| 419 |
.PP |
| 420 |
Using the sequence above as is, creating and destroying the callback |
| 421 |
each time it is invoked will yield very poor performance and lots of |
| 422 |
undesirable memory churn\&. |
| 423 |
.SS "MULTIPLE METHODS" |
| 424 |
While we can use the methodology of the previous section when a single |
| 425 |
(Tcl-level) callback is invoked from different places in C, with |
| 426 |
different methods, simply having another argument slot and filling it |
| 427 |
an invokation time with the method object, a second methodology is |
| 428 |
open to us due to \fBcritcl_callback_extend\fR\&. |
| 429 |
.CS |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
// Create one callback manager per different method the callback |
| 434 |
// will be used with\&. Fill the first of the two declared arguments |
| 435 |
// with the different methods\&. |
| 436 |
critcl_callback_p cb_a = critcl_callback_new (interp, oc, ov, 2); |
| 437 |
critcl_callback_p cb_b = critcl_callback_new (interp, oc, ov, 2); |
| 438 |
|
| 439 |
critcl_callback_extend (cb_a, Tcl_NewStringObj ("method1", -1)); |
| 440 |
critcl_callback_extend (cb_b, Tcl_NewStringObj ("method2", -1)); |
| 441 |
|
| 442 |
// After the extension we have one free argument left, for use in |
| 443 |
// the invokations\&. |
| 444 |
|
| 445 |
critcl_callback_invoke (cb_a, 1, &dataX); |
| 446 |
|
| 447 |
critcl_callback_invoke (cb_b, 1, &dataY); |
| 448 |
|
| 449 |
|
| 450 |
// At the end release both managers again |
| 451 |
critcl_callback_destroy (cb_a); |
| 452 |
critcl_callback_destroy (cb_b); |
| 453 |
|
| 454 |
.CE |
| 455 |
The nice thing here is that the method objects are allocated only once |
| 456 |
and automatically shared by all the calls\&. No memory churn to |
| 457 |
repeatedly allocate the same string objects over and over again\&. |
| 458 |
.SH AUTHORS |
| 459 |
Andreas Kupries |
| 460 |
.SH "BUGS, IDEAS, FEEDBACK" |
| 461 |
This document, and the package it describes, will undoubtedly contain |
| 462 |
bugs and other problems\&. |
| 463 |
Please report such at \fIhttps://github\&.com/andreas-kupries/critcl\fR\&. |
| 464 |
Please also report any ideas for enhancements you may have for either |
| 465 |
package and/or documentation\&. |
| 466 |
.SH KEYWORDS |
| 467 |
C code, Embedded C Code, code generator, compile & run, compiler, dynamic code generation, dynamic compilation, generate package, linker, on demand compilation, on-the-fly compilation |
| 468 |
.SH CATEGORY |
| 469 |
Glueing/Embedded C code |
| 470 |
.SH COPYRIGHT |
| 471 |
.nf |
| 472 |
Copyright (c) 2011-2024 Andreas Kupries |
| 473 |
|
| 474 |
.fi |