critcl::enum(3tcl)
π critcl::enum β the manual, but make it make sense π
okay so bestie, you're reading the critcl::enum(3tcl) page. Yes, that 3tcl bit is just saying "this is a Tcl manpage, section 3." Sit down, grab a strawberry, we're doing stringβinteger mapping today and it's gonna be cute. π
β¨ NAME β¨
critcl::enum β CriTcl β Wrap Support β String/Integer mapping
β¨ SYNOPSIS β¨
package require Tcl 8.6
package require critcl ?3.2?
package require critcl::enum ?1.2?
::critcl::enum::def name definition ?mode?
β¨ DESCRIPTION β¨
Hiii, welcome to the C Runtime In Tcl (short: CriTcl), which is a whole system for embedding and using C code from inside your Tcl [http://core.tcl-lang.org/tcl] scripts. iconic behaviour honestly.
This doc right here is the reference manpage for the critcl::enum package. This package hands you convenience commands for advanced functionality, built on top of BOTH the critcl core AND the critcl::literals package. teamwork π
Think of it as an extended form of a string pool. It doesn't just turn integer values into Tcl-level strings β it also does the reverse direction, turning strings back into their associated integer values. bi-directional, obsessed.
So like, in essence: it gives you a bi-directional mapping between a C enumeration type and a set of strings, one string per enumeration value. β οΈ Important vibe check though: the C enumeration in question is created by the definition. It is not possible to use the symbols of an existing enumeration type. No stealing other people's enums, bestie.
This package exists to make declaring and managing those mappings easy. It uses a string pool for one of the two directions, leaning on the pool's ability to return shared literals and conserve memory. frugal queen π
Who's it for? Mainly developers who want to write Tcl packages with embedded C code. iykyk.
This package resides in the Core Package Layer of CriTcl, like so:
+----------------+
|Applications |
| critcl |
| critcl::app |
+----------------+
*================*
|Core Packages |
| critcl |
| critcl::util |
*================*
+----------------+
|Support Packages|
| stubs::* |
| md5, platform |
| ... |
+----------------+
β¨ API β¨
::critcl::enum::def name definition ?mode?β this command defines two C functions for converting between C values andTcl_Obj'ects, and the names of both are derived fromname.
Let's break it down:
- The
definitiondictionary is the thing that provides the mapping from the C-level symbolic names over to the strings themselves. π¦ - The mode-list configures the output a lil bit. The two allowed modes are
+listandtcl. All modes can be used together. The default mode istcl. Using mode+listimpliestclas well. - For mode
tcl: the new function takes two arguments β aTcl_Interp*pointer refering to the interpreter holding the string pool, and a code of type"name_pool_names"(see below), which is the symbolic name of the string to return. The result of the function is aTcl_Obj*pointer to the requested string constant. - For mode
+list: everything abouttclapplies, PLUS an additional function gets generated. That one takes three arguments, in order: aTcl_Interp*pointer refering to the interpreter holding the string pool, anintholding the size of the last argument, and an array of type"name_pool_names"holding the codes (see below) β the symbolic names of the strings to return. The result of that function is aTcl_Obj*pointer to a Tcl list holding the requested string constants. π - The underlying string pool is automatically initialized on first access, and finalized on interpreter destruction. no manual labour required β¨
- The package generates multiple things (declarations and definitions) with names derived from
name, andnamehas to be a proper C identifier. non-negotiable.
the generated things π
name_pool_namesβ the C enumeration type containing the specified symbolic names.name_ToObjβ the function converting from integer value to Tcl string. Its signature is:
Tcl_Obj* name_ToObj (Tcl_Interp* interp, name_names literal);
name_ToObjListβ the mode+listfunction converting from integer array to Tcl list of strings. Its signature is:
Tcl_Obj* name_ToObjList (Tcl_Interp* interp, int c, name_names* literal);
name_GetFromObjβ the function converting from Tcl string to integer value. Its signature is:
int name_GetFromObj (Tcl_Interp* interp, Tcl_Obj* obj, int flags, int* literal);
The flags are like for Tcl_GetIndexFromObj. fr fr.
name.hβ a header file containing the declarations for the converter functions, for use by other parts of the system, if necessary.
The generated file is stored in a place where it will not interfere with the overall system outside of the package, yet will also be available for easy inclusion by package files (csources). best of both worlds, we love to see it π
nameβ at the level of critcl itself, the command registers a new result-type forcritcl::cproc, which takes an integer result from the function and converts it to the equivalent string in the pool for the script.nameβ at the level of critcl itself, the command registers a new argument-type forcritcl::cproc, which takes a Tcl string and converts it to the equivalent integer for delivery to the function. (Yes, same name, two registrations β one arg, one result. not me double-dipping π)
β¨ EXAMPLE β¨
The example shown below is the specification for a set of actions, methods, and the like, a function may take as argument.
package require Tcl 8.6
package require critcl 3.2
critcl::buildrequirement {
package require critcl::enum
}
critcl::enum::def action {
w_create "create"
w_directory"directory"
w_events "events"
w_file "file"
w_handler "handler"
w_remove "remove"
}
# Declarations: action.h
# Type: action_names
# Accessor: Tcl_Obj* action_ToObj (Tcl_Interp* interp, int literal);
# Accessor: int action_GetFromObj (Tcl_Interp* interp, Tcl_Obj* o, int flags, int* literal);
# ResultType: action
# ArgType: action
yes bestie, w_directory"directory" really is squished together with no space in the original. copy it exactly like that, don't "fix" it. π
β¨ AUTHORS β¨
Andreas Kupries π
β¨ BUGS, IDEAS, FEEDBACK β¨
This document, and the package it describes, will undoubtedly contain bugs and other problems. (their words, not mine, but honestly? relatable.)
Please report such at https://github.com/andreas-kupries/critcl
Please also report any ideas for enhancements you may have for either the package and/or the documentation. they want the feedback, give them the feedback π«Ά
β¨ KEYWORDS β¨
C code, Embedded C Code, Tcl Interp Association, code generator, compile & run, compiler, conversion, dynamic code generation, dynamic compilation, generate package, int to string mapping, linker, literal pool, on demand compilation, on-the-fly compilation, singleton, string pool, string to int mapping
β¨ CATEGORY β¨
Glueing/Embedded C code
β¨ COPYRIGHT β¨
Copyright (c) 2011-2024 Andreas Kupries
and that's the whole page, you ate that π this was critcl::enum, version 1.2, from source doc, and the copyright runs 2011-2024 under Andreas Kupries. periodt. πβ¨π
π¦ where this page comes from
- ubuntu resolute: critcl 3.3.1+dfsg-1build1 Β· BSD
- ubuntu stonking: critcl 3.3.1+dfsg-1.1 Β· BSD
- Copyright (c) 2011-2024 Andreas Kupries
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms