Tcl_SetAssocData(3)

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

πŸ’– Tcl_GetAssocData, Tcl_SetAssocData, Tcl_DeleteAssocData πŸ’–

okay so bestie, this is a page from the Tcl Library Procedures manual, and it's all about managing associations of string keys and user specified data with Tcl interpreters. three whole functions. let's get into it πŸ¦‹


✨ SYNOPSIS ✨

#include <tcl.h>

void *
Tcl_GetAssocData(interp, key, delProcPtr)

Tcl_SetAssocData(interp, key, delProc, clientData)

Tcl_DeleteAssocData(interp, key)

like look at that. three little calls. iconic behavior honestly πŸ’…


πŸŽ€ ARGUMENTS πŸŽ€

  • Tcl_Interp *interp (in) β€” the interpreter where we're gonna execute the specified command. she's the main character here, everything happens inside her 🌸
  • const char *key (in) β€” the key for the association. you use it to store data, or to delete data, or to retrieve data. typically it's the module prefix for a package, iykyk.
  • Tcl_InterpDeleteProc *delProc (in) β€” the procedure to call when interp gets deleted. it's giving cleanup crew energy 🧹
  • Tcl_InterpDeleteProc **delProcPtr (in) β€” a pointer to a location where we stash the address of the current deletion procedure for the association. and listen: if it's NULL, it's just ignored, no cap.
  • void *clientData (in) β€” an arbitrary one-word value associated with the given key in this interpreter. heads up though: this data is owned by the caller. not Tcl's problem. yours. periodt πŸ’€

πŸ‘‘ DESCRIPTION πŸ‘‘

These procedures let extensions associate their own data with a Tcl interpreter. An association is literally just two things: a string key (typically the name of the extension) and a one-word value (typically a pointer to a data structure holding data specific to the extension). and here's the thing β€” Tcl makes no interpretation of either the key or the value for an association. she does not care. she does not look. she simply holds it for you 🫢

Storage management is facilitated by storing with each association a procedure to call when the interpreter is deleted. This procedure can dispose of the storage occupied by the client's data in any way it sees fit. your data, your rules, your little cleanup ritual ✨

Tcl_SetAssocData creates an association between a string key and a user specified datum in the given interpreter. and lowkey important: if there's already an association with the given key, Tcl_SetAssocData overwrites it with the new information. the old one? gone. she didn't even say goodbye 😭 It is up to callers to organize their use of names to avoid conflicts, for example, by using package names as the keys. so like, be a considerate bestie and namespace your stuff, fr fr.

If the deleteProc argument is non-NULL it specifies the address of a procedure to invoke if the interpreter is deleted before the association is deleted. DeleteProc should have arguments and result that match the type Tcl_InterpDeleteProc:

typedef void Tcl_InterpDeleteProc(
        void *clientData,
        Tcl_Interp *interp);

When deleteProc is invoked the clientData and interp arguments will be the same as the corresponding arguments passed to Tcl_SetAssocData. cute, right? it remembers πŸ₯Ή

⚠️ the deletion procedure will not be invoked if the association is deleted before the interpreter is deleted. so whoever goes first decides what happens. it's giving tragic romance ngl πŸ’”

Tcl_GetAssocData returns the datum stored in the association with the specified key in the given interpreter. and if the delProcPtr field is non-NULL, the address indicated by it gets the address of the delete procedure stored with this association. If no association with the specified key exists in the given interpreter, Tcl_GetAssocData returns NULL. empty. nothing. void. πŸ™ˆ

Tcl_DeleteAssocData deletes an association with a specified key in the given interpreter. Then it calls the deletion procedure. that's it, that's the whole move. slay πŸ”₯


🌈 KEYWORDS 🌈

association, data, deletion procedure, interpreter, key


and that's the whole page bestie, you ate it up πŸ’…βœ¨

this little number is from the Tcl Library Procedures manual, page Tcl_SetAssocData(3), and it's stamped with Tcl version 7.5. mwah, go forth and associate responsibly πŸŽ€πŸ’–

πŸ“¦ where this page comes from