home *** CD-ROM | disk | FTP | other *** search
/ C!T ROM 2 / ctrom_ii_b.zip / ctrom_ii_b / PROGRAM / PASCAL / PASTUT34 / INC&UNIT.TXT < prev    next >
Text File  |  1993-01-19  |  8KB  |  183 lines

  1.                     INCLUDE FILES, UNITS and OVERLAYS.
  2.                     ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
  3.  
  4. Good economical programming makes use of as much existing material as
  5. possible. With Turbo Pascal, version 3, that existing material was in the
  6. form of Source Code ( i.e. files with extension .PAS ) and this source
  7. code, called the Include File, was included with the user program in the
  8. compilation process.
  9. Include files can still be used, but Units consisting of compiled code are
  10. clearly more attractive with version 6.0 of Turbo Pascal, especially as
  11. they may be associated with Overlays and also with Objects.
  12.  
  13. Include Files.
  14. ══════════════
  15. The syntax for an Include File is   {$I filename}
  16.  
  17. This include directive ensures that the named file is included in the
  18. source code of the main program at that point in the program. The
  19. include file cannot be specified in the middle of a procedure or function,
  20. but can appear at any point in the program between procedures or functions.
  21. It is frequently placed at the end of the declaration part, as shown below:
  22.  
  23. Program LinFit;         {For a linear fit to given data}
  24.  
  25. Uses Crt, .....;
  26. Var
  27.    x,y,z     :real;
  28.    ......
  29.  
  30. {$I FIT.PAS }           {Include the Least Squares curve fitting procedure}
  31.  
  32. Begin                   {Main program to provide the data, analyse it and}
  33.    ...                  {then display it graphically
  34.    ...
  35. End.
  36.  
  37. The program FIT.PAS was included in the Turbo Graphics Toolbox, originally
  38. supplied by Borland.
  39.  
  40. Units.
  41. ══════
  42. With Turbo Pascal, version 4 and onwards, the process of using existing
  43. material can be improved still further by the use of Units. A Unit is a
  44. piece of source code that can be compiled as a stand-alone entity. It
  45. contains data and program code and provides an Interface between the
  46. unit's code and data and other programs that will use that unit. Programs
  47. and other units can use units.
  48.  
  49. The Unit LIMITKEY.PAS which compiles to a Turbo Pascal Unit (.TPU) file is
  50. an example of a unit. It contains a procedure which ensures that only a
  51. selected range of keys will be accepted as the reply to a prompt. The
  52. procedure is:
  53.  
  54.    Procedure LimitChar(s : string; var Key : char; var Ekey : boolean);
  55.  
  56. where s is a string such as '1234567Qq' (see program KEYCHECK.PAS)
  57.       Key is the character for a single key press
  58.  and  EKey is a boolean (True of False) indicating whether an Escape
  59.       sequence or an ASCII extended code, employing a key press along
  60.       with either Escape, Shift, Ctrl or Alt is involved.
  61.  
  62. A Unit differs from an ordinary program in a number of respects:
  63.  
  64. 1. The first line is   'Unit LimitKey'   and not 'Program .....'
  65.  
  66. 2. A Unit has two sections  an Interface  and an Implementation.
  67.  
  68.    The Interface is a 'header' that makes the procedures and functions
  69.    of the unit available to the program that uses the unit. It contains
  70.    a Uses clause, if other units are used by the unit, and then a list
  71.    of all the procedures and functions with all their parameters, as with
  72.    the procedure LimitChar above. The information in the Interface section
  73.    must be documented and made available to the user of the unit.
  74.  
  75.    The Implementation section contains all the code to implement the
  76.    procedures and functions of the Interface section. Documentation for
  77.    this section and the source code for a unit is not available to the
  78.    user of the unit. It is therefore protected from external modification.
  79.    Only for educational purposes is the source code provided as with the
  80.    unit LIMITKEY.PAS
  81.  
  82.    All the procedures and variables declared in the Interface section are
  83.    'public' and can be accessed from outside the unit.
  84.    Any procedures, functions and variables used in the Implementation
  85.    section but not declared in the Interface section are 'private' and
  86.    cannot be accessed from outside the unit.
  87.  
  88. Writing your own units is described in detail in pages 68-72 of the User's
  89. Guide. The standard units, including System, Crt, Dos, are described on
  90. pages 66-67 of the User's Guide, whilst the 200 or more procedures and
  91. functions contained in these standard units are documented in the Library
  92. Reference, with a sample layout of the documentation given on page 3.
  93.  
  94. Unit modification dangers.
  95. ══════════════════════════
  96. There is clearly a danger that the source code for a unit may be modified
  97. at some stage without being recompiled, so that the .TPU file is out-of-
  98. date. There are two ways to make sure that unit files are up-to-date, the
  99. first makes use of the fact that DOS stamps all files with time and date.
  100.  
  101. The MAKE option tells the compiler to check the date and time of the source
  102. code and the compiled unit file (.TPU), used by the main program. If the
  103. source code was modified since the unit was compiled, the compiler will
  104. recompile the unit to bring it up-to-date.
  105.  
  106. The BUILD option will recompile the source code of all units used by the
  107. main program, without checking date and time, so making absolutely sure
  108. that they are up-to-date.
  109.  
  110.  
  111. Overlays.
  112. ═════════
  113. Overlays are parts of a program that share a common memory area, called the
  114. Overlay Buffer, which is located between the Stack Segment and the Heap
  115. (See page 210 of the Programmer's Guide).
  116.  
  117. Each Overlay consists of a Turbo Pascal Unit, and these overlay units are
  118. controlled by an Overlay Manager, which is initialized by the OvrInit
  119. procedure found in the standard Overlay Unit.
  120.  
  121. The Overlay Buffer defaults to a size large enough to hold the largest unit
  122. that is overlaid and hence uses a minimum amount of memory, so that the
  123. heap can have a maximum size.
  124.  
  125. When a program involving overlays is compiled an overlay file (.OVR) is
  126. created along with the normal .EXE file. The .EXE file contains the
  127. non-overlaid parts of the program, whilst the .OVR file contains all the
  128. overlay units that will be swapped in and out of the overlay buffer
  129. during program execution.
  130.  
  131. An overlay unit is similar to an ordinary unit except in the following
  132. respects:
  133.  
  134.     1. All overlay units must include a {$O+} compiler directive to
  135.        ensure that the generated code can be overlaid.
  136.     2. The {$F+} compiler directive must be used to ensure that all
  137.        procedures and function use the FAR call model.
  138.  
  139.        Thus {$O+,F+} should appear at the start of the unit declaration,
  140.        as in the examples OVRDEMO1.PAS and OVRDEMO2.PAS
  141.  
  142. Programs that use overlays must include the following features:
  143.  
  144.     1.  The unit 'Overlay' must be declared in the uses clause before
  145.         any other unit.
  146.     2.  Initialize the overlay manager with a call to OvrInit and pass
  147.         the name of the .OVR file, which should be the program name.OVR
  148.     3.  Issue separate {$O unitname} directives for each overlay unit.
  149.     4.  Test OvrResult after OvrInit calls (optional).
  150.  
  151. Thus the main program OVRDEMO.PAS, which uses the units OVRDEMO1.TPU and
  152. OVRDEMO2.TPU includes the following lines:
  153.  
  154. {$F+,O+}
  155. program OvrDemo;
  156.  
  157. uses
  158.   Overlay, Crt, OvrDemo1, OvrDemo2;
  159.  
  160. {$O OvrDemo1}
  161. {$O OvrDemo2}
  162.  
  163.   ....
  164.   ....
  165.   OvrInit('OVRDEMO.OVR');       { init overlay system, reserve heap space }
  166.   if OvrResult <> 0 then
  167.   begin
  168.     Writeln('Overlay error: ', OvrResult);
  169.     Halt(1);
  170.   end;
  171.   ....
  172.  
  173. The main program OVRDEMO.PAS then calls a procedure Write1 from unit
  174. OVRDEMO1.TPU and then a procedure Write2 from unit OVRDEMO2.TPU
  175.  
  176. This program and these units are Borland's examples, with slight
  177. modifications by Ron Shaw to give an indication as to which unit is in use.
  178.  
  179.  
  180.  
  181. INC&UNIT.TXT
  182. Modified 18.1.93
  183.