home *** CD-ROM | disk | FTP | other *** search
- Unit GenDatum; {Generic Datum}
- {$R-,O+,V-}
-
- { Unit GenDatum written by Eric C. Wentz Last Mod - 7/9/89 }
-
- { Type Datum is not truly intended for direct use in programs, rather }
- { it is designed to be the ancestor object for a whole series of heirs }
- { which may range from Bytes to large Records. It is NOT suitable for }
- { definition of Linked data structures, but could certainly provide the }
- { nodes for such a structure. SEE UNITS DOUBLINK and GENLIST. }
-
- { All Datum variables must be CREATEd before any use. They must then be }
- { INITed to whatever data type you wish to use. They may then be re-used }
- { as another type by using DESTROY and then re-INITting to the new size. }
-
- { The only other Methods defined for Datum are Equality and Assignment, as }
- { These are generally applicable to any conceivable Polymorph of Datum. }
- { Set_Data is used in place of a construct such as REALDATUM := 3.1415, and }
- { Get_Data is used in place of a construct such as X := REALDATUM, where X }
- { is a simple Real variable. Copy will copy all the Bytes of the }
- { target to (Self), and the correct size, in the process re-INITting (Self) }
- { to the new data size. THERE IS NO GUARANTEE OF THE DESCENDANT-TYPE BEING }
- { PRESERVED!! This must be attended to by the programmer. Function Equal will }
- { return True for two CREATEd but un-INITted Datums (or ANY descendants), }
- { True when the data for two elements are precisely the same (again }
- { regardless of descendant type), and False under any other condition, }
- { Including unCREATEd Datums. }
-
- { Final IMPORTANT note: Code which uses Create more than once on a Datum will }
- { (probably) run for a while, but you will be using Heap space without any }
- { effort to return it. DESTROY and INIT are all that is needed or correct }
- { after a Datum has been CREATEd!! }
-
-
- INTERFACE
- Uses FlexPntr;
-
- Type
-
- Datum = Object
- DataList : FlexPtr;
-
- Procedure Create; {Datum's MUST be CREATEd before using!}
- Procedure Init (Size : Word);
- Procedure Destroy;
-
- Function DataSize : Word;
-
- Procedure Set_Data (Var Input_Data; Size : Word); {MUST send size of}
- Procedure Get_Data (Var Output_Data; Size : Word); {ACTUAL type of }
- {input parameter }
- {to these routines}
- Procedure Copy (From : Datum);
-
- Function Equal (EqualTo : Datum) : Boolean;
-
- End;
-
-
- IMPLEMENTATION
-
- Procedure Error (Num : Byte);
- Begin
- WriteLn;
- Write ('ERROR: ');
- Case Num of
- 0 : Write ('Attempted Set_Data with Incompatible Data Size.');
- 1 : Write ('Attempted Get_Data with Incompatible Data Size.');
- 2 : Write ('Attempted Destroy on Non-Initialized Datum.');
- 3 : Write ('Attempted Init on Initialized or UnCREATEd Datum.');
- 4 : Write ('**** OUT OF MEMORY ****');
- End;
- WriteLn;
- WriteLn ('**** PROGRAM TERMINATED ****');
- WriteLn;
- Write ('Press <Return> to Continue.... ');
- ReadLn;
- HALT (0)
- End;
-
-
- Procedure Datum.Init (Size : Word);
- Begin
- If DataList = Nil
- Then
- Begin
- GetMem (DataList,SizeOf(FlexCount)+Size);
- If DataList = Nil
- Then
- Error (4);
- DataList^.NumBytes := Size
- End
- Else
- Error (3)
- End;
-
- Procedure Datum.Destroy;
- Begin
- If DataList <> Nil
- Then
- Begin
- FreeMem (DataList,DataList^.NumBytes + SizeOf(FlexCount));
- DataList := Nil
- End
- Else
- Error (2)
- End;
-
- Procedure Datum.Create;
- Begin
- DataList := Nil
- End;
-
- Function Datum.DataSize : Word;
- Begin
- If DataList = Nil
- Then
- DataSize := 0
- Else
- DataSize := DataList^.NumBytes
- End;
-
- Procedure Datum.Set_Data (Var Input_Data; Size : Word);
- Begin
- If Size = DataSize
- Then
- Move (Input_Data,DataList^.Flex,Size)
- Else
- Error (0);
- End;
-
- Procedure Datum.Get_Data (Var Output_Data; Size : Word);
- Begin
- If Size = DataSize
- Then
- Move (DataList^.Flex,Output_Data,Size)
- Else
- Error (1)
- End;
-
- Procedure Datum.Copy (From : Datum);
- Var
- Temp : FlexPtr;
- Begin
- GetMem (Temp,From.DataSize);
- If DataList <> Nil Then Destroy;
- Init (From.DataSize);
- From.Get_Data (Temp^,DataSize);
- Set_Data (Temp^,DataSize);
- FreeMem (Temp,From.DataSize)
- End;
-
- Function Datum.Equal (EqualTo : Datum) : Boolean;
- Var
- Index : Word;
- Begin
- If DataSize <> EqualTo.DataSize
- Then
- Equal := False
- Else
- If DataSize = 0
- Then
- Equal := True
- Else
- Begin
- Index := 0;
- While (Index < (DataSize - 1))
- And ((EqualTo.DataList^.Flex[Index]) = (DataList^.Flex[Index]))
- Do Inc (Index);
- Equal := Index = DataSize - 1
- End
- End;
-
-
- BEGIN
- HeapError := @HeapErrorTrap {Exported from FlexPntr}
- END.