home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / programm / prog3 / chap29.txt < prev    next >
Encoding:
Text File  |  1991-07-01  |  11.8 KB  |  292 lines

  1.  
  2.  
  3.  
  4.  
  5.                                                        Chapter 29
  6.                                                  GENERIC PACKAGES
  7.  
  8.  
  9.  
  10. OUR FIRST GENERIC PACKAGE
  11. _________________________________________________________________
  12.  
  13. In the last chapter we studied the use of generic subprograms.
  14. This chapter will be devoted to the study of generic packages, and
  15. we will exhaust the topic because only procedures, functions, and
  16. packages can be used as generic units.
  17.  
  18. Examine the file named GENPKG.ADA for our first  ================
  19. example of a generic package.  This package is      GENPKG.ADA
  20. named EasyPkg, because it is so easy to          ================
  21. understand, and is composed of one procedure and
  22. one function.  The generic package begins with
  23. the reserved word generic, followed by two generic formal
  24. parameters, and the standard format for an Ada package
  25. specification in lines 5 through 9.  The first generic formal
  26. parameter is a discrete type, and the second is a floating point
  27. type as we discussed in the last chapter.
  28.  
  29. The instantiating statements are given in lines 39 and 41 of the
  30. main program and are no different than those used in the last
  31. chapter except for the first word, the reserved word package,
  32. because we are declaring an instance of a package in this case.
  33. If you replace the formal parameter types with the types declared
  34. in each instantiation statement, you will have a normal package
  35. just as we studied previously.  In the case of a package, we can
  36. add the use clause as we have in lines 40 and 42, eliminating the
  37. need for the extended naming notation, commonly called the dot
  38. notation.  After declaring a few variables to work with, we are
  39. ready to exercise the two procedures and functions we have
  40. declared.  It should be clear to you that we have a procedure and
  41. a function in the package named Funny_Stuff, and another procedure
  42. and function in the package named Usual_Stuff.
  43.  
  44. A fine point must be mentioned about the use clause when used with
  45. the generic package.  It is not legal to use a use clause with the
  46. generic package itself.  Every instantiation must explicitly give
  47. the extended name for the generic package.  Of course this only
  48. occurs with nested generic packages which are the topic of the next
  49. example program.  The use clause however, is legal for use with any
  50. and every instantiated copy of a generic package.  The instantiated
  51. package's new name cannot be overloaded because it is not permitted
  52. to overload the name of any package.
  53.  
  54.  
  55.  
  56.  
  57.  
  58.                                                         Page 29-1
  59.  
  60.                                     Chapter 29 - Generic Packages
  61.  
  62. USING THE INSTANTIATED PACKAGES
  63. _________________________________________________________________
  64.  
  65. In line 53, we call the procedure named Trade_Values, but since
  66. there are two procedures with this name, Ada will pick the correct
  67. one by comparing the types.  This is our old friend called
  68. overloading again.  In line 54, because of the types used, the
  69. other procedure will be used as indicated in the comments.  In
  70. lines 55 and 56, we explicitly tell the system which overloading
  71. to use, but it will still check the types to see if they are
  72. compatible.  In line 57 we tell the system to use the wrong one
  73. which leads to a type mismatch and a compile error, and in line 58,
  74. we use two different types for the parameters, which is another
  75. type mismatch.  (Lines 57 and 58 are commented out so that we will
  76. not actually get the error, but you should remove the comments to
  77. see that the compiler does report an error.)
  78.  
  79. Lines 60 through 64 give examples of proper usage of the two
  80. functions instantiated above and illustrate that the literals of
  81. type universal_real are compatible with both copies of the function
  82. as you would expect.  The compiler uses the type of the assignment
  83. variable on the left hand side of the assignment statement to
  84. decide which overloading to use for each statement.
  85.  
  86. After you spend enough time to understand this program, compile and
  87. execute it even though it has no output.
  88.  
  89.  
  90.  
  91. NESTED GENERIC PACKAGES
  92. _________________________________________________________________
  93.  
  94. The example program named NESTPKG.ADA contains    ===============
  95. a generic package nested within another             NESTPKG.ADA
  96. non-generic package.  The outer package contains  ===============
  97. a single procedure, and the nested generic
  98. package contains a single formal generic
  99. parameter of a floating point type, and a single function.  The
  100. procedure and function are the same as those in the last program,
  101. but they are organized differently here for illustration.
  102.  
  103. Note carefully that since the procedure is not in the generic part
  104. of the outer package, it is directly available in the same manner
  105. as if it were in a package without a generic part.  The package
  106. name can be declared in a use clause which will make the
  107. non-generic portion of the package usable.  This is not really too
  108. new to you because the Text_IO package we have been using
  109. throughout this tutorial contains several nested generic packages.
  110. The procedure to Put a string, as well as the procedures named
  111. Put_Line and New_Line are in the non-generic part of the package,
  112. and the procedures to output variables are nested in generic parts.
  113. It would be beneficial for you to take a look at the package named
  114. Text_IO in section 14.3.10 of the LRM.  Spending a few minutes
  115. studying that package at this time will be time well spent.
  116.  
  117.                                                         Page 29-2
  118.  
  119.                                     Chapter 29 - Generic Packages
  120.  
  121.  
  122. The executable part of the program is very simple and very similar
  123. to the last example program so it will be left to the student to
  124. study, compile, and execute this program.
  125.  
  126.  
  127.  
  128. OBJECTS AS GENERIC FORMAL PARAMETERS
  129. _________________________________________________________________
  130.  
  131. Examine the program named OBJGEN.ADA for an      ================
  132. example of using objects for formal generic         OBJGEN.ADA
  133. parameters instead of just types.  In this case  ================
  134. the number of ROWS and the number of COLUMNS
  135. will be part of the instantiation, and the
  136. resulting procedure and function will be ready to work with the
  137. desired size matrices.  In addition, the constant named ROWS, and
  138. the constant named COLUMNS are initialized to the values given in
  139. lines 4 and 5.  If a value is not given with the instantiation,
  140. these values will be used in much the same way that we use default
  141. values with a procedure or function.
  142.  
  143. In this case, the package body uses the standard Text_IO package
  144. and outputs a string to the monitor each time one of the
  145. subprograms is called.  This is only done to illustrate to you that
  146. there is nothing magic about the package Text_IO, and that it can
  147. be used within another package.
  148.  
  149.  
  150.  
  151. USING THE OBJECTS
  152. _________________________________________________________________
  153.  
  154. In line 58, the package is instantiated using the positional
  155. aggregate notation with values of 3 and 5 for the matrix size.
  156. Line 60 illustrates the use of the defaults declared in the generic
  157. part above, and line 61 illustrates the use of the named aggregate
  158. notation used during package instantiation.
  159.  
  160.  
  161.  
  162. AN EXPORTED TYPE CAN BE USED
  163. _________________________________________________________________
  164.  
  165. Referring back to line 8, we have the type named LOCAL_MATRIX
  166. declared in the package specification and therefore available to
  167. any calling program.  If you refer to the private part of the
  168. package specification, you will see that the type LOCAL_MATRIX is
  169. declared to be a function of the formal generic objects.  After we
  170. instantiate a copy of the package, we have the exported type
  171. available for use in the calling program.  We use the types
  172. exported in lines 66 through 68 to declare a few variables, but
  173. even though two of the instantiations make use of the use clause,
  174. we must explicitly declare the desired type by using the extended
  175.  
  176.                                                         Page 29-3
  177.  
  178.                                     Chapter 29 - Generic Packages
  179.  
  180. naming notation.  This is because the compiler has no way of
  181. knowing which exported type we are interested in using for each
  182. variable.
  183.  
  184. With the above descriptions of the new concepts in this program,
  185. you should be able to understand the details of it.  Be sure to
  186. compile and execute this program.
  187.  
  188.  
  189.  
  190. A PROCEDURE AS A GENERIC PARAMETER
  191. _________________________________________________________________
  192.  
  193. Examine the program named PROCPKG.ADA for an      ===============
  194. example of a procedure being used as a generic      PROCPKG.ADA
  195. formal parameter.  The syntax uses the reserved   ===============
  196. word with to begin the formal parameter in line
  197. 4, and the complete header for the procedure is
  198. given with the list of formal parameters and their types.  This
  199. procedure can then be called from within the body of the package
  200. and will refer to a procedure that is actually outside of the
  201. generic package.  Now we must define the procedure that will be
  202. called in order to satisfy a call to this formal parameter.  We
  203. will see where this procedure is defined shortly.
  204.  
  205. Refer to the main program where the procedure named R_Average is
  206. declared.  It has exactly the same formal parameter structure as
  207. that declared in the generic formal parameter, so it can be used
  208. in the instantiating call in line 40.  A call to the procedure in
  209. the generic instantiation actually results in a call back to the
  210. procedure in the calling program to do some calculations.
  211.  
  212.  
  213.  
  214. WHAT CAN THIS BE USED FOR?
  215. _________________________________________________________________
  216.  
  217. This capability gives you the ability to write a generic package
  218. that can be used in several places but with a slight difference in
  219. each place, because each instantiation can use a different
  220. procedure for the reference back to the calling program.  This is
  221. simply another one of the available entities that add to the
  222. flexibility of Ada.  After you understand the logic here, you
  223. should compile and execute this program.
  224.  
  225.  
  226.  
  227. A FUNCTION AS A GENERIC PARAMETER
  228. _________________________________________________________________
  229.  
  230. Examine the program named FUNCPKG.ADA for an example of using a
  231. function as a generic formal parameter.  The logic is similar to
  232. that described in the last program except that a function is used
  233. for the reference back to the calling program.  No further
  234.  
  235.                                                         Page 29-4
  236.  
  237.                                     Chapter 29 - Generic Packages
  238.  
  239. explanation will be given since it should not be  ===============
  240. needed.  It should not come as much of a            FUNCPKG.ADA
  241. surprise to you that several procedures or        ===============
  242. functions, or a combination of them, can be used
  243. as generic formal parameters.  Any of the
  244. isolated examples can be combined as needed to achieve the desired
  245. goal.  Nesting of generic and normal packages can be done as
  246. needed.  It will be up to you to decide how to modularize and
  247. package your program to best accomplish the stated goals.  Ada
  248. gives you many options.  Be sure to compile and execute this
  249. program.
  250.  
  251.  
  252.  
  253. PROGRAMMING EXERCISES
  254. _________________________________________________________________
  255.  
  256. 1.   Add an enumerated type to GENPKG.ADA, instantiate a copy of
  257.      EasyPkg, and use it to swap some values of the enumerated
  258.      type.  You will have to supply a floating point type with the
  259.      enumerated type when you instantiate it.
  260.  
  261. 2.   Convert CHARSTAK.ADA from chapter 16 of this tutorial into a
  262.      generic package that can be used with any discrete type.
  263.      Modify TRYSTAK.ADA from the same chapter to instantiate and
  264.      use both an enumerated type and an INTEGER type.
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.                                                         Page 29-5