Tcl_Ensemble(3)

πŸ“¦
version 9.0.3 Β· 3 versions
, in alpine 3.23, alpine 3.24, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable

πŸ’– Tcl_Ensemble β€” Tcl Library Procedures πŸ’–

okay bestie, gather round, because this is the Tcl_Ensemble(3) manual page and it's literally the whole ensemble-command situation. yes, "ensemble" as in a command that's secretly a whole friend group of subcommands. main character energy fr fr. πŸŽ€


✨ NAME ✨

Tcl_CreateEnsemble, Tcl_FindEnsemble, Tcl_GetEnsembleFlags, Tcl_GetEnsembleMappingDict, Tcl_GetEnsembleNamespace, Tcl_GetEnsembleParameterList, Tcl_GetEnsembleUnknownHandler, Tcl_GetEnsembleSubcommandList, Tcl_IsEnsemble, Tcl_SetEnsembleFlags, Tcl_SetEnsembleMappingDict, Tcl_SetEnsembleParameterList, Tcl_SetEnsembleSubcommandList, Tcl_SetEnsembleUnknownHandler β€” manipulate ensemble commands πŸ’…


🌸 SYNOPSIS 🌸

#include <tcl.h>

Tcl_Command
Tcl_CreateEnsemble(interp, name, namespacePtr, ensFlags)

Tcl_Command
Tcl_FindEnsemble(interp, cmdNameObj, flags)

int
Tcl_IsEnsemble(token)

int
Tcl_GetEnsembleFlags(interp, token, ensFlagsPtr)

int
Tcl_SetEnsembleFlags(interp, token, ensFlags)

int
Tcl_GetEnsembleMappingDict(interp, token, dictObjPtr)

int
Tcl_SetEnsembleMappingDict(interp, token, dictObj)

int
Tcl_GetEnsembleParameterList(interp, token, listObjPtr)

int
Tcl_SetEnsembleParameterList(interp, token, listObj)

int
Tcl_GetEnsembleSubcommandList(interp, token, listObjPtr)

int
Tcl_SetEnsembleSubcommandList(interp, token, listObj)

int
Tcl_GetEnsembleUnknownHandler(interp, token, listObjPtr)

int
Tcl_SetEnsembleUnknownHandler(interp, token, listObj)

int
Tcl_GetEnsembleNamespace(interp, token, namespacePtrPtr)

πŸ¦‹ ARGUMENTS πŸ¦‹

so here's the full cast list of every parameter these functions take, no skips, no "etc." β€” every single one, periodt. πŸ’œ

  • Tcl_Interp *interp (in/out) β€” the interpreter where the ensemble is gonna be created or found. it's also where error result messages get written, so it's doing double duty. 🫢 and listen: the functions whose names start with Tcl_GetEnsemble are the chill ones and may have a NULL for the interp, but every other function must not β€” do not try it, bestie, that's not the vibe. πŸ’€
  • const char *name (in) β€” the name of the ensemble command to be created. that's it, that's the one. ✨
  • Tcl_Namespace *namespacePtr (in) β€” the namespace the ensemble command is gonna be bound to, or NULL if you just want the current namespace. no biggie either way. 🌸
  • int ensFlags (in) β€” an OR'ed set of flag bits describing the basic configuration of the ensemble. right now literally only one bit has any meaning: TCL_ENSEMBLE_PREFIX, which is present when the ensemble command should also match unambiguous prefixes of subcommands. (yes, prefix matching. slay.) ⭐
  • Tcl_Obj *cmdNameObj (in) β€” a value holding the name of the ensemble command to look up. πŸ”
  • int flags (in) β€” an OR'ed set of flag bits controlling the behavior of Tcl_FindEnsemble. currently only TCL_LEAVE_ERR_MSG is supported β€” that's the whole roster, don't go inventing more. πŸ™ˆ
  • Tcl_Command token (in) β€” a normal command token that refers to an ensemble command, or one you wish to use for testing as an ensemble command in Tcl_IsEnsemble. πŸŽ€
  • int *ensFlagsPtr (out) β€” pointer to a variable to write the current ensemble flag bits into; currently only the bit TCL_ENSEMBLE_PREFIX is defined. πŸ’…
  • Tcl_Obj *dictObj (in) β€” a dictionary value to use for the subcommand-to-implementation-command-prefix mapping dictionary in the ensemble. it may be NULL if the mapping dictionary is to be removed. πŸ—‘οΈ
  • Tcl_Obj **dictObjPtr (out) β€” pointer to a variable to write the current ensemble mapping dictionary into. πŸ“
  • Tcl_Obj *listObj (in) β€” a list value to use for the list of formal pre-subcommand parameters, the defined list of subcommands in the dictionary, or the unknown subcommand handler command prefix. may be NULL if the subcommand list or unknown handler are to be removed. 🧚
  • Tcl_Obj **listObjPtr (out) β€” pointer to a variable to write the current list of formal pre-subcommand parameters, the defined list of subcommands, or the current unknown handler prefix into. πŸ“
  • Tcl_Namespace **namespacePtrPtr (out) β€” pointer to a variable to write the handle of the namespace the ensemble is bound to into. πŸ‘‘

ok so that's the intro, the synopsis and the whole arguments lineup β€” every flag, every type, every NULL-ability, every in/out marker, all of it. and there's more coming, so don't go anywhere bestie, we're not done. πŸ’–

πŸ’« DESCRIPTION πŸ’«

okay so bestie, an ensemble is literally just a command that's bound to some namespace, and it's made up of a whole collection of subcommands that are implemented by other Tcl commands. the first argument you hand to the ensemble command is always read as a selector that says which subcommand to run. main character behavior tbh. πŸ‘‘

ensembles get created with Tcl_CreateEnsemble, which takes four arguments: the interpreter you're working in, the name of the ensemble to create, the namespace inside the interpreter to bind the ensemble to, and the default set of ensemble flags. the result of the function is the command token for the ensemble, and you can use that token to configure the ensemble further with the API down below in ENSEMBLE PROPERTIES. slay.

if you've got the name of an ensemble command, you can grab its token with Tcl_FindEnsemble. but heads up β€” if the command name you passed (in cmdNameObj) does not refer to an ensemble command, the result is NULL, and (if the TCL_LEAVE_ERR_MSG bit is set in flags) an error message gets left in the interpreter result. not me getting rejected 😭

and if you wanna check whether a command token even refers to an ensemble, use Tcl_IsEnsemble. it returns 1 if the token refers to an ensemble, or 0 otherwise. simple as. ✨


πŸŽ€ ENSEMBLE PROPERTIES πŸŽ€

every ensemble has four read-write properties and one read-only property. the whole lineup:

  • flags (read-write) β€” the set of flags for the ensemble, expressed as a bit-field. right now the only public flag is TCL_ENSEMBLE_PREFIX, which is set when unambiguous prefixes of subcommands are permitted to be resolved to implementations, on top of exact matches. you read and write these with Tcl_GetEnsembleFlags and Tcl_SetEnsembleFlags respectively. the result of both of those functions is a Tcl result code (TCL_OK, or TCL_ERROR if the token does not refer to an ensemble). periodt.
  • mapping dictionary (read-write) β€” a dictionary containing a mapping from subcommand names to lists of words to use as a command prefix (replacing the first two words of the command, which are the ensemble command itself and the subcommand name), or NULL if every subcommand is to be mapped to the command with the same unqualified name in the ensemble's bound namespace. defaults to NULL. you read and write it with Tcl_GetEnsembleMappingDict and Tcl_SetEnsembleMappingDict respectively. the result of both of those functions is a Tcl result code (TCL_OK, or TCL_ERROR if the token does not refer to an ensemble), and the dictionary you get from Tcl_GetEnsembleMappingDict should always be treated as immutable even if it is unshared. also: all command names in prefixes set via Tcl_SetEnsembleMappingDict must be fully qualified. no shortcuts bestie. πŸ’…
  • formal pre-subcommand parameter list (read-write) β€” a list of formal parameter names (the names only being used when generating error messages) that come at invocation of the ensemble between the name of the ensemble and the subcommand argument. NULL (the default) is equivalent to the empty list. read and write it with Tcl_GetEnsembleParameterList and Tcl_SetEnsembleParameterList respectively. the result of both of those functions is a Tcl result code (TCL_OK, or TCL_ERROR if the token does not refer to an ensemble), and the dictionary obtained from Tcl_GetEnsembleParameterList should always be treated as immutable even if it is unshared.
  • subcommand list (read-write) β€” a list of all the subcommand names for the ensemble, or NULL if this is to be derived from either the keys of the mapping dictionary (see above) or (if that is also NULL) from the set of commands exported by the bound namespace. read and write it with Tcl_GetEnsembleSubcommandList and Tcl_SetEnsembleSubcommandList respectively. the result of both of those functions is a Tcl result code (TCL_OK, or TCL_ERROR if the token does not refer to an ensemble), and the list obtained from Tcl_GetEnsembleSubcommandList should always be treated as immutable even if it is unshared.
  • unknown subcommand handler command prefix (read-write) β€” a list of words to prepend on the front of any subcommand when the subcommand is unknown to the ensemble (according to the current prefix handling rule); peep the namespace ensemble command for more details. if NULL, the default behavior β€” generate a suitable error message β€” will be used when an unknown subcommand is encountered. read and write it with Tcl_GetEnsembleUnknownHandler and Tcl_SetEnsembleUnknownHandler respectively. the result of both functions is a Tcl result code (TCL_OK, or TCL_ERROR if the token does not refer to an ensemble), and the list obtained from Tcl_GetEnsembleUnknownHandler should always be treated as immutable even if it is unshared.
  • bound namespace (read-only) β€” the namespace to which the ensemble is bound; when the namespace is deleted, so too will the ensemble be, and this namespace is also the namespace whose list of exported commands is used if both the mapping dictionary and the subcommand list properties are NULL. you can read it with Tcl_GetEnsembleNamespace, which returns a Tcl result code (TCL_OK, or TCL_ERROR if the token does not refer to an ensemble). look but don't touch, babe. πŸ™ˆ

πŸ¦‹ REFERENCE COUNT MANAGEMENT πŸ¦‹

Tcl_FindEnsemble does not modify the reference count of its cmdNameObj argument β€” it only reads. but ngl, watch out: this function may set the interpreter result, and if that's the only place holding a reference to the object, it will be deleted. πŸ’€

the ensemble property getters (Tcl_GetEnsembleMappingDict, Tcl_GetEnsembleParameterList, Tcl_GetEnsembleSubcommandList, and Tcl_GetEnsembleUnknownHandler) do not manipulate the reference count of the values they provide out; if those are non-NULL, they will have a reference count of at least 1. also note that these functions may set the interpreter result.

the ensemble property setters (Tcl_SetEnsembleMappingDict, Tcl_SetEnsembleParameterList, Tcl_SetEnsembleSubcommandList, and Tcl_SetEnsembleUnknownHandler) will increment the reference count of the new value of the property they are given if they succeed (and decrement the reference count of the old value of the property, if relevant). and if the property setters return TCL_ERROR, the reference count of the Tcl_Obj argument is left unchanged. no takebacks. 🫢

okay bestie this is the part where you actually keep track of your refcounts, because Tcl will not do it for you and the crash is not cute.

🌈 SEE ALSO 🌈


πŸ”₯ KEYWORDS πŸ”₯

command, ensemble


and that's the whole page, bestie!! this one's from Tcl 8.5 and it's the Tcl_Ensemble(3) man page πŸ’œβœ¨ go forth and ensemble responsibly, you absolute icon πŸ‘‘πŸŽ€

πŸ“¦ where this page comes from