home: hub: 9ficl

ref: ec465f2985094bcb6c352ceb3866ac4e528fb317
dir: /doc/ficl_oop.html/

View raw version
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
   <meta name="Author" content="john sadler">
   <meta name="Description" content="object oriented programming in the coolest embedded scripting language ever">
   <meta name="GENERATOR" content="Mozilla/4.73 [en] (Win98; U) [Netscape]">
   <title>Ficl Object Oriented Programming</title>
    <LINK media="screen" href="ficlstyle.css" type="text/css" rel="stylesheet">
<STYLE type="text/css">
    dt { font-family: monospace }
</STYLE>

</head>
<body>

<h1>
<b>Object Oriented Programming in ficl</b></h1>


<script language="javascript" src="ficlheader.js"></script>

<DIV style="width:675px">

<h2>
Contents</h2>

<ul>
<li>
<a href="#objects">Introduction</a></li>

<li>
<a href="#ootutorial">Tutorial</a></li>

<li>
<a href="#cstring">Ficl String Classes</a></li>

<li>
<a href="#theory">Theory</a></li>
<li>

<a href="#oopgloss">OOP glossary</a></li>

<li>
<a href="#glossinstance">Instance variable glossary</a></li>

<li>
<a href="#glossclass">Class methods glossary</a></li>

<li>
<a href="#objectgloss">OBJECT methods glossary</a></li>

<li>
<a href="#stockclasses">Supplied Classes</a></li>
</ul>
<br><hr>
<h2><a NAME="objects">Introduction</a></h2>

<h3>
Review of <a href="http://whatis.techtarget.com/definition/0,289893,sid9_gci212681,00.html">OO</a> ideas</h3>
Click <a href="oo_in_c.html#review">here</a> for a short review of OO ideas,
terms, and implementations in other languages, or <a href="http://www.soft-design.com/softinfo/objects.html">here</a>
for an introduction to the terms and principles of Object Oriented Programming
<h3>
Design goals of Ficl OO syntax</h3>
Ficl's object extensions provide the traditional OO benefits of associating
data with the code that manipulates it, and reuse through single inheritance.
Ficl also has some unusual capabilities that support interoperation with
systems written in C.
<ul>
<li>
Ficl objects are normally late bound for safety (late binding guarantees
that the appropriate method will always be invoked for a particular object).
Early binding is also available, provided you know the object's class at
compile-time.</li>

<li>
Ficl OOP supports single inheritance, aggregation, and arrays of objects.</li>

<li>
Classes have independent name spaces for their methods: methods are only
visible in the context of a class or object. Methods can be overridden
or added in subclasses; there is no fixed limit on the number of methods
of a class or subclass.</li>

<li>
Ficl OOP syntax is regular and unified over classes and objects. In ficl,
all classes are objects. Class methods include the ability to subclass
and instantiate.</li>

<li>
Ficl can adapt legacy data structures with object wrappers. You can model
a structure in a Ficl class, and create an instance that refers to an address
in memory that holds an instance of the structure. The <i>ref object</i>
can then manipulate the structure directly. This lets you wrap data structures
written and instantiated in C.</li>
</ul>

<h3>Acknowledgements</h3>
<p>Ficl is not the first Forth to include Object Oriented extensions. Ficl's
OO syntax owes a debt to the work of John Hayes and Dick Pountain, among
others. OO Ficl is different from other OO Forths in a few ways, though
(some things never change). First, unlike several implementations, the
syntax is documented (<a href="#ootutorial">below</a>) beyond the source
code. In Ficl's spirit of working with C code, the OO syntax provides means
to adapt existing data structures. I've tried to make Ficl's OO model simple
and safe by unifying classes and objects, providing late binding by default,
and separating namespaces so that methods and regular Forth words are not
easily confused.</p>
<br>
<hr>
<h2>
<a NAME="ootutorial"></a>Tutorial</h2>
It's helpful to have some familiarity with Forth and the customary Forth
stack notation to understand this tutorial. To get started, take a look
at this <a href="primer.html">Forth tutorial</a>. 
<p>
This tutorial works best if you follow along by pasting the examples
into ficlWin, the Win32 version of Ficl included with the release sources
(or some other build that includes the OO part of softcore.c). 
<p>
You need to use this incantation to make Ficl's OOP words visible to the interpreter:
<pre>
ONLY   ( Reset to default search order )
ALSO OOP DEFINITIONS  ( Add OOP wordlist to search order )
</pre>
<dl>
<dt>??? not found</dt> 
<dd>
If you run into this error message you will need to repeat the above incantation to reset the search order. Ficl resets to a default search order if it runs into an error.
</dd>
<dt>For beginners</dt> 
<dd>
To see the effect of the commands above, type <code>ORDER</code> after each word. You can repeat the sequence above if you like. All of the above words are described in the 
<a href="http://ficl.sourceforge.net/dpans/dpansf.htm">Standard</a>
</dd>
</dl>
<p>To start, we'll work with the base class <code>OBJECT</code>.
Try this:
<pre>
object
</pre>
FiclWin's stack viewer shows that the stack now holds two values - the one on top is the address of <code>class</code> and the other is the address of a special class called <code>METACLASS</code> that describes the behavior of all classes (including itself). If you're using someother version of Ficl, you can type <code>.s</code> to view the contents of the stack non-destructively. 
Now try:
<pre> 
object --> methods
</pre>
The line above contains three words. The first is the name of a class,
so it pushes its signature on the stack. The next word (<code>--></code>) finds a method in the context of class <code>object</code> and executes it. In this case, the name of the method is <code>methods</code>.
Its job is to list all the methods that a class knows. What you get when
you execute this line is a list of all the class methods Ficl provides.
<p>
Now let's create a class that can do something interesting with the simulated hardware that FiclWin provides...
<pre>
object --> sub c-led
</pre>
<p>
Causes base-class <code>OBJECT</code> to derive from itself a new class called
<code>c-led</code>.</p>
<dl> 
<dt>About class naming</dt>
<dd> I like to prefix the names of classes with "c-", and the
names of member variables with a dot, but this is just a convention. If
you don't like it, you can pick your own.</dd>
<dt>About !oreg</dt>
<dd>The following methods use the !oreg word defined by FiclWin to control its simulated LED bar. Usage: <code>!oreg  ( value -- )</code>. To test it, try <code>0xff !oreg</code> or <code>0 !oreg</code>.</dd>
</dl>
<p>
Now we'll add some instance variables and methods to the new class...</p> 
<pre>
  
c-byte obj: .state     \  an instance variable

: init   { 2:this -- }
    0 dup !oreg
    this --> .state --> set 
    ." initializing an instance of "
    this --> class --> id type cr ;

: on   { led# 2:this -- }
    this --> .state --> get
    1 led# lshift or dup !oreg
    this --> .state --> set  ;
: off   { led# 2:this -- }
    this --> .state --> get
    1 led# lshift invert and dup !oreg
    this --> .state --> set ;
end-class
</pre>
The first line adds an instance variable called <code>.state</code> to the
class. This particular instance variable is an object - it will be an instance
of <code>c-byte</code>, one of ficl's stock classes (the source for which can be found in the distribution in sorftowrds/classes.fr). Try this...
<pre>
c-led --> see .state
</pre>
The <code>see</code> method (inherited from <code>object</code>) decompiles methods so that you can see exactly what they do. There's also a <code>debug</code> method that lets you step through a method.
<p>
Next we've defined a method called <code>init</code>. This line also declares
a <a href="ficl_loc.html">local variable</a> called <code>this</code>
(the 2 in front tells Ficl that this is a double-cell local). All methods
by convention expect the address of the class and instance on top of the
stack when called. The next four lines define <code>init</code>'s behavior.
It first clears the simulated LED bar and <code>.state</code>. The rest displays some text and causes the instance to print its class name (<code>this --> class --> id</code>).
<br>The <code>init</code> method is special for Ficl objects: whenever
you create an initialized instance using <code>new</code> or <code>new-array</code>,
Ficl calls the class's <code>init</code> method for you on that instance. The
default <code>init</code> method supplied by <code>object</code> sets the instance variables to zero (see the source code in ficl/softwords/oo.fr).
<br>The <code>ON</code> and <code>OFF</code> methods defined above hide the details
of turning LEDs on and off. The interface to FiclWin's simulated hardware
is handled by <code>!OREG</code>. The class keeps the LED state in a shadow
variable (<code>.STATE</code>) so that <code>ON</code> and <code>OFF</code> can work
in terms of LED number rather than a bitmask.
<p>Now make an instance of the new class:&nbsp;
<pre>
c-led --> new led
</pre>
And try a few things...
<pre>
led --> methods
led --> pedigree
1 led --> on
1 led --> off
</pre>
Or you could type this with the same effect:
<pre>
led  2dup  --> methods  --> pedigree
</pre>
Notice (from the output of <code>methods</code>) that we've overridden the
init method supplied by object, and added two more methods for the member
variables. If you type <code>WORDS</code>, you'll see that these methods are
not visible outside the context of the class that contains them. The method
finder <code>--></code> uses the class to look up methods. You can use
this word in a definition, as we did in <code>init</code>, and it performs
late binding, meaning that the mapping from message (method name) to method
(the code) is deferred until run-time. To see this, you can decompile the
init method like this:
<pre>
c-led --> see init
</pre>
or
<pre>
led --> class --> see init
</pre>

<h3>
More About Instance Variables</h3>
<i>Untyped</i> instance variable methods (created by <code>cell: cells: char:</code>
and <code>chars:</code>) just push the address of the corresponding instance
variable when invoked on an instance of the class. It's up to you to remember
the size of the instance variable and manipulate it with the usual Forth
words for fetching and storing.&nbsp;
<p>As advertised earlier, Ficl provides ways to objectify existing data
structures without changing them. Instead, you can create a Ficl class
that models the structure, and instantiate a <b>ref </b>from this class,
supplying the address of the structure. After that, the <i>ref instance</i>
behaves as a Ficl object, but its instance variables take on the values
in the existing structure. Example (from ficlclass.fr):
<pre>
object subclass c-wordlist
    c-wordlist ref: .parent
    c-ptr     obj: .name
    c-cell    obj: .size
    c-word    ref: .hash
    : ?
        2drop ." ficl wordlist " cr ;
    : push  drop  >search ;
    : pop   2drop previous ;
    : set-current   drop set-current ;
    : words   --> push  words previous ;
end-class
</pre>
In this case, <code>c-wordlist</code> describes Ficl's wordlist structure;
named-wid creates a wordlist and binds it to a ref instance of <code>c-wordlist</code>.
The fancy footwork with <code>POSTPONE</code> and early binding is required
because classes are immediate. An equivalent way to define named-wid with
late binding is:
<pre>
: named-wid   ( "name" -- )
    wordlist  postpone c-wordlist --> ref ;
</pre>
To do the same thing at run-time (and call it my-wordlist):
<pre>
wordlist   
c-wordlist --> ref  my-wordlist
</pre>
Now you can deal with the wordlist through the ref instance:
<pre>
my-wordlist --> push
my-wordlist --> set-current
order
</pre>
Ficl can also model linked lists and other structures that contain pointers
to structures of the same or different types. The class constructor word
<code><a href="#exampleref:">ref:</a></code>
makes an aggregate reference to a particular class. See the <a href="#glossinstance">instance
variable glossary</a> for an <a href="#exampleref:">example</a>.
<p>Ficl can make arrays of instances, and aggregate arrays into class descripions.
The <a href="#glossclass">class methods</a> <code>array</code> and <code>new-array</code>
create uninitialized and initialized arrays, respectively, of a class.
In order to initialize an array, the class must define (or inherit) a reasonable
<code>init</code>
method. <code>New-array</code> invokes it on each member of the array
in sequence from lowest to highest. Array instances and array members use
the object methods <code>index</code>, <code>next</code>, and <code>prev</code>
to navigate. Aggregate a member array of objects using <code><a href="#arraycolon">array:</a></code>.
The objects are not automatically initialized in this case - your class
initializer has to call <code>array-init</code> explicitly if you want
this behavior.
<p>For further examples of OOP in Ficl, please see the source file <code>ficl/softwords/ficlclass.fr</code>.
This file wraps several Ficl internal data structures in objects and gives
use examples.
<br>
<h3><a NAME="cstring"></a>Ficl String classes</h3>
<code>c-string</code> (ficl 2.04 and later) is a reasonably useful dynamic string class. Source code for the class is located in <code>ficl/softwords/string.fr</code>. Features: dynamic creation and resizing; deletion, concatenation, output,
comparison; creation from quoted string constant (<code>s"</code>).
<p>Examples of use:
<pre>
c-string --> new homer
s" In this house, " homer --> set
s" we obey the laws of thermodynamics!" homer --> cat
homer --> type
</pre>
<br>
<h3>Early binding</h3>
Ficl also provides early binding if you ask for it. Early binding is not
as safe as late binding, but it produces code that is more compact and
efficient because it compiles method addresses rather then their names.
In the preferred uses of early binding, the class is assumed to be the
one you're defining. This kind of early binding can only be used inside
a class definition. Early bound methods still expect to find a class and
instance cell-pair on top of the stack when they run.
<br>Here's an example that illustrates a potential problem:
<pre>
object --> sub c1
: m1   { 2:this -- }  ." c1's m1" cr ;
: m2   { 2:this -- }  ." Running  " this  my=> m1 ; ( early )
: m3   { 2:this -- }  ." Running  " this --> m1     ( late )
end-class
c1     --> sub c2
: m1   { 2:this -- }  ." c2's m1" cr ;
end-class
c2 --> new i2
i2 --> m1   ( runs the m1 defined in c2 )
i2 --> m2   ( is this what you wanted? )
i2 --> m3   { runs the overridden m1)
</pre>
Even though we overrode method m1 in class c2, the definition of m2 with
early binding forced the use of m1 as defined in c1. If that's what you
want, great, but more often you'll want the flexibility of overriding parent
class behaviors appropriately.&nbsp;
<ol>
<li>
<code>my=></code> binds early to a method in the class being defined,
as in the example above.
</li>
<li>
<code>my=[ ]</code> binds a sequence of methods in the current class.
Useful when the class has object members. Lines like <code>this --> state
--> set</code> in the definition of c-led above can be replaced with
<code>this my=[ state set ]</code> to get early binding.
</li>
<li>
<code>=></code> (dangerous) pops a class off the stack and compiles
the method in that class. Since you have to specify the class explicitly,
there is a real danger that this will be out of sync with the class you
really wanted. I recommend the <code>my=</code> operations.
</li>
</ol>
Early binding using <code>=></code> is dangerous because it partially
defeats the data-to-code matching mechanism object oriented languages were
created to provide, but it does increase run-time speed by binding the
method at compile time. In many cases, such as the <code>init</code> method,
you can be reasonably certain of the class of thing you're working on.
This is also true when invoking class methods, since all classes are instances
of <code>metaclass</code>. Here's an example from the definition of <code>metaclass</code>
in oo.fr (don't paste this into ficlWin - it's already there):
<pre>
: new   \ ( class metaclass "name" -- )
    metaclass => instance --> init ;
</pre>
Try this...
<pre>
metaclass --> see new
</pre>
<p>
Decompiling the method with <code>SEE</code> shows the difference between the
two strategies. The early bound method is compiled inline, while the late-binding
operator compiles the method name and code to find and execute it in the
context of whatever class is supplied on the stack at run-time.
<br>Notice that the primitive early-binding operator <code>=></code> requires
a class at compile time. For this reason, classes are <code>IMMEDIATE</code>,
meaning that they push their signature at compile time or run time. I'd
recommend that you avoid early binding until you're very comfortable with
Forth, object-oriented programming, and Ficl's OOP syntax.
</p>
<br><hr>
<h2><a name="theory"></a>Theory</h2>
All classes in Ficl are derived from the common base class <code><a href="#objectgloss">OBJECT,</a></code>
as shown in the <a href="#figure1">figure</a> below. All classes are instances
of <code><a href="#glossclass">METACLASS</a></code>. This means that classes
are objects, too. <code>METACLASS</code> implements the methods for messages
sent to classes. Class methods create instances and subclasses, and give
information about the class. Each class is represented by a data stucture
of three elements:&nbsp;
<ul>
<li>
The address (named <code>.CLASS</code> ) of a parent class, or zero if it's
a base class (only <code>OBJECT</code> and <code>METACLASS</code> have this property)</li>

<li>
The size (named <code>.SIZE</code> ) in address units of an instance of the
class</li>

<li>
A wordlist ID (named <code>.WID</code> ) for the methods of the class</li>
</ul>
In the figure below, <code>METACLASS</code> and <code>OBJECT</code> are real system-supplied
classes. The others are contrived to illustrate the relationships among
derived classes, instances, and the two system base classes. The dashed
line with an arrow at the end indicates that the object/class at the arrow
end is an instance of the class at the other end. The vertical line with
a triangle denotes inheritance.&nbsp;
<p>Note for the curious: <code>METACLASS</code> behaves like a class - it responds
to class messages and has the same properties as any other class. If you
want to twist your brain in knots, you can think of <code>METACLASS</code>
as an instance of itself.&nbsp;
<br>&nbsp;

<p><a NAME="figure1"></a><img SRC="ficl_oop.jpg" VSPACE=10 height=442 width=652>
<br>
<p>A Ficl <a href="oo_in_c.html#object-def">object</a> associates a <a href="oo_in_c.html#class-def">class</a>
with an <a href="oo_in_c.html#instance-def">instance</a> (the storage for
one set of instance variables). This is done explicitly on Ficl's stack:
any Ficl object is represented by a cell pair:
<pre>
( instance-addr class-addr )
</pre>
The <code>instance-addr</code> is the address of the object's storage, and the <code>class-addr</code> is the address of its class. Whenever a named Ficl object executes (eg. when you type its name and press enter at the Ficl prompt), it leaves this "signature". All methods by convention expect a class and instance on the
stack when they execute, too. In many other OO languages, including C++,
instances contain information about their classes (a <a href="http://www.mvps.org/vbvision/vtable.htm">vtable</a>
pointer, for example). By making this pairing explicit, Ficl can be OO about chunks of data that don't realize that they are objects, without sacrificing any robustness for native objects. That means that you can use Ficl to write object wrappers for data structures created in C, C++, or even assembly language, as long as you can determine how they're laid out in memory.
<br>Whenever you create an object in Ficl, you specify its class.
After that, the object always pushes its class and the address of its payload (instance variable space) when invoked by name. 
<p>Classes are special kinds of objects that store the methods of their
instances, the size of an instance's payload, and a parent class pointer.
Classes themselves are instances of a special base class called <code>METACLASS</code>,
and all classes inherit from class <code>OBJECT</code>. While confusing at
first, this gives Ficl a very simple syntax for constructing
and using objects. Class methods include subclassing (<code>SUB</code>), creating
initialized and uninitialized instances (<code>NEW</code> and <code>INSTANCE</code>),
and creating reference instances (<code>REF</code>), described later. Classes
also have methods for disassembling their methods (<code>SEE</code>), identifying
themselves (<code>ID</code>), and listing their pedigree (<code>PEDIGREE</code>).
All objects inherit (from <code>OBJECT</code>) methods for initializing instances
and arrays of instances, for performing array operations, and for getting
information about themselves.

<h3>Methods and messages</h3>
Methods are the functions that objects execute in response to messages.
A message is a request to an object for a behavior that the object supports.
When it receives a message, the target object looks up a method that performs
the behavior for its class, and executes it. Any specific message may be
bound to different methods in different objects, according to class. This
separation of messages and methods allows objects to behave <a href="http://whatis.techtarget.com/definition/0,,sid9_gci212803,00.html">
polymorphically</a>.
(In Ficl, methods are words defined in the context of a class, and messages
are the names of those words.) Ficl classes associate messages with methods
for their instances (a fancy way of saying that each class owns a wordlist).
Ficl provides a late-binding operator <code>--></code> that sends messages
to objects at run-time, and an early-binding operator <code>=></code>
that compiles a specific class's method. These operators are the only supported
way to invoke methods. Regular Forth words are not visible to the method-binding
operators, so there's no chance of confusing a message with a regular
word of the same name.
<br><hr>
<h2><a NAME="oopgloss"></a>OOP Glossary</h2>
Note: with the exception of the binding operators (the first two definitions
here), all of the words in this section are internal factors that you don't
need to worry about. These words provide method binding for all classes
and instances. Also described are supporting words and execution factors.
All are defined in <code>softwords/oo.fr</code>.
<dl>
<dt>
-->   ( instance class "method-name" -- xn )
</dt>
<dd>
Late binding: looks up and executes the given method in the context of
the class on top of the stack.</dd>

<dt>
c->   ( instance class "method-name" -- xn exc )
</dt>
<dd>
Late binding with <code>CATCH</code>: looks up and <code>CATCH</code>es the given
method in the context of the class on top of the stack, pushes zero or
exception code upon return.</dd>

<dt>
my=> comp: ( "method-name" -- )&nbsp; exec: ( inst class -- xn )
</dt>

<dd>
Early binding: compiles code to execute the method of the class being defined.
Only visible and valid in the scope of a <code>--> sub</code> .. <code>end-class</code>
class definition.</dd>

<dt>
my=[ comp: ( "obj1 obj2 .. method ]" -- ) exec:( inst class -- xn )
</dt>
<dd>
Early binding: compiles code to execute a chain of methods of the class
being defined. Only visible and valid in the scope of a <code>--> sub</code>
.. <code>end-class</code> class definition.</dd>

<dt>
=>   comp: ( class meta "method-name" -- )  exec: ( inst class -- xn )
</dt>
<dd>
Early binding: compiles code to execute the method of the class specified
at compile time.
</dd>

<dt>
do-do-instance
</dt>
<dd>
When executed, causes the instance to push its ( instance class ) stack
signature. Implementation factor of <code>metaclass --> sub</code>.
Compiles <code>.do-instance</code> in the context of a class; <code>.do-instance</code>
implements the <code>does></code> part of a named instance.
</dd>

<dt>
exec-method   ( instance class c-addr u -- xn )
</dt>
<dd>
Given the address and length of a message (method name) on the stack, finds
the method in the context of the specified class and invokes it. Upon entry
to the method, the instance and class are on top of the stack, as usual.
If unable to find the method, prints an error message and aborts.
</dd>

<dt>
find-method-xt   ( class "method-name" -- class xt )
</dt>
<dd>
Attempts to map the message to a method in the specified class. If successful,
leaves the class and the execution token of the method on the stack. Otherwise
prints an error message and aborts.
</dd>

<dt>
lookup-method   ( class c-addr u -- class xt )
</dt>

<dd>
Given the address and length of a message (method name) on the stack, finds
the method in the context of the specified class. If unable to find the
method, prints an error message and aborts.
</dd>

<dt>
parse-method   comp: ( "method-name" -- )  exec: ( -- c-addr u )
</dt>

<dd>
Parse "name" from the input stream and compile code to push its length
and address when the enclosing definition runs.</dd>
</dl>
<h3>
<a NAME="glossinstance"></a>Instance Variable Glossary</h3>
<b>Note</b>: these words are only visible when creating a subclass! To
create a subclass, use the <code>sub</code> method on <code>object</code> or any
class derived from it (<i>not</i> <code>metaclass</code>). Source code for
Ficl OOP is in ficl/softwords/oo.fr.&nbsp;
<br>Instance variable words do two things: they create methods that do
an action appropriate for the type of instance variable they represent,
and they reserve space in the class template for the instance variable.
We'll use the term <i>instance variable</i> to refer both to the method
that gives access to a particular field of an object, and to the field
itself. Rather than give esentially the same example over and over, here's
one example that shows several of the instance variable construction words
in use:
<pre>
object subclass c-example
   cell:          .cell0
   c-4byte   obj: .nCells
 4 c-4byte array: .quad
            char: .length
79         chars: .name
end-clas
</pre>
This class only defines instance variables, and it inherits some methods
from <code>object</code>. Each untyped instance variable (.cell0, .length,
.name) pushes its address when executed. Each object instance variable
pushes the address and class of the aggregate object. Similar to C, an
array instance variable leaves its base address (and its class) when executed.
The word <code>subclass</code> is shorthand for <code>--> sub</code>
<dl>
<dt>cell:   ( offset "name" -- offset' )
<br>
Execution:  ( -- cell-addr )
</dt>

<dd>
Create an untyped instance variable one cell wide. The instance variable
leaves its payload's address when executed.</dd>

<dt>
cells:    ( offset nCells "name" -- offset' )
</dt>
<dt>
Execution:  ( -- cell-addr )
</dt>

<dd>
Create an untyped instance variable n cells wide.</dd>

<dt>
char:   ( offset "name" -- offset' )
</dt>

<dt>
Execution:  ( -- char-addr )
</dt>
<dd>
Create an untyped member variable one char wide</dd>

<dt>
chars:   ( offset nChars "name" -- offset' )
</dt>
<dt>
Execution:  ( -- char-addr )
</dt>
<dd>
Create an untyped member variable n chars wide.
</dd>

<dt>
obj:   ( offset class meta "name" -- offset' )
</dt>
<dt>
Execution:  ( -- instance class )
</dt>
<dd>
Aggregate an uninitialized instance of <code>class</code> as a member variable
of the class under construction.
</dd>

<dt>
<a NAME="arraycolon"></a>
array:  ( offset n class meta "name" -- offset' )
</dt>
<dt>
Execution:  ( -- instance class )
</dt>

<dd>
Aggregate an uninitialized array of instances of the class specified as
a member variable of the class under construction.</dd>

<dt>
<a NAME="exampleref:"></a>
ref:  ( offset class meta "name" -- offset' )
<br>
Execution:&nbsp; ( -- ref-instance ref-class )
</dt>
<dd>
Aggregate a reference to a class instance. There is no way to set the value
of an aggregated ref - it's meant as a way to manipulate existing data
structures with a Ficl OO model. For example, if your system contains a
linked list of 4 byte quantities, you can make a class that represents
a list element like this:
</dd>
</dl>

<pre>
object subclass c-4list
c-4list ref: .link
c-4byte obj: .payload
end-class;

address-of-existing-list c-4list --> ref mylist
</pre>
<p>
The last line binds the existing structure to an instance of the class
we just created. The link method pushes the link value and the class c_4list,
so that the link looks like an object to Ficl and like a struct to C (it
doesn't carry any extra baggage for the object model - the Ficl methods
alone take care of storing the class information).</p>

<p>
Note: Since a ref: aggregate can only support one class, it's good for
modeling static structures, but not appropriate for polymorphism. If you
want polymorphism, aggregate a c_ref (see classes.fr for source) into your
class - it has methods to set and get an object.</p>

<p>
By the way, it is also possible to construct a pair of classes that contain
aggregate pointers to each other. Here's an example:</p>

<pre>
object subclass akbar
suspend-class          \ put akbar on hold while we define jeff

object subclass jeff
    akbar ref: .significant-other
  ( your additional methods here )

end-class              \ done with jeff

akbar --> resume-class \ resume defining akbar
    jeff ref: .significant-other
  ( your additional methods here )
end-class              \ done with akbar
</pre>

<h3>
<a NAME="glossclass"></a>
Class Methods Glossary
</h3>
These words are methods of <code>metaclass</code>. They define the manipulations
that can be performed on classes. Methods include various kinds of instantiation,
programming tools, and access to member variables of classes. Source is
in <code>softwords/oo.fr</code>.
<dl>
<dt>
instance     ( class metaclass "name" -- instance class )
</dt>
<dd>
Create an uninitialized instance of the class, giving it the name specified.
The method leaves the instance 's signature on the stack (handy if you
want to initialize). Example:
</dd>
</dl>
<pre>
c_ref --> instance uninit-ref  2drop
</pre>
<dl>
<dt>
new    ( class metaclass "name" -- )
</dt>
<dd>
Create an initialized instance of class, giving it the name specified.
This method calls init to perform initialization.
</dd>

<dt>
array    ( nObj class metaclass "name" -- nObjs instance class )
</dt>
<dd>
Create an array of nObj instances of the specified class. Instances are
not initialized. Example:
</dd>
</dl>
<pre>
c_4byte --> array  40-raw-bytes  2drop drop
</pre>
<dl>
<dt>
new-array    ( nObj class metaclass "name" -- )
</dt>
<dd>
Creates an initialized array of nObj instances of the class. Same syntax
as <code>array</code>

<dt>
<a NAME="alloc"></a>
alloc   ( class metaclass -- instance class )
</dt>
<dd>
Creates an anonymous instance of <code>class</code> from the heap (using a call
to ficlMalloc() to get the memory). Leaves the payload and class addresses
on the stack. Usage example:
</dd>
</dl>
<pre>
c-ref --> alloc  2constant instance-of-ref
</pre>
<dl>
<dd>
Creates a double-cell constant that pushes the payload and class address
of a heap instance of c-ref.
</dd>

<dt>
<a NAME="allocarray"></a>
alloc-array   ( nObj class metaclass -- instance class )
</dt>
<dd>
Same as new-array, but creates anonymous instances from the heap using
a call to ficlMalloc(). Each instance is initialized using the class's
<code>init</code> method
</dd>

<dt>
<a NAME="allot"></a>
allot   ( class metaclass -- instance class )
</dt>
<dd>
Creates an anonymous instance of <code>class</code> from the dictionary. Leaves
the payload and class addresses on the stack. Usage example:
</dd>
</dl>
<pre>
c-ref --> allot  2constant instance-of-ref
</pre>
<dl>
<dd>
Creates a double-cell constant that pushes the payload and class address
of a heap instance of c-ref.
</dd>

<dt>
<a NAME="allotarray"></a>
allot-array   ( nObj class metaclass -- instance class )
</dt>
<dd>
Same as new-array, but creates anonymous instances from the dictionary.
Each instance is initialized using the class's
<code>init</code> method
</dd>

<dt>
ref    ( instance-addr class metaclass "name" -- )
</dt>
<dd>
Make a ref instance of the class that points to the supplied instance address.
No new instance space is allotted. Instead, the instance refers to the
address supplied on the stack forever afterward. For wrapping existing
structures.
</dd>

<dt>
sub    ( class metaclass -- old-wid addr[size] size )
</dt>
<dd>
Derive a subclass. You can add or override methods, and add instance variables.
Alias: <code>subclass</code>. Examples:
</dd>
</dl>
<pre>
c_4byte --> sub c_special4byte
( your new methods and instance variables here )
end-class

( --OR-- )

c_4byte subclass c_special4byte
( your new methods and instance variables here )
end-class
</pre>
<dl>
<dt>
.size   ( class metaclass -- instance-size )
</dt>
<dd>
Returns address of the class's instance size field, in address units. This
is a metaclass member variable.</dd>

<dt>
.super   ( class metaclass -- superclass )
</dt>
<dd>
Returns address of the class's superclass field. This is a metaclass member
variable.</dd>

<dt>
.wid   ( class metaclass -- wid )
</dt>
<dd>
Returns the address of the class's wordlist ID field. This is a metaclass
member variable.</dd>

<dt>
get-size
</dt>
<dd>
Returns the size of an instance of the class in address units. Imeplemented
as</dd>
</dl>

<pre>
: get-size   metaclass => .size @ ;
</pre>
<dl>

<dt>
get-wid
</dt>
<dd>
Returns the wordlist ID of the class. Implemented as</dd>

</dl>
<pre>
: get-wid   metaclass => .wid @ ;
</pre>
<dl>
<dt>
get-super</dt>
<dd>
Returns the class's superclass. Implemented as</dd>
</dl><pre>
: get-super   metaclass => .super @ ;
</pre>
<dl>
id   ( class metaclass -- c-addr u )
</dt>
<dd>
Returns the address and length of a string that names the class.</dd>

<dt>
methods   ( class metaclass -- )
</dt>
<dd>
Lists methods of the class and all its superclasses</dd>

<dt>
offset-of   ( class metaclass "name" -- offset )
</dt>
<dd>
Pushes the offset from the instance base address of the named member variable.
If the name is not that of an instance variable method, you get garbage.
There is presently no way to detect this error. Example:</dd>

</dl>
<pre>
metaclass --> offset-of .wid
</pre>
<dl>
<dt>
pedigree   ( class metaclass -- )
</dt>
<dd>
Lists the pedigree of the class (inheritance trail)</dd>

<dt>
see   ( class metaclass "name" -- )
</dt>
<dd>
Decompiles the specified method - obect version of <code>SEE</code>, from the
<code>TOOLS</code>
wordset.
</dd>
<dt>
debug   ( class metaclass "name" -- )
</dt>
<dd>
Invokes the debugger on the specified method - obect version of <code>DEBUG</code>.
</dd>
</dl>

<h3>
<a NAME="objectgloss"></a>
OBJECT Methods Glossary
</h3>
These are methods that are defined for all instances by the base class
<code>object</code>. The methods include default initialization, array manipulations, aliases of class methods, upcasting, and programming tools.
<dl>
<dt>
init   ( instance class -- )
</dt>
<dd>
Default initializer called automatically for all instances created with
<code>new</code> or <code>new-array</code>. Zero-fills the instance. You do not normally need to invoke <code>init</code> explicitly.</dd>

<dt>
array-init   ( nObj instance class -- )
</dt>
<dd>
Applies <code>init</code> to an array of objects created by <code>new-array</code>.
Note that <code>array:</code> does not cause aggregate arrays to be initialized
automatically. You do not normally need to invoke <code>array-init</code> explicitly.</dd>

<dt>
<a NAME="oofree"></a>
free   ( instance class -- )
</dt>
<dd>
Releases memory used by an instance previously created with <code>alloc</code>
or <code>alloc-array</code>. Note - this method is not presently protected
against accidentally deleting something from the dictionary. If you do
this, Bad Things are likely to happen. Be careful for the moment to apply
free only to instances created with <code>alloc</code> or <code>alloc-array</code>.
</dd>

<dt>
class   ( instance class -- class metaclass )
</dt>
<dd>
Convert an object signature into that of its class. Useful for calling
class methods that have no object aliases.</dd>

<dt>
super   ( instance class -- instance parent-class )
</dt>
<dd>
Upcast an object to its parent class. The parent class of <code>object</code>
is zero. Useful for invoking an overridden parent class method.</dd>

<dt>
pedigree   ( instance class -- )
</dt>
<dd>
Display an object's pedigree - its chain of inheritance. This is an alias
for the corresponding class method.</dd>

<dt>
size   ( instance class -- sizeof(instance) )
</dt>
<dd>
Returns the size, in address units, of one instance. Does not know about
arrays! This is an alias for the class method <code>get-size</code></dd>

<dt>
methods   ( instance class -- )
</dt>
<dd>
Class method alias. Displays the list of methods of the class and all superclasses
of the instance.</dd>

<dt>
index   ( n instance class -- instance[n] class )
</dt>
<dd>
Convert array-of-objects base signature into signature for array element
n. No check for bounds overflow. Index is zero-based, like C, so:
</dd>
</dl>
<pre>
0 my-obj --> index
\  is equivalent to
my-obj
</pre>
<dl>
<dd>
Check out the <a href="#minusrot">description of <code>-ROT</code></a> for
help in dealing with indices on the stack.</dd>

<dt>
next   ( instance[n] class -- instance[n+1] class )
</dt>
<dd>
Convert an array-object signature&nbsp; into the signature of the next
object in the array. No check for bounds overflow.</dd>

<dt>
prev    ( instance[n] class -- instance[n-1] class )
</dt>
<dd>Convert an object signature into the signature of the previous object
in the array. No check for bounds underflow.
</dd>
</dl>

<h3>
<a NAME="stockclasses"></a>Supplied Classes (See classes.fr)</h3>

<dl>
<dt>metaclass</dt>

<dd>
Describes all classes of Ficl. Contains class methods. Should never be
directly instantiated or subclassed. Defined in oo.fr. Methods described
above.</dd>

<dt>
object
</dt>
<dd>
Mother of all Ficl objects. Defines default initialization and array indexing
methods. Defined in oo.fr. Methods described above.</dd>

<dt>c-ref</dt>
<dd>
Holds the signature of another object. Aggregate one of these into a data
structure or container class to get polymorphic behavior. Methods &amp;
members:</dd>
</dl>

<pre>
get   ( inst class -- ref-inst ref-class )
set   ( ref-inst ref-class inst class -- )
.instance   \  cell member that holds the instance
      ( inst class -- a-addr ) 
.class      \  cell member that holds the class
      ( inst class -- a-addr )
</pre>
<dl>
<dt>
c-byte
</dt>
<dd>
Primitive class derived from <code>object</code>, with a 1-byte payload. Set
and get methods perform correct width fetch and store. Methods and members:
</dd>
</dl>
<pre>
get   ( inst class -- c )
set   ( c inst class -- )
.payload    \ member holds instance's value
      ( inst class -- addr ) 

</pre>
<dl>
<dt>c-2byte</dt>
<dd>
Primitive class derived from <code>object</code>, with a 2-byte payload. Set
and get methods perform correct width fetch and store. Methods and members:
</dd>
</dl>
<pre>
get   ( inst class -- 2byte )
set   ( 2byte inst class -- )
.payload   \ member holds instance's value
      ( inst class -- addr ) 
</pre>
<dl>
<dt>c-4byte</dt>
<dd>
Primitive class derived from <code>object</code>, with a 4-byte payload. Set
and get methods perform correct width fetch and store. Methods and members:</dd>
</dl>
<pre>
get   ( inst class -- x )
set   ( x inst class -- )
.payload    \ member holds instance's value
      ( inst class -- addr ) 
</pre>
<dl>
<dt>c-cell</dt>
<dd>
Primitive class derived from <code>object</code>, with a cell payload (equivalent
to c-4byte in 32 bit implementations, 64 bits wide on Alpha). Set and get
methods perform correct width fetch and store. Methods and members:
</dd>
</dl>
<pre>
get   ( inst class -- x )
set   ( x inst class -- )
.payload    \ member holds instance's value
      ( inst class -- addr ) 
</pre>
<dl>
<dt>c-ptr</dt>
<dd>
Base class derived from <code>object</code> for pointers to non-object types.
This class is not complete by itself: several methods depend on a derived
class definition of <code>@size</code>. Methods and members:</dd>
</dl>
<pre>
.addr   ( inst class -- a-addr )
\  member variable - holds the pointer address

get-ptr   ( inst class -- ptr )
set-ptr   ( ptr inst class -- )
inc-ptr   ( inst class -- )
\ Adds @size to pointer address
dec-ptr   ( inst class -- )
\ Subtracts @size from pointer address

index-ptr   ( i inst class -- )
\ Adds i*@size to pointer address
</pre>
<dl>
<dt>c-bytePtr</dt>
<dd>
Pointer to byte derived from c-ptr. Methods and members:
</dd>
</dl>
<pre>
@size   ( inst class -- size )
\  Push size of the pointed-to thing

get   ( inst class -- c )
\ Fetch the pointer's referent byte

set   ( c inst class -- ) 
\ Store c at the pointer address
</pre>
<dl>
<dt>c-2bytePtr</dt>
<dd>
Pointer to double byte derived from c-ptr. Methods and members:</dd>
</dl>
<pre>
@size   ( inst class -- size )
\  Push size of the pointed-to thing

get   ( inst class -- n )
\ Fetch the pointer's referent c-2byte

set   ( n inst class -- ) 
\ Store n at the pointer address
</pre>

<dl>
<dt>c-4bytePtr</dt>
<dd>
Pointer to quad-byte derived from c-ptr. Methods and members:
</dd>
</dl>
<pre>
@size   ( inst class -- size )
\  Push size of the pointed-to thing (a c-4byte)

get   ( inst class -- n )
\ Fetch the pointer's referent c-4byte

set   ( n inst class -- ) 
\ Store n at the pointer address
</pre>

<dl>
<dt>c-cellPtr</dt>
<dd>
Pointer to cell derived from c-ptr. Methods and members:</dd>
</dl>
<pre>
@size   ( inst class -- size )
\  Push size of the pointed-to thing (a cell)

get   ( inst class -- x )
\ Fetch the pointer's referent cell

set   ( x inst class -- ) 
\ Store n at the pointer address
</pre>

<dl>
<dt>c-string (see string.fr)</dt>
<dd>
Dynamically allocated string similar to MFC CString (Partial list of methods
follows)</dd>
</dl>
<pre>
set ( c-addr u 2:this -- )
\ Initialize buffer to the specified string

get ( 2:this -- c-addr u ) 
\ Return buffer contents as counted string

cat ( c-addr u 2:this -- ) 
\ Append given string to end of buffer

compare ( 2string 2:this -- n )
\ Return result of lexical compare

type ( 2:this -- )
\ Print buffer to the output stream


hashcode ( 2:this -- x )
\ Return hashcode of string (as in dictionary)

free ( 2:this -- )
\ Release internal buffer
</pre>

<dl>
<dt>c-hashstring (see string.fr)</dt>
<dd>
Derived from c-string. This class adds a hashcode member variable.</dd>
</dl>
</div>
</body>
</html>