Tcl_Class(3tcl)
๐ฆ Tcl_Class โ manipulate objects and classes ๐ฆ
okay bestie, buckle up, because we're deep in TclOO territory. This is the Tcl_Class(3tcl) page from the TclOO Library Functions section, and it's literally the whole "how do I poke at objects and classes from C" starter pack. ๐
No cap, it's a lot of functions, but they all make sense together, promise.
โจ NAME โจ
Tcl_ClassGetMetadata, Tcl_ClassSetMetadata, Tcl_CopyObjectInstance, Tcl_GetClassAsObject, Tcl_GetObjectAsClass, Tcl_GetObjectCommand, Tcl_GetObjectFromObj, Tcl_GetObjectName, Tcl_GetObjectNamespace, Tcl_NewObjectInstance, Tcl_ObjectDeleted, Tcl_ObjectGetMetadata, Tcl_ObjectGetMethodNameMapper, Tcl_ObjectSetMetadata, Tcl_ObjectSetMethodNameMapper โ manipulate objects and classes ๐ซถ
๐ธ SYNOPSIS ๐ธ
#include <tclOO.h>
Tcl_Object
Tcl_GetObjectFromObj(interp, objPtr)
Tcl_Object
Tcl_GetClassAsObject(class)
Tcl_Class
Tcl_GetObjectAsClass(object)
Tcl_Obj *
Tcl_GetObjectName(interp, object)
Tcl_Command
Tcl_GetObjectCommand(object)
Tcl_Namespace *
Tcl_GetObjectNamespace(object)
Tcl_Object
Tcl_NewObjectInstance(interp, class, name, nsName, objc, objv, skip)
Tcl_Object
Tcl_CopyObjectInstance(interp, object, name, nsName)
int
Tcl_ObjectDeleted(object)
void *
Tcl_ObjectGetMetadata(object, metaTypePtr)
Tcl_ObjectSetMetadata(object, metaTypePtr, metadata)
void *
Tcl_ClassGetMetadata(class, metaTypePtr)
Tcl_ClassSetMetadata(class, metaTypePtr, metadata)
Tcl_ObjectMapMethodNameProc
Tcl_ObjectGetMethodNameMapper(object)
Tcl_ObjectSetMethodNameMapper(object, methodNameMapper)
Tcl_Class
Tcl_GetClassOfObject(object)
Tcl_Obj *
Tcl_GetObjectClassName(interp, object)
๐ ARGUMENTS ๐
Tcl_Interp *interp(in/out) โ the interpreter that's giving you the context to look up or create an object, and the place where error messages get dropped into the result if stuff goes sideways. ๐Tcl_Obj *objPtr(in) โ the name of the object you're trying to look up.Tcl_Object object(in) โ a reference to the object you wanna operate on.Tcl_Class class(in) โ a reference to the class you wanna operate on.const char *name(in) โ the name of the object to create, orNULLif you want a fresh unused name auto-picked for you. โจconst char *nsName(in) โ the name of the namespace to create for the object's private use, orNULLif you want a new unused name auto-picked. Heads up: the namespace must not already exist. ๐ซTcl_Size objc(in) โ how many elements are in theobjvarray.Tcl_Obj *const *objv(in) โ the arguments to the command that creates the instance of the class.Tcl_Size skip(in) โ the number of arguments at the very start ofobjvthat are not arguments to any constructors. This exists so you can still generate correct error messages even when the calling pattern is complicated (looking at you,nextcommand ๐).Tcl_ObjectMetadataType *metaTypePtr(in) โ the type of metadata being set withTcl_ClassSetMetadataor retrieved withTcl_ClassGetMetadata.void *metadata(in) โ an item of metadata to attach to the class, orNULLto remove the metadata associated with a particularmetaTypePtr.Tcl_ObjectMapMethodNameProc methodNameMapper(in) โ a pointer to a function to call to adjust the mapping of objects and method names to implementations, orNULLwhen no such mapping is required.
๐ DESCRIPTION ๐
So, the whole vibe: objects are typed entities that have a set of operations โ we call those "methods" โ associated with them. Classes are objects that can manufacture objects. The way that every class is also an object itself? iconic, honestly.
Here's the tea on how you move between the two views:
- Grab the object view of a class with
Tcl_GetClassAsObjectโ it always returns the object when you hand it a non-destroyed class. ๐ - Grab the class view of an object with
Tcl_GetObjectAsClassโ it returns the class, orNULLif that object isn't a class. - Look up an object by name with
Tcl_GetObjectFromObjโ returns the object, orNULL(and drops an error message in the interpreter result) if it can't be found.
The correct way to look up a class by name: look up the object with that name first, then hit it with Tcl_GetObjectAsClass. Don't skip steps bestie. ๐
Every single object has its own command and namespace attached to it, fr fr:
Tcl_GetObjectCommandgets you the command.Tcl_GetObjectNamegets you the name of the object (which is also the name of the command, iykyk).Tcl_GetObjectNamespacegets you the namespace.
โ ๏ธ TheTcl_Objreference thatTcl_GetObjectNamereturns is a shared reference. Don't be out here freeing it like it's yours.
You can also check whether an object has been marked for deletion with Tcl_ObjectDeleted โ it returns true if deletion of the object has begun. That's genuinely useful while you're processing methods. ๐ง
The class of an object comes from Tcl_GetClassOfObject, and the name of the class of an object comes from Tcl_GetObjectClassName. Important: both of these can return NULL during deletion of an object. That's transient, and it only happens when the object is a long way through being deleted. Not a bug, just a moment. ๐
Instances of classes get created with Tcl_NewObjectInstance, which creates an object from any class (and which is internally called by both the create and new methods of the oo::class class). It takes parameters that optionally give the name of the object and namespace to create, plus the arguments to pass to the class's constructor (if it has one). The result is either a reference to the freshly created object, or NULL if creation failed (in which case there's an error message chilling in the interpreter result). ๐
You can also copy objects with Tcl_CopyObjectInstance, which creates a copy of an object without running any constructors. Sneaky. I love it.
Now for the serious bit. ๐ฅ Note that the lifetime management of objects is handled internally within TclOO, and it does not use Tcl_Preserve. So:
๐จ It is not safe to put aTcl_Objecthandle in a C structure with a lifespan different to the object. Use the object's command name instead (the one you got fromTcl_GetObjectName).
It is safe to use a Tcl_Object handle for the lifespan of a call of a method on that object. Handles do not become invalid while there's an outstanding call on their object โ even if the only operation guaranteed to be safe on them is Tcl_ObjectDeleted. Every other operation is only guaranteed to work on non-deleted objects. Periodt.
๐ OBJECT AND CLASS METADATA ๐
Every object and every class can have arbitrary amounts of metadata attached to it. The object or class attaches no meaning to that metadata beyond what's described in a Tcl_ObjectMetadataType structure instance. ๐ซ
Here's how it works:
- Metadata to attach is described by the type of the metadata (the
metaTypePtrargument) and an arbitrary pointer (themetadataargument), both handed toTcl_ObjectSetMetadataandTcl_ClassSetMetadata. - A particular piece of metadata can be retrieved given its type using
Tcl_ObjectGetMetadataandTcl_ClassGetMetadata. - If the
metadataparameter to eitherTcl_ObjectSetMetadataorTcl_ClassSetMetadataisNULL, the metadata is removed if it was attached. - The results of
Tcl_ObjectGetMetadataandTcl_ClassGetMetadataareNULLif the given type of metadata was not attached. - It is not an error to request or remove a piece of metadata that was not attached. ๐ซถ
TCL_OBJECTMETADATATYPE STRUCTURE
The contents of the Tcl_ObjectMetadataType structure are as follows:
typedef const struct {
int version;
const char *name;
Tcl_ObjectMetadataDeleteProc *deleteProc;
Tcl_CloneProc *cloneProc;
} Tcl_ObjectMetadataType;
Breaking it down:
- The
versionfield allows for future expansion of the structure, and should always be declared equal toTCL_OO_METADATA_VERSION_CURRENT. - The
namefield provides a human-readable name for the type, and is reserved for debugging. - The
deleteProcfield gives a function of typeTcl_ObjectMetadataDeleteProcthat's used to delete a particular piece of metadata. It gets called when the attached metadata is replaced or removed โ and this field must not beNULL. ๐ซ - The
cloneProcfield gives a function that's used to copy a piece of metadata (used when a copy of an object is created usingTcl_CopyObjectInstance). If it'sNULL, the metadata will just be directly copied. easy peasy. โจ
TCL_OBJECTMETADATADELETEPROC FUNCTION SIGNATURE
Functions matching this signature are used to delete metadata associated with a class or object.
typedef void Tcl_ObjectMetadataDeleteProc(
void *metadata);
The metadata argument gives the address of the metadata to be deleted.
TCL_CLONEPROC FUNCTION SIGNATURE
Functions matching this signature are used to create copies of metadata associated with a class or object.
typedef int Tcl_CloneProc(
Tcl_Interp *interp,
void *srcMetadata,
void **dstMetadataPtr);
The details, bestie:
- The
interpargument gives a place to write an error message when the attempt to clone the object is to fail โ in which case the clone procedure must also returnTCL_ERROR. It should returnTCL_OKotherwise. - The
srcMetadataargument gives the address of the metadata to be cloned. - The cloned metadata should be written into the variable pointed to by
dstMetadataPtr. ANULLshould be written if the metadata is to not be cloned but the overall object copy operation is still to succeed. ๐
๐ฎ OBJECT METHOD NAME MAPPING ๐ฎ
okay bestie so listen โ you can actually control, per-object, which methods get invoked when a particular method gets invoked. normally that's just: look up the method name in the object, then look it up in the class hierarchy, done, slay. but if you want fine control over exactly what value is used to do that lookup, you can set a method name mapper callback with Tcl_ObjectSetMethodNameMapper โ and its introspection bestie Tcl_ObjectGetMethodNameMapper, which hands you back the current mapper. the current mapper (if there even is one) gets invoked immediately before the lookup of what chain of method implementations to use. periodt. ๐
๐ง TCL_OBJECTMAPMETHODNAMEPROC FUNCTION SIGNATURE ๐ง
the Tcl_ObjectMapMethodNameProc callback is defined like this, no cap:
typedef int Tcl_ObjectMapMethodNameProc(
Tcl_Interp *interp,
Tcl_Object object,
Tcl_Class *startClsPtr,
Tcl_Obj *methodNameObj);
and the return values? let's vibe check them:
- if the result is
TCL_OK, we just assume the remapping has been done. โ - if the result is
TCL_ERROR, an error message will have been left in interp and the method call fails. ๐ญ - if the result is
TCL_BREAK, the standard method name lookup rules get used instead. ๐ - the behavior of other result codes is currently undefined โ so like, don't. ๐
about the params, because details matter:
objectโ says which object is being processed.startClsPtrโ points to a variable that holds the first class to provide a definition in the method chain to process, orNULLif the whole chain is to be processed (and btw the argument itself is neverNULL). this variable may be updated by the callback.methodNameObjโ gives an unshared object containing the name of the method being invoked, as provided by the user. this object may be updated by the callback too.
๐ซถ REFERENCE COUNT MANAGEMENT ๐ซถ
okay this is the part where we're careful, bestie, because reference counts are giving danger if you fumble them:
- the
objPtrargument toTcl_GetObjectFromObjwill not have its reference count manipulated. but heads up โ this function may modify the interpreter result (to report any error), so interpreter results should not be fed into this without an additional reference being used. ๐จ - the result of
Tcl_GetObjectNameis a value that is owned by the object, and it gets regenerated when this function is first called after the object is renamed. so if you wanna retain that value at all, the caller should increment the reference count. ๐ - the first
objcvalues in theobjvargument toTcl_NewObjectInstanceare the arguments to pass to the constructor. they must have a reference count of at least 1, and they may have their reference counts changed while the constructor is running. also constructors may modify the interpreter result, which consequently means interpreter results should not be used as arguments without an additional reference being taken. ๐ฅ - the
methodNameObjargument to aTcl_ObjectMapMethodNameProcimplementation will be a value with a reference count of at least 1, where at least one reference is not held by the interpreter result. and the expectation? method name mappers will only read theirmethodNameObjarguments. ๐
๐ SEE ALSO ๐
Method(3tcl)oo::class(3tcl)oo::copy(3tcl)oo::define(3tcl)oo::object(3tcl)
โญ KEYWORDS โญ
class, constructor, object
and that's a wrap on the whole page, bestie!! ๐ this was TclOO, version 0.1, and the page is Tcl_Class(3tcl). go forth and slay your object system, ily, mwah ๐โจ๐ฆ
๐ฆ where this page comes from
- ubuntu questing: tcl9.0-doc 9.0.2+dfsg-1
- ubuntu resolute: tcl9.0-doc 9.0.3+dfsg-1
- ubuntu stonking: tcl9.0-doc 9.0.4+dfsg-1
- Copyright (c) 2007 Donal K. Fellows
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms