home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / GENERI.ZIP / GENLIST.ARC / GENDATUM.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1989-07-23  |  5.1 KB  |  178 lines

  1. Unit GenDatum; {Generic Datum}
  2. {$R-,O+,V-}
  3.  
  4. { Unit GenDatum  written by Eric C. Wentz  Last Mod - 7/9/89 }
  5.  
  6. { Type Datum is not truly intended for direct use in programs, rather   }
  7. { it is designed to be the ancestor object for a whole series of heirs  }
  8. { which may range from Bytes to large Records.  It is NOT suitable for  }
  9. { definition of Linked data structures, but could certainly provide the }
  10. { nodes for such a structure. SEE UNITS DOUBLINK and GENLIST.           }
  11.  
  12. { All Datum variables must be CREATEd before any use. They must then be   }
  13. { INITed to whatever data type you wish to use.  They may then be re-used }
  14. { as another type by using DESTROY and then re-INITting to the new size.  }
  15.  
  16. { The only other Methods defined for Datum are Equality and Assignment, as    }
  17. { These are generally applicable to any conceivable Polymorph of Datum.       }
  18. { Set_Data is used in place of a construct such as REALDATUM := 3.1415, and   }
  19. { Get_Data is used in place of a construct such as X := REALDATUM, where X    }
  20. { is a simple Real variable.  Copy will copy all the Bytes of the             }
  21. { target to (Self), and the correct size, in the process re-INITting (Self)   }
  22. { to the new data size.  THERE IS NO GUARANTEE OF THE DESCENDANT-TYPE BEING   }
  23. { PRESERVED!! This must be attended to by the programmer. Function Equal will }
  24. { return True for two CREATEd but un-INITted Datums (or ANY descendants),     }
  25. { True when the data for two elements are precisely the same (again           }
  26. { regardless of descendant type), and False under any other condition,        }
  27. { Including unCREATEd Datums.                                                 }
  28.  
  29. { Final IMPORTANT note: Code which uses Create more than once on a Datum will }
  30. { (probably) run for a while, but you will be using Heap space without any    }
  31. { effort to return it.  DESTROY and INIT are all that is needed or correct    }
  32. { after a Datum has been CREATEd!!                                            }
  33.  
  34.  
  35. INTERFACE
  36. Uses FlexPntr;
  37.  
  38. Type
  39.  
  40.   Datum = Object
  41.     DataList : FlexPtr;
  42.  
  43.     Procedure Create;              {Datum's MUST be CREATEd before using!}
  44.     Procedure Init (Size : Word);
  45.     Procedure Destroy;
  46.  
  47.     Function DataSize : Word;
  48.  
  49.     Procedure Set_Data (Var Input_Data; Size : Word);  {MUST send size of}
  50.     Procedure Get_Data (Var Output_Data; Size : Word); {ACTUAL type of   }
  51.                                                        {input parameter  }
  52.                                                        {to these routines}
  53.     Procedure Copy (From : Datum);
  54.  
  55.     Function Equal (EqualTo : Datum) : Boolean;
  56.  
  57.   End;
  58.  
  59.  
  60. IMPLEMENTATION
  61.  
  62. Procedure Error (Num : Byte);
  63. Begin
  64.   WriteLn;
  65.   Write ('ERROR: ');
  66.   Case Num of
  67.            0 : Write ('Attempted Set_Data with Incompatible Data Size.');
  68.            1 : Write ('Attempted Get_Data with Incompatible Data Size.');
  69.            2 : Write ('Attempted Destroy on Non-Initialized Datum.');
  70.            3 : Write ('Attempted Init on Initialized or UnCREATEd Datum.');
  71.            4 : Write ('**** OUT OF MEMORY ****');
  72.           End;
  73.   WriteLn;
  74.   WriteLn ('**** PROGRAM TERMINATED ****');
  75.   WriteLn;
  76.   Write ('Press <Return> to Continue.... ');
  77.   ReadLn;
  78.   HALT (0)
  79. End;
  80.  
  81.  
  82. Procedure Datum.Init (Size : Word);
  83. Begin
  84.   If DataList = Nil
  85.     Then
  86.       Begin
  87.         GetMem (DataList,SizeOf(FlexCount)+Size);
  88.         If DataList = Nil
  89.           Then
  90.             Error (4);
  91.         DataList^.NumBytes := Size
  92.       End
  93.     Else
  94.       Error (3)
  95. End;
  96.  
  97. Procedure Datum.Destroy;
  98. Begin
  99.   If DataList <> Nil
  100.     Then
  101.       Begin
  102.         FreeMem (DataList,DataList^.NumBytes + SizeOf(FlexCount));
  103.         DataList := Nil
  104.       End
  105.     Else
  106.       Error (2)
  107. End;
  108.  
  109. Procedure Datum.Create;
  110. Begin
  111.   DataList := Nil
  112. End;
  113.  
  114. Function Datum.DataSize : Word;
  115. Begin
  116.   If DataList = Nil
  117.     Then
  118.       DataSize := 0
  119.     Else
  120.       DataSize := DataList^.NumBytes
  121. End;
  122.  
  123. Procedure Datum.Set_Data (Var Input_Data; Size : Word);
  124. Begin
  125.   If Size = DataSize
  126.     Then
  127.       Move (Input_Data,DataList^.Flex,Size)
  128.     Else
  129.       Error (0);
  130. End;
  131.  
  132. Procedure Datum.Get_Data (Var Output_Data; Size : Word);
  133. Begin
  134.   If Size = DataSize
  135.     Then
  136.       Move (DataList^.Flex,Output_Data,Size)
  137.     Else
  138.       Error (1)
  139. End;
  140.  
  141. Procedure Datum.Copy (From : Datum);
  142. Var
  143.   Temp : FlexPtr;
  144. Begin
  145.   GetMem (Temp,From.DataSize);
  146.   If DataList <> Nil Then Destroy;
  147.   Init (From.DataSize);
  148.   From.Get_Data (Temp^,DataSize);
  149.   Set_Data (Temp^,DataSize);
  150.   FreeMem (Temp,From.DataSize)
  151. End;
  152.  
  153. Function Datum.Equal (EqualTo : Datum) : Boolean;
  154. Var
  155.   Index : Word;
  156. Begin
  157.   If DataSize <> EqualTo.DataSize
  158.     Then
  159.       Equal := False
  160.     Else
  161.       If DataSize = 0
  162.         Then
  163.           Equal := True
  164.         Else
  165.           Begin
  166.             Index := 0;
  167.             While (Index < (DataSize - 1))
  168.               And ((EqualTo.DataList^.Flex[Index]) = (DataList^.Flex[Index]))
  169.                   Do Inc (Index);
  170.             Equal := Index = DataSize - 1
  171.           End
  172. End;
  173.  
  174.  
  175. BEGIN
  176.   HeapError := @HeapErrorTrap  {Exported from FlexPntr}
  177. END.
  178.