TclOO Package

Artifact Content
Login
Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2018 Conference, Houston/TX, US, Oct 15-19
Send your abstracts to tclconference@googlegroups.com
or submit via the online form by Aug 20.

Artifact d3b68699b29e01813a5e54bec4eb38d84b5c0dcf:

Wiki page [Doc: Tcl_Class] by dkf 2016-09-17 13:35:06.
D 2016-09-17T13:35:06.422
L Doc:\sTcl_Class
N text/x-markdown
P c47b2ccfb6e7db2af8c614968ed26110386e32a3
U dkf
W 11859
<h1>Tcl\_Class, Tcl\_Object and supporting functions</h1>
<DL style="font-size:.85em">
<DD><A HREF="#M2" NAME="L86">NAME</A>
<DD><A HREF="#M3" NAME="L87">SYNOPSIS</A>
<DD><A HREF="#M4" NAME="L88">ARGUMENTS</A>
<DD><A HREF="#M5" NAME="L89">DESCRIPTION</A>
<DD><A HREF="#M6" NAME="L90">OBJECT AND CLASS METADATA</A>
<DD><A HREF="#M7" NAME="L91">TCL\_OBJECTMETADATATYPE STRUCTURE</A>
<DD><A HREF="#M8" NAME="L92">TCL\_OBJECTMETADATADELETEPROC FUNCTION SIGNATURE</A>
<DD><A HREF="#M9" NAME="L93">TCL\_CLONEPROC FUNCTION SIGNATURE</A>
<DD><A HREF="#M10" NAME="L94">OBJECT METHOD NAME MAPPING</A>
<DD><A HREF="#M11" NAME="L95">TCL\_OBJECTMAPMETHODNAMEPROC FUNCTION SIGNATURE</A>
<DD><A HREF="#M12" NAME="L96">SEE ALSO</A>
<DD><A HREF="#M13" NAME="L97">KEYWORDS</A>
</DL>
<H3><A NAME="M2">NAME</A></H3>
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 &mdash; manipulate objects and classes
<H3><A NAME="M3">SYNOPSIS</A></H3>
<pre>
<B>#include &lt;tclOO.h&gt;</B><br>
Tcl\_Object <B>Tcl\_GetObjectFromObj</B>(<I>interp, objPtr</I>)
Tcl\_Object <B>Tcl\_GetClassAsObject</B>(<I>class</I>)
Tcl\_Class <B>Tcl\_GetObjectAsClass</B>(<I>object</I>)
Tcl\_Obj \*<B>Tcl\_GetObjectName</B>(<I>interp, object</I>)
Tcl\_Command <B>Tcl\_GetObjectCommand</B>(<I>object</I>)
Tcl\_Namespace \*<B>Tcl\_GetObjectNamespace</B>(<I>object</I>)
Tcl\_Object <B>Tcl\_NewObjectInstance</B>(<I>interp, class, name, nsName, objc, objv, skip</I>)
Tcl\_Object <B>Tcl\_CopyObjectInstance</B>(<I>interp, object, name, nsName</I>)
int <B>Tcl\_ObjectDeleted</B>(<I>object</I>)
ClientData <B>Tcl\_ObjectGetMetadata</B>(<I>object, metaTypePtr</I>)
void <B>Tcl\_ObjectSetMetadata</B>(<I>object, metaTypePtr, metadata</I>)
ClientData <B>Tcl\_ClassGetMetadata</B>(<I>class, metaTypePtr</I>)
void <B>Tcl\_ClassSetMetadata</B>(<I>class, metaTypePtr, metadata</I>)
Tcl\_ObjectMapMethodNameProc <B>Tcl\_ObjectGetMethodNameMapper</B>(<I>object</I>)
void <B>Tcl\_ObjectSetMethodNameMapper</B>(<I>object</I>, <I>methodNameMapper</I>)
</pre>
<H3><A NAME="M4">ARGUMENTS</A></H3>
<DL class="arguments">
<DT>Tcl\_Interp <B>\*interp</B> (in/out)<DD>
Interpreter providing the context for looking up or creating an object, and
into whose result error messages will be written on failure.
<P><DT>Tcl\_Obj <B>\*objPtr</B> (in)<DD>
The name of the object to look up.
<P><DT>Tcl\_Object <B>object</B> (in)<DD>
Reference to the object to operate upon. This is an opaque token.
<P><DT>Tcl\_Class <B>class</B> (in)<DD>
Reference to the class to operate upon. This is an opaque token.
<P><DT>const char <B>\*name</B> (in)<DD>
The name of the object to create, or NULL if a new unused name is to be
automatically selected.
<P><DT>const char <B>\*nsName</B> (in)<DD>
The name of the namespace to create for the object's private use, or NULL if a
new unused name is to be automatically selected. The namespace must not
already exist.
<P><DT>int <B>objc</B> (in)<DD>
The number of elements in the <I>objv</I> array.
<P><DT>Tcl\_Obj \*const <B>\*objv</B> (in)<DD>
The arguments to the command to create the instance of the class.
<P><DT>int <B>skip</B> (in)<DD>
The number of arguments at the start of the argument array, <I>objv</I>, that
are not arguments to any constructors.
<P><DT>Tcl\_ObjectMetadataType <B>\*metaTypePtr</B> (in)<DD>
The type of <I>metadata</I> being set with <B>Tcl\_ClassSetMetadata</B> or
retrieved with <B>Tcl\_ClassGetMetadata</B>.
<P><DT>ClientData <B>metadata</B> (in)<DD>
An item of metadata to attach to the class, or NULL to remove the metadata
associated with a particular <I>metaTypePtr</I>.
<P><DT>Tcl\_ObjectMapMethodNameProc <B>methodNameMapper</B> (in)<DD>
A pointer to a function to call to adjust the mapping of objects and method
names to implementations, or NULL when no such mapping is required.
<P></DL>
<H3><A NAME="M5">DESCRIPTION</A></H3>
Objects are typed entities that have a set of operations (&quot;methods&quot;)
associated with them. Classes are objects that can manufacture objects. Each
class can be viewed as an object itself; the object view can be retrieved
using <B>Tcl\_GetClassAsObject</B> which always returns the object when applied
to a non-destroyed class, and an object can be viewed as a class with the aid
of the <B>Tcl\_GetObjectAsClass</B> (which either returns the class, or NULL if
the object is not a class). An object may be looked up using the
<B>Tcl\_GetObjectFromObj</B> function, which either returns an object or NULL
(with an error message in the interpreter result) if the object cannot be
found. The correct way to look up a class by name is to look up the object
with that name, and then to use <B>Tcl\_GetObjectAsClass</B>.
<P>
Every object has its own command and namespace associated with it. The command
may be retrieved using the <B>Tcl\_GetObjectCommand</B> function, the name of
the object (and hence the name of the command) with <B>Tcl\_GetObjectName</B>,
and the namespace may be retrieved using the <B>Tcl\_GetObjectNamespace</B>
function. Note that the Tcl\_Obj reference returned by <B>Tcl\_GetObjectName</B>
is a shared reference.
<P>
Instances of classes are created using <B>Tcl\_NewObjectInstance</B>, which
creates an object from any class (and which is internally called by both
the <B>create</B> and <B>new</B> methods of the <B><A HREF="?name=Doc:+oo::class">oo::class</A></B> class). It takes
parameters that optionally give the name of the object and namespace to
create, and which describe the arguments to pass to the class's constructor
(if any). The result of the function will be either a reference to the newly
created object, or NULL if the creation failed (when an error message will be
left in the interpreter result). In addition, objects may be copied by using
<B>Tcl\_CopyObjectInstance</B> which creates a copy of an object without running
any constructors.
<H3><A NAME="M6">OBJECT AND CLASS METADATA</A></H3>
Every object and every class may have arbitrary amounts of metadata attached
to it, which the object or class attaches no meaning to beyond what is
described in a Tcl\_ObjectMetadataType structure instance. Metadata to be
attached is described by the type of the metadata (given in the
<I>metaTypePtr</I> argument) and an arbitrary pointer (the <I>metadata</I>
argument) that are given to <B>Tcl\_ObjectSetMetadata</B> and
<B>Tcl\_ClassSetMetadata</B>, and a particular piece of metadata can be
retrieved given its type using <B>Tcl\_ObjectGetMetadata</B> and
<B>Tcl\_ClassGetMetadata</B>. If the <I>metadata</I> parameter to either
<B>Tcl\_ObjectSetMetadata</B> or <B>Tcl\_ClassSetMetadata</B> is NULL, the
metadata is removed if it was attached, and the results of
<B>Tcl\_ObjectGetMetadata</B> and <B>Tcl\_ClassGetMetadata</B> are NULL if 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.
<H4><A NAME="M7">TCL\_OBJECTMETADATATYPE STRUCTURE</A></H4>
The contents of the Tcl\_ObjectMetadataType structure are as follows:
<P>
<PRE>typedef const struct {
    int <I>version</I>;
    const char \*<I>name</I>;
    Tcl\_ObjectMetadataDeleteProc \*<I>deleteProc</I>;
    Tcl\_CloneProc \*<I>cloneProc</I>;
} <B>Tcl\_ObjectMetadataType</B>;</PRE>
<P>
The <I>version</I> field allows for future expansion of the structure, and
should always be declared equal to TCL\_OO\_METADATA\_VERSION\_CURRENT. The
<I>name</I> field provides a human-readable name for the type, and is reserved
for debugging.
<P>
The <I>deleteProc</I> field gives a function of type
Tcl\_ObjectMetadataDeleteProc that is used to delete a particular piece of
metadata, and is called when the attached metadata is replaced or removed; the
field must not be NULL.
<P>
The <I>cloneProc</I> field gives a function that is used to copy a piece of
metadata (used when a copy of an object is created using
<B>Tcl\_CopyObjectInstance</B>); if NULL, the metadata will be just directly
copied.
<H4><A NAME="M8">TCL\_OBJECTMETADATADELETEPROC FUNCTION SIGNATURE</A></H4>
Functions matching this signature are used to delete metadata associated with
a class or object.
<P>
<PRE>typedef void <B>Tcl\_ObjectMetadataDeleteProc</B>(
        ClientData <I>metadata</I>);</PRE>
<P>
The <I>metadata</I> argument gives the address of the metadata to be
deleted.
<H4><A NAME="M9">TCL\_CLONEPROC FUNCTION SIGNATURE</A></H4>
Functions matching this signature are used to create copies of metadata
associated with a class or object.
<P>
<PRE>typedef int <B>Tcl\_CloneProc</B>(
        Tcl\_Interp \*<I>interp</I>,
        ClientData <I>srcMetadata</I>,
        ClientData \*<I>dstMetadataPtr</I>);</PRE>
<P>
The <I>interp</I> argument 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 return TCL\_ERROR; it should return TCL\_OK otherwise.
The <I>srcMetadata</I> argument gives the address of the metadata to be cloned,
and the cloned metadata should be written into the variable pointed to by
<I>dstMetadataPtr</I>; a NULL should be written if the metadata is to not be
cloned but the overall object copy operation is still to succeed.
<H3><A NAME="M10">OBJECT METHOD NAME MAPPING</A></H3>
It is possible to control, on a per-object basis, what methods are invoked
when a particular method is invoked. Normally this is done by looking up the
method name in the object and then in the class hierarchy, but fine control of
exactly what the value used to perform the look up is afforded through the
ability to set a method name mapper callback via
<B>Tcl\_ObjectSetMethodNameMapper</B> (and its introspection counterpart,
<B>Tcl\_ObjectGetMethodNameMapper</B>, which returns the current mapper). The
current mapper (if any) is invoked immediately before looking up what chain of
method implementations is to be used.
<H4><A NAME="M11">TCL\_OBJECTMAPMETHODNAMEPROC FUNCTION SIGNATURE</A></H4>
The <I>Tcl\_ObjectMapMethodNameProc</I> callback is defined as follows:
<P>
<PRE>typedef int <B>Tcl\_ObjectMapMethodNameProc</B>(
        Tcl\_Interp \*<I>interp</I>,
        Tcl\_Object <I>object</I>,
        Tcl\_Class \*<I>startClsPtr</I>,
        Tcl\_Obj \*<I>methodNameObj</I>);</PRE>
<P>
If the result is TCL\_OK, the remapping is assumed to have been done. If the
result is TCL\_ERROR, an error message will have been left in <I>interp</I> and
the method call will fail. If the result is TCL\_BREAK, the standard method
name lookup rules will be used; the behavior of other result codes is
currently undefined. The <I>object</I> parameter says which object is being
processed. The <I>startClsPtr</I> parameter points to a variable that contains
the first class to provide a definition in the method chain to process, or
NULL if the whole chain is to be processed (the argument itself is never
NULL); this variable may be updated by the callback. The <I>methodNameObj</I>
parameter 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.
<H3><A NAME="M12">SEE ALSO</A></H3>
<B><A HREF="?name=Doc:+Tcl_Method">Tcl\_Method</A></B>, <B><A HREF="?name=Doc:+oo::class">oo::class</A></B>, <B><A HREF="?name=Doc:+oo::copy">oo::copy</A></B>, <B><A HREF="?name=Doc:+oo::define">oo::define</A></B>, <B><A HREF="?name=Doc:+oo::object">oo::object</A></B>
<H3><A NAME="M13">KEYWORDS</A></H3>
class, constructor, object
Z c6521846517b2de8c68acbb5efaa35e8