File: external_enum.inc

package info (click to toggle)
critcl 3.3.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,680 kB
  • sloc: ansic: 41,058; tcl: 12,090; sh: 7,230; pascal: 3,456; asm: 3,058; ada: 1,681; cpp: 1,001; cs: 879; makefile: 333; perl: 104; xml: 95; f90: 10
file content (57 lines) | stat: -rw-r--r-- 1,843 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
[comment {-*- mode: tcl ; fill-column: 90 -*-}]

This section demonstrates how to convert from any kind of enumeration provided by an
external library to Tcl strings, and the converse.

[list_begin enumerated]

[enum] For all that this is a part of how to
[sectref {Using External Libraries} {Use External Libraries}], for the demonstratation
only the basics are needed.

[enum][vset base][example {
    package require critcl::emap

    # no header included due to use of literal ints instead of symbolic names

    critcl::emap::def yaml_sequence_style_t {
	any   0
	block 1
	flow  2
    }

    # encode: style to int
    critcl::cproc encode {yaml_sequence_style_t style} int {
	return style;
    }

    # decode: int to style
    critcl::cproc decode {int style} yaml_sequence_style_t {
	return style;
    }
}][vset rebuild]

[enum] The map converts between the Tcl level strings listed on the left side to the C
values on the right side, and the reverse.

[enum] It automatically generates [cmd critcl::argtype] and [cmd critcl::resulttype]
definitions.

[enum] [emph Attention] Like the default values for [cmd cproc] arguments, and the results
for [cmd cconst] definitions the values on the right side have to be proper C
rvalues. They have to match C type [type int].

[para] In other words, it is perfectly ok to use the symbolic names provided by the header
file of the external library.

[para] [emph Attention] This however comes at a loss in efficiency. As [vset critcl] then
has no insight into the covered range of ints, gaps, etc. it has to perform a linear
search when mapping from C to Tcl. When it knows the exact integer values it can use a
table lookup instead.

[para] [emph Attention] It also falls back to a search if a lookup table would contain more
than 50 entries.

[list_end]

[para] Packages: [term critcl::emap]