Oberon || Library || Module Index || Search Engine || Definition || Module


Ulm's Oberon Library:
Operations


NAME

Operations - generic interface for arithmetic operations

SYNOPSIS

CONST add = 0; sub = 1; mul = 2; div = 3; cmp = 4;
TYPE Operation = SHORTINT; (* [add..div] *)
TYPE Operand = POINTER TO OperandRec;
TYPE OperandRec = RECORD (PersistentDisciplines.ObjectRec) END;
TYPE CapabilitySet = SET; (* SET OF [add..cmp] *)
TYPE CreateProc = PROCEDURE (VAR op: Operand);
TYPE OperatorProc = PROCEDURE (op: Operation; op1, op2: Operand; VAR result: Operand);
TYPE AssignProc = PROCEDURE (VAR target: Operand; source: Operand);
TYPE CompareProc = PROCEDURE (op1, op2: Operand) : INTEGER;
TYPE Interface = POINTER TO InterfaceRec;
TYPE InterfaceRec =
   RECORD
      (Objects.ObjectRec)
      create: CreateProc;
      assign: AssignProc;
      op: OperatorProc;
      compare: CompareProc;
   END;


PROCEDURE Init(op: Operand; if: Interface; caps: CapabilitySet); PROCEDURE Capabilities(op: Operand) : CapabilitySet; PROCEDURE Compatible(op1, op2: Operand) : BOOLEAN; PROCEDURE Add(op1, op2: Operand) : Operand; PROCEDURE Add2(VAR op1: Operand; op2: Operand); PROCEDURE Add3(VAR result: Operand; op1, op2: Operand); PROCEDURE Sub(op1, op2: Operand) : Operand; PROCEDURE Sub2(VAR op1: Operand; op2: Operand); PROCEDURE Sub3(VAR result: Operand; op1, op2: Operand); PROCEDURE Mul(op1, op2: Operand) : Operand; PROCEDURE Mul2(VAR op1: Operand; op2: Operand); PROCEDURE Mul3(VAR result: Operand; op1, op2: Operand); PROCEDURE Div(op1, op2: Operand) : Operand; PROCEDURE Div2(VAR op1: Operand; op2: Operand); PROCEDURE Div3(VAR result: Operand; op1, op2: Operand); PROCEDURE Compare(op1, op2: Operand) : INTEGER; PROCEDURE Assign(VAR target: Operand; source: Operand); PROCEDURE Copy(source, target: Operand);

DESCRIPTION

Operations provides a generic interface for arithmetic operators which covers the most usual operations: addition, subtraction, multiplication and division. Comparison and assignment are also supported.

The interface describes the set of available operations and contains the necessary procedures. The interface procedures should meet the specifications following:

create: PROCEDURE(VAR op: Operand);
create op and call Init for op. Note that Operand is an extension of PersistentObjects.Object. Thus, PersistentObjects.Init must be called for newly created operands.

assign: PROCEDURE(VAR target: Operand; source: Operand);
copy the value of source to target; target is of the appropriate type and already initialized.

op: PROCEDURE(op: Operation; op1, op2: Operand; VAR result: Operand);
execute the given operator for the given operands op1 and op2 and store the result in the already initialized result. op1 and op2 are already checked for type equality and result is of the appropriate type. This procedure is not called with op values outside the capability set of the associated interface. result is guaranteed to be not equal to op1 or op2.

compare: PROCEDURE(op1, op2: Operand) : INTEGER;
compare the given operands and return an integer value less than zero if op1 is less than op2, or equal to zero if op1 equals op2, or greater than zero if op1 is greater than op2. This procedure needs not to be implemented if cmp is not in the set of capabilities.

Init is typically called by modules which implement an arithmetic data type to connect the interface if to the operand op. Capabilities returns the set of capabilities of op. Compatible checks op1 and op2 for being compatible, i.e. they share the same interface.

Arithmetic operations are implemented in three forms:

XXX(op1, op2: Operand) : Operand
Executes the named operation and returns a newly created operand.
XXX2(VAR op1, op2: Operand)
Operates on op1 and op2 and stores the result into op1.
XXX3(VAR result: Operand; op1, op2: Operand)
Operates on op1 and op2 and stores the result into result.

This procedures are provided for XXX equal to Add, Sub, Mul or Div. XXX2 and XXX3 allow result to be equal to op1 or op2.

Compare compares the given operands and returns an integer value which corresponds to the comparison (see interface description of compare).

Assign copies the value of source to target. Remember, that operands are pointers and a pointer-assignment (i.e. target := source) does not copy the value of an operand. target is newly created unless it is not equal to NIL and its type is identical to that of source.

The VAR parameters of the operation procedures are always newly created.

Copy copies the value of source to target without recreating target. target must be of the appropriate type and already initialized. This is useful if old references to target should reference the new value.

DIAGNOSTICS

Operations checks for some errors which may lead to failed assertions:

SEE ALSO

PersistentObjects
interface for persistent objects
Scales
specialized variant of Operations for measures

Edited by: borchert, last change: 1996/09/16, revision: 1.6, converted to HTML: 1999/02/23

Oberon || Library || Module Index || Search Engine || Definition || Module