critcl::enum(3tcl)

πŸ“¦
version 3.3.1 Β· 3 versions
, in ubuntu resolute, ubuntu stonking

πŸ’– 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 and Tcl_Obj'ects, and the names of both are derived from name.

Let's break it down:

  • The definition dictionary 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 +list and tcl. All modes can be used together. The default mode is tcl. Using mode +list implies tcl as well.
  • For mode tcl: the new function takes two arguments β€” a Tcl_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 a Tcl_Obj* pointer to the requested string constant.
  • For mode +list: everything about tcl applies, PLUS an additional function gets generated. That one takes three arguments, in order: a Tcl_Interp* pointer refering to the interpreter holding the string pool, an int holding 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 a Tcl_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, and name has 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 +list function 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 for critcl::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 for critcl::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 (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