- Robert Hunter and Jerry Garcia
#===================================================================================================
# Example Script: Aztec "Using Classes" Script
# Demonstrates use of Aztec classes, inheritance, polymorphism, templates and satellite classes.
#===================================================================================================
# Main class that is derived from Thread. VM automatically creates us and executes Run().
class Main from<Thread>
{
method Main
{
# Create the single linked list object to be used below.
MyLinkedList = new<LinkedList<SimpleBaseClass>>
}
method virtual Run()
{
data<int> Count
data<SimpleBaseClass> MySimpleClass
# Sit in a short loop, create new objects and add them to the linked list.
iterate ( Count in 1..10 )
{
# Create a SimpleClass1 or SimpleClass2 object (both derived from SimpleBaseClass).
if ( (Count % 2) == 0 )
MySimpleClass = new<SimpleClass2("Aztec_" + Count.Str() + "_b")>
else
MySimpleClass = new<SimpleClass1("Aztec_" + Count.Str() + "_a")>
# Add the new object to the linked list.
MyLinkedList.AddItem(MySimpleClass)
}
# Call the method to access the linked list dump out the contents.
PrintList()
}
method PrintList()
{
data<SimpleBaseClass> MySimpleClass
# Loop through the linked list and dump out each item.
for ( MySimpleClass = MyLinkedList.GetTopItem() ;
MySimpleClass != null ;
MySimpleClass = MyLinkedList.GetNextItem() )
{
StdIO.Write(MySimpleClass.GetName())
}
}
data<LinkedList<SimpleBaseClass>> MyLinkedList
}
#--------------------------------------------------------------------------
# Simple class hierarchy to use for LinkedList demonstration. There is an
# abstract base class and two classes derived from that base. The linked
# list template will store the "base" class and rely on polymorphism to
# get the name of the object.
#--------------------------------------------------------------------------
class SimpleBaseClass
{
method<string> abstract GetName()
method SimpleBaseClassCleanup()
{
StdIO.Write("Inside SimpleBaseClass destructor for " + GetName())
}
}
# This class is derived from abstract SimpleBaseClass and overrides GetName().
class SimpleClass1 from<SimpleBaseClass>
{
method SimpleClass1(string Name)
{
ClassName = Name
}
method<string> virtual GetName()
{
return("SimpleClass1->" + ClassName)
}
data<string> ClassName
}
#-----------------------------------------------------------------------------------
# This class is also derived from abstract SimpleBaseClass and overrides GetName().
# For demonstration purposes, it is split into two parts - primary and satellite.
#-----------------------------------------------------------------------------------
class dynamic SimpleClass2 from<SimpleBaseClass>
{
method SimpleClass2(string Name)
{
ClassName = Name
}
data<string> ClassName
}
class satellite SimpleClass2
{
method<string> virtual GetName()
{
return("SimpleClass2->" + ClassName)
}
}
#---------------------------------------------------------------------------
# Simple linked list class demonstrating template usage. Note that it is a
# barebones class, and not derived from Collection, to keep it simple.
#---------------------------------------------------------------------------
class LinkedList<T>
{
method LinkedList()
{
LastNode = null
FirstNode = null
CurrentNode = null
}
# Currently adds item to end of list only.
method AddItem(T Item)
{
data<LinkedListNode<T>> NewNode
# Create new node and pass in the item to be attached to it.
NewNode = new<LinkedListNode<T>(Item)>
if ( (FirstNode != null) & (LastNode != null) )
{
# Attach it to last item and then reset last to this new one.
LastNode.SetNext(NewNode)
LastNode = NewNode
}
else
{
# Empty list, so set 'first' and 'last' to this new node.
FirstNode = NewNode
LastNode = NewNode
}
}
method<T> GetTopItem()
{
data<T> Item
CurrentNode = null
if ( FirstNode != null )
{
Item = FirstNode.GetItem()
CurrentNode = FirstNode
}
return(Item)
}
method<T> GetNextItem()
{
data<T> Item
if ( CurrentNode != null )
{
CurrentNode = CurrentNode.GetNext()
if ( CurrentNode != null )
{
Item = CurrentNode.GetItem()
}
}
return(Item)
}
# Data item to control and manage simple linked list.
data<LinkedListNode<T>> LastNode
data<LinkedListNode<T>> FirstNode
data<LinkedListNode<T>> CurrentNode
}
# Support class for the LinkedList<> class, which also uses templates.
class LinkedListNode<T>
{
method LinkedListNode(T Item)
{
ThisItem = Item
NextNode = null
}
method<LinkedListNode<T>> GetNext()
{
return(NextNode)
}
method SetNext(LinkedListNode<T> Node)
{
NextNode = Node
}
method<T> GetItem()
{
return(ThisItem)
}
# Data items to manage a single LinkedListNode.
data<T> ThisItem
data<LinkedListNode<T>> NextNode
}