| critcl::class(3tcl) | C Runtime In Tcl (CriTcl) | critcl::class(3tcl) |
critcl::class - CriTcl Utilities: C Classes
package require Tcl 8.4
package require critcl ?3.1.6?
package require critcl::class ?1.1.1?
::critcl::class::define name script
include path
support code
type name
classconstructor body
classdestructor body
constructor body ?postbody?
destructor body
classvariable ctype name ?comment? ?constructor? ?destructor?
classmethod name command arguments body
classmethod name proc arguments resulttype body
classmethod name as funname ?arg...?
insvariable ctype name ?comment? ?constructor? ?destructor?
method name command arguments body
method name proc arguments resulttype body
method name as funname ?arg...?
method_introspection
C Runtime In Tcl, or CriTcl , is a system for compiling C code embedded in Tcl on the fly and either loading the resulting objects into Tcl for immediate use or packaging them for distribution. Use CriTcl to improve performance by rewriting in C those routines that are performance bottlenecks.
This document is the reference manpage for the critcl::class package. This package provides convenience commands for advanced functionality built on top of the core.
With it a user wishing to create a C level object with class and instance commands can concentrate on specifying the class- and instance-variables and -methods in a manner similar to a TclOO class, while all the necessary boilerplate around it is managed by this package.
Its intended audience are mainly developers wishing to write Tcl packages with embedded C code.
This package resides in the Core Package Layer of CriTcl.
+----------------+ |Applications | | critcl | | critcl::app | +----------------+ *================* |Core Packages | | critcl | | critcl::util | *================* +----------------+ |Support Packages| | stubs::* | | md5, platform | | ... | +----------------+
Here we documents all class specification commands available inside of the class definition script argument of ::critcl::class::define.
Calls to this command are cumulative. It is of course possible to not use this command at all, for classes not making use of external definitions.
The result is the empty string.
Calls to this command are cumulative. It is of course possible to not use this command at all, for classes not requiring supporting code.
The result of the command is the empty string.
Initialization and release of the structure with the given type are the responsibility of the user, through constructor and destructor code fragments.
Attention: Using this command precludes the use of regular class- and instance variables. It further precludes the use of method-introspection as well, as this make use of generated instance-variables.
If class- and/or instance-variable have to be used in conjunction with an external C type, simply create and use a class- or instance-variable with that type.
The result of the command is the empty string.
For the initialization (and release) of a class variable it is recommended to use the constructor and destructor arguments of the variable's definition (See command classvariable) for this instead of using a separate classconstructor.
This is an optional command. Using it more than once is allowed too and each use will add another C code fragment to use during construction. I.e. multiple calls aggregate.
The C code blocks of multiple calls (including the constructors of classvariable definitions) are executed in order of specification.
The result of the command is the empty string.
The C code in body has access to the following environment:
For the initialization (and release) of a class variable it is recommended to use the constructor and destructor arguments of the variable's definition (See command classvariable) for this instead of using a separate classconstructor.
This is an optional command. Using it more than once is allowed too and each use will add another C code fragment to use during construction. I.e. multiple calls aggregate.
The C code blocks of multiple calls (including the constructors of class variable definitions) are executed in order of specification.
The result of the command is the empty string.
The C code in body has access to the same environment as the class constructor code blocks.
For the initialization (and release) of an instance variable it is recommended to use the constructor and destructor arguments of the variable's definition (See command insvariable) for this instead of using a separate constructor.
This is an optional command. Using it more than once is allowed too and each use will add another C code fragment to use during construction. I.e. multiple calls aggregate.
The C code blocks of multiple calls (including the constructors of instance variable definitions) are executed in order of specification.
The result of the command is the empty string.
The C code in body has access to the following environment:
The C code in postbody is responsible for construction actions to be done after the primary construction was done and the Tcl-level instance command was successfully created. It has access to a slightly different environment:
For the initialization (and release) of an instance variable it is recommended to use the constructor and destructor arguments of the variable's definition (See command insvariable) for this instead of using a separate constructor.
This is an optional command. Using it more than once is allowed too and each use will add another C code fragment to use during construction. I.e. multiple calls aggregate.
The C code blocks of multiple calls (including the constructors of instance variable definitions) are executed in order of specification.
The result of the command is the empty string.
The C code in body has access to the following environment:
Attention: Specification of a class variable precludes the use of an external C type for the instance structure.
Attention: Specification of a class variable automatically causes the definition of an instance variable named class, pointing to the class structure.
Beyond the basic name and C type of the new variable the definition may also contain a comment describing it, and C code blocks to initialize and release the variable. These are effectively local forms of the commands classconstructor and classdestructor. Please read their descriptions for details regarding the C environment available to the code.
The comment, if specified will be embedded into the generated C code for easier cross-referencing from generated ".c" file to class specification.
For this the body has access to
The body has access to
The class method is implemented by the external function funname, i.e. a function which is declared outside of the class code itself, or in a support block.
It is assumed that the first four arguments of that function represent the parameters
Attention: Specification of an instance variable precludes the use of an external C type for the instance structure.
Attention: Specification of an instance variable automatically causes the definition of an instance variable of type Tcl_Command, and named cmd, holding the token of the instance command, and the definition of an instance method named destroy. This implicit instance variable is managed by the system.
Beyond the basic name and C type of the new variable the definition may also contain a comment describing it, and C code blocks to initialize and release the variable. These are effectively local forms of the commands constructor and destructor. Please read their descriptions for details regarding the C environment available to the code.
The comment, if specified will be embedded into the generated C code for easier cross-referencing from generated ".c" file to class specification.
For this the body has access to
The body has access to
The instance method is implemented by the external function funname, i.e. a function which is declared outside of the instance code itself, or in a support block.
It is assumed that the first four arguments of that function represent the parameters
The two methods and the class variable are all named methods.
This section documents the various interactions between the specification commands. While these are are all documented with the individual commands here they are pulled together to see at a glance.
The example shown below is the specification of queue data structure, with most of the method implementations and support code omitted to keep the size down.
The full implementation can be found in the directory "examples/queue" of the critcl source distribution/repository.
package require Tcl 8.4
package require critcl 3.1
critcl::buildrequirement {
package require critcl::class ; # DSL, easy spec of Tcl class/object commands.
}
critcl::cheaders util.h
critcl::class::define ::queuec {
include util.h
insvariable Tcl_Obj* unget {
List object unget elements
} {
instance->unget = Tcl_NewListObj (0,NULL);
Tcl_IncrRefCount (instance->unget);
} {
Tcl_DecrRefCount (instance->unget);
}
insvariable Tcl_Obj* queue {
List object holding the main queue
} {
instance->queue = Tcl_NewListObj (0,NULL);
Tcl_IncrRefCount (instance->queue);
} {
Tcl_DecrRefCount (instance->queue);
}
insvariable Tcl_Obj* append {
List object holding new elements
} {
instance->append = Tcl_NewListObj (0,NULL);
Tcl_IncrRefCount (instance->append);
} {
Tcl_DecrRefCount (instance->append);
}
insvariable int at {
Index of next element to return from the main queue
} {
instance->at = 0;
}
support {... queue_peekget, queue_size, etc.}
method clear {} {...}
method destroy {...}
method get as queue_peekget 1
method peek as queue_peekget 0
method put {item ...}
method size {} {
if ((objc != 2)) {
Tcl_WrongNumArgs (interp, 2, objv, NULL);
return TCL_ERROR;
}
Tcl_SetObjResult (interp, Tcl_NewIntObj (queue_size (instance, NULL, NULL, NULL)));
return TCL_OK;
}
method unget {item} {...}
}
package provide queuec 1
Andreas Kupries
This document, and the package it describes, will undoubtedly contain bugs and other problems. 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.
C class, C code, C instance, C object, Embedded C Code, code generator, compile & run, compiler, dynamic code generation, dynamic compilation, generate package, linker, on demand compilation, on-the-fly compilation
Glueing/Embedded C code
Copyright (c) 2011-2018 Andreas Kupries
| 1.1.1 | doc |