critcl::literals(3tcl)

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

πŸ’– NAME πŸ’–

critcl::literals β€” CriTcl β€” Code Gen β€” Constant string pools

(btw this page lives in manual section 3tcl, so like, that's the whole critcl::literals(3tcl) thing bestie πŸ’…)


✨ SYNOPSIS ✨

package require Tcl 8.6

package require critcl ?3.2?

package require critcl::literals ?1.4?

::critcl::literals::def name definition ?mode?

πŸ¦‹ DESCRIPTION πŸ¦‹

Ok so first things first: welcome to the C Runtime In Tcl (we call it CriTcl for short, slay name honestly), which is a whole system for embedding and using C code from inside your Tcl scripts ✨ (yes, the Tcl over at [http://core.tcl-lang.org/tcl](http://core.tcl-lang.org/tcl), the one you already know and love).

This document right here is the reference manpage for the critcl::literals package. It gives you convenience commands for some genuinely advanced functionality, built on top of both critcl core and the critcl::iassoc package. Two parents, one iconic package. Periodt 🫢

So here's the situation. A LOT of packages have a fixed set of string constants that show up in one place or another. And most of the time?? People code it so that a fresh Tcl_Obj* gets made out of a C char* every single time the constant is needed. It's easy to do, yeah, but it's also just... wasting memory. Every time. Not cute 😭

This package exists so you can declare and manage string pools that don't waste memory, while being just as easy as the wasteful solution β€” and it hides literally all the attendant complexity from you. You don't have to think about it. She handles it πŸ’…

Who is this for? Mainly developers who wanna write Tcl packages with embedded C code in them. iykyk 🧚

This package resides in the Core Package Layer of CriTcl.

+----------------+
|Applications    |
| critcl         |
| critcl::app    |
+----------------+

*================*
|Core Packages   |
| critcl         |
| critcl::util   |
*================*

+----------------+
|Support Packages|
| stubs::*       |
| md5, platform  |
|  ...           |
+----------------+

πŸ‘‘ API πŸ‘‘

::critcl::literals::def πŸ‘‘

  • ::critcl::literals::def name definition ?mode? β€” This command defines a C function with the given name which provides access to a pool of constant strings with a Tcl interpreter. ✨

The definition dictionary provides the mapping from the C-level symbolic names to the string themselves.

The mode-list configures the output somewhat. The three allowed modes are c, +list and tcl. All modes can be used together. The default mode is tcl. Using mode +list implies tcl as well. (so like, +list doesn't roll alone, she brings tcl with her 🫢)

For mode tcl the new function has two arguments, a Tcl_Interp* pointer refering to the interpreter holding the string pool, and a code of type "name_names" (see below), the symbolic name of the literal to return. The result of the function is a Tcl_Obj* pointer to the requested string constant.

For mode c the new function has one argument, a code of type "name_names" (see below), the symbolic name of the literal to return. The result of the function is a const char* pointer to the requested string constant.

For mode +list all of tcl applies, plus an additional function is generated which 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_names" holding the codes (see below), the symbolic names of the literals to return. The result of the 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. You literally do not have to touch it, bestie πŸŽ€

🌈 What gets generated 🌈

The package generates multiple things (declarations and definitions) with names derived from name, which has to be a proper C identifier. Like, an actual C identifier, don't get cute with it πŸ™ˆ

  • name β€” The mode tcl function providing access to the string pool. Its signature is:
Tcl_Obj* name (Tcl_Interp* interp, name_names literal);
  • name_list β€” The mode +list function providing multi-access to the string pool. Its signature is:
Tcl_Obj* name_list (Tcl_Interp* interp, int c, name_names* literal);
  • name_cstr β€” The mode c function providing access to the string pool. Its signature is:
const char* name_cstr (name_names literal);
  • name_names β€” A C enumeration type containing the symbolic names of the strings provided by the pool.
  • name.h β€” A header file containing the declarations for the accessor functions and the enumeration type, 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 also be available for easy inclusion by package files (csources). Iconic behaviour honestly πŸ‘‘

  • name β€” New in version 1.1: For mode tcl the command registers a new result-type for critcl::cproc with critcl, which takes an integer result from the function and converts it to the equivalent string in the pool for the script. πŸ’«

🌸 EXAMPLE 🌸

The example shown below is the specification of the string pool pulled from the draft work on a Tcl binding to Linux's inotify APIs. She's a little rough around the edges (draft work, we said what we said πŸ™ˆ) but the vibes are immaculate:

package require Tcl 8.6
package require critcl 3.2

critcl::buildrequirement {
    package require critcl::literals
}

critcl::literals::def tcl_inotify_strings {
    w_create  "create"
    w_directory"directory"
    w_events  "events"
    w_file "file"
    w_handler "handler"
    w_remove  "remove"
} {c tcl}

# Declarations: tcl_inotify_strings.h
# Type:         tcl_inotify_strings_names
# Accessor:     Tcl_Obj*    tcl_inotify_strings      (Tcl_Interp*               interp,
#                                                     tcl_inotify_strings_names literal);
# Accessor:     const char* tcl_inotify_strings_cstr (tcl_inotify_strings_names literal);
# ResultType:   tcl_inotify_strings

🫢 AUTHORS 🫢

Andreas Kupries


πŸ’€ BUGS, IDEAS, FEEDBACK πŸ’€

This document, and the package it describes, will undoubtedly contain bugs and other problems. (not me being honest about my own manpage 😭). Please report such at

https://github.com/andreas-kupries/critcl

Please also report any ideas for enhancements you may have for either package and/or documentation. We want them. Give them to us. Fr fr πŸ’œ


πŸŽ€ KEYWORDS πŸŽ€

C code, Embedded C Code, Tcl Interp Association, code generator, compile & run, compiler, dynamic code generation, dynamic compilation, generate package, linker, literal pool, on demand compilation, on-the-fly compilation, singleton, string pool


⭐ CATEGORY ⭐

Glueing/Embedded C code


Copyright (c) 2011-2024 Andreas Kupries


okay that's the whole page bestie, you're officially literate in critcl::literals now πŸ’…βœ¨ this was critcl::literals(3tcl), manpage doc version 1.4, go forth and build your string pools responsibly, no cap πŸ¦‹πŸ’–πŸŒΈ

πŸ“¦ where this page comes from