Debian Codegraph @

embedded/www/files/critcl_class.html

raw
1 <!DOCTYPE html><html><head>
2 <meta charset="UTF-8">
3 <meta name="viewport" content="width=device-width, initial-scale=1">
4 <title>critcl::class - 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_class.man' by tcllib/doctools with format 'html'
97 -->
98 <!-- Copyright &amp;copy; 2011-2024 Andreas Kupries
99 -->
100 <!-- critcl::class.n
101 -->
102 <body><hr> [
103 <a href="../toc.html">Table Of Contents</a>
104 &#124; <a href="../index.html">Keyword Index</a>
105 ] <hr>
106 <div class="doctools">
107 <h1 class="doctools_title">critcl::class(n) 1.1 doc &quot;C Runtime In Tcl (CriTcl)&quot;</h1>
108 <div id="name" class="doctools_section"><h2><a name="name">Name</a></h2>
109 <p>critcl::class - CriTcl - Code Gen - C Classes</p>
110 </div>
111 <div id="toc" class="doctools_section"><h2><a name="toc">Table Of Contents</a></h2>
112 <ul class="doctools_toc">
113 <li class="doctools_section"><a href="#toc">Table Of Contents</a></li>
114 <li class="doctools_section"><a href="#synopsis">Synopsis</a></li>
115 <li class="doctools_section"><a href="#section1">Description</a></li>
116 <li class="doctools_section"><a href="#section2">API</a></li>
117 <li class="doctools_section"><a href="#section3">Class Specification API</a>
118 <ul>
119 <li class="doctools_subsection"><a href="#subsection1">General configuration</a></li>
120 <li class="doctools_subsection"><a href="#subsection2">Class lifetime management</a></li>
121 <li class="doctools_subsection"><a href="#subsection3">Instance lifetime management</a></li>
122 <li class="doctools_subsection"><a href="#subsection4">Class variables and methods</a></li>
123 <li class="doctools_subsection"><a href="#subsection5">Instance variables and methods</a></li>
124 <li class="doctools_subsection"><a href="#subsection6">Context dependent interactions</a></li>
125 </ul>
126 </li>
127 <li class="doctools_section"><a href="#section4">Example</a></li>
128 <li class="doctools_section"><a href="#section5">Authors</a></li>
129 <li class="doctools_section"><a href="#section6">Bugs, Ideas, Feedback</a></li>
130 <li class="doctools_section"><a href="#keywords">Keywords</a></li>
131 <li class="doctools_section"><a href="#category">Category</a></li>
132 <li class="doctools_section"><a href="#copyright">Copyright</a></li>
133 </ul>
134 </div>
135 <div id="synopsis" class="doctools_section"><h2><a name="synopsis">Synopsis</a></h2>
136 <div class="doctools_synopsis">
137 <ul class="doctools_requirements">
138 <li>package require <b class="pkgname">Tcl 8.6</b></li>
139 <li>package require <b class="pkgname">critcl <span class="opt">?3.2?</span></b></li>
140 <li>package require <b class="pkgname">critcl::class <span class="opt">?1.1?</span></b></li>
141 </ul>
142 <ul class="doctools_syntax">
143 <li><a href="#1"><b class="cmd">::critcl::class::define</b> <i class="arg">name</i> <i class="arg">script</i></a></li>
144 <li><a href="#2"><b class="cmd">include</b> <i class="arg">path</i></a></li>
145 <li><a href="#3"><b class="cmd">support</b> <i class="arg">code</i></a></li>
146 <li><a href="#4"><b class="cmd">type</b> <i class="arg">name</i></a></li>
147 <li><a href="#5"><b class="cmd">classconstructor</b> <i class="arg">body</i></a></li>
148 <li><a href="#6"><b class="cmd">classdestructor</b> <i class="arg">body</i></a></li>
149 <li><a href="#7"><b class="cmd">constructor</b> <i class="arg">body</i> <span class="opt">?<i class="arg">postbody</i>?</span></a></li>
150 <li><a href="#8"><b class="cmd">destructor</b> <i class="arg">body</i></a></li>
151 <li><a href="#9"><b class="cmd">classvariable</b> <i class="arg">ctype</i> <i class="arg">name</i> <span class="opt">?<i class="arg">comment</i>?</span> <span class="opt">?<i class="arg">constructor</i>?</span> <span class="opt">?<i class="arg">destructor</i>?</span></a></li>
152 <li><a href="#10"><b class="cmd">classmethod</b> <i class="arg">name</i> <b class="method">command</b> <i class="arg">arguments</i> <i class="arg">body</i></a></li>
153 <li><a href="#11"><b class="cmd">classmethod</b> <i class="arg">name</i> <b class="method">proc</b> <i class="arg">arguments</i> <i class="arg">resulttype</i> <i class="arg">body</i></a></li>
154 <li><a href="#12"><b class="cmd">classmethod</b> <i class="arg">name</i> <b class="method">as</b> <i class="arg">funname</i> <span class="opt">?<i class="arg">arg</i>...?</span></a></li>
155 <li><a href="#13"><b class="cmd">insvariable</b> <i class="arg">ctype</i> <i class="arg">name</i> <span class="opt">?<i class="arg">comment</i>?</span> <span class="opt">?<i class="arg">constructor</i>?</span> <span class="opt">?<i class="arg">destructor</i>?</span></a></li>
156 <li><a href="#14"><b class="cmd">method</b> <i class="arg">name</i> <b class="method">command</b> <i class="arg">arguments</i> <i class="arg">body</i></a></li>
157 <li><a href="#15"><b class="cmd">method</b> <i class="arg">name</i> <b class="method">proc</b> <i class="arg">arguments</i> <i class="arg">resulttype</i> <i class="arg">body</i></a></li>
158 <li><a href="#16"><b class="cmd">method</b> <i class="arg">name</i> <b class="method">as</b> <i class="arg">funname</i> <span class="opt">?<i class="arg">arg</i>...?</span></a></li>
159 <li><a href="#17"><b class="cmd">method_introspection</b></a></li>
160 </ul>
161 </div>
162 </div>
163 <div id="section1" class="doctools_section"><h2><a name="section1">Description</a></h2>
164 <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
165 code from within <a href="http://core.tcl-lang.org/tcl">Tcl</a> scripts.</p>
166 <p>This document is the reference manpage for the <b class="package">critcl::class</b>
167 package. This package provides convenience commands for advanced
168 functionality built on top of the core.</p>
169 <p>With it a user wishing to create a C level object with class
170 and instance commands can concentrate on specifying the class- and
171 instance-variables and -methods in a manner similar to a TclOO class,
172 while all the necessary boilerplate around it is managed by this
173 package.</p>
174 <p>Its intended audience are mainly developers wishing to write
175 Tcl packages with embedded C code.</p>
176 <p>This package resides in the Core Package Layer of CriTcl.</p>
177 <p><img alt="arch_core" src="../image/arch_core.png"></p>
178 </div>
179 <div id="section2" class="doctools_section"><h2><a name="section2">API</a></h2>
180 <dl class="doctools_definitions">
181 <dt><a name="1"><b class="cmd">::critcl::class::define</b> <i class="arg">name</i> <i class="arg">script</i></a></dt>
182 <dd><p>This is the main command to define a new class <i class="arg">name</i>, where
183 <i class="arg">name</i> is the name of the Tcl command representing the class,
184 i.e. the <i class="term">class command</i>. The <i class="arg">script</i> provides the
185 specification of the class, i.e. information about included headers,
186 class- and instance variables, class- and instance-methods, etc.
187 See the section <span class="sectref"><a href="#section3">Class Specification API</a></span> below for the
188 detailed list of the available commands and their semantics.</p></dd>
189 </dl>
190 </div>
191 <div id="section3" class="doctools_section"><h2><a name="section3">Class Specification API</a></h2>
192 <p>Here we documents all class specification commands available inside of
193 the class definition script argument of <b class="cmd">::critcl::class::define</b>.</p>
194 <div id="subsection1" class="doctools_subsection"><h3><a name="subsection1">General configuration</a></h3>
195 <dl class="doctools_definitions">
196 <dt><a name="2"><b class="cmd">include</b> <i class="arg">path</i></a></dt>
197 <dd><p>This command specifies the path of a header file to include within the
198 code generated for the class. This is separate from the <b class="cmd">support</b>
199 because the generated include directives will be put at the very
200 beginning of the generated code. This is done to allow the use of the
201 imported declarations within the instance type, and elsewhere.</p>
202 <p>Calls to this command are cumulative.
203 It is of course possible to not use this command at all, for classes
204 not making use of external definitions.</p>
205 <p>The result is the empty string.</p></dd>
206 <dt><a name="3"><b class="cmd">support</b> <i class="arg">code</i></a></dt>
207 <dd><p>This command specifies supporting C code, i.e. any definitions (types,
208 functions, etc.) needed by the <em>whole</em> class and not fitting into
209 class- and instance-methods. The code is embedded at global level,
210 outside of any function or other definition.</p>
211 <p>Calls to this command are cumulative.
212 It is of course possible to not use this command at all, for classes
213 not requiring supporting code.</p>
214 <p>The result of the command is the empty string.</p></dd>
215 <dt><a name="4"><b class="cmd">type</b> <i class="arg">name</i></a></dt>
216 <dd><p>This command specifies the name of an external C type to be used as
217 the type of the instance structure.</p>
218 <p>Initialization and release of the structure with the given type
219 are the responsibility of the user, through <b class="cmd">constructor</b> and
220 <b class="cmd">destructor</b> code fragments.</p>
221 <p><em>Attention:</em> Using this command precludes the use of
222 regular class- and instance variables. It further precludes the use of
223 <b class="cmd">method-introspection</b> as well, as this make use of generated
224 instance-variables.</p>
225 <p>If class- and/or instance-variable have to be used in
226 conjunction with an external C type, simply create and use a class- or
227 instance-variable with that type.</p>
228 <p>The result of the command is the empty string.</p></dd>
229 </dl>
230 </div>
231 <div id="subsection2" class="doctools_subsection"><h3><a name="subsection2">Class lifetime management</a></h3>
232 <dl class="doctools_definitions">
233 <dt><a name="5"><b class="cmd">classconstructor</b> <i class="arg">body</i></a></dt>
234 <dd><p>This command specifies a C code block surrounding the initialization
235 of the class variables, i.e. the fields of the class structure.
236 <em>Note</em> that allocation and release of the class structure itself
237 is done by the system andf not the responsibility of the user.</p>
238 <p>For the initialization (and release) of a class variable it is
239 recommended to use the <i class="arg">constructor</i> and <i class="arg">destructor</i>
240 arguments of the variable's definition (See command
241 <b class="cmd">classvariable</b>) for this instead of using a separate
242 <b class="cmd">classconstructor</b>.</p>
243 <p>This is an optional command. Using it more than once is allowed
244 too and each use will add another C code fragment to use during
245 construction. I.e. multiple calls aggregate.</p>
246 <p>The C code blocks of multiple calls (including the constructors
247 of classvariable definitions) are executed in order of specification.</p>
248 <p>The result of the command is the empty string.</p>
249 <p>The C code in <i class="arg">body</i> has access to the following
250 environment:</p>
251 <dl class="doctools_definitions">
252 <dt><b class="variable">interp</b></dt>
253 <dd><p>Pointer to the Tcl interpreter (Tcl_Interp*) the
254 class structure will be associated with. It enables the generation
255 of a Tcl error message should construction fail.</p></dd>
256 <dt><b class="variable">class</b></dt>
257 <dd><p>Pointer to the class structure to initialize.</p></dd>
258 <dt>error</dt>
259 <dd><p>A C code label the constructor can jump to should it have
260 to signal a construction failure. It is the responsibility of the
261 constructor to release any variables already initialized before
262 jumping to this label. This also why the 'execution in order of
263 specification' is documented and can be relied on. It gives us the
264 knowledge which other constructors have already been run and
265 initialized what other fields.</p></dd>
266 </dl></dd>
267 <dt><a name="6"><b class="cmd">classdestructor</b> <i class="arg">body</i></a></dt>
268 <dd><p>This command specifies a C code block surrounding the release of the
269 class variables, i.e. the fields of the class structure.
270 <em>Note</em> that allocation and release of the class structure itself
271 is done by the system and not the responsibility of the user.</p>
272 <p>For the initialization (and release) of a class variable it is
273 recommended to use the <i class="arg">constructor</i> and <i class="arg">destructor</i>
274 arguments of the variable's definition (See command
275 <b class="cmd">classvariable</b>) for this instead of using a separate
276 <b class="cmd">classconstructor</b>.</p>
277 <p>This is an optional command. Using it more than once is allowed
278 too and each use will add another C code fragment to use during
279 construction. I.e. multiple calls aggregate.</p>
280 <p>The C code blocks of multiple calls (including the constructors
281 of class variable definitions) are executed in order of specification.</p>
282 <p>The result of the command is the empty string.</p>
283 <p>The C code in <i class="arg">body</i> has access to the same
284 environment as the class constructor code blocks.</p></dd>
285 </dl>
286 </div>
287 <div id="subsection3" class="doctools_subsection"><h3><a name="subsection3">Instance lifetime management</a></h3>
288 <dl class="doctools_definitions">
289 <dt><a name="7"><b class="cmd">constructor</b> <i class="arg">body</i> <span class="opt">?<i class="arg">postbody</i>?</span></a></dt>
290 <dd><p>This command specifies a C code block surrounding the initialization
291 of the instance variables, i.e. the fields of the instance structure.
292 <em>Note</em> that allocation and release of the instance structure
293 itself is done by the system and not the responsibility of the user.
294 <em>On the other hand</em>, if an external <b class="cmd">type</b> was specified
295 for the instance structure, then instance variables are not possible,
296 and the system has no knowledge of the type's structure. In that case
297 it is the responsibility of the <i class="arg">body</i> to allocate and free the
298 structure itself too.</p>
299 <p>For the initialization (and release) of an instance variable it
300 is recommended to use the <i class="arg">constructor</i> and <i class="arg">destructor</i>
301 arguments of the variable's definition (See command <b class="cmd">insvariable</b>)
302 for this instead of using a separate <b class="cmd">constructor</b>.</p>
303 <p>This is an optional command. Using it more than once is allowed
304 too and each use will add another C code fragment to use during
305 construction. I.e. multiple calls aggregate.</p>
306 <p>The C code blocks of multiple calls (including the constructors
307 of instance variable definitions) are executed in order of specification.</p>
308 <p>The result of the command is the empty string.</p>
309 <p>The C code in <i class="arg">body</i> has access to the following
310 environment:</p>
311 <dl class="doctools_definitions">
312 <dt><b class="variable">interp</b></dt>
313 <dd><p>Pointer to the Tcl interpreter (Tcl_Interp*) the
314 instance structure will be associated with. It enables the generation
315 of a Tcl error message should construction fail.</p></dd>
316 <dt><b class="variable">instance</b></dt>
317 <dd><p>Pointer to the instance structure to initialize.</p></dd>
318 <dt>error</dt>
319 <dd><p>A C code label the constructor can jump to should it have
320 to signal a construction failure. It is the responsibility of the
321 constructor to release any variables already initialized before
322 jumping to this label. This also why the 'execution in order of
323 specification' is documented and can be relied on. It gives us the
324 knowledge which other constructors have already been run and
325 initialized what other fields.</p></dd>
326 </dl>
327 <p>The C code in <i class="arg">postbody</i> is responsible for construction
328 actions to be done after the primary construction was done and the
329 Tcl-level instance command was successfully created. It has access to
330 a slightly different environment:</p>
331 <dl class="doctools_definitions">
332 <dt><b class="variable">interp</b></dt>
333 <dd><p>Pointer to the Tcl interpreter (Tcl_Interp*) the
334 instance structure will be associated with. It enables the generation
335 of a Tcl error message should construction fail.</p></dd>
336 <dt><b class="variable">instance</b></dt>
337 <dd><p>Pointer to the instance structure to initialize.</p></dd>
338 <dt><b class="variable">cmd</b></dt>
339 <dd><p>The Tcl_Command token of the Tcl-level instance
340 command.</p></dd>
341 <dt><b class="variable">fqn</b></dt>
342 <dd><p>The fully qualified name of the instance command,
343 stored in a Tcl_Obj*.</p></dd>
344 </dl></dd>
345 <dt><a name="8"><b class="cmd">destructor</b> <i class="arg">body</i></a></dt>
346 <dd><p>This command specifies a C code block surrounding the release of the
347 instance variables, i.e. the fields of the instance structure.
348 <em>Note</em> that allocation and release of the instance structure
349 itself is done by the system and not the responsibility of the user.
350 <em>On the other hand</em>, if an external <b class="cmd">type</b> was specified
351 for the instance structure, then instance variables are not possible,
352 and the system has no knowledge of the type's structure. In that case
353 it is the responsibility of the <i class="arg">body</i> to allocate and free the
354 structure itself too.</p>
355 <p>For the initialization (and release) of an instance variable it
356 is recommended to use the <i class="arg">constructor</i> and <i class="arg">destructor</i>
357 arguments of the variable's definition (See command <b class="cmd">insvariable</b>)
358 for this instead of using a separate <b class="cmd">constructor</b>.</p>
359 <p>This is an optional command. Using it more than once is allowed
360 too and each use will add another C code fragment to use during
361 construction. I.e. multiple calls aggregate.</p>
362 <p>The C code blocks of multiple calls (including the constructors
363 of instance variable definitions) are executed in order of specification.</p>
364 <p>The result of the command is the empty string.</p>
365 <p>The C code in <i class="arg">body</i> has access to the following
366 environment:</p>
367 <dl class="doctools_definitions">
368 <dt><b class="variable">instance</b></dt>
369 <dd><p>Pointer to the instance structure to release.</p></dd>
370 </dl></dd>
371 </dl>
372 </div>
373 <div id="subsection4" class="doctools_subsection"><h3><a name="subsection4">Class variables and methods</a></h3>
374 <dl class="doctools_definitions">
375 <dt><a name="9"><b class="cmd">classvariable</b> <i class="arg">ctype</i> <i class="arg">name</i> <span class="opt">?<i class="arg">comment</i>?</span> <span class="opt">?<i class="arg">constructor</i>?</span> <span class="opt">?<i class="arg">destructor</i>?</span></a></dt>
376 <dd><p>This command specifies a field in the class structure of the class.
377 Multiple fields can be specified, and are saved in the order
378 specified.</p>
379 <p><em>Attention:</em> Specification of a class variable precludes
380 the use of an external C <b class="cmd">type</b> for the instance structure.</p>
381 <p><em>Attention:</em> Specification of a class variable
382 automatically causes the definition of an instance variable named
383 <b class="const">class</b>, pointing to the class structure.</p>
384 <p>Beyond the basic <i class="arg">name</i> and C type of the new variable the
385 definition may also contain a <i class="arg">comment</i> describing it, and C code
386 blocks to initialize and release the variable.
387 These are effectively local forms of the commands
388 <b class="cmd">classconstructor</b> and <b class="cmd">classdestructor</b>. Please read their
389 descriptions for details regarding the C environment available to the
390 code.</p>
391 <p>The comment, if specified will be embedded into the generated C
392 code for easier cross-referencing from generated &quot;<b class="file">.c</b>&quot; file to
393 class specification.</p></dd>
394 <dt><a name="10"><b class="cmd">classmethod</b> <i class="arg">name</i> <b class="method">command</b> <i class="arg">arguments</i> <i class="arg">body</i></a></dt>
395 <dd><p>This command specifies a class method and the C code block
396 implementing its functionality. This is the first of three forms. The
397 method is specified like a <b class="cmd">critcl::ccommand</b>, with a fixed set of
398 C-level arguments. The <i class="arg">body</i> has to perform everything
399 (i.e. argument extraction, checking, result return, and of course the
400 actual functionality) by itself.</p>
401 <p>For this the <i class="arg">body</i> has access to</p>
402 <dl class="doctools_definitions">
403 <dt><b class="variable">class</b></dt>
404 <dd><p>Pointer to the class structure.</p></dd>
405 <dt><b class="variable">interp</b></dt>
406 <dd><p>Pointer to the Tcl interpreter (Tcl_Interp*) the
407 class structure is associated with</p></dd>
408 <dt><b class="variable">objc</b></dt>
409 <dd><p>The number of method arguments.</p></dd>
410 <dt><b class="variable">objv</b></dt>
411 <dd><p>The method arguments, as C array of Tcl_Obj pointers.</p></dd>
412 </dl>
413 <p>The <i class="arg">arguments</i> of the definition are only a human readable form
414 of the method arguments and syntax and are not used in the C code,
415 except as comments put into the generated code. Again, it is the
416 responsibility of the <i class="arg">body</i> to check the number of arguments,
417 extract them, check their types, etc.</p></dd>
418 <dt><a name="11"><b class="cmd">classmethod</b> <i class="arg">name</i> <b class="method">proc</b> <i class="arg">arguments</i> <i class="arg">resulttype</i> <i class="arg">body</i></a></dt>
419 <dd><p>This command specifies a class method and the C code block
420 implementing its functionality. This is the second of three forms. The
421 method is specified like a <b class="cmd">critcl::cproc</b>.
422 Contrary to the first variant here the <i class="arg">arguments</i> are computer
423 readable, expected to be in the same format as the <i class="arg">arguments</i> of
424 <b class="cmd">critcl::cproc</b>. The same is true for the <i class="arg">resulttype</i>.
425 The system automatically generates a wrapper doing argument checking
426 and conversion, and result conversion, like for <b class="cmd">critcl::cproc</b>.</p>
427 <p>The <i class="arg">body</i> has access to</p>
428 <dl class="doctools_definitions">
429 <dt><b class="variable">class</b></dt>
430 <dd><p>Pointer to the class structure.</p></dd>
431 <dt><b class="variable">interp</b></dt>
432 <dd><p>Pointer to the Tcl interpreter (Tcl_Interp*) the
433 class structure is associated with</p></dd>
434 <dt>...</dt>
435 <dd><p>All <i class="arg">arguments</i> under their specified names and C types
436 as per their definition.</p></dd>
437 </dl></dd>
438 <dt><a name="12"><b class="cmd">classmethod</b> <i class="arg">name</i> <b class="method">as</b> <i class="arg">funname</i> <span class="opt">?<i class="arg">arg</i>...?</span></a></dt>
439 <dd><p>This command specifies a class method and the C code block
440 implementing its functionality. This is the third and last of three
441 forms.</p>
442 <p>The class method is implemented by the external function
443 <i class="arg">funname</i>, i.e. a function which is declared outside of the class
444 code itself, or in a <b class="cmd">support</b> block.</p>
445 <p>It is assumed that the first four arguments of that function
446 represent the parameters</p>
447 <dl class="doctools_definitions">
448 <dt><b class="variable">class</b></dt>
449 <dd><p>Pointer to the class structure.</p></dd>
450 <dt><b class="variable">interp</b></dt>
451 <dd><p>Pointer to the Tcl interpreter (Tcl_Interp*) the
452 class structure is associated with</p></dd>
453 <dt><b class="variable">objc</b></dt>
454 <dd><p>The number of method arguments.</p></dd>
455 <dt><b class="variable">objv</b></dt>
456 <dd><p>The method arguments, as C array of Tcl_Obj pointers.</p></dd>
457 </dl>
458 <p>Any additional arguments specified will be added after these and are
459 passed into the C code as is, i.e. are considered to be C expressions.</p></dd>
460 </dl>
461 </div>
462 <div id="subsection5" class="doctools_subsection"><h3><a name="subsection5">Instance variables and methods</a></h3>
463 <dl class="doctools_definitions">
464 <dt><a name="13"><b class="cmd">insvariable</b> <i class="arg">ctype</i> <i class="arg">name</i> <span class="opt">?<i class="arg">comment</i>?</span> <span class="opt">?<i class="arg">constructor</i>?</span> <span class="opt">?<i class="arg">destructor</i>?</span></a></dt>
465 <dd><p>This command specifies a field in the instance structure of the class.
466 Multiple fields can be specified, and are saved in the order
467 specified.</p>
468 <p><em>Attention:</em> Specification of an instance variable
469 precludes the use of an external C <b class="cmd">type</b> for the instance
470 structure.</p>
471 <p><em>Attention:</em> Specification of an instance variable
472 automatically causes the definition of an instance variable of type
473 <b class="const">Tcl_Command</b>, and named <b class="const">cmd</b>, holding the token of the
474 instance command, and the definition of an instance method named
475 <b class="const">destroy</b>. This implicit instance variable is managed by the
476 system.</p>
477 <p>Beyond the basic <i class="arg">name</i> and C type of the new variable the
478 definition may also contain a <i class="arg">comment</i> describing it, and C code
479 blocks to initialize and release the variable.
480 These are effectively local forms of the commands <b class="cmd">constructor</b>
481 and <b class="cmd">destructor</b>. Please read their descriptions for details
482 regarding the C environment available to the code.</p>
483 <p>The comment, if specified will be embedded into the generated C
484 code for easier cross-referencing from generated &quot;<b class="file">.c</b>&quot; file to
485 class specification.</p></dd>
486 <dt><a name="14"><b class="cmd">method</b> <i class="arg">name</i> <b class="method">command</b> <i class="arg">arguments</i> <i class="arg">body</i></a></dt>
487 <dd><p>This command specifies an instance method and the C code block
488 implementing its functionality. This is the first of three forms. The
489 method is specified like a <b class="cmd">critcl::ccommand</b>, with a fixed set of
490 C-level arguments. The <i class="arg">body</i> has to perform everything
491 (i.e. argument extraction, checking, result return, and of course the
492 actual functionality) by itself.</p>
493 <p>For this the <i class="arg">body</i> has access to</p>
494 <dl class="doctools_definitions">
495 <dt><b class="variable">instance</b></dt>
496 <dd><p>Pointer to the instance structure.</p></dd>
497 <dt><b class="variable">interp</b></dt>
498 <dd><p>Pointer to the Tcl interpreter (Tcl_Interp*) the
499 instance structure is associated with</p></dd>
500 <dt><b class="variable">objc</b></dt>
501 <dd><p>The number of method arguments.</p></dd>
502 <dt><b class="variable">objv</b></dt>
503 <dd><p>The method arguments, as C array of Tcl_Obj pointers.</p></dd>
504 </dl>
505 <p>The <i class="arg">arguments</i> of the definition are only a human readable form
506 of the method arguments and syntax and are not used in the C code,
507 except as comments put into the generated code. Again, it is the
508 responsibility of the <i class="arg">body</i> to check the number of arguments,
509 extract them, check their types, etc.</p></dd>
510 <dt><a name="15"><b class="cmd">method</b> <i class="arg">name</i> <b class="method">proc</b> <i class="arg">arguments</i> <i class="arg">resulttype</i> <i class="arg">body</i></a></dt>
511 <dd><p>This command specifies an instance method and the C code block
512 implementing its functionality. This is the second of three
513 forms. The method is specified like a <b class="cmd">critcl::cproc</b>.
514 Contrary to the first variant here the <i class="arg">arguments</i> are computer
515 readable, expected to be in the same format as the <i class="arg">arguments</i> of
516 <b class="cmd">critcl::cproc</b>. The same is true for the <i class="arg">resulttype</i>.
517 The system automatically generates a wrapper doing argument checking
518 and conversion, and result conversion, like for <b class="cmd">critcl::cproc</b>.</p>
519 <p>The <i class="arg">body</i> has access to</p>
520 <dl class="doctools_definitions">
521 <dt><b class="variable">instance</b></dt>
522 <dd><p>Pointer to the instance structure.</p></dd>
523 <dt><b class="variable">interp</b></dt>
524 <dd><p>Pointer to the Tcl interpreter (Tcl_Interp*) the
525 instance structure is associated with</p></dd>
526 <dt>...</dt>
527 <dd><p>All <i class="arg">arguments</i> under their specified names and C types
528 as per their definition.</p></dd>
529 </dl></dd>
530 <dt><a name="16"><b class="cmd">method</b> <i class="arg">name</i> <b class="method">as</b> <i class="arg">funname</i> <span class="opt">?<i class="arg">arg</i>...?</span></a></dt>
531 <dd><p>This command specifies an instance method and the C code block
532 implementing its functionality. This is the third and last of three
533 forms.</p>
534 <p>The instance method is implemented by the external function
535 <i class="arg">funname</i>, i.e. a function which is declared outside of the instance
536 code itself, or in a <b class="cmd">support</b> block.</p>
537 <p>It is assumed that the first four arguments of that function
538 represent the parameters</p>
539 <dl class="doctools_definitions">
540 <dt><b class="variable">instance</b></dt>
541 <dd><p>Pointer to the instance structure.</p></dd>
542 <dt><b class="variable">interp</b></dt>
543 <dd><p>Pointer to the Tcl interpreter (Tcl_Interp*) the
544 instance structure is associated with</p></dd>
545 <dt><b class="variable">objc</b></dt>
546 <dd><p>The number of method arguments.</p></dd>
547 <dt><b class="variable">objv</b></dt>
548 <dd><p>The method arguments, as C array of Tcl_Obj pointers.</p></dd>
549 </dl>
550 <p>Any additional arguments specified will be added after these and are
551 passed into the C code as is, i.e. are considered to be C expressions.</p></dd>
552 <dt><a name="17"><b class="cmd">method_introspection</b></a></dt>
553 <dd><p>This command generates one class- and one instance-method both of
554 which will return a list of the instance methods of the class, and
555 supporting structures, like the function to compute the information,
556 and a class variable caching it.</p>
557 <p>The two methods and the class variable are all named
558 <b class="const">methods</b>.</p></dd>
559 </dl>
560 </div>
561 <div id="subsection6" class="doctools_subsection"><h3><a name="subsection6">Context dependent interactions</a></h3>
562 <p>This section documents the various interactions between the
563 specification commands. While these are are all documented with the
564 individual commands here they are pulled together to see at a glance.</p>
565 <ol class="doctools_enumerated">
566 <li><p>If you are using the command <b class="cmd">type</b> to specify an external
567 C type to use for the instance structure you are subject to
568 the following constraints and rules:</p>
569 <ol class="doctools_enumerated">
570 <li><p>You cannot define your own instance variables.</p></li>
571 <li><p>You cannot define your own class variables.</p></li>
572 <li><p>You cannot use <b class="cmd">method_introspection</b>.</p></li>
573 <li><p>You have to allocate and release the instance structure on your
574 own, through <b class="cmd">constructor</b> and <b class="cmd">destructor</b> code blocks.</p></li>
575 </ol>
576 </li>
577 <li><p>If you declare class variables you are subject to the
578 following constraints and rules:</p>
579 <ol class="doctools_enumerated">
580 <li><p>You cannot use <b class="cmd">type</b>.</p></li>
581 <li><p>The system generates an instance variable <b class="const">class</b> for
582 you, which points from instance to class structure. This makes
583 you also subject to the rules below, for instance variables.</p></li>
584 </ol>
585 </li>
586 <li><p>If you declare instance variables (possibly automatic, see
587 above) you are subject to following constraints and rules:</p>
588 <ol class="doctools_enumerated">
589 <li><p>You cannot use <b class="cmd">type</b>.</p></li>
590 <li><p>The system generates and manages an instance variable
591 <b class="const">cmd</b> for you, which holds the Tcl_Command token
592 of the instance command.</p></li>
593 <li><p>The system generates an instance method <b class="const">destroy</b> for
594 you.</p></li>
595 <li><p>The system manages allocation and release of the instance
596 structure for you. You have to care only about the instance
597 variables themselves.</p></li>
598 </ol>
599 </li>
600 </ol>
601 </div>
602 </div>
603 <div id="section4" class="doctools_section"><h2><a name="section4">Example</a></h2>
604 <p>The example shown below is the specification of queue data structure,
605 with most of the method implementations and support code omitted to
606 keep the size down.</p>
607 <p>The full implementation can be found in the directory
608 &quot;<b class="file">examples/queue</b>&quot; of the critcl source distribution/repository.</p>
609 <pre class="doctools_example">
610 package require Tcl 8.6
611 package require critcl 3.2
612 critcl::buildrequirement {
613 package require critcl::class ; # DSL, easy spec of Tcl class/object commands.
614 }
615 critcl::cheaders util.h
616 critcl::class::define ::queuec {
617 include util.h
618 insvariable Tcl_Obj* unget {
619 List object unget elements
620 } {
621 instance-&gt;unget = Tcl_NewListObj (0,NULL);
622 Tcl_IncrRefCount (instance-&gt;unget);
623 } {
624 Tcl_DecrRefCount (instance-&gt;unget);
625 }
626 insvariable Tcl_Obj* queue {
627 List object holding the main queue
628 } {
629 instance-&gt;queue = Tcl_NewListObj (0,NULL);
630 Tcl_IncrRefCount (instance-&gt;queue);
631 } {
632 Tcl_DecrRefCount (instance-&gt;queue);
633 }
634 insvariable Tcl_Obj* append {
635 List object holding new elements
636 } {
637 instance-&gt;append = Tcl_NewListObj (0,NULL);
638 Tcl_IncrRefCount (instance-&gt;append);
639 } {
640 Tcl_DecrRefCount (instance-&gt;append);
641 }
642 insvariable int at {
643 Index of next element to return from the main queue
644 } {
645 instance-&gt;at = 0;
646 }
647 support {... queue_peekget, queue_size, etc.}
648 method clear {} {...}
649 method destroy {...}
650 method get as queue_peekget 1
651 method peek as queue_peekget 0
652 method put {item ...}
653 method size {} {
654 if ((objc != 2)) {
655 Tcl_WrongNumArgs (interp, 2, objv, NULL);
656 return TCL_ERROR;
657 }
658 Tcl_SetObjResult (interp, Tcl_NewIntObj (queue_size (instance, NULL, NULL, NULL)));
659 return TCL_OK;
660 }
661 method unget {item} {...}
662 }
663 package provide queuec 1
664 </pre>
665 </div>
666 <div id="section5" class="doctools_section"><h2><a name="section5">Authors</a></h2>
667 <p>Andreas Kupries</p>
668 </div>
669 <div id="section6" class="doctools_section"><h2><a name="section6">Bugs, Ideas, Feedback</a></h2>
670 <p>This document, and the package it describes, will undoubtedly contain
671 bugs and other problems.
672 Please report such at <a href="https://github.com/andreas-kupries/critcl">https://github.com/andreas-kupries/critcl</a>.
673 Please also report any ideas for enhancements you may have for either
674 package and/or documentation.</p>
675 </div>
676 <div id="keywords" class="doctools_section"><h2><a name="keywords">Keywords</a></h2>
677 <p><a href="../index.html#c_class">C class</a>, <a href="../index.html#c_code">C code</a>, <a href="../index.html#c_instance">C instance</a>, <a href="../index.html#c_object">C object</a>, <a href="../index.html#embedded_c_code">Embedded C Code</a>, <a href="../index.html#code_generator">code generator</a>, <a href="../index.html#compile_run">compile &amp; run</a>, <a href="../index.html#compiler">compiler</a>, <a href="../index.html#dynamic_code_generation">dynamic code generation</a>, <a href="../index.html#dynamic_compilation">dynamic compilation</a>, <a href="../index.html#generate_package">generate package</a>, <a href="../index.html#linker">linker</a>, <a href="../index.html#on_demand_compilation">on demand compilation</a>, <a href="../index.html#on_the_fly_compilation">on-the-fly compilation</a></p>
678 </div>
679 <div id="category" class="doctools_section"><h2><a name="category">Category</a></h2>
680 <p>Glueing/Embedded C code</p>
681 </div>
682 <div id="copyright" class="doctools_section"><h2><a name="copyright">Copyright</a></h2>
683 <p>Copyright &copy; 2011-2024 Andreas Kupries</p>
684 </div>
685 </div></body></html>