diff --git "a/Ada/AdaDistilled07-27-2003.pdf.txt" "b/Ada/AdaDistilled07-27-2003.pdf.txt" new file mode 100644--- /dev/null +++ "b/Ada/AdaDistilled07-27-2003.pdf.txt" @@ -0,0 +1,12637 @@ +Ada Distilled + +by Richard Riehle + +An Introduction to Ada Programming +for +Experienced Computer Programmers + +by +Richard Riehle + +AdaWorks Software Engineering +http://www.adaworks.com +Copyright 2002, AdaWorks Software Engineering +Public Edition. Permission to copy if AdaWorks is acknowledged in copies +Version: July 2003 + +Page 1 of 113 + + Ada Distilled + +by Richard Riehle + +Acknowledgments + + There are always a lot of people involved in the creation of any book, even one as +small and modest as this one. Those who have contributed to the best features of this +book include my students at Naval Postgraduate School, Mr. Michael Berenato of +Computer Sciences Corporation, Mr. Ed Colbert of Absolute Software, and many +students from Lockheed-Martin Corporation, Computer Sciences Corporation, British +Aerospace, various branches of the uniformed services, to name a few. I also owe a +special thanks to Dr. Ben Brosgol, Dr. Robert Dewar, Mr. Mark Gerhardt, and Dr. +Mantak Shing for what I have learned from them. Also thanks to the contributors to +comp.lang.ada Usenet forum and the Team_Ada Listserve. Phil Thornley deserves +extra credit for his detailed reading of the manuscript and many corrections. + +Special thanks goes to Ed Colbert for his careful study of some of my program +examples. He is one of those people who can spot a program error at fifty paces. +Using this unique skill, Ed brought many errors, some big and some small, to my +attention. Also thanks to more recent input from Phil Thornley and Adrian Hoe. + +Any other errors are strictly mine. Any mistakes in wording, spelling, or facts are +mine and mine alone. + +I hope this book will be valuable to the intended audience. It is moderate in its intent: +help the beginning Ada programmer get a good start with some useful examples of +working code. More advanced books are listed in the bibliography. The serious +student should also have one of those books at hand when starting in on a real project. + + Richard Riehle + +Intended Audience for this Book + +This book is aimed at experienced programmers who want to learn Ada at the programming level. +It is not a "...for dummies" book, nor is it intended as a program design book. Instead, we +highlight some key features of the Ada language, with coded examples, that are essential for +getting started as an Ada programmer. + +Ada is a rich and flexibile language used for designing large-scale software systems. This book +emphasizes syntax, control structures, subprogram rules, and how-to coding issues rather than +design issues. There are other really fine books available that deal with design. Also, this is not a +comprehensive treatment of the Ada language. The bibliography lists some books that can help +you once you have finished the material in this book. + +Think of this a quick-start book, one that enables you, the experienced programmer, to get into the +Ada language quickly and easily. The examples use text-oriented programs for simplicity of +learning. If you prefer to do the exercises for a GUI environment, check out the sites listed for +compilers and tools so you can download bindings for programming in a variety of windowing +environments, including Wintel, Linux, and Unix. Of particular interest to those wanting to +explore Window style programming are GtkAda, CLAW, GWindows, and JEWL. The last of +these, JEWL, is especially useful if you have no prior experience writing Windows programs. + + Happy Coding, + Richard Riehle + +Page 2 of 113 + + Ada Distilled + +by Richard Riehle + +Table of Contents + +TABLE OF CONTENTS ............................................................................................................................. 3 + +1. WHAT IS ADA DISTILLED? ................................................................................................................ 4 + +2. SUMMARY OF LANGUAGE ................................................................................................................ 5 + +3. TYPES AND THE TYPE MODEL ...................................................................................................... 16 + +4. CONTROL STRUCTURES FOR ALGORITHMS............................................................................ 26 + +5. ACCESS TYPES (POINTERS) ............................................................................................................ 33 + +6. SUBPROGRAMS................................................................................................................................... 40 + +7. PACKAGE DESIGN.............................................................................................................................. 47 + +8. CHILD LIBRARY UNITS .................................................................................................................... 52 + +9. OBJECT-ORIENTED PROGRAMMING WITH PACKAGES....................................................... 57 + +10. USING STANDARD LIBRARIES AND ANNEXES ....................................................................... 64 + +11. EXCEPTION MANAGEMENT ........................................................................................................ 71 + +12. GENERIC COMPONENTS................................................................................................................ 74 + +13. NEW NAMES FROM OLD ONES .................................................................................................... 82 + +14. CONCURRENCY WITH TASKING................................................................................................. 87 + +A. ANNEXES, APPENDICES AND STANDARD LIBRARIES.......................................................... 92 + +ANNEX L PRAGMAS - LANGUAGE-DEFINED COMPILER DIRECTIVES............................... 104 + +WINDOWS 95/98/NT/XP/ME/2000 PROGRAMMING ...................................................................... 105 + +C. BIBLIOGRAPHY............................................................................................................................... 107 + +Author's Observation and Opinion + +The Ada language is designed to maximize the possibilities for error detection early in the development process. This reduces the overall +cost of software development since it is known that errors corrected early, will cost less than those detected late. No other language, not +C++, not Java, not Eiffel, provides the level of support for safety-critical software found in Ada. Developers can and do make mistakes, +even using Ada, but the probability of such mistakes is substantially less when using Ada than it is when using other languages. +Therefore, when failure is not acceptable, during development or during software execution, the wise developer will select Ada. +Although Ada is not as popular as some of the more well-known languages, it has substantial benefits over most of them when software +dependability is a primary objective. In addition, contemporary Ada is as appropriate for developing GUI-based applications, information +systems applications, and computationally intensive systems as any existing language. It is truly a general-purpose language, well-suited +to any kind of software problem you might encounter. + +Page 3 of 113 + + Ada Distilled + +by Richard Riehle + +1. What is Ada Distilled? + +This book is for experienced programmers new to Ada. Heavily commented example programs help +experienced programmer experiment with Ada. This is not a comprehensive book on the entire Ada +language. In particular, we say very little about Ada.Finalization, Storage Pool Management, +Representation Specifications, Concurrency, and other more advanced topics. Other books, listed in the +bibliography, cover advanced topics. This book is an entry point to your study of Ada. + +The text is organized around example programs with line-by-line comments. Ada comments are the +double-hyphen and continue to the end of a line. Comments might be explanatory notes and/or +corresponding section of the Ada Language Reference Manual (ALRM) in the format of ALRM X.5.3/22. + +This will compile +and execute with +any Ada compiler. + +with Ada.Text_IO; +procedure Hello is +begin + +Ada.Text_IO.Put_Line(“Hello Ada”); + +end Hello; + +-- 1 10.1.2, A.10 Context clause +-- 2 6.3 Specification with "is" +-- 3 6.3 Start algorithmic code +-- 4 A.10.6 Executable source code +-- 5 6.3 End of procedure scope + +A hello program in Ada. + +where each line is numbered. The 10.1.2 and 6.3, etc. refer to ALRM Chapters 6.3, 10.1.2. A.10.6 refers to +Annex A.10.6. There is occasional commentary by source code line number. The line numbers are not +part of Ada, but provided on our examples for ease of commentary. Boxed notes are also included for +some examples. The preceding example shows one of these as: + +1.1 Ada Compilers and Tools + +Ada 95 compilers support a wide range of platforms. A free, open source, compiler, GNAT, based on +GNU technology, can be downloaded from the Web. Commercial Ada compilers and tools are available +from: Ada Core Technologies (GNAT), DDC-I, Rational (recently acquired by IBM), RR Software, Irvine +Compiler Corporation, Green Hills, SofCheck, Aonix, and OC Systems. Free editors, including AdaGide, +are useful for developing small Ada programs. More information on tools, including GUI development +tools, can be accessed using one of the URL's mentioned in the introduction and in the bibliography. + +Development tools exist for many operating systems. These include CLAW for Microsoft Operating +Systems, and GtkAda for other GUI environments including Microsoft operating systems, Linux, BSD, +OS/2, Java Virtual Machine, and every variety of Unix. The AdaGide editor is available for MS Windows. + +1.2 Ada Education + +The bibliography of this book lists some of the books and educational resources available to the student of +Ada. Some colleges and universities offer Ada courses. In addition, companies such as AdaWorks +Software Engineering provide classes for anyone interested in Ada software development. + +1.3 Ada Software Practice + +If the software must absolutely work without error, where a software accident could +kill or maim, Ada is the correct choice. + +There is a lot of misinformation about Ada. One misconception is that it is a large, bloated language +designed by committee. This is not true. Ada is designed around a few simple principles that provide the +architecture for the language syntax and semantics. Once you understand these principles, Ada will be as +easy as many other languages. We highlight some of those design principles in this book. One important +principle is that the Ada compiler never assumes anything. Everything is explicit. Nothing in Ada is +implicit. This helps the compiler help you write more dependable code. Oh, and you'll rarely need the +debugger once you are experienced with Ada. Also, your Ada programs will usually compile to nearly +any contemporary platform and execute on that platform without change. + +Page 4 of 113 + + Ada Distilled + +by Richard Riehle + +2. Summary of Language + +2.1 Goals and Philosophy + +Ada is not an acronym. It is the name of the daughter of the English Poet, Lord Byron. She +is credited with being the "first computer programmer" because of the prescience +demonstrated in her early writings that described Charles Babbage's Analytical Engine. She +was honored for this contribution by having a language named after her. + +Every programming language is intended to satisfy some purpose, some set of goals. Sometimes the goals +are stated in terms of a programming paradigm. For example, a goal might be to design an object-oriented +programming language. Another goal might call for a language that conforms to some existing +programming model with extensions to satisfy some new notions of programming techniques. Ada's goals +correspond to the final product of the software process. Ada is not designed to satisfy an academic notion +of how programs should be designed and written. Ada's Goals are: + +High reliability and dependability for safety-critical environments, + +• +• Maintainable over a long span by someone who has never seen the code before, +• +• + +Emphasis on program readability instead of program writeability, +Capability for efficient software development using reusable components + +Yes, you can hack solutions +in Ada if you want. +However, it is intended as a +language for disciplined +design and construction of +high reliability software. + +In summary, Ada is designed to maximize the error checking a compiler can do early in the development +process. Each syntactic construct is intended to help the compiler meet this goal. This means some Ada +syntax may initially seem extraneous but has an important role in alerting the compiler to potential errors in +your code. The default for every Ada construct is safe. Ada allows you to relax that default when +necessary. Contrast Ada's default of safe with most of the C family of languages where the default is +usually, unsafe. The safety default is one of Ada's most important contributions to engineering software. + +Another important idea is expressiveness over expressibility. Nearly any idea can be expressed in any +programming language. That is not good enough. Ada puts emphasis on expressiveness, not just +expressibility. In Ada, we map the solution to the problem rather than the problem to the solution. + +2.2 Elementary Syntax + +The syntax of Ada is actually easy to learn and use. It is only when you get further in your study that you +will discover its full power. Just as there is "no royal road to mathematics," there is no royal road to +software engineering. Ada can help, but much of programming still requires diligent study and practice. + +2.2.1 Identifiers +Identifiers in Ada are not case sensitive. The identifiers Niacin, NIACIN, NiAcIn will be interpreted by +the compiler as the same. Underbars are common in Ada source code identifiers; e.g. Down_The_Hatch. +There is a worldwide shortage of curly braces. Consequently, Ada does not use { and }. Also, Ada does +not use square braces such as [ and ] . Ada has sixty-nine reserved words. Reserved words will usually +be shown in bold-face type in this book. (See Appendix A for a complete list of reserved words). + +2.2.2 Statements, Scope Resolution, Visibility + +Ada's unique idea of visibility often causes difficulties for new Ada programmers. +Once you understand visibility, nearly everything else about Ada will be clear to you. + +An Ada statement is terminated with a semicolon. The entire scope of a statement is contained within the +start of that statement and the corresponding semicolon. Compound statements are permitted. A +compound statement has an explicit end of scope clause. A statement may be a subprogram call, a simple +expression, or an assignment statement. Here are some sample statements: + +X := C * (A + B); +Move (X , Y); +if A = B then + +J := Ada.Numerics.Pi * Diameter; + +else + +J := Ada.Numerics.Pi * Radius ** 2; + +end if; +if (A and B) or ((X and T) and (P or Q)) then + +Compute(A); + +else + +Compute(P); + +end if; + +-- 1 Simple assignment statement +-- 2 A procedure call statement +-- 3 Start a compound if statement +-- 4 Compute the circumference of a circle +-- 5 Part of compound if statement +-- 6 Compute area of a circle +-- 7 End of compound statement scope +-- 8 Parentheses required in mixed and/or construct +-- 9 Call Compute subprogram +-- 10 Part of compound statement +-- 11 Subprogram call statement +-- 12 End of compound statement scope + +No curly braces in Ada + +Page 5 of 113 + + Ada Distilled + +by Richard Riehle + +Note on Line 8 that an Ada conditional statement cannot mix and and or unless the expression includes +parentheses. This eliminates problems associated with such expressions. It also eliminates arguments +about precedence of mixed expressions, and errors due to incorrect assumptions about precedence. + +2.2.3 Methods (Operators and Operations) + +Methods in Ada are subprograms (procedures/functions) and include both operators and operations. +Operators include the symbols: =, /=, <, >, <=, >=, &, +, -, /, *. Other operators are the reserved words, +and, or, xor, not, abs, rem, mod. One issue that annoys newcomers to Ada is the strictness of the +visibility rules associated with operators. We will discuss the visibility rules and techniques for using them +sensibly in Section 2.4 and elsewhere in this book. For detailed operator rules, see ALRM 4.5. + +All other methods are called operations. One operation, assignment uses the compound symbol: := . The +Ada programmer may not directly overload the assignment operation. Assignment is predefined for most +Ada types. It is prohibited for limited types. We describe limited types later in this book. + +The Ada programmer may declare type-specific methods. An experienced Ada designer uses the package +specification for declaring such methods, and for overriding/overloading existing methods. + +The membership test, not considered an operation by the language, has important method-like properties. +Membership test uses the reserved word in. Combine the word in with the word not (not in) for a +negative membership test. Membership testing is permitted for every Ada type, including limited types. + +See 4.2.2 + +2.3 Library and Compilation Units + +2.3.1 Library Units + +A single library unit may be composed of more than one compilation unit. This is +called separate compilation. Ada ensures that separately compiled units preserve their +continuity in relationship to related units. That is, date and time checking, library name +resolution, and date and time checking of compiled units ensures every unit is always in +phase with every other related complation and library unit + +An Ada program is composed of library units. A library unit is a unit that can be referred to using a with +clause. The technical name for the with clause is context clause. A context clause is a little like a #include +compiler directive in other languages, but with important differences. A library unit must successfully +compile before the compiler will recognize it in a context clause. Each compiled unit is placed in a +[sometimes virtual] library. Unlike a #include , the context clause does not make elements of a library unit +visible. Instead, a context clause simply puts the library unit in scope, making it potentially visible. + +A library unit may be a package or a subprogram. Subprograms are either functions or procedures. + +1) package +2) procedure +3) +4) child unit + +function + +A collection of resources with something in common, usually a data type. +A simple executable series of declarations and associated algorithmic code. +An executable entity which always returns a data type result. +A package, procedure, or function that is a child of a package. + +Note: Library +units may be +generic. See +chapter 12. + +An Ada library unit consists of a specification part and implementation part. The implementation is +sometimes called a body. For a subprogram the specification part could be coded as, + +procedure Open (F : in out File); +function Is_Open (F : File) return Boolean; + +-- Procedure specification; requires body. +-- Function specification; requires body + +C/C++ programmer note: An Ada +subprogram specification is analogous to, +but not identical to, a function prototype. + +A package is a collection of services (public and private), usually related through some data type. Most +Ada library units will be packages. A package specification includes type declarations, subprograms +(procedures and functions), and exceptions. Also, a package usually consists of a specification part +(public and private) and an implementation part. The implementation part of a package is called the +package body. A package specification almost always (not always) requires a package body. + +Page 6 of 113 + + Ada Distilled + +by Richard Riehle + +Here is a typical specification for a package library unit. The specification has two parts, public and +private. A client of a package can access only specification's public part, not its private part. + +package Machinery_1_3 is +type Machine is private; +procedure Turn_On (M : in out Machine); +procedure Turn_Off (M : in out Machine); +function Is_On (M : in Machine) return Boolean; + +private + +type Machine is record + +Turned_On : Boolean := False; + +end record; + +end Machinery_1_3; + +-- 1 Package specification; requires body +-- 2 Specifies the visible part of the data type; +-- 3 procedure specification +-- 4 procedure specification +-- 5 function specification +-- 6 private part hidden from a client of contract +-- 7 full definition of the publicly declared type +-- 8 component of the type; OOP attribute +-- 9 scope terminator for the component +-- 10 scope terminator for the specification + +Public part + +Private part + +Here is a possible package body, which implements the specification. It is separately compiled. + +package body Machinery_1_3 is + +procedure Turn_On (M : in out Machine) is +begin + +M.Turned_ON := True; + +end Turn_On; + +-- 1 Package body; implements specification declarations +-- 2 Repeat procedure specification; compiler checks this +-- 3 Starts algorithmic section of procedure +-- 4 Simple assignment statement of boolean value +-- 5 Procedure scope terminator is required + +procedure Turn_Off (M : in out Machine) is +begin + +M.Turned_On := False; + +end Turn_Off; + +-- 6 Must match profile in specification +-- 7 Algorithms between begin and end +-- 8 M.Turned called dot notation +-- 9 Name is optional but end is required + +Body + +function Is_On (M : in Machine) return Boolean is +begin + +return M.Turned_On; + +end Is_On; + +end Machinery_1_3; + +-- 10 In mode is like a constant; it may +-- 11 not be on left side of assignment +-- 12 return statement required of every function +-- 13 Scope terminator for function +-- 14 End of all declarations for this package + +Most often, the specification and the body are compiled separately. A specification must compile without +errors before its body can be compiled. The Ada compiler will issue a fatal error if the body is out of phase +with the specification. A programmer creating a client of the package, can only see the public part of the +specification. The specification is a contract with a client of the package. The contract must be sufficient +for the client to engage the promised services. Every declaration in the specification must conform, +exactly, to the code in the body. The Ada compiler detects non-conformance to ensure consistency over the +lifetime of the whole library unit. A change to a specification requires recompilation of the body. A +change to the body does not require recompilation of the specification. Consider this client subprogram: + +with Machinery_1_3; +procedure Test_Machinery_1_3 is + +Widget : Machinery_1_3.Machine; + +begin + +Machinery_1_3.Turn_On (M => Widget); +Machinery_1_3.Turn_Off (M => Widget); + +end Test_Machinery_1_3; + +-- 1 Context clause. Puts Machinery_1_3 in scope +-- 2 Specifxication for the procedure +-- 3 Local object of type Machine +-- 4 Starts the algorithmic section of this procedure +-- 5 Call the Turn_On using dot notation and named association +-- 6 Call the Turn_On using dot notation and named association +-- 7 Scope of subprogram terminates with the end clause + +A client of the package, such as Test_Machinery_1_3, never has visibility to the private part or the body of +the package. Its only v iew is to the public part. However, the entire package is in scope, including the +body. The body is completely hidden from all views from outside the package even though it in scope. + +Note the use of named association in the procedure call. The syntax includes the compound symbol => where the +formal parameter is on the left and the actual parameter is on the right. This Ada feature helps to make code more +readable and eliminates a lot of errors in the final source code. Named association is optional, but it is very powerful +and used often in production code. + +Page 7 of 113 + + Ada Distilled + +by Richard Riehle + +2.3.2 Compilation Units + +As noted earlier, library units can be composed of smaller units called compilation units. The library unit +is the full entity referenced in a context clause. An Ada package, as a library unit, is usually compiled as +two compilation units: package specification and package body. Do not think of a package specification +as a C++ .h file. The Ada rules are more rigorous than those for C++ .h files. The package body does not +need to with its own specification. A package body can be further subdivided into even smaller +compilation units called subunits. Subunits, used wisely, benefit the maintenance cycle of existing Ada +programs. + +The specification of Machinery_1_3 in the previous section can be compiled by itself. Later, the package +body can be compiled. The procedure Test_Machinery_1_3 may be compiled before the package body of +Machinery_1_3. The test program cannot be linked until all separately compiled units are compiled. + +The package body for Machinery_1_3 could have been coded for separate compilation as, + + package body Machinery_1_3 is + +procedure Turn_On (M : in out Machine) is separate; +procedure Turn_Off (M : in out Machine) is separate; +function Is_On (M : in Machine) +return Boolean is separate; + +end Machinery_1_3; + +-- 1 +-- 2 +-- 3 +-- 4 +-- 5 +-- 6 + +A subprogram declared is separate places a subunit in the library. The +subunit may have its own context clauses, its own local variables, and its +own algorithmic code. Also, each subunit may be compiled independently +once its parent has been successfully compiled. This means easier, faster +maintenance and better unit testing. During development, each subunit can +be assigned to a different programmer + +Compilation units in most Ada programs will be a package specification and package body. Sometimes, as +in lines 2, 3, 5, you may see a subprogram specification compiled with the word separate instead of an ... +is ... end implementation. This implies separate compilation of the body for that subprogram. + +Ada does not force separate compilation, but some Ada compilers do. An implementation is free to impose +this requirement. The standards for most Ada development shops also require separate compilation. + +An Ada package may have child library units. A package, such as package Machinery, may be the root of +a tree of child library units. This provides a unique opportunity for separate compilation and extension. + +Here is an example of parent-child library units. + +See Chapter 8 for more on child library units. + +package Messenger is + +type Message is private; +function Create (S : String) return Message; +procedure Send (M in Message); +procedure Receive (M : out Message); +function Size (M : in Message) return Natural; + +private + +type Message is record + +Text : String (1..120) := (others => ' '); +Length : Natural := 0; + +end record; +end Messenger; + +with Ada.Calendar; +package Messenger.Dated is + +type Dated_Message is private; +function Create (M : in Message) + + return Dated_Message; + +private + +type Dated_Message is record + +Text : Message; +Date : Ada.Calendar.Time; + +end record; + +end Messenger.Dated; + +-- 1 Package specification; requires body +-- 2 Visible part of the data type; name only +-- 3 function specification +-- 4 procedure specification +-- 5 procedure specification +-- 6 function specification +-- 7 private part hidden from a client of contract +-- 8 full definition of the publicly declared type +-- 9 string component of the type; OOP attribute +-- 10 how many of the 120 values are in use +-- 11 scope terminator for the component +-- 12 scope terminator for the specification + +-- 1 Package specification; requires body +-- 2 Visible part of the data type; name only +-- 3 function specification +-- 4 function always specifies a return type +-- 5 private part hidden from a client of contract +-- 6 full definition of the publicly declared type +-- 7 string component of the type; OOP attribute +-- 8 how many of the 120 values are in use +-- 9 scope terminator for the component +-- 10 scope terminator for the specification + +Parent +Library +Unit + +Child +Library +Unit + +Note how a child library unit is formed using the parent name followed by a dot and the child + +Page 8 of 113 + + Ada Distilled + +by Richard Riehle + +At first, a child library unit might be mistaken as a form of inheritance. The experienced OOP practitioner +will see that it is not inheritance; the is_a is relationship is absent. Rather, it allows one to extend the +original package and add more features. The declarative region for Messenger has been extended to +include the declarations of Messenger.Dated. Any client of Messenger.Dated has direct visibility to the +public declarations of Messenger. The private part of Messenger.Dated and the body of Messenger.Dated +has direct visibility to the private and public parts of Messenger. + +Dated_Message is implemented is a has_a relationship. This means that Dated_Message contains a value +of type Message. Dated_Message cannot be converted to an object of type Message. They are two +distinct types, even though one is nested within another. + +Important + +2.4 Scope and Visibility + +Some programmers find the concept of visibility more difficult than any other part of Ada. +Once they really understand visibility, everything else in language makes sense. + +Failure to understand the difference between scope and visibility causes more problems for new Ada +programmers than any other single topic. It is an idea central to the design of all Ada software. There is an +entire ALRM chapter devoted to it, Chapter 8. A with clause puts a library unit into scope; but none of the +resources of that unit are directly visible to a client. This is different from a #include in the C family of +languages. Separating scope from visibility is an important software engineering concept. It is seldom +designed into other programming languages. This book has many coded examples that illustrate visibility +rules. Ada has several techniques for making in-scope elements directly visible. + +NOTE: ISO +Standard C++ +namespace adopts a +weakened form of +Ada's scope and +visibility model. + +2.4.1 Scope + +Every Ada statement has an enclosing scope. Sometimes the scope is easy to see in the source code. There +is an entry point (declare, subprogram identifier, composite type identifier, package identifier, etc.) and an +explicit point of termination. Explicit terminations are coded with an end statement. Anytime you see an +end clause, you know it is closing a scope. Scope can be nested. For example, a procedure may be +declared inside another procedure. The scope of context clause (with clause) is not as obvious. The +context clause puts the full resources of a library unit in scope, but makes none directly visible. + +A pure interpretation of the scope mechanism might better describe this in terms of a declarative region. However, since this book is +intended as an introduction to the practical aspects of the Ada, we limit our discussion to a more pragmatic view of the visibility +mechanism. For a more rigorous description, please consult the Ada LRM, Chapter 8. + +A with (context) clause implies a dependency on the library unit named in that clause. This dependency +can be at either the specification level of the withing library unit or deferred to the body of that unit. + +2.4.2 Visibility + +Understanding visibility is the key to understanding Ada. + +An entity may be in scope but not directly visibile. This concept is better developed in Ada than in most +programming languages. Throughout Ada Distilled you will see visibility examples such as: + +• use clauses +• use type clauses +• entity dot notation +• renaming , locally, of operations/operators + +makes all public resources of a package directly visible +makes public operators directly visible for designated type +entity in notation is directly visible; usually the best option +usually best option for making operators directly visible + +During development, an Ada compiler error message may advise you that some entity is not +visible at the point where it is declared or used. Most often a visibility problem will relate to +operators (2.7). One of the mechanisms from the above list can make that entity visible. It will be easier to +demonstrate visibility in the code examples than to trudge through a tedious jungle of prose. Watch for +uses of the visibility mechanisms above in our coded examples. + +The general rule for +the use clause is to +not use it. However, +for lots of programs +where dependability +and maintainability +are less important +(e.g., experimental +programs), one may +use the use clause +freely to simplify the +coding process. + +Page 9 of 113 + + Ada Distilled + +by Richard Riehle + +2.5 Declarations, Elaboration, Dependencies + +Most Ada software systems are composed of many independent components, most in the form of packages. +These packages are associated with each other through context clauses (i.e., with clause). + +specification + +body + +with A; +with B; +with C; +package Q is + ... +with T; +end Q; +package body Q is + ... +end Q; + +specification + +body + +with A; +with B; +with C; +package R is + ... +with E; +end R; +with F; +package body R is + ... +end R; + +specification + +body + +with R; +package T is + ... +end T; + +with A; +package body T is + ... +end T; + +Notice that dependencies between library units can be deferred to the package body. This unique feature +of Ada is based on the integral nature of library units and takes advantage of the separate compilation +capability. Ada gives us the best of both capabilities. We can minimize the design dependencies by +declaring context clauses for the package body instead for the package specification. This eliminates the +need to re-compile (or re-examine) the relationships each time we make a change somewhere in our design. + +An Ada program includes declarations and executable statements. A package specification is a set of +declarations. The package body may also contain localized declarations. The scope of the declarations +can be thought of as a declarative region. In the declarative region, declarations are in scope but not +necessarily visible. Declarations in a package body are in the declarative region, but are not visible to a +client or child library unit. + +2.5.1 Elaboration + +Elaboration brings declarations into existence, usually at run-time + +Declarations must be elaborated before the program can begin its algorithmic part. Elaboration is the set of +actions a program must complete before it can begin its algorithmic actions. It usually takes place without +action by the programmer. Ada does provide some pragmas (compiler directives) for control over the +timing and order of elaboration. Usually, elaboration occurs at execution time. A programmer may +specify compile-time elaboration through pragma Preelaborate or pragma Pure. If that compile-time +elaboration is possible, it may occur according to the semantics of each pragma. + +Library units named in a context (with) clause must be elaborated before they are actually available to a +client. When there are multiple context clauses, each must be elaborated. In some circumstances, +resources of one library unit must finish elaborating before another library unit complete its elaboration. + +2.5.2 Ada Comb + +An Ada program unit may sometimes be viewed in terms of the "Ada Comb," an idea first introduced to +me years ago by Mr. Mark Gerhardt. The Ada Comb demonstrates how declarations and algorithms are +related within an implementation; i.e, subprogram body, task body, declare block, package body, etc. + +kind-of-unit unit-name +local declarations + +begin + +handled-sequence-of-statements + +exception + +sequence-of-statements + +end unit-name; + +-- 1 procedure, function, package body, declare block, etc. +-- 2 Must be elaborated prior to begin statement +-- 3 Elaboration is done. Now start executing statements +-- 4 Handled because of the exception handler entry +-- 5 Optional. Not every comb needs this. +-- 6 This is the area for exception handler code +-- 7 Every comb requires a scope terminator + +Page 10 of 113 + + Ada Distilled + +by Richard Riehle + +Ada is a block-structured language. Local declarations may contain: other subprogram declarations +(including their body), instances of types, instances of generic units, tasks or task types, protected objects +or protected types, use clauses, compiler directives (pragmas), local type declarations, constants, and +anything else that falls into the category of the items just listed. Even though the list of legal entities in a +declaration is long, only a few elements are actually used in practice. Be aware of the Ada Comb when +studying the subprograms and algorithmic structures in this book. + +The handled-sequence-of-statements includes statements that operate on declarations. This includes +assignment, comparisons, transfers of control, algorithmic code. More specifically, we see the three +fundamental control structures of the structure theorem (Jacopini and Böhm): sequence, iteration, selection. +You may also see a declare block, with local declarations, within the handled-sequence-of-statements. + +with Ada.Text_IO; +with Machinery; +procedure Ada_Comb_Example_1 is + +Data : Machinery.Machine; + +begin + +declare + +Data : Integer := 42; + +begin + +Data := Data + 1; + +exception + +when some-exception => + -- sequence of statements + + end; +end Ada_Comb_Example_1; + +-- 1 Is elaborated before being used +-- 2 Is elaborated before being used +-- 3 Name of enclosing unit +-- 4 Declarations local to enclosing unit +-- 5 +-- 6 Can declare local variables in this block +-- 7 The name, Data, hides the global declarations +-- 8 Integer Data now is visible; Outer Data is not +-- 9 Handled sequence of statements +-- 10 Start exception handler part of unit +-- 11 Name the exception after reserved word, when +-- 12 Any legal sequence of statements here +-- 13 End of scope of declare block +-- 14 End of enclosing scope + +The Ada comb may be found in most algorithmic units. This includes procedures, functions, package +bodies, task bodies, and declare blocks. These units may also include some kind of identifier (label). In +production code, it is helpful to include the label at the beginning of the comb as well as at the end of it. +Here is a variation on the previous example + +procedure Ada_Comb_Example_2 is + +Data : Float := 0.0; + +begin + Integer_Block: + +declare + +Data : Integer := 42; + +begin + +Data := Data + 1; + +exception + +when Constraint_Error => ... + + end Integer_Block; +Data := Data + 451.0; +end Ada_Comb_Example_2; + +-- 1 Name of procedure +-- 2 Floating point declaration in scope +-- 3 +-- 4 A label for the declare block +-- 5 Can declare local variables in this block +-- 6 The name, Data, hides the global declarations +-- 7 Integer Data now is visible; Float Data is not directly visible +-- 8 Simple incrementing statement +-- 9 Localized exception handling region +-- 10 Statements to handle the exception +-- 11 Named end of scope for declare block +-- 12 Float data is once more visible +-- 13 End of scope of procedure + +The second example has an exception handler localized in the declare block. Note the identifier (label) for +this declare block. A block label is any user-defined name followed by a colon. The block repeats the +identifier at the end of its scope. In the scope of the declare block, the floating point variable with the +same name as the item in the declare block is automatically made invisible. Because it is still in scope, it +could be made visible with dot notation (Ada_Comb_Example_2.Data ...). Try to avoid identical names +within the same scope. In large-scale systems with many library units, avoiding this is not always +possible. + +Page 11 of 113 + + + Ada Distilled + +by Richard Riehle + +2.6 Variables and Constants + +A variable is an entity that can change its value within your program. That is, you may assign new values +to it after it is declared. A constant, once declared with an assigned value, may not be changed during its +lifetime in your program. Variables and constants may be declared in a certain place in your program, +called the declarative part. Every variable and constant must be associated with some type. The basic +syntax for a variable declaration is, + +name_of_variable : name_of_type; +name_of_variable : name_of_type(constraint) ; + +-- for a scalar or constrained composite type +-- for an unconstrained composite type + +Declarations for predefined types (see package Standard in the appendices of this book) + +Value +Degrees +Sentinel +Result +Text + +: Integer; +: Float; +: Character; +: Boolean; +: String(1..120); + +-- see Annex A.1, package Standard +-- see Annex A.1, package Standard +-- see Annex A.1, package Standard +-- see Annex A.1, package Standard +-- Must always constrain a string variable + +String is defined in package Standard as an unconstrained array + +We could also initialize a variable at the time it is declared, + +Strings in Ada have index +values of subtype positive +which means the lowest +possible value for a string +index is 1, not zero. + +Channel +Pi +ESC +Is_On +Text + +: Integer := 42; +: Float := Ada.Numerics.Pi; +: Character := Ada.Characters.Latin_1.ESC; -- from Annex A, ALRM +: Boolean := True; +: String(1..120) := (others => '*'); + +-- from Annex A.1, ALRM +-- Every element initialized to asterisk + +-- "...life, the universe, and everything." +-- from Annex A.5, ALRM + +2.7 Operations and Operators + +Ada distinguishes between operations and operators. Operators are usually the infix methods used for +arithmetic, comparison, and logical statements. Operators present a visibility problem for a new Ada +programmer. Watch for the discussion of operator visibility that follows in this section. + +2.7.1 Assignment Operation + +:= + +Somewhere among his published aphorisms and deprecations, Edsger Dijkstra observes that too few +programmers really understand the complexities of the assignment statement. I have not been able to +excavate the exact quote from those of his publications immediately at hand. It is true, however, that +assignment is increasingly complicated as new programming languages are invented. Ada is no exception, +and may actually have more complicated rules about assignment than some other languages. + +The Ada assignment operation, := , is a compound symbol composed of a colon symbol and equal symbol. +It is predefined for every Ada type except limited types. It is illegal, in Ada, to directly overload, +rename, or alias the assignment operation. In a statement such as, + + A := B + C * (F / 3); + +Reminder: the assignment operator is legal only on non-limited types. Also, both sides of the assignment +operator must conform to each other. Composite types must have the same size and constraints. + +the expression on the right side of the assignment operation is evaluated and the result of that evaluation is +placed in the location designated by the variable on the left side. All the variables on both sides must be +of the same type. In an expression, + + X := Y; + +Note: Although Ada does not allow direct overloading of the assignment operator, it is sometimes useful to do that kind of overloading, and +Ada has a facility for designing in this feature safely but indirectly, by deriving from a controlled type. + +Page 12 of 113 + + Ada Distilled + +by Richard Riehle + +X and Y must both be of the same type. If values in an assignment statement are not of the same type, the +programmer may, under strictly defined rules, convert Y to a type corresponding to the type of X. + +type X_Type is ... +type Y_Type is ... +X := X_Type(Y); + +-- Ellipses are not part of the Ada language; used for simplification here + +-- When type conversion is legal between the types + +Type conversion is not legal between all types. If both types are numeric, the conversion is probably legal. +If one type is derived from another, it is legal. Otherwise, type conversion is probably not legal. + +Assignment may be more complicated if the source and target objects in the assigment statement are +composite types. It is especially complicated if those composite types include pointers (access values) that +reference some other object. In this case, access value components may create entertaining problems for +the programmer. For this reason, composite types constructed from pointers should be limited types. +For limited types, one would define a Deep Copy procedure. Ada makes it illegal to directly overload the +assignment operator. Study an example of a deep copy in the generic Queue_Manager later in this book. + +Sometimes two types are so completely different that assignment must be performed using a special +generic function, Ada.Unchecked_Conversion. Do not be too hasty to use this function. Often there is +another option. Note the following example: + +with Ada.Unchecked_Conversion; +procedure Unchecked_Example is + +type Vector is array (1 .. 4) of Integer; + + for Vector'Size use 4 * Integer'Size; + +type Data is record + + V1, V2, V3, V4 : Integer; + end record; + for Data'Size use 4 * Integer'Size; + +function Convert is new Unchecked_Conversion + (Source => Vector, Target => Data); + +The_Vector : Vector := (2, 4, 6, 8); + + The_Data : Data := (1, 3, 5, 7); +begin + +The_Data := Convert(The_Vector); + +end Unchecked_Example; + +-- 1 Chapter 13 or ALRM +-- 2 Generally speaking, don't do this +-- 3 Array with four components +-- 4 Define number of bits for the array +-- 5 +-- 6 A record with four components +-- 7 +-- 8 Same number of bits as the array +-- 9 +-- 10 Convert a Vector to a Data +-- 11 Intilialize a Vector with values +-- 12 Intilialize a Record with values +-- 13 +-- 14 Assignment via unchecked conversion +-- 15 + +Even though Line 14 probably works just fine in all cases, many Ada practitioners will prefer to do the +assignments one at a time from the components of Vector to the components of Data. There will be more +code, but selected component assignment is guaranteed to work under all circumstances. Unchecked +conversion may be less certain unless you are careful about what you are doing. + +2.7.2 Other Operations + +There are several reserved words that behave like operations. Most of these such as abort, delay, +accept, select, and terminate are related to tasking. Others include raise (for exceptions), goto, +and null. Some Ada practitioners might not agree with the notion that these are operations, however, in +any other language they would be so considered. + +Other operations, for non-limited types, are described in Chapter Four of the Ada Language Reference +Manual. Again, these might not be thought of as operations, but they do have functionality that leads us to +classify them as operations. These include array slicing, type conversion, type qualification, dynamic +allocation of access objects, and attribute modification (Annex K of ALRM). + +Because Ada supports object technology, the designer is allowed to create, overload, and override +operations (except assignment) and operators. Subprogram (procedure and function) specifications may + +Page 13 of 113 + + Ada Distilled + +by Richard Riehle + +be declared in the public part of a package specification. They are implemented in the body of a package. +For example, in a stack package, the operations might be Push, Pop, Is_Full, Is_Empty. For abstract data +types, the operations are described as subprograms on the type. + +2.7.3 Operators + +Understanding visibility is the key to understanding Ada + +As mentioned in Section 3.2.2, Ada distinguishes between operators and operations. This distinction is +useful for visibility management. Operators may be overloaded. + +Operators can be thought of as functions. For example, for a type, T, function signatures might be: + + function "=" (Left, Right : T) return Boolean; -- signature for equality operator + function ">=" (Left, Right : T) return Boolean; -- signature for equality operator + function "+" (Left, Right : T) return T; -- signature for addition operator + +This signature style applies to all operators. The name of the operator is named in double quotes as if it +were a string. You may overload operators for your own types. In Ada, the return type is part of the +signature. There is a special visibility clause that makes all the operators for a named type fully visible: + + use type typename; -- typename is the name of the type in scope. It might need to be dot qualified + +Some designers prefer to make selected operators visible using the renames clause instead of the the use +type clause. For example, if type T is defined in package P, + +function "+" (Left, Right : P.T) return P.T renames P."+"; -- makes "+" directly visible + +The above function renames the addition operator for a specific type. It uses dot notation to reference the +package in which the type is defined. You can code this in the declarations of a unit that has a context +clause for (for example) P and a type P.T. This makes the plus operator directly visible in the immediately +enclosing scope. Many Ada practitioners feel this is a better engineering solution to controlling visibility +than any of the other options. It does have the effect of ensuring that no accidental coding of some other +operator is possible since only this one is directly visible. + +2.8 Elementary Sequential Programs + +There is a more in-depth discussion of this topic in Chapter 6. + +Ada supports two kinds of subprograms: procedures and functions. A subprogram may be a standalone +library unit. Often it a subprogram is declared in some other unit such as a package specification. The +implementation part of the subprogram is called the "body." The body for Open might be coded as: + +procedure Open(F : in out File) is + -- optional local declarations +begin + -- some sequence of statements +end Open; -- Most standards require repeating the identifier here -- 5 End required; Identifier optional but usual + +-- 1 Note the reserved word, is +-- 2 Between is and begin, local declarations +-- 3 Subprogram body requires a begin +-- 4 Some statements or reserved word null; + +Sometimes we code the subprogram specification and body together, as just shown. There are many cases +of this in the example subprograms in this book. The optional local declarations on Line 2 are local to the +subprogram. That is, they are never visible to another unit. This is one more level of visibility. When +you fully understand the visibility rules, you will understand most of Ada. + +Page 14 of 113 + + Ada Distilled + +by Richard Riehle + +2.8.1 Subprogram Parameters + +Subprograms may have formal parameters. Formal parameters must have a name, a type, and a mode. A +mode tells the compiler how a parameter will be used in a subprogram. The parameter mode may be in, +out, in out, or access. The following table simplifies the concept of parameter mode: + +Mode Function Procedure Assigment Operator Position +in +out +in out +access + +Only right side of := (a constant in subprogram) +Right or Left side of := (but has no initial value) +Right or Left side of := (has initial value) +Only right side of := (but might assign to component) + +Yes +Yes +Yes +Yes + +Yes +No +No +Yes + +The table is an over-simplification. It will work well for you as a programmer. Just understand that out +mode parameters are not called with an initial value, and access mode parameters (See Chapter 5) are +pointing to some other data. The data being accessed may be modified even though the access value itself +may not. Examples of parameters and their modes within a subprogram, + +2.8.2 Subprogram Specifications with Parameters + +procedure Clear (The_List : in out List); +function Is_Empty (The_List : in List) return Boolean; +function Is_Full (The_List : List) return Boolean; +procedure Get (The_List : in List; Data : out Item); +procedure Set_Col (To : in Positive_Count := 1); +procedure Update (The_List : in out List; Data : in Item); +function Item_Count (The_List : access List) return Natural; +procedure Item_Count (The_List : access List; + Count : out Count); +function M_Data (Azimuth, Elevation, Time : Float) return Float; + +-- The_List can be on either side of := +-- The_List can be on right side of := +-- default in mode +-- two modes; two parameters +-- default value for in mode +-- two modes; two parameters +-- The_List can be on right side of := +-- The_List can be on allowed on right of := +-- unitialized; left or right of := +-- Three parameters, same type + +A call to a formal parameter with an actual parameter should usually include named association. +Consider function M_Data, above. Which is more readable and more likely to be accurate? + +Named association +where actual +parameter is +associated with +formal formal + + R := M_Data (42.8, 16.2, 32.8); + +R := M_Data (Elevation => 16.2, Time => 32.8, Azimuth => 42.8); + +The compound symbol means associate the formal +parameter with the named actual parameter. + +Consider a problem that often occurs in languages such as C or C++ with three parameters of the same +type: + + int mdata (int x, int y, int z) { ... } + +What happens if the wrong parameters of the right type are passed? + +In C, there is no easy way to explicitly ensure correct actual values are being sent to the right formal +arguments. Consequently, it is easy to accidentally call a function with the wrong data, even if that data is +of the correct type. Some more recent languages have adopted this feature from Ada. + +Ada programmers, using named association, prevent this kind of accident because errors are easily detected +by the compiler. This can save countless hours of debugging time. Later, when someone needs to +maintain a program using named association there is less difficulty determining what parameters are being +used and when. This is especially useful for parameter lists where some of the formal parameters have +default values. + +Named association enables explicit interfaces. This is an important engineering principle for software. + +Page 15 of 113 + + Ada Distilled + +by Richard Riehle + +3. Types and the Type Model + +Ada has no structural equivalence as found in C, C++, and Modula-3. Strict name equivalence +model. Ada has no automatic promotion of types from one level to another. We enjoy better +type safety under these rules. Even Java falls short of Ada when considering type safety. This is +one reason Ada is the right choice for safety-critical and human-rated software applications. + +3.1 Rigorous Type Rules +Type safety is the language feature for which Ada is best known. It is actually only one of the many strong +points in Ada. The following discussion will clarify how it works. A type, in Ada, consists of four parts, + +1. A name for the type +2. A set of operations for the type +3. A set of values for the type +4. A set of rules governing type relationships; e.g., a wall between objects of differing types + +The last feature, the wall, is the default of the Ada typing model. Ada does provide capabilities for getting +around or over the wall, but the wall is always there. There are two general categories of type, elementary +and composite. A composite type is a record or an array. Everything else, for our purposes in this book, is +an elementary type. (Note: there are minor exceptions to this definition when you get into more advanced +Ada). Some types are predefined in a package Standard (see this Appendix A of this book). From the +object-oriented viewpoint, a type has state, operations to modify state and operations to query state. + +3.2 Type Safety + +A better way to view strong typing is to think in terms of type safety. Every construct in Ada is type safe. +For Ada, type safe is the default. For most languages, type safe is not the default. In still other languages, +type safety is an illusion because of structural equivalence or implicit type promotion. Ada does not +support either of those concepts because they are not type-safe. An Ada designer declares data types, +usually in a package specification. The declared types include a constrained set of values and operations +appropriate to the problem being solved. This ensures a rigorous contract between the client of a type and +the promise made by the package in which the type is defined. + +3.3 Declaring and Defining Types + +3.3.1 Categories of types + +Ada types can be viewed in two broad categories: limited, and non-limited. A type with a limited view +cannot be used with the := expression, ever. All other types can be used with := as long as that +assignment is between compatible (or converted view of) types. Ada defines certain types as always +limited. These include task types, protected types, and record types with access discriminants. + +Types in Ada may be considered in terms of their view. A type may be defined with a public view which +can be seen by a client of the type, and a non-public view that is seen by the implementation of the type. +We sometimes speak of the partial view of the type. A partial view is a public view with a corresponding +non-public view. Partial views are usually defined as private or limited private. Also, the public view of a +type may be limited where the implementation view of that same type may be non-limited. Ada does not +define a protected view directly analogous to C++ or Java. However, some of the essential properties of +that view are available as necessary. + +Another important category is private type versus non-private type. A limited type may also be private. +A type with a private view may also have a view that is not private. Any Ada data type may have a view +that is private with a corresponding view that is not private. The predefined operations for a non-limited +private type include: := operation, = operator, /= operator. Any other operations for a private type must +be declared explicitly by the package specification in which the type is publicly declared. + +Page 16 of 113 + + Ada Distilled + +by Richard Riehle + +3.3.2 A Package of Non-private Type Definitions + +In addition to predefined types declare in package Standard, the designer may also define types. These +may be constrained or unconstrained, limited or non limited. Here are some sample type declarations. + +package Own_Types is + +type Color is (Red, Orange, Yellow, Green, Blue, Indigo, Violet); + +-- an ordered set of values; not a synonym for a set of integer values + +type Farenheit is digits 7 range -473.0..451.0; +type Money is delta 0.01 digits 12; +type Quarndex is range -3_000..10_000; +type Vector is array(1..100) of Farenheit; +type Color_Mix is array(Color) of Boolean; +type Inventory is record + Description : String(1..80) := (others => ' '); + Identifier : Positive; +end record; +type Inventory_Pointer is access all Inventory; +type QData is array(Positive range <>) of Quarndex; +type Account is tagged record + ID : String (1..20); + Amount : Money := 0.0; +end record; +type Account_Ref is access all Account'Class; + +end Own_Types; + +3.3.3 A Private type Package + +-- 1 An enumerated type; +-- 2 A single line comment +-- 3 Floating point type +-- 4 Financial data type for accounting +-- 5 Integer type; note underbar notation +-- 6 Constrained array type +-- 7 Constrained by Color set +-- 8 A constrained record type +-- 9 Initialized string type record component +-- 10 A positive type record component +-- 11 End of record scope required by Ada +-- 12 Declaring a pointer type in Ada +-- 13 Unconstrained array type +-- 14 See next example: 1.5.3.3 +-- 15 Uninitialized string type component +-- 16 See line 4 of this package +-- 17 Required by language +-- 19 Classwide pointer type for tagged type + +Public view of +specification + +Private view +of +specification + +package Own_Private_Types is + +type Inventory is limited private; +type Inventory_Pointer is access all Inventory; +procedure Create(Inv : in out Inventory); +-- More operations for type Inventory +type Account is tagged private; +type Account_Ref is access all Account'Class; +procedure Create(Inv : in out Inventory); +function Create (D : String; ID : Positive) return Account_Ref; +-- More operations for tagged type, Account + +private + +type Inventory is record + Description : String(1..80) := (others => ' '); + Identifier : Positive; +end record; + +type Account is tagged record + ID : String(1..12); + Amount : Float := 0.0; +end record; + +end Own_Private_Types; + +-- 1 +-- 2 Partial definition of limited private type +-- 3 Declaring a pointer type in Ada +-- 4 Create an empty instance of Inventory +-- 5 +-- 6 Partial definition of a tagged type +-- 7 Classwide pointer type for tagged type +-- 8 Creates an empty Inventory record +-- 9 returns access to new Inventory record +-- 10 +-- 11 Begin private part of package +-- 12 A constrained record type +-- 13 Initialized string type record component +-- 14 A positive type record component +-- 15 End of record scope required by Ada +-- 16 +-- 17 Extensible record tagged type +-- 18 Uninitialized string type component +-- 19 A float type record component +-- 20 Required by language +-- 21 + +Note the signature of the Create procedure on Line 4. Since the inventory type is limited private, we would often want the mode of +parameter list to be in out. However, it is legal to have mode of out only. + +3.4 Deriving and Extending Types + +A new type may be derived from an existing type. Using the definitions from the previous package, + +type Repair_Parts_Inventory is new Inventory; + +-- no extension of parent record is possible here +-- because it is not a tagged type + +where Repair_Parts inherits all the operations and data definitions included in its parent type. Also, + +Page 17 of 113 + + Ada Distilled + +by Richard Riehle + +type Liability is new Account + +with record + +Credit_Value : Float; +Debit_Value : Float; + +end record; + +-- 1 extended from tagged parent, lines 6, 17-20, above +-- 2 required ;phrase for this construct +-- 3 extends with third component of the record +-- 4 fourth component of the record +-- 5 record now extended with four elements + +in which Liability, derived from Account, inherits all the operations and components of its parent type but +also adds two more components. This means that Liability now has four components, not just two. This +is called extensible type inheritance. From the above list of types, one might have a access (pointer) +variable, + +Current_Account : Account_Ref; + +-- Points to Account or Liability objects + +which can point to objects of any type derived from Account. That is, any type in Account'Class. This +permits the construction of heterogeneous data structures. + +3.5 Operations on Types + +As mentioned in Section 2.7.3, Ada distinguishes between operators and operations. Legal syntax for +operations on types is defined in 4.5 of the ALRM. In general the rules are pretty simple. A limited type +has no language-defined operations, not even the := (assignment) operation. Every other type has :=, at +minimum. The following table summarizes some (not all) of these possibilities. + +A. Non-Limited Types + 1. Elementary + a. Scalar + 1) Discrete + Enumerated + Integer + Modular (Unsigned) + 2) Not Discrete + Float + Fixed + Decimal + 2. Composite + 1) Record + 2) Array + Constrained + Unconstrained + 3. Private +B. Limited +C. May Overload? + + := +Y +Y +Y +Y +Y +Y +Y +Y +Y +Y +Y +Y +Y +.. +Y +.. +Y +N +N + += +Y +Y +Y +Y +Y +Y +Y +Y +Y +Y +Y +Y +Y +.. +Y +.. + +N +Y + +<, >, <=, >= + .. + .. + Y + Y + Y + Y + Y + Y + Y + Y + Y + .. + N + .. + Y + N + N + N + Y + +& +, -, *, / + .. +.. + .. +.. + .. +.. + .. +.. + .. +.. + Y +.. + Y +.. + .. +.. + Y +.. + Y +.. + Y +.. + N +.. + N +.. + N +Y + N +Y + N +.. + N + N + Y + +N +Y + +abs + .. + .. + .. + .. + .. + Y + N + .. + Y + Y + Y + N + N + N + N + N + N + N + Y + +rem/mod + .. + .. + .. + .. + .. + Y + Y + N + N + N + N + N + N + N + N + N + N + N + Y + +in +Y +Y +Y +Y +Y +Y +Y +Y +Y +Y +Y +Y +Y +Y +Y +Y +Y +Y +N + +not in + Y + Y + Y + Y + Y + Y + Y + Y + Y + Y + Y + Y + Y + Y + Y + Y + Y + Y + N + +3.6 Where to Declare a Type + +Note: membership test not officially an operation or operator. It cannot be overloaded. It is available for limited types. + +Usually, a type will be declared in a package specification along with its exported operations. Therefore, + +package Machinery is + +type Machine is private; +procedure Turn_On (M : in out Machine); +procedure Turn_Off (M : in out Machine); +function Is_On (M : in Machine) return Boolean; +function ">" (L, R : Machine) return Boolean; + +private + +type Machine is record + +Turned_On : Boolean := False; + +end record; +end Machinery; + +-- 1 Package specification; requires body +-- 2 Specifies the visible part of the data type; +-- 3 procedure specification +-- 4 procedure specification +-- 5 function specification +-- 6 Declare the ">" function for private type +-- 7 private part hidden from a client of contract +-- 8 full definition of the publicly declared type +-- 9 component of the type; OOP attribute +-- 10 scope terminator for the component +-- 11 scope terminator for the specification + +Page 18 of 113 + + Ada Distilled + +by Richard Riehle + +will imply that the public operations available to a client of Machinery, for the type Machine, are: + +• +• +• +• + +pre-defined assignment and test for equality and inequality +procedures Turn_On and Turn_Off +functions Is_On and ">" +no other operations on type Machine are available in package Machinery. + +Note: subprograms (procedures and functions) are analogous to methods +or member functions in other languages. Most of the time these are +public, but sometimes it is useful to make them private. + +The language defined operations for a private type, Machine, are only assignment ( := ) , Equality ( = ), +and Inequality (/=). All other operations and operators for Machine must be explicitly declared in the +contract, i.e., the package specification. The package has overloaded the ">" operator, so a client of this +package can do a greater than compare on two machine objects. + +3.7 The Wall Between Types + +Note: by a "wall" we mean that values of differing types may not be directly mixed in +expressions. Type conversion can sometimes help you across the wall. Other times, more +roundabout approaches are required. This is in keeping with Ada's charter to be as type safe as + +The fourth property for a type, the wall, is illustrated using the following declarations, + +package Some_Types is + +type Channel is range 2..136; +type Signal is new Integer + + range 1..150 + +type Level is digits 7; +subtype Small_Signal is Signal + range 2..14; + +type Color is (Red, Yellow, Green, Blue); +type Light is (Red, Yellow, Green); +type Traffic is new Color + + range Red..Green; + +end Some_Types; + +-- 1 Declare specification name +-- 2 A constrained integer +-- 3 Derived from Standard.Integer +-- 4 with a range constraint +-- 5 A floating point type +-- 6 No wall with objects of type Signal +-- 7 but smaller range than Signal +-- 8 Enumerated type with four values +-- 9 Another enumerated type +-- 10 Derived from Color but with a +-- 11 smaller range of values. +-- 12. + +Warning. Most Ada practitioners recommend against this kind of package. It works well for our teaching +example, but is poor design practice. Generally, a package should be designed so each type is +accompanied by an explicit set of exported operations rather than depending on those predefined. + +3.7.1 Type Rule Examples + +The following procedure uses the package, Some_Types. It illustrates how the typing rules work. +Therefore, this procedure will not compile for reasons shown. A corrected example will follow . + +with Some_Types; +procedure Will_Not_Compile is + Ch1, Ch2, Ch3 : Some_Types.Channel := 42; + Sig1, Sig2 : Some_Types.Signal := 27; + Level_1, Level_2 : Some_Types.Level := 360.0; + Tiny : Some_Types.Small_Signal := 4; + Color_1, Color_2 : Some_Types.Color := Some_Types.Red; + Light_1, Light_2 : Some_Types.Light := Some_Types.Red; + Tr1, Tr2, Tr3 : Some_Types.Traffic := Some_Types.Red; +begin + +Ch3 := Ch1 + ch2; +Level_1 := Ch1; +Tiny := Sig1; +Color_1 := Light_1; +Light_2 := Tr1; +Light_3 := Some_Types.Light(Color_1); +Tr3 := Color_1; +Tr1 := Some_Types.Traffic'Succ(Tr2); + +end Will_Not_Compile; + +-- 1 No corresponding use clause; in scope only +-- 2 Correct. Too many errors for this to compile +-- 3 Notice the dot notation in declaration +-- 4 Dot notatation makes type Signal visible +-- 5 Dot notation again. No use clause so this is required +-- 6 +-- 7 Dot notation required here +-- 8 +-- 9 +-- 10 +-- 11 Cannot compile; + operator not directly visible +-- 12 Incompatible data types +-- 13 This is OK because of subtype +-- 14 Incompatible types in expression +-- 15 Incompatible types +-- 16 Type conversion not permitted for these types +-- 17 Incompatible types +-- 18 This statement is OK +-- 19 + +The following example corrects some of the problems with the preceding one. Note the need for type +conversion. We include an example of unchecked conversion. Generally, unchecked conversion is a bad + +Page 19 of 113 + + Ada Distilled + +by Richard Riehle + +idea. The default in Ada is to prevent such conversions. However, Ada does allow one to relax the default +so operations can be closer to what is permitted in C and C++, when necessary. + +with Some_Types; +with Ada.Unchecked_Conversion; +use Ada; +procedure Test_Some_Types is + Ch1, Ch2, Ch3 : Some_Types.Channel := 42; + Sig1, Sig2 : Some_Types.Signal := 27; + Level_1, Level_2 : Some_Types.Level := 360.0; + Tiny : Some_Types.Small_Signal := 4; + Color_1, Color_2 : Some_Types.Color := Some_Types.Red; + Light_1, Light_2 : Some_Types.Light := Some_Types.Red; + Tr1, Tr2, Tr3 : Some_Types.Traffic := Some_Types.Red; + use type Some_Types.Channel; + function Convert is new Unchecked_Conversion + (Source => Some_Types.Light, Target => Some_Types.Traffic); +begin + Ch3 := Ch1 + ch2; + Level_1 := Some_Types.Level(Ch1); + Tiny := Sig1; + Tr3 := Some_Types.Traffic(Color_1); + Tr1 := Some_Types.Traffic'Succ(Tr2); + Tr2 := Convert(Light_1); + + Light_2 := Convert(TR3); -- Illegal Illegal Illegal + +end Test_Some_Types; + +-- 1 Context clause from prior example +-- 2 Context clause for generic Ada library function +-- 3 Makes package Ada directly visible +-- 4 Name for unparameterized procedure +-- 5 Initialize declared variables +-- 6 Note dot notation in declared variables +-- 7 Declared variables with dot notation +-- 8 +-- 9 Enumerated type declarations +-- 10 +-- 11 +-- 12 Makes operators visible for this type +-- 13 Enable asssignment between variables of +-- 14 differing types without compile-time checking +-- 15 +-- 16 use type makes + operator visible +-- 17 Type conversion legal between numeric types +-- 18 This will compile because of subtype +-- 19 OK. Traffic is derived from Color +-- 21 This statement is OK +-- 22 Assign dissimilar data without checking +-- 23 Convert is only one direction +-- 24 + +Notice that operations are not permitted between incompatible types even if they have a set of values with +identical names and internal structure. In this regard, Ada is more strongly typed than most other +languages, including the Modula family and the C/C++ family. Type conversion is legal, in Ada, when +one type is derived from another such as types defined under the substitutability rules of object technology. + +3.7.2 Subtype Declarations + +There is a slight deviation in orthogonality in meaning of subtypes in the Ada Language Reference +Manual. This discussion relates to the reserved word, subtype, not the compiler design model. + +Ada has a reserved word, subtype. This is not the same as a subclass in other languages. If a subtype of a +type is declared, operations between itself and its parent are legal without the need for type conversion. + +procedure Subtype_Examples is +type Frequency is digits 12; +subtype Full_Frequency is Frequency range 0.0 .. 100_000.0; +subtype High_Frequency is Frequency range 20_000.0 .. 100_000.0; +subtype Low_Frequency is Frequency range 0.0 .. 20_000.0; + + FF : Full_Frequency := 0.0; + +HF : High_Frequency := 50_000.0; +LF : Low_Frequency := 15_000.0; + +begin + +FF := HF; +FF := LF; +LF := FF; +HF := LF; + +end Subtype_Examples is + +3.8 Elementary Types + +-- 1 Subprogram specification +-- 2 Floating point type definition +-- 3 subtype definition +-- 4 subtype definition +-- 5 sutype definition +-- 6 Variable declaration +-- 7 Variable declaration +-- 8 Variable declaration +-- 9 +-- 10 OK; no possible constraint error +-- 11 OK; no possible constraint error +-- 12 Legal, but potential constraint error +-- 13 Legal, but potential constraint error +-- 14 + +Elementary types are of two main categories, scalar and access. An access type is a kind of pointer and is +discussed in Chapter 5 of this book. Scalar types are discrete and real. Discrete types are enumerated +types and integer types. Technically, integer types are also enumerated types with the added functionality +of arithmetic operators. Numeric discrete types are signed and unsigned integers. + +Non-discrete, real numbers include floating point, ordinary fixed point, and decimal fixed point. The Ada +programmer never uses pre-defined real types for safety-critical, production quality software. + +Page 20 of 113 + + Ada Distilled + +by Richard Riehle + +All scalar types may be defined in terms of precision and acceptable range of values. The designer is even +allowed to specify the internal representation (number of bits) for a scalar value. + +type Index is mod 2**16 +for Index'Size use 16 +type Int16 is range -2 ** 15.. 2**15 - 1; +for Int16'Size use 16; +type Int32 is range -2 ** 31 .. 2**31 - 1 +for Int32'Size use 32; + +-- an unsigned number type +-- allot sixteen bits for this type +-- a signed integer number type +-- allot sixteen bits for this type +-- a signed integer numeric type +-- allot 32 bits for this type + +3.9 Composite Types + +Composite types contain objects/values of some other type. There are four general categories of composite +types: arrays, records, task types, and protected types. An array has components of the same type. A +record may have components of different types. Task types and protected types are discussed later. + +3.9.1 Arrays + +An array may have components of any type as long as they are all the same storage size. Ada has three +main options for array definition: anonymous, type-based unconstrained, type-based constrained. Other +combinations are possible, but not discussed in this book. Ada allows true multi-dimensional arrays, as +well as arrays of arrays. Two common formats for a one dimensional array are: + +type Array_Type is array(Index_Type range <>) of Component_Type; +type Array_Type is array(Range_Constraint) of Component_Type; + +-- One dimensional unconstrained array +-- One dimensional constrained array + +Ada also has something called anonymous arrays. An anonymous array is less flexible than a typed array +and cannot be passed as a parameter to a subprogram. We will not use them much in this book. + +3.9.1.1 Array Procedural Example + +The following procedure demonstrates a constrained array and an unconstrained array, along with +declarations and some procedural behavior. The constrained array is a boolean array. We show this array +because of its special properties when used with logical or, and, and xor. The unconstrained array simply +demonstrates that an unconstrained array must be constrained before it may be used. + +with Ada.Text_IO; +use Ada; +procedure Array_Definitions is + package BIO is new Text_IO.Enumeration_IO(Enum => Boolean); + +type Boolean_Set is array(1..4) of Boolean; + + pragma Pack(Boolean_Set); + for Boolean_Set'Alignment use 2; + +type Float_Vector is array(Natural range <>) of Float; + +-- 1 Context clause +-- 2 Visibility clause +-- 3 +-- 5 IO package for Boolean type +-- 6 Constrained boolean array +-- 7 Forces array to four bits +-- 7.1 Align storage on 2 bytes +-- 8 Unconstrained array + + -- Note that the index is of type Natural and can be any range of values from 0 through Integer'Last + B1 : Boolean_Set := (True, True, True, False); + B2 : Boolean_Set := (False, False, True, False); + B3 : Boolean_Set := (True, True, False, True); + F1 : Float_Vector(0..9) ; + F2 : Float_Vector(1..10); + procedure Display (Data : Boolean_Set; Comment : String) is + +Bitwise Logical operators +and, or, and xor may be +used on a boolean array. + +-- 9 +-- 10 +-- 11 +-- 12 +-- 13 +-- 14 +-- 15 +-- 16 +-- 17 + +procedure Display factors +out the responsibility for +displaying the results of the +boolean operations in the +body of this example. + + begin + +Text_IO.Put(Comment); +for I in Data'Range loop -- Cannot run off the end of an array + +Page 21 of 113 + + + + + + Ada Distilled + +by Richard Riehle + + BIO.Put(Data(I)); + Text_IO.Put(" "); +end loop; +Text_IO.New_Line; + + end Display; +begin + F1(2) := F2(4); + F1(5..7) := F2(6..8); -- This is sometimes called "sliding" + Display (B1, "B1 is "); Display(B2, "B3 is "); Display(B3, "B3 is "); + Display (B2, "B2 is "); + B3 := B1 and B2; + Display(B3, "B1 and B2 = "); + B3 := B1 or B2; + Display(B3, "B1 or B2 = "); + B3 := B1 xor B2; + Display(B3, "B1 xor B2 = "); +end Array_Definitions; + +-- 18 +-- 19 +-- 20 +-- 21 +-- 22 +-- 23 +-- 24 Simple component assignment +-- 25 Assign slices of different sizes +-- 26 +-- 27 +-- 28 Logical and of B1 and B2 +-- 29 +-- 30 Logical or of B1 and B2 +-- 31 +-- 32 Logical xor of B1 and B2 +-- 33 +-- 34 + +Line 8, in the previous program illustrates an unconstrained array. When an array is declared as +unconstrained, a constrained instance of it is required before it can be used in an algorithm. Here are some +other examples of one dimensional, arrays, constrained and unconstrained: + +type Float_Vector is array(Integer range <>) of Float; +type Float_Vector is array(-473..451) of Float; +type Day is (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday); +type Float_Vector is array(Day) of Integer; + +-- One dimensional unconstrained array +-- One dimensional constrained array + +-- One dimensional constrained array + +Note that an array index can be any discrete type and does not have to begin with zero. Also, type String, +defined in package Standard is defined as an unconstrained array with a Positive index type. All the +operations permitted on ordinary arrays are also permitted on Strings. + +3.9.1.2 Multi-dimensional Arrays + +Ada allows both multiple-dimension arrays such as those found in Fortran or arrays of arrays such as those +in the C family of languages. There is no language defined limit of number of dimensions. For example, + +type Float_Matrix is array(Integer range <>, Positive range <>) of Float; +type Bool_Matrix is array (Natural range <>, +Positive range <>, +Color range <>) of Boolean; + +type Mat_Vector is array (Positive range <>) of Float_Matrix(1..20, 5..15); + +-- Two dimensional array +-- First dimension of three +-- Second dimension of three +-- Third dimension of three +-- One dimension of two dimensions + +3.9.1.3 Array Initialization + +In Ada, arrays may be initialized using a concept called an aggregate. The word aggregate is not a +reserved word, but it is an important part of the language. An unconstrained array may include an +aggregate at the time it is constrained. Any array may be re-initialized with a new aggregate in the +algorithmic part of a module. The rule is that an aggregate must be complete. That is, every component +must be included in the aggregate. Here are some examples, using the definitions already shown in this +section (2.5.9.1). + +For one dimensional array: + +See unconstrained array, Float_Vector, defined in the previous section. + +V1 : Float_Vector (1..6) := (others => 0.0); +V2 : Float_Vector (1..3) := (1 => 12.3, 3 => 6.2, 2 => 9.4); +V3 : Float_Vector (0..120) := (0 => 2.6, 120 => 7.5, others => 9.4); + +-- Instance initialized to all 0.0 +-- Instance with initial values +-- others must appear last + +Page 22 of 113 + + + + + + Ada Distilled + +by Richard Riehle + +V4 : Float_Vector (12..80) := (12 => 16.3, 20 => 6.2, others => 1.5); +V5 : Float_Vector (-473..-1) := (others => Float'First); + +-- Instance with initial values +-- Negative index range + +In the above instances, V1 has six elements and is initialized to all 0.0, V2 has three elements and is +initialized using named association. Named association allows the programmer to associate a component +value with a named index. V3 has 121 elements. It is initialized using named association with an others +option. V4 has 68 elements, starting with an index of 12. + +In Ada, an integer type index value may begin anywhere in the number range. It may even be a negative +value, as in example V5. The value of V4'First is 12. The index bound of V4'Range is 12 through 80. + +For a two dimensional array: + + M1 : Float_Matrix(1..10, 1..10) := ( 1 => (1 => 0.0, others => 1.0), + 10 => (10 => 0.0, others => 1.0), -- 2 dimension of the array and + others => (others => 1.0)); +-- 3 others specified last + +-- 1 Named association for each + +If you wanted to write a loop that would use Text_IO to display all of the values for M1 on a console, it +might look like the following code, + +for I in M1'Range(1) + loop + for J in M1'Range(2) + loop + Text_IO.Put(Float'Image(M1(I, J)) & " " ); + end loop; + Text_IO.New_Line; + end loop; + +-- 1 Range(1) specifies first dimension of array +-- 2 outer loop; should have been named +-- 3 Range(2) specifies second dimension of array +-- 4 Always name nested loops in production code +-- 5 Convert component to text and print it +-- 6 +-- 7 Carriage return/Line feed on display +-- 8 + +3.9.1.4 Array Catenation + +Some prefer the word concatenation; same idea. + +One of the more useful operations on arrays is catenation. Catenation is predefined in the language using +the ampersand (&) symbol. As with most operators, you may overload the catenator operator. The rules +for catenation are in ALRM 4.5.3/4. Taking the Float_Vector, defined above, we can have the following: + +V10 : Float_Vector (1..10) := V1 & V2 & 42.9; + +-- Catenate 42.9, V1 and V2 + +Often it is useful to catenate a value of a different type after converting it to an appropriate representation. +Let's say we have a variable, + + Bango : Integer := 451; -- bango is the Japanese word for number. + +Suppose we want to display the value of Bango on the video display. We could do the following: + + Ada.Text_IO.Put_Line("Paper burns at " & Integer'Image(Bango) & " Farenheit "); + +This prints a string to the screen. The ampersand catenates the result of the image attribute (as if it were a +built-in function) which in turn is catenated to the constant string, Farenheit, (notice the leading space to +make formatting more readable). Attributes help to make Ada programs more portable. + +Page 23 of 113 + + Ada Distilled + +3.9.2 Records + +by Richard Riehle + +Ada records come in several forms, many of which are not covered in this book. Some of the record +forms such as variant records, unconstrained records, and discriminated records, are not important to the +novice. This book is not concerned with advanced or seldom used language features. However, we will +include a few examples of constrained records, some records with a single discriminants , and some tagged +records for the student's future study. The following Ada package specfication declares some record types. + +package Record_Declarations is + +type Library_Book is + +record + +ISBN : String (1..12); +Title : String(1..30); +Author : String(1..40); +Purchase_Price : Float; +Copies_Available : Natural; + +end record; + +type Message_1 is + +record + +Text : Unbounded_String; +Length : Natural; + +end record; + +type Message_2 (Size : Positive) is + +record + +Text : String(1..Size); +Length : Natural; + +end record; + +type Message_3 (Size : Positive := 1) is + +record + +Text : String(1..Size); +Length : Natural; + +end record; + +type Message_4 is tagged + +.record + +Text : Unbounded_String; +Length : Natural; + +end record; + +n +o +i +s +n +e +t +x +e + +e +p +y +t + +. + +type Message_5 is new Message_4 with + +record + +Stamp : Calendar.Time + +end record; + +type Message_6 is + +record + +Message_Data : Message_1; +Library_Data : Library_Book; + +end record; + +end Record_Declarations; + +-- 1 This specification might require a pragma Elaborate_Body +-- 2 Simple constrained record +-- 3 reserved word, record +-- 4 String component +-- 5 String component +-- 6 String component +-- 7 Floating point component +-- 8 Subtype natural from package Standard +-- 9 Must identify end of scope of each record +-- 10 +-- 11 Simple record with an +-- 12 unconstrained data type +-- 13 See ALRM A.4.5 +-- 14 See package Standard +-- 15 +-- 16 +-- 17 Record with a discriminant +-- 18 This must be constrained before +-- 19 it may be used. Note that the Size +-- 20 has a corresponding entry in the record +-- 21 Dynamically allocated records might not +-- 22 be as efficient as you would like. +-- 23 Record with a default discriminant +-- 24 This may be constrained or may use +-- 25 the default constraint. There are more +-- 26 rules for this, but we defer them to an +-- 27 advancd discussion of the language +-- 28 +-- 29 A tagged type. This may be extended +-- 30 with more components +-- 31 Unbounded String(See Ada.Fixed.Unbounded). +-- 32 +-- 33 +-- 34 +-- 35 Derived from a tagged type and one +-- 36 additional component. This record now x +-- 37 has a total of three components, those +-- 38 it inherits and the one defined within it. +-- 39 +-- 40 Record containing another record +-- 41 +-- 42 See line 11 +-- 43 See line 2 +-- 44. +-- 45 This package might require a pragma Elaborate_Body + +Note that some Ada +practitioners believe this +kind of record is not a +good idea. Since the +Size might be variable at +run-time, each compiler +will have a unique way +of addressing how to +best implement the code + +Page 24 of 113 + + + + Ada Distilled + +by Richard Riehle + +The package, Record_Declarations, has no subprograms. Therefore, the rules of the language might +require a special pragma (compiler directive) to advise the compiler when there is a package body. + +Note that, on line 35, the type Message_5 is derived from and extended from Message_4. This is a form +of inheritance. We could have the following: + +M4 : Message_4; +M5 : Message_5; + + ... + M4 := Message_4(M5); -- provide a Message_4 view of the object of derived type, Message_5 +or + + M5 := (M4 with Library_Book); -- extends M5 with necessary components during assignment + +In the case of the assignment to M4, the target of the assignment is provided a view of M5 that is restricted +to components in a type Message_4. However, the tag of the object does not change and the content +remains stable. This has important implications. Consider the following package. + +with Record_Declarations; use Record_Declarations; +package Messenger_Processes is + +procedure Display +-- 3 +procedure Display +-- 4 +procedure Print +procedure Process (Process_Data : in out Message_4); + +(Print_Data : Message_4'Class); + +end Messenger_Process; + +package body Messenger_Processes is + +procedure Display (The_Data : Message_4) is +begin + + -- display the data for Message_4 + +end Display; +procedure Display (The_Data : Message_5) is +begin + + -- display the data for Message_5 + +end Display; +procedure Print (Print_Data : Message_4'Class) is +begin + + Display (Print_Data); + +end Print; +procedure Process (Process_Data : in out Message_4) is +begin + + -- perform some algorithms + + Print(Process_Data); + +end Process; + +end Messenger_Process; + +-- 1 +-- 2 +(The_Data : Message_4); + +(The_Data : Message_5); + +-- 5 +-- 6 +-- 7 + +-- 1 +-- 2 +-- 3 +-- 4 +-- 5 +-- 6 +-- 7 +-- 8 +-- 9 +-- 10 +-- 11 +-- 12 +-- 13 +-- 14 +-- 15 +-- 16 +-- 17 +-- 18 +-- 19 + +Suppose we called Process with the following statement: + +Process(Message_4(M5)); -- see the immeditately preceding example + +The procedure Process does its work and then calls Print. Print has a classwide parameter. The tag of the +object operated upon in Process corresponds to Message_5. The result is a call to the correct version of +Display for Messsage_5 because of the tag. All information originally included in M5 is intact because a +view conversion does not change the data or the tag. This is called re-dispatching. For a full discussion +of inheritance and dispatching, see Chapter Nine of this book. + +Page 25 of 113 + + + + + + + + Ada Distilled + +by Richard Riehle + +4. Control Structures for Algorithms + +Even in an object-oriented language, there comes the point where we must actually code the algorithmic +implementation. Ada has a rich set of algorithmic constructs that are easy to code and easy to read. + +4.1 Iteration Algorithms in Ada + +One of the three fundamental building blocks of every computer program is iteration. In nearly every +serious program there is at least one loop. I realize some enthusiasts of recursion and/or functional +programming (LISP, ML, CLOS, Haskell, etc.) may object to this statement. + +4.1.1 For Loops + +The famous proof in Italian by Jacopini and Bohm is important here since it is a foundation idea for program structure. From their proof, we understand the +three fundamental control structures for imperative languages to be: sequence, iteration, and selection + +A for loop is simple in Ada. Every loop must have an end loop. The type of the index is derived from the +type of the range variables. The scope of the index is the scope of the loop. The index is never visible +outside the loop. Also, during each iteration of the loop, the index is a constant within the loop; that is, the +index of a loop may not be altered via assignment. Iteration safety is fundamental to Ada. + +with Ada.Integer_Text_IO; +procedure Sawatdee (Start, Stop : in Integer ) is +begin + +for I in Start..Stop + +loop + +Ada.Integer_Text_IO.Put(I); + +end loop; + +end SaWatDee; -- Ada is not case sensitive! + +-- 1 Put Library Unit in Scope; +-- 2 "Good morning" in Thailand; +-- 3 Required to initiated sequence of statements +-- 4 I is a constant to the loop in each iteration; +-- 5 Reserved word loop is required; +-- 6 Note the use of “dot notation” to achieve visibility; A.10.8 +-- 7 end loop is required for every loop; +-- 8 Note the label for the enclosing procedure; + +5.5/9 +5.5 + +5.5 +6 + +A.10.8/21 +6.2 + +Test before loop + +An Ada enumerated type is an ordered set and may be used as the index of a loop. Also, the machine +values for the enumerated type are not necessarily simple numbers as they are in C of C++. You will not +need to do arithmetic on them. For an enumerated type, declared as: + +type Week is (Sun, Mon, Tue, Wed, Thu, Fri, Sat); -- An enumerated type is an ordered set; (Sun < Mon) + +consider the following loop. + +with Ada.Text_IO; +procedure Dobroe_Uutra is +begin + +Loop_Name: +for Index in Week + +loop + +Ada.Text_IO.Put(Week’Image(Index)); + +end loop Loop_Name; + +end Dobroe_Uutra; + +-- 1 Put Library Unit in Scope; 8.2, 10.1.2 +-- 2 "Good morning" in Russian +-- 3 Required to initiated sequence of statements +-- 4 This is a named loop; good coding style; 5.5 +-- 5 Loop index may be any discrete type +-- 6 Reserved word loop is required; +5.5 +-- 7 ‘Image converts Value to Text for printing +-- 8 The name is required if the loop is named; 5.5 +-- 9 Note the label for the enclosing procedure + +Always label +loops in +production code. +It helps with both +maintenance and +documentation + +Next consider an anonymous array with a range from fifteen through sixty. We can traverse this with a +simple loop statement and a 'Range attribute. There can be no indexing off the end of the array. + + Set : array (15..60) of Integer; + +an anonymous array; one of a kind; no named type + +consider the following loop with a loop label, + +with Text_IO; +procedure Magandang_Umaga is +begin + +-- 1 Put Library Unit in Scope +-- 2 "Good morning" in Tagalog (language of Phillipines) +-- 3 Required to initiated sequence of statements + +Page 26 of 113 + + Ada Distilled + +by Richard Riehle + +Outer: +for Index in Set’Range + +loop + +-- 4 This is a named loop; good coding style +-- 5 Index'First = 15; Index'Last = 60 +-- 6 Traverse the anonymous array +-- 7 ’Image converts Integer to Text for printing + +Text_IO.Put(Integer’Image(Index)); +Text_IO.Put_Line(Integer’Image(Set(Index))); -- 8 Print the value in the array using ‘Image +Inner: +for Day in Week loop + Text_IO.Put(Week’Image(Day)); +end loop Inner; + +-- 9 Give the inner loop a name +-- 10 Note how we use type name for the range +-- 11 Convert the Day to Text for printing +-- 12 The name of the loop is required +-- 13 The name is required if the loop is named +-- 14 Note the label for the enclosing procedure + +end loop Outer; +end Magandang_Umaga; + +Remember, the +apostrophe when +used as a qualifier +in a statment is +pronounced "tick" + +Lines 7, 8, and 11 have code with the 'Image attribute. Check ALRM, Annex K/88 for details. Line 5 +could have been coded as, for Index in Set'First .. Set'Last loop … + +Sometimes you need to traverse a for loop in reverse. Line 5, above could have been coded as, + +for Index in reverse Set’Range + +-- 5 Cannot code: for Index in 60..15 loop + +A for loop might be used to traverse a two dimensional array. A nested loop will be required. Always +label each loop when coding a nested loop. Here is the declaration of such an array. + +type Matrix is array (Positive range <>, Natural range <>) of Integer; -- an unconstrained Matrix + +procedure Process (M : in out Matrix) is +begin + +Outer: +for I in M'Range(1) loop + +Inner: +for J in M'Range(2) loop + +-- do some actions on the matrix + +end loop Inner; + +end loop Outer; + +end Process; + +4.1.2 While Loops ALRM 5.5 + +-- 1 Specification for the procedure +-- 2 Simple begin +-- 3 Label for outer loop +-- 4 M'Range(1) is first dimension of array +-- 5 Label for nested loop +-- 6 M'Range(2) is second dimension +-- 7 Algorithmic statements +-- 8 Inner end loop +-- 9 Outer end loop +-- 10 End of procedure scope + +Always use +loop labels +when coding +nested loop +structures + +A while loop is often the preferred type of loop in structured programming. + +with Text_IO; +procedure Jo_Regelt is + +The_File : Text_IO.File_Type; +As_Input : constant Text_IO.File_Mode := Text_IO.In_File; +External_Name : String := “C:\Data\My.Txt”; +The_Data : String (1..80); +Line_Length : Natural; + +begin + +Text_IO.Open(The_File, As_Input, External_Name); +Input_Routine: +while not Text_IO.End_Of_File(The_File) + +loop + +Text_IO.Get_Line(The_File, The_Data, Line_Length); +Text_IO.Put_Line(The_Data(1..Line_Length)); + +end loop Input_Routine; + +end Jo_Regelt; + +-- 1 Put a library unit in scope +-- 2 "Good morning" in Hungarian +-- 3 Declare internal file handle +-- 4 Is it input or output +-- 5 Declare the external file name +-- 6 A simple string variable; +-- 7 For the input line parameter +-- 8 Required to initiate a sequence of statements +-- 9 See Text_IO for the types of the parameters +-- 10 You may name any kind of loop, and should! +-- 11 Read The_File until finding the EOF mark +-- 12 Reserved word loop is required +-- 13 Get a delimited string from the file +-- 14 Echo the string with carriage / return line feed +-- 15 end loop name is required if the loop is named +-- 16 Note the label for the enclosing procedure + +The following while loop uses the Get_Immediate feature of Ada.Text_IO, ALRM A.10.1/44. + +with Ada.Text_IO; +with Ada.Characters.Latin_1; + +-- 1 Correct context clause +-- 2 Replaces Ada 83 package ASCII + +Page 27 of 113 + + Ada Distilled + +by Richard Riehle + +procedure Hello_By_Input is + ESC : Character renames Ada.Characters.Latin_1.Esc; + Input : Character := Ada.Characters.Latin_1.Space; + Index : Natural := 0; + Hello : String(1..80) := (others => Input); +begin + +Ada.Text_IO.Get_Immediate(Input); +while Input /= ESC loop -- /= is Ada "not equal" symbol + +Ada.Text_IO.Put(Input); -- Echo input +Index := Index + 1; +Hello(Index) := Input; +Ada.Text_IO.Get_Immediate(Input); + + end loop; + Ada.Text_IO.New_Line; + Ada.Text_IO.Put_Line(Hello); +end Hello_By_Input; + +-- 3 Long procedure name +-- 4 A.3.3/5; Ada is not case sensitive +-- 5 Initial value for Variable +-- 6 package Standard, A.1/13 +-- 7 Input is intialized as space +-- 8 Normally comment this line +-- 9 ALRM A.101./44 +-- 10 Negative condition while loop +-- 11 Only Echo if it is not ESC +-- 12 Need to maintain own index +-- 13 Assign the input to the string +-- 14 No need to press enter key +-- 15 Every loop needs an end loop +-- 16 Carriage Return/ Line Feed +-- 17 Put the string and advance one line +-- 18 Must be same name as procedure + +The above loop could be coded to avoid the while condition and simply do an exit. This would eliminate +the initial Get_Immediate on Line 9 but would require an if statement to effect the exit. +Sometimes we want to exit a loop before we reach the pre-defined conditions. This can be used for a loop +with no conditions or a loop in which some associated value goes abnormal. Exit can emulate the Pascal +repeat ... until construct. There are several forms of the exit: exit when, if condition then exit, and the +simple unconditional exit. For each form, the careful programmer will include the name of the loop. + +Test after loop + +4.1.3 Exit Loop ALRM 5.7 + +with Text_IO; +procedure Salaam_Ahlay_Kham is +The_File : Text_IO.File_Type; +As_Input : Text_IO.File_Mode := Text_IO.In_File; +External_Name : String := “C:\Data\My.Txt”; +The_Data : String(1..80) := (others => ‘ ‘); +Line_Length : Natural; + +begin + +Text_IO.Open(The_File, As_Input, External_Name); +Controlled_Input: +loop + +Text_IO.Get(The_File, The_Data, Line_Length); +exit Controlled_Input + +when The_Data(1..2) = “##”; + +Text_IO.Put_Line(The_Data(1..Line_Length)); + +end loop Controlled_Input; + +end Salaam_Ahlay_Kham; + +-- 1 Put a library unit in scope +-- 2 Parameterless declaration +-- 3 Declare internal file handle +-- 4 Is it input or output +-- 5 Declare the external file name +-- 6 Constrained, initialized string +-- 7 For the input line parameter +-- 8 Required to initiated sequence of statements +-- 9 See Text_IO for the types of the parameters +-- 10 You may name any kind of loop, and should +-- 11 Unconditional loop statement +-- 12 Get a delimited string from the file +-- 13 Note the use of the label name +-- 14 A conditional exit; should always be labled +-- 15 Print the string with carriage return/line feed +-- 16 The name is required if the loop is named +-- 17 Note the label for the enclosing procedure + +Pay attention to line 10 in this example. A loop label makes this kind of loop easier to maintain. Many +Ada practitioners suggest you never use an exit without a label. For consistency checking, the compiler +will require the name of the loop at the end loop statement if there is a label. Here is some alternative +syntax for lines 13 through 14 of the loop in P5, above, + +if The_Data(1..2) = "##" then +exit Controlled_Input; + +else +... +end if; + +-- 13 An if statement to control the exit +-- 14 Exit a loop with a label name +-- +-- +-- + +The exit statement only works within a loop. The syntax and rules of the if statement are discussed in the +next section. + +Page 28 of 113 + + + Ada Distilled + +by Richard Riehle + +4.2 Selection Statements + +Selection comes in two flavors. There is the alternation form, usually represented as an if ...end if, and the +multiway selection, often coded as a case ... end case. Ada allows a multiway selection with an elsif in an +if statement. As is true of every elementary structure, there is an entry point and a well-defined end of +scope. + +4.2.1 If Statements ALRM 5.3 + +The ordinary if statement in Ada is not very complicated. The rule is that every if must have an “end if.” +Also, unlike Pascal, an if condition may be compound. There is a reserved word, elsif, which permits a +kind of multi-way condition selection. The following example is somewhat contrived, but it does illustrate +the idea of the if along with the elsif. The most important thing to observe about elsif is that it might drop +through all conditions if none are true. Therefore, you will almost always want a final else, even though it +is not required by the language. If you fall through all possibilities in a function you may never reach a +return statement which will cause the RTE to raise a Program_Error (ALRM, A.1/46) as an exception. + +function Select_Option (A,B,C : Float) return Float is + +Result : Float := 0.0; + +begin + +if A > B then + +Result := A ** 2; + +elsif A < B then + +Result := B ** 2; + +elsif A <= C then + +Result := C * B; + +else + +Result := C * A; + +end if; +return Result; +end Select_Option + +-- 1 Parameterized function +-- 2 Local Variable for return statement. +-- 3 Required to initiate sequence of statements +-- 4 Simple logical comparison +-- 5 Exponentiation of A; 4.5.6/7 +-- 6 Note the spelling;4.5.2/9 +-- 7 4.5.6/7 +-- 8 4.5.2/9 +-- 9 4.5.5 +-- 10 Optional else; but always include it +-- 11 4.5.5 +-- 12 Try to have only one return statement. +-- 13 If no return is found, compiler will reject the code +-- 14 Always label a subprogram end statement + +The if statement is legal for nearly every Ada data type. Some types designated as limited have no +predefined equality or relational testing but do permit membership if tests. Record types and private types +have predefined if tests for equality and membership. The creator of a limited type may define an equality +or relational operator. For a private type or record the designer may overload equality or define a relational +operator. Sometimes it is better to create an entirely new operation such as Is_Equal or Is_Greater For +example, using the data type, Inventory, defined in Section 3.3..3, Line 12. + +function "=" (L, R : Inventory) return Boolean; +function Is_Equal (L, R : Inventory) return Boolean; -- Specify an equality operation; Could be more readable +function ">" (L, R : Inventory) return Boolean; + +-- Specify an equality operator; operator overloading + +-- Specify an greater-than operator + +An implementation of "=" might look like this + +In code examples, see procedure + +function "=" (L, R : Inventory) return Boolean is +begin + return L.ID = R.ID; +end "="; + +-- 1 Redefines (overloads) an equal operator +-- 2 The usual begin statement +-- 3 Compare only the ID part. +-- 4 Required scope terminator + +An implementation of ">" might look like this + +function ">" (L, R : Inventory) return Boolean is + +-- 1 Redefines (overloads) ">" operator + +Page 29 of 113 + + Ada Distilled + +by Richard Riehle + +begin + return L.ID > R.ID; +end "="; + +-- 2 The usual begin statement +-- 3 Compare only the ID part. +-- 4 Required scope terminator + +There is also a form of the if statement called short-circuit form. This takes two syntactic formats:and +then and or else. The and then format explicitly indicates that when comparison of the first operand +fails, ignore the second operand. The or else format says if expression in the first operand is not TRUE, +evaluate the second operand. If it is TRUE, then don't bother to evaluate the second operand. + +4.2.2 Membership Testing 4.5.2/2 + +Tip: This is one of those powerful Ada syntactic constructs that can make code more readable and easier to maintain. + +Sometimes you want a simple membership test. The in and not in options permit testing membership of a +value within a type or type range. Membership test is permitted for any data type. + +function Continue(Data : Item) return Boolean is + +Result : Boolean := False; + +begin -- Continue + +if Data in 1..20 then +Result := True; + +end if; +return Result; + +end Continue; + +or for a data type derived from another type + +-- 1 Parameterized function +-- 2 Initialized return variable. +-- 3 Comment the begin statement +-- 4 Simple membership test for a range +-- 5 Set the result +-- 6 Always need an end if +-- 7 At least one return statement; required +-- 8 Always label the end statement + +type Bounded_Integer is new Integer range -473..451; -- Derived type; derived from Standard Integer + +procedure Demand + + (Data : in out Bounded_Integer'Base) is +Local : Bounded_Integer'Base := 0; + +begin -- Demand + +Data := Data + Local; +if Data in Bounded_Integer then + +null; + +end if; +end Demand; + +4.2.3 Case Statements ALRM 5.4 + +-- 1 Procedure Identifier +-- 2 Parameter list for Base type +-- 3 Initialized variable. +-- 4 Comment the begin statement +-- 5 Increment by value of Local +-- 6 Simple membership test for a range +-- 7 Some Action +-- 8 Always need an end if. +-- 9 Use a label for the end statement + +Ada case statements are easy and consistent. Unlike pathological case constructs in the C family of +languages, Ada never requires a “break” statement. A case statement only applies to a discrete type such +as an integer or enumerated type. When coding a case statement, all possible cases must be covered. The +following case statement illustrates several of these ideas. Consider an enumerated type, Color defined as: + +type Color is (White, Red, Orange, Yellow, Chartreuse, Green, + Blue, Indigo, Violet, Black, Brown); + +-- The values are the names of the +-- colors. No need for numerics + +The following function evaluates many of the alternatives. + +function Evaluate (C : Color) return Integer is + +Result : Integer := 0; + +begin -- Evaluate +case C is + +when Red => Result := 1; +when Blue =>Result := 2; +when Black .. Brown => Result := 3; +when Orange | Indigo => Result := 4; +when others => Result := 5; + +end case; +return Result; + +-- 1 Simple function declaration +-- 2 Local variable +-- 3 Comment the begin statement +-- 4 Start a case statement +-- 5 The => is an association symbol +-- 6 Am I blue? Set result to 2 +-- 7 For black through brown ... +-- 8 For either orange or indigo +-- 9 others required for unspecified cases. +-- 10 Must use others if any cases are not specified +-- 11 Compiler will look for a return statement + +It is not always a good idea to +initialize variables. You might +get a valid result even if there is +an error in the algorithm. + +Page 30 of 113 + + Ada Distilled + +by Richard Riehle + +end Evaluate; + +-- 12 + +Sometimes, when a case statement result requires a long sequence of statements, consider using a begin.. +end block sequences (see above discussion on blocks). Always label a begin..end block. + +function Decide (C : Color) return Integer is + +Result : Integer := 0; + +begin -- Decide + +case C is + +when Red => +begin + -- sequence-of-statements +end; +when Blue => +Label_1: +begin + -- sequence-of-statements +end Label_1; +when others => +Label_2: +begin + -- handled-sequence-of-statements +exception + -- sequence-of-statements +end Label_2; + +end case; +return Result; + +end Decide; + +-- 1 Simple function declaration +-- 2 Local variable +-- 3 Comment the begin statement +-- 4 Start a case statement +-- 5 One of the enumerated values +-- 6 An unlabeled begin ... end sequence; see 4.4 +-- 7 Any sequence of Ada statements +-- 8 Unlabeled end statement +-- 9 One of the enumerated values +-- 10 Better style; use a block label +-- 11 Alternative: consider calling nested subprogram +-- 12 A labeled begin requires label name at end +-- 13 The label is required for the end statement +-- 14 Ada requires others if some choices are unmentioned +-- 15 Yes. Still using the label; label an embedded begin block +-- 16 +-- 17 We expect a local exception handler. +-- 18 This is a good use of begin...end blocks +-- 19 The exception handling statements +-- 20 The compiler will look for this +-- 21 Scope terminator is required +-- 22 Compiler will look for a return statement +-- 23 As usual, label the end statement + +On line 14, the when others is required when some possible choices are not explicitly stated. An Ada +compiler checks for the label at the end of a labeled begin..end block. If there is a when others and there +are no other choices, the compiler issues an error message. Lastly, a choice may be stated only once. If +you repeat the same choice, the Ada compiler will pummel you about the head and shoulders soundly. + +4.3 Blocks + +As shown in the preceding example, Ada allows the programmer to label in-line blocks of code. +Sometimes these are labled loops. Other times they are simply short algorithmic fragments. A block may +even include localized declarations. This kind of block is called a "declare block." Some Ada +programming managers think in-line declare blocks are a reflection of poor program planning. In spite of +that, they appear often in production code. In fact, a declare block is the only way to declare a local +variable for a code fragment. + +4.3.1 Begin ... End Blocks ALRM 5.6 + +This is a useful feature of Ada for trapping exceptions and sometimes for debugging. Good coding style +suggests that they be labeled. Some Ada practitioners suggest using a labeled begin end with a case +statement as noted in Section 3.3.3 of this book. + +with Ada.Text_IO, +Ada.Integer_Text_IO; +function Get return Integer is + +package IIO renames Ada.Integer_Text_IO; +package TIO renames Ada.Text_IO; +Data : Integer := -0; +Try_Limit : constant := 3; -- universal integer constant +Try_Count : Natural := 0; + +-- 1 Note the comma instead of semicolon +-- 2 Predefined package for Integer I/O +-- 3 Parameterless function +-- 4 Make the name shorter via renames clause +-- 5 Make the name shorter +-- 6 In scope for all of P8 +-- 7 A constant cannot be changed +-- 8 Natural cannot be less than zero + +Page 31 of 113 + + Ada Distilled + +by Richard Riehle + +begin + +Input_Loop: +loop + +Try_Block: +begin + +Try_Count := Try_Count + 1; +IIO.Get(Data); +exit Input_Loop; + +exception + +when TIO.Data_Error => + +if Try_Count > Try_Limit then + Text_IO.Put_Line(“Too many tries"); + exit Input_Loop; +end if; +end Try_Block; +end loop Input_Loop; +return Data; + +end Get; + +4.3.2 Declare Blocks ALRM 5.6 + +-- 9 Required to initiated sequence of statements +-- 10 Optional label for the loop +-- 11 Required reserved word +-- 12 Always name a begin..end block +-- 13 Start begin ... end block +-- 14 Increment a variable by one +-- 15 Convert external text to internal number +-- 16 unconditional loop exit +-- 17 Placed between begin ... end sequence +-- 18 Exception handling +-- 19 Decide whether to exit the loop +-- 20 Because the Try_Count is too high +-- 21 exit the loop +-- 22 Every if requires an end if +-- 23 The label is required if block is labeled +-- 24 Loop is labeled so label is required +-- 25 One return statement for this function +-- 26 Always label a subprogram end statement + +A declare block is an in-line block of code which includes some local declarations. The scope of the +declarations ends with the end statement of the block. If any local name is the same as some other name in +the enclosing scope, the local name is the only one directly visible. + +with Text_IO; +procedure Tip_A is + + (properly, Ada.Text_IO, but this works too) + +Rare_E : Float := 2.72; -- natural number, e +Data : Integer := 42; + +begin + +Text_IO.Put(Integer’Image(Data)); +declare + +Data : Float := 3.14; -- a short slice of pi + +begin + +Text_IO.Put(Float’Image(Data)); + +end; +Text_IO.Put(Float'Image(Rare_E)); + +end Tip_A; + +-- 1 Put a library unit in scope +-- 2 Parameterless declaration +-- 3 A rare E; see ALRM A.5 +-- 4 In scope for entire procedure +-- 5 Required to initiate sequence of statements +-- 6 What will print? Integer is converted to a string +-- 7 begin a new scope (declarative region) +-- 8 Hide visibility of Integer, Data; see ALRM A.5 +-- 9 [optionally Handled ] sequence of statements +-- 10 X‘Image is allowed for Floating Point +-- 11 A scope terminator is required +-- 12 A long way to tip a rare e. +-- 13 Always include a unit name + +You may want to access the Data from an outer scope within a declare block. Names in an outer scope, +with names in conflict with those within a declare block, can be made visible with “dot notation.” It is +sometimes observed that declare blocks can be used for ad hoc routines that someone forgot to design into +the software. For this reason, some Ada practitioners recommend frugality when using them. Also, +because declare blocks can be so easily sprinkled through the code, it is essential that production declare +blocks are always labeled. The following declare block illustrates several of these points. + +with Ada.Text_IO; +with Ada.Integer_Text_IO, Ada.Float_Text_IO; +with Ada.Numerics; +procedure P7 is + +package IIO renames Ada.Integer_Text_IO; +X : Integer := 42; + +begin + +IIO.Put(X); +Local_Block: +declare + +use Ada.Float_Text_IO; +X : Float := Ada.Numerics.Pi; + +begin + +Put(X); +IIO.Put(P7.X); +end Local_Block; + +end P7; + +-- 1 Put a library unit in scope and make it directly visible +-- 2 Predefined numeric IO packages +-- 3 ALRM, Annex A.5 +-- 4 Parameterless declaration +-- 5 Make the name shorter via a renames clause +-- 6 In scope for entire procedure +-- 7 Required to initiate sequence of statements +-- 8 What will print? +-- 9 Always name a declare block +-- 10 begin a new scope (declarative region) +-- 11 controversial localization of use clause +-- 12 Hide visibility of global Integer, P7.X +-- 13 [optionally Handled ] sequence of statements +-- 14 Put is visible because of “use clause” +-- 15 Dot qualifier makes Integer X visible +-- 16 Labeled end name required for labeled block +-- 17 Always label a subprogram end statement + +Tip: Consider promoting a declare block to a local (nested) parameterless procedure in the declarations of the enclosing unit. This is +Page 32 of 113 +more maintainable. It can be made more efficient with an inline pragma. + + Ada Distilled + +by Richard Riehle + +5. Access Types (Pointers) + +5.1 Overview of Access Types + +We don't really have true pointers in Ada. The use of +the word pointer is simply to acknowledge a +corresponding capability via access types. The +important thing is that the default for Ada access types +is safe, unlike pointers in the C family of languages; no +void pointers in Ada. + +The British computing pioneer, Maurice Wilkes, is credited with inventing indirection. Indirection is a +generalized notion of a pointer. According to Dr. Wilkes, "There is no problem in computer programming +that cannot be solved by not adding yet one more level of indirection." Pointers, in many languages have +been problematic. The C family of languages encourages one to do arithmetic on pointers, thereby +creating some really tricky errors. Ada pointers, called access types, do not have default capability for +pointer arithmetic. Java, to its credit, adopted some of the Ada philosophy on pointers. Whenever we use +the term pointer in Ada, we really mean access type or access object. When we refer to an access type, we +are referring to a pointer with a default a set of safe rules and no arithmetic operators. + +There are three forms of access type. + Access Type Form Terminology +storage pool access type +general access type +access to subprogram type + +• Access to a value in a storage pool +• Access to a declared value +• Access to a supbprogram (procedure or function) + +Every access type is type specific to some designated type. + +Storage pool access types will require some +kind of storage pool management since objects +are dynamically allocated to an area of +memory, possibly the “Heap.” Ada does not +require automatic garbage collection but some +compilers may provide it. Otherwise, use the +package System.Storage_Pools defined in +ALRM Chapter 13. + +type Reference is access Integer; +type Float_Reference is access all Float; +type Container is limited private; +type Container_Pointer is access all Container; +type Method is access procedure … ; +type Method is access function … ; + +-- Can only point to predefined type Integer; storage pool access type +-- Can only point to predefined type Float; general access type +-- Defines a data type with limited format; ordinary limited type +-- Can only point to objects of type Container; access to a limited type +-- Points to a procedure with corresponding parameter profile +-- Points to function with corresponding parameter profile and return type + +5.2 Storage Pool Access Type + +A storage pool access type requires an associated set of storage locations for its allocation. This might be a +simple heap operation, or the serious Ada programmer can override the operations in System.Storage_Pool +to enable some form of automatic garbage collection within a bounded storage space. + +with Ada.Integer_Text_IO; use Ada; +procedure Access_Type_1 is + +type Integer_Pointer is access Integer; +Number : Integer := 42; +Location : Integer_Pointer; + +begin + +Location := new Integer; +Location.all := Number; +Integer_Text_IO.Put(Location); +Integer_Text_IO.Put(Location.all); + +end Access_Type_1; + +-- 1 Library package for Integer IO +-- 2 +-- 3 Storage pool access type +-- 4 Declared value +-- 5 Storage pool access value +-- 6 +-- 7 The word new is an allocator +-- 8 all permits reference to the data being referenced +-- 9 Illegal. Location is not an Integer type +-- 10 Legal. Location.all is data of Integer type +-- 11 + +Line 3 declares a type that points [only] to objects of type Integer. It cannot point to any other type. +There is no pointer type in Ada that allows one to point to different types (except for classwide types). +Line 4 declares an object of the pointer type. It has no value. The default initial value is null. An Ada +pointer can never point to some undefined location in memory. Line 7 uses the reserved word new. In +this context, new is an allocator. An allocator reserves memory, at run time, for an object of some data +type. On Line 7, the address of that memory is assigned to the variable named Location. The pointer +named Location is not an Integer. Instead, it points to a storage location that contains an integer. + +Page 33 of 113 + + Ada Distilled + +by Richard Riehle + +Ada, by default, prohibits arithmetic on a pointer. The following statement is not allowed in Ada. + +Location := Location + 1; -- illegal. No pointer arithmetic allowed + +If one really needs to do pointer arithmetic, it is possible through a special packages from Chapter 13 of the ALRM, package +System.Address_To_Access_Conversions and package System.Storage_Elements. In practice, pointer arithmetic is unnecessary. + +Line 8 refers to Location.all. This how one refers to the data in the memory where Location points. +Notice that Line 9 will be rejected by the compiler, but Line 10 would compile OK. + +5.3 General Access Type + +A general access type provides additional capabilities to the storage pool access type. It permits storage +allocation like storage pool access types. It also allows access to declared objects when those objects are +labeled aliased. Returning the example above, + +with Ada.Integer_Text_IO; use Ada; +procedure Access_Type_2 is + +type Integer_Pointer is access all Integer; +N1 : aliased Integer := 42; +N2 : Integer := 360; +Location : Integer_Pointer; + +begin + +Location := N1'Access; +Integer_Text_IO.Put(Location); +Integer_Text_IO.Put(Location.all); +Location := N2'Access; + +end Access_Type_2; + +-- 1 Library package for Integer IO +-- 2 +-- 3 General access type; requires all +-- 4 Aliased declared value +-- 5 Non-aliased declared value +-- 6 General access type value +-- 7 +-- 8 Point to value declared on Line 4 +-- 9 Illegal. Location is not an Integer type +-- 10 Legal. Location.all is data of Integer type +-- 11 Illegal. N2 was not aliased +-- 12 + +The first difference in this example is on Line 3. Integer_Pointer is a general access type because the +declaration includes the word, all. The next difference is Line 4. N1 is an aliased declared value. A +general access type may only reference aliased values. The reserved word, aliased, is required under most +circumstances. Tagged type parameters for subprograms are automatically aliased. Line 8 is a direct +assignment to an aliased value. This is legal. Contrast this with Line 11, which is not legal. Do you see +that Line 11 is not legal because N2, on line 5, is not aliased? + +5.3.1 Preventing General Access Type Errors + +There is a potential danger with direct assignment to pointers. This danger is present all the time in the C +family of languages. What happens when a data item goes out of scope and still has some other variable +pointing to it? Ada has compiler rules to prevent this. The following example illustrates this. + +with Ada.Integer_Text_IO; use Ada; +procedure Access_Type_3 is + +type Integer_Pointer is access all Integer; +Location : Integer_Pointer; + +begin + +declare + +N1 : aliased Integer := 42; + +begin + +Location := N1'Access; + +end; + +end Access_Type_3; + +-- 1 Library package for Integer IO +-- 2 +-- 3 General access type; requires all +-- 4 General access type value +-- 5 +-- 6 A declare block with local scope +-- 7 Declare an aliased value locally +-- 8 +-- 9 Point to value declared on Line 4 +-- 10 End of declare block scope +-- 11 Compilation failed! Sorry about that. ☺ + +The Ada compiler will reject this program. The rule is that the general access type declaration must be at +the same level (same scope) as its corresponding variables. If you look at this example carefully, you will + +Page 34 of 113 + + Ada Distilled + +by Richard Riehle + +see that, when the declare block leaves its scope, Location would still be pointing to a value that has +disappeared. Instead of using ‘Access on line 9, the programmer could have coded ‘Unchecked_Access, +thereby bypassing the compile-time checks. Wisdom would dictate thinking very carefully before +resorting to the use of any “unchecked” feature of the language. The word “unchecked” means the +compiler does not check the validity or legality of your code. It is almost always an unsafe programming +practice. + +While the accessibility rules (See 5.3.2) might seem a drawback, they are easily managed in practice. +Often it is enough to simply declare a local general access type and use it in a call to appropriate +subprograms. The following example shows how this could happen. + +procedure Access_Type_4 is + +function Spritz (I : access Integer) return Integer is +begin + +return I.all + 1; + +end Spritz; + +begin + +declare + +type Integer_Pointer is access all Integer; +Location : Integer_Pointer; +N1 : aliased Integer := 42; +N2 : Integer := 0; + +begin + +Location := N1'Access; +N2 := Spritz(Location); + +end; + +end Access_Type_4; + +Not good coding style. Avoid these kinds of +side-effect statements. This is the one and only +place where C++ can be more reliable than Ada +because of the way C++ controls constants. + +-- 1 +-- 2 +-- 3 +-- 4 +-- 5 +-- 6 +-- 7 +-- 8 +-- 9 +-- 10 +-- 11 +-- 12 +-- 13 Assign location of N1 to Location +-- 14 Call function with access variable parameter +-- 15 +-- 16 + +All uses of the general access type are localized +and the lifetime of each entity is appropriate to the +others. There will be no potential dangling +references when the declare block leaves its scope. + +On line 14, a local access variable is used to call a function that has an access parameter. The access +parameter is anonymous. We may not assign a location to it. However, it would be legal to code. + +I.all := I.all + 1; -- N1 would also be incremented by 1 +return I.all; + +But this is a very naughty thing to do. Shame on +you if you do it! + +This code would change the actual value of what Location is pointing to. Avoid doing this sort of thing. +If you were to print the value for both N1 and N2, you would see the number 43. Some practitioners +consider this a side-effect. Side-effects are rare in Ada and usually considered bad programming style. + +5.3.2 The Accessibility Rules + +ALRM Section 3.10.2, paragraphs 3 through 22, describe the accessibility rules. The purpose of the rules +is to prevent dangling references. That is, when a variable is no longer in scope, there should be no +access value trying to reference it. This is checked by the compiler. Under some rare circumstances, it +might not be checked until run-time. + +The rules can be summarized in terms of the lifetime of the access type itself. An object referenced by the +'Access attribute may not exist longer that the the access type to which it applies. Also, if an object is +referenced with the 'Access attribute, it must be able to exist as long as the access type. The following +three examples illustrate the point. + +procedure Accessibility_Problem_1 is + type Integer_Reference is access all Integer; + Reference : Integer_Reference; + Data : aliased Integer; +begin + Reference := Data'Access; + +This example will work just fine. No data will be +left dangling when the scope is exited. Lifetime +of all entities is the same. + +-- 1 +-- 2 General access type in scope +-- 3 Access value in immediate scope +-- 4 Data at the same accessibility level +-- 5 +-- 6 OK because types and declarations + +Page 35 of 113 + + Ada Distilled + +by Richard Riehle + +end Accessibility_Problem_1; + +-- 7 are at the same accessibility level + +procedure Accessibility_Problem_2 is + type Integer_Reference is access all Integer; + Reference : Integer_Reference; +begin + declare + Data : aliased Integer; + begin + Reference := Data'Access; + end; +end Accessibility_Problem_2; + +This will not compile. When +the program exits the declare +block, an outer pointer named +Reference would still be +pointing to data that no longer +existed. This is not simply a +dangling reference. It is a +reference to data that is no +longer valid. The Ada compiler +will not let you do this. + +procedure Accessibility_Problem_3 is + type Integer_Reference is access all Integer; +begin + declare + Reference : Integer_Reference; + Data : aliased Integer; + begin + Reference := Data'Access; + end; +end Accessibility_Problem_3; + +This will not compile. You +might think that putting the +actual pointer in the same local +scope as the data being reference +would work. The rule is that +access value named Reference +must exist at least as long as the + +5.4 Access to Subprogram Types + +-- 1 +-- 2 General access type +-- 3 Access value +-- 4 +-- 5 +-- 6 An aliased integer value +-- 7 +-- 8 Will not compile; at wrong level of +-- 9 accessibility for corresponding types. +-- 10 + +-- 1 +-- 2 +-- 3 +-- 4 +-- 5 +-- 6 +-- 7 +-- 8 +-- 9 +-- 10 + +One of the problems with the Ada 83/87 standard for Ada was the unavailability of some kind of pointer +capability for subprograms. The current Ada standard does permit this. The rules for formation of such +an access type are rather simple. The rules for visibility and accessibility of access to subprogram types +are often difficult to manage in one’s design. + +5.4.1 Declaring an Access to Subprogram Type + +• The type must have a parameter list corresponding to the subprogram being accessed +• The return type of a function access type must match that of the function being accessed +• Variables of the type may access any subprogram with a conforming profile + +Examples: + +type Action is access procedure(Data : in out Integer); +type Channel is access procedure(M : in out Message; L : out Natural); + +type Condition_Stub is access function (Expression : Boolean) return Boolean; +type Compute is access function (L, R : Float) return Float; + +The signature (parameter profile) +of each subprogram access type +must exactly match any +subprogram being accessed. + +5.4.2 Using an access to Subprogram Type + +5.4.2.1 A Procedure Example + +The following example demonstrates how to create an array of procedures. This is often useful when you +have multiple procedures with the same profile but different behaviors. In this example we have kept the +behavior simple to avoid confusion. The astute reader will immediately see the possibilities. + +with Ada.Integer_Text_IO; +with Ada.Text_IO; +use Ada; + +-- 1 ALRM Annex A +-- 2 ALRM Annex A +-- 3 Makes Ada directly visible + +Page 36 of 113 + + Ada Distilled + +by Richard Riehle + +procedure Array_Of_Procedures is + +type Action is access procedure (Data : in out Integer); + procedure Process (D : in out Integer) is + + begin + D := D + D; + end Process; +type Process_Set is array(1..10) of Action; +Index : Positive; + Value : Integer := 0; + The_Process : Process_Set := (others => Process'Access); +begin + +loop + +Text_IO.Put("Enter Index(1..10): "); +Integer_Text_IO.Get(Index); +exit when Index not in 1..10; +Text_IO.New_Line; +Text_IO.Put("Enter Integer Value: "); +Integer_Text_IO.Get(Value); +The_Process(Index)(Data => Value); +Text_IO.New_Line; +Text_IO.Put("The result for Index " & Positive'Image(Index) + + & "is" & Integer'Image(Value)); + end loop; +end Array_Of_Procedures; + +5.4.2.2 A function Example + +-- 4 Name of enclosing procedure +-- 5 Access to subprogram definition +-- 6 Procedure with correct profile +-- 7 +-- 8 Details; procedure behavior +-- 9 end of scope of procedure +-- 10 Array type of access types +-- 11 Used for array index later +-- 12 Used for actual parameter +-- 13 access object array with aggregate +-- 14 +-- 15 +-- 16 +-- 17 +-- 18 membership test for exit +-- 19 +-- 20 +-- 21 +-- 22 Named association clarifies +-- 23 +-- 24 +-- 25 +-- 26 +-- 27 + +The following function example has behavior similar to the previous example. It has been altered a little +bit to illustrate some additional capabilities. + +with Ada.Text_IO; use Ada; + +procedure Function_Access_Type is +type Real is digits 12; +package FIO is new Text_IO.Float_IO(Num => Real); +function Method (D : in Real) return Real is + begin + + return D + D; + end Method; + +type Compute is access function (D : in Real) return Real; +Result, Value : Real := 0.0; + procedure Process (Behavior : Compute; Input : in Real; + + Output : out Real) is + begin + +Output := Behavior(Input); + + end Process; +begin + +loop + +Text_IO.New_Line; +Text_IO.Put("Enter Real Value (0 to exit): "); +FIO.Get(Value); +exit when Value = 0.0; +Process(Behavior => Method'Access, Input => Value, Output => Result); +Text_IO.New_Line; +Text_IO.Put_Line("The result is "); + FIO.Put(Result, Fore => 4, Aft => 3, Exp => 0); + Text_IO.New_Line; + end loop; +end Function_Access_Type; + +-- 1 +-- 2 +-- 3 Define a floating point type +-- 4 Instantiate IO package +-- 5 function w/correct profile +-- 6 +-- 7 +-- 8 +-- 9 Corresponding access type +-- 10 +-- 11 Note first parameter type +-- 12 +-- 13 +-- 14 Reference to a function +-- 15 +-- 16 +-- 17 +-- 18 +-- 19 +-- 20 +-- 21 +-- 22 Key statement in example +-- 23 +-- 24 +-- 25 +-- 26 +-- 27 +-- 28 + +Page 37 of 113 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Ada Distilled + +by Richard Riehle + +5.4.2.2 A Package Example + +Many newcomers to Ada find the accessibility rules frustrating when trying to implement access to +subprogram solutions across packages. The accessibility rule remains the same, but one must design a bit +more carefully to ensure that access types are at the same level (have the same lifetime) as their access +objects and vice versa. The following package specification declares some access types. + +package Reference_Types is + type Int_32 is range -2**31..2**31 - 1; + for Int_32'Size use 32; + +type Data_Set is array (Natural range <>) of Int_32; + + type Data_Set_Reference is access all Data_Set; + type Validate_Routine is access function(Data : Int_32) + return Boolean; + type Process_Method is access Procedure(Data : Int_32); + procedure Process (Data : in out Data_Set; + Method : in Process_Method); + function Validate (Data : access Data_Set; + Validator : in Validate_Routine) return Boolean; + function Validate (Data : in Data_Set; + Validator : in Validate_Routine) return Boolean; +end Reference_Types; + +-- 1 +-- 2 a signed integer with range +-- 3 use 32 bits for the integer +-- 4 unconstrained array of int_32 +-- 5 pointer type to the array type +-- 6 access type that points to a +-- 7 function; access to function +-- 8 access type points to +-- 9 procedure +-- 10 +-- 11 access parameter; in mode +-- 12 access to function parameter +-- 13 access parameter; in mode +-- 14 access to function parameter +-- 15 + +There are a few new ideas in this package. Line 2 has a signed integer type with a range that can be +represented in thirty-two bits. On line 3 we force the representation to thirty-two bits using the 'Size +clause. See the Annex K attributes for the definition of this clause. On lines 6 through 8 we declare +some access to subprogram types which for parameters in lines 9 through 15. The following package +contains declarations for functions for our final example. It depends on package Reference_Types. + +with Reference_Types; +package Reference_Functions is + +function My_Process return Reference_Types.Process_Method; +function My_Validator return Reference_Types.Validate_Routine; + +end Reference_Functions; + +Note that this package is a the +same level, the package level as +the access types in package +Reference_Types + +-- 1 +-- 2 +-- 3 +-- 4 +-- 5 + +Implementation for both packages will be presented a little later. Here is a little test procedure. + +with Reference_Types; +with Reference_Functions; +with Ada.Text_IO; +procedure Test_Reference_Types is + Test_Data : Reference_Types.Int_32 := 42; + package Int_32_IO is new Ada.Text_IO. + Integer_IO(Num => Reference_Types.Int_32); + Test_Data_Set : Reference_Types.Data_Set(0..20) + := (others => Test_Data); +begin + Reference_Types.Process (Data => Test_Data_Set, + Method => Reference_Functions.My_Process); +end Test_Reference_Types; + +-- 1 Put reference types in scope +-- 2 Reference functions in scope +-- 3 +-- 4 +-- 5 +-- 6 +-- 7 +-- 8 +-- 9 +-- 10 +-- 11 +-- 12 +-- 13 + +Note that Lines 11 and 12 make a +call to a procedure using a +"pointer" to a function as the +actual parameter. + +Line 6 simply demonstrates an instantiation of an I/O package for the Int_32 type. Line 11 calls the +procedure, Process from Reference_Types and gives it an actual parameter of My_Process from the +package containing the Reference_Functions. So far, everything is at the same level of accessibility. Here +are the package bodies for Reference_Types and Reference_Functions. + +Page 38 of 113 + + + + + Ada Distilled + +by Richard Riehle + +Method is an access +value that +references a +procedure. + +Validate is an +access value that +references a +function. + +package body Reference_Types is + procedure Process (Data : in out Data_Set; + + Method : in Process_Method) is + + begin + +for I in Data'Range + +loop + + Method(Data(I)); + end loop; +end Process; +function Validate (Data : access Data_Set; + + Validator : in Validate_Routine) return Boolean is + begin + +return Validate(Data.all, Validator); + +end Validate; + +function Validate (Data : in Data_Set; + + Validator : in Validate_Routine) return Boolean is + +Without_Error : Boolean := True; + + begin + +for I in Data'Range +loop + + Without_Error := Validator(Data => Data(I)); + +exit when not Without_Error; + + end loop; + +return Without_Error; + +end Validate; +end Reference_Types; + +package body Reference_Functions is + procedure My_Process (Data : Reference_Types.Int_32) is + begin + +null; + +end My_Process; +function My_Validator (Data : Reference_Types.Int_32) return Boolean is + + begin + +return True; +end My_Validator; +function My_Process return Reference_Types.Process_Method is + +Test_Process : Reference_Types.Process_Method := My_Process'Access; + + begin + +return Test_Process; + +end My_Process; +function My_Validator return Reference_Types.Validate_Routine is +Test_Validation : Reference_Types.Validate_Routine + + := My_Validator'Access; + begin + +return Test_Validation; + +end My_Validator; +end Reference_Functions; + +-- 1 +-- 2 +-- 3 +-- 4 +-- 5 +-- 6 +-- 7 +-- 8 +-- 9 +-- 10 +-- 11 +-- 12 +-- 13 +-- 14 +-- 15 +-- 16 +-- 17 +-- 18 +-- 19 +-- 20 +-- 21 +-- 22 +-- 23 +-- 24 +-- 25 +-- 26 +-- 27 + +-- 1 +-- 2 +-- 3 +-- 4 +-- 5 +-- 6 +-- 7 +-- 8 +-- 9 +-- 10 +-- 11 +-- 12 +-- 13 +-- 14 +-- 15 +-- 16 +-- 17 +-- 18 +-- 19 +-- 20 +-- 21 + +Study these to determine where the 'Access attribute is applied. Note how this can actually work and still +prevent the dangling references. Accessibility rules are there to keep you from making stupid errors. + +Page 39 of 113 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Ada Distilled + +by Richard Riehle + +6. Subprograms + +procedures and functions + +Subprograms are either functions or procedures. A subprogram may have parameters or not. Subprogram +parameters were introduced in an earlier section. The algorithmic code in your program will almost always +be contained within some kind of subprogram (or a task). A subprogram may have locally declared +variables, locally declared types, and locally nested subprograms or packages. + +6.1 Procedures + +6.1.1 Procedure Format and Syntax + +A procedure in Ada may be used to implement algorithms. As shown earlier, procedure have a rich set of +parameter types and parameter modes. The format of a procedure body is, + +procedure Ahoy_There is + -- procedure declarations +begin + +-- handled sequence of statements + +exception + +-- a sequence of statements handling the exception + +end Ahoy_There ; + +6.1.2 Procedure Compilation Units + +-- 1 Procedure declaration with no parameters; 6.3 +-- 2 Local to this procedure +-- 3 Begins sequence of algorithmic statements; 6.3 +-- 4 Handled by exception handler on error +-- 5 An optional exception handler for the procedure +-- 6 Any handling statements legal +-- 4 Scope terminator with name of unit + +6.3 + +A.10.6 + +Note the four parts to the procedure. This is sometimes called the "Ada comb." You may compile a +procedure specification as a source file separately from its implementation. + +with Ada.Text_IO; +procedure Simple_2; + +-- 1 Put Text_IO library unit in scope; +-- 2 Specification for a procedure may be compiled + +10.1.2, A.10 + +6.3 + +with clause could +be moved to + +The implementation may be coded and compiled later. The implementation for Simple_2 could be, + +procedure Simple_2 is +begin + +Ada.Text_IO.Put_Line(“Hello Ada”); + +end Simple_2 ; + +-- 1 Parameterless declaration; 6.3 +-- 2 Begins sequence of algorithmic statements; +-- 3 Dot notation makes Put_Line visible +-- 4 Scope terminator with name of unit + +A.10.6 +6.3 + +6.3 + +Another version of this might execute the Put_Line some given number of times using a for loop. A for +loop includes an index value declared locally to the loop and a range of values for the index. The loop will +then iterate the number of times indicated by the index range. For example, + +with Ada.Text_IO; +procedure Simple_3 is +begin + +for Index in 1..10 loop + +Ada.Text_IO.Put_Line(“Hello Ada”); + +end loop; +end Simple_3 ; + +-- 1 Put Text_IO library unit in scope; +-- 2 Parameterless declaration; 6.3 +-- 3 Begins sequence of algorithmic statements; +-- 4 Specification of a for loop +-- 5 Dot notation makes Put_Line visible +-- 6 End of loop scope. End of loop index scope +6.3 +-- 7 Scope terminator with name of unit + +A.10.6 + +10.1.2, A.10 + +6.3 + +A variation on the previous program uses some local declarations, a function with a parameter and a simple +call from the main part of the procedure. + +with Ada.Text_IO; +procedure Simple_4 is + +function Is_Valid (S : String) + + return Boolean is + +... + +-- 1 Put Ada.Text_IO Library Unit in scope +-- 2 Declaration for parameterless procedure +-- 3 Declaration for a function with a parameter +-- 4 Specify the type of the return value +-- 5 three dots not legal Ada + +Page 40 of 113 + + Ada Distilled + +by Richard Riehle + +end Is_Valid; +Text : String (1..80); +Len : Natural; + +begin + +Ada.Text_IO.Get_Line(Text, Len); +if Is_Valid(Text(1..Len)) then + +Text_IO.Put_Line(Text(1..Len)); + +end if; +end Simple_4 ; + +-- 6 End of scope for function Is_Valid +-- 7 Declare a String variable with constraint +-- 8 Uninitialized variable +-- 9 Begin handled-sequence-of-statments +-- 10 Call to Get_Line procedure with two parameters +-- 11 Call the function with string parameter +-- 12 Put string w/carriage return and line feed +-- 13 Ends scope of if statement +-- 14 Ends scope of Simple_2 + +6.1.3 A Main Subprogram + +Technically, a main subprogram is either a procedure or function. Most often it is a procedure. +When it is a function, the return value is an integer to the operating system as in Unix or Linux. + +A main subprogram is not required, but most programs have one. Here is an example of a main procedure. + +with Application; -- This could be any Application package +procedure Main is + The_Application : Application.Application_Type; +begin -- Main + +Restart_Iterator: +loop + +Application_Control: +begin -- Application_Control + +Application.Start(Data => The_Application); +Application.Stop(Data => The_Application); +exit Restart_Iterator; + +exception + +when others => + +Application.Cleanup(Data => The_Application); +Application.Restart (Data => The_Application); + +end Application_Control; + +end loop Restart_Iterator; +Application.Finalization (Data => The_Application); + +end Main; + +6.3 + +-- 1 Put package Application in scope; 10.1.2, +-- 2 Parameterless declaration; +-- 3 Some kind of type for the application +-- 4 Begins Main subprogram; 6.3 +-- 5 We want a non-stop system so we +-- 6 create a restart iterator as a loop. +-- 7 Label the Application control block +-- 8 No harm in commenting every begin +-- 9 Start the application code +-- 10 Stop the application code +-- 11 If all goes well, exit the loop here. +-- 12 If there is an exception anywhere, do this. +-- 13 Others captures any kind of exception +-- 14 Start the cleanup before Restarting +-- 15 Now restart the application +-- 16 Block label required because it is labeled +-- 17 Loop label required because it is labeled +-- 18 The finalization routines for application +-- 19 Scope terminator with unit name 6.3 + +6.1.4 Procedure Parameters + +Any procedure or function may have parameters. The following example is a variation on the Diamond +procedure and demonstrates the use of named association in calling formal parameters. The syntax for +named association is (formal-parameter-name => actual-parameter-name). This example was originally designed +and programmed by a young US Marine Corps Lance Corporal who, at the time, had a high-school +education. Notice that he used elementary algebra to write this program with only one loop and simply +called the inner procedure by changing the algebraic signs of the actual parameters. While this code can be +improved , it demonstrates how this young Marine thought about the problem before coding it. + +-- ================================================== +-- diamond.ada +-- Solution to Diamond Problem by LCPL Mathiowetz, USMC +-- Camp Kinser, Okinawa. June 1993. AdaWorks Intro to Ada Class +-- ================================================== +with ada.text_io; use Ada; -- Makes all of package Ada visible +procedure Diamond is + +package TIO renames Text_IO; +subtype Column is TIO.Positive_Count; +Center : constant := 37; +Left_Temp, Right_Temp : Integer := Center; +Plus_2 : constant := 2; +Minus_2 : constant := -2; +procedure Draw (Left, Right, Depth : in Integer) is + +Symbol : String(1..1) := "X"; +Left_Col, Right_Col : Column; + +begin + +These first five lines illustrate a +style for documenting an Ada source +code unit. The author of this solution +was a USMC Lance Corporal with a +High School education. + +-- 1 +-- 2 +-- 3 +-- 4 +-- 5 . +-- 6 Only Text_IO is required for this program +-- 7 Specification with no parameters +-- 8 A shortened name for Text_IO +-- 9 Subtype may be used with its parent type +-- 10 A named constant +-- 11 Temporary values, initialized +-- 12 Positve constant value +-- 13 Negative constant value +-- 14 Nested procedure with parameter list +-- 15 The character we will print +-- 16 These are probably extraneous +-- 17 We are in a nested procedure + +Page 41 of 113 + + Ada Distilled + +by Richard Riehle + + for Index in 1..Depth loop + if Left_Temp = Center then + TIO.Set_Col(Center); + TIO.Put(Symbol); + else + Left_Col := Column(Left_Temp); + Right_Col := Column(Right_Temp); + TIO.Set_Col(Left_Col); + TIO.Put(Symbol); + TIO.Set_Col(Right_Col); + TIO.Put(Symbol); + end if; + TIO.New_Line; + Left_Temp := Left_Temp + Left; + Right_Temp := Right_Temp + Right; + end loop; + end Draw; +begin -- Diamond + Draw (Left => Minus_2, Right => Plus_2, Depth => 9); + Draw (Left => Plus_2, Right => Minus_2, Depth => 10); +end Diamond; + +-- 18 Index declared here; type is range type +-- 19 Is it time to Put the center character? +-- 20 Using renamed Text_IO.Count +-- 21 +-- 22 +-- 23 Extraneous assignment on these two lines; +-- 24 we could do type conversion in Set_Col +-- 25 TIO.Set_Col(Column(Right_Temp)) +-- 26 might be better coding on line 25 and 27 +-- 27 +-- 28 Symbol on line 15 +-- 29 +-- 30 +-- 31 Arithmetic on Temporary values using +-- 32 algebraic addition on negative parameter +-- 33 +-- 34 End of nested procedure +-- 35 Always comment this kind of thing +-- 36 Use named association for these calls. +-- 37 Reverse the signs to get a different shape +-- 38 End of unit with named unit at end + +Sometimes we want a variable to enter the procedure with one value and exit with a new value. Here is a +simple procedure which uses in out parameter mode. Although this example is trivially simple, it can be +extended to a large range of other data types where one must alter that state of an object in some carefully +controlled way. + +procedure Update (Data : in out Integer) is +begin + +Data := Data + 1; + +end Update; + +-- 1 in out allowed on either side of := +-- 2 start algorithmic part of procedure +-- 3 In with one value; out with a new value +-- 4 end of unit with unit name + +Other times, it is useful to get a variable with an in value and return some other value within a procedure +parameter list. This is not always a good design model since it leads us to combine two ideas, modifier and +query, into a single operation. Many OOP practitioners suggest that modifiers and queries should be kept +separate. This example shows an update operation on an AVL Tree in which the procedure returns a +Boolean to indicate whether the tree is now in balance. + +procedure Balance (The_Tree : in out AVL_Tree; Balanced : out Boolean) is +begin + +-- long, complex, dynamically self-balancing algorithm +Balanced := -- a boolean result from the balancing algorithm + +end Balance; + +-- 1 Dynamically, self-balancing tree +-- 2 built on access types for flexibility. +-- 3 node rotations: LL, LR, RR, RL +-- 4 Must be checked by caller +-- 5 + +The problem with the above example is that, any subprogram making the call, must also be sure to check +the Boolean result. If the Balanced parameter is not evaluated, the Boolean out parameter is of no value. + +procedure Insert (Tree : in out AVL_Tree; Value : in Item) is + +OK_To_Proceed : Boolean := False; + +begin -- Insert + +-- algorithm to insert a node in the tree +Balance(The_Tree => Tree, Balanced => OK_To_Proceed); +if OK_To_Proceed then + +-- some additional source code here + +end if; +end Insert; + +-- 1 From collection of AVL_Tree methods +-- 2 Should be initialized +-- 3 Good practice to comment a begin +-- 4 Pre-order, in-order, post-order? +-- 5 Named association call +-- 6 If you fail to do this check, you are +-- 7 Making use of the out parameter of +-- 8 type Boolean. +-- 9 If name is supplied, compiler checks. + +Some Ada practitioners believe it is better to raise an exception in a function than to return a Boolean out +parameter in a procedure. Their rationale for this is that an exception cannot be ignored, but an out +parameter, is easy to overlook or ignore. + +Page 42 of 113 + + Ada Distilled + +6.2 Functions + +by Richard Riehle + +A function must return a result of the type indicated in its profile. The compiler will check this and +disallow a wrong return type. A function may be called in an assignment statement or as an argument +within another function or procedure call. Ada also allows pointers (access types) to functions and +procedures. + +6.2.1 Function Format and Design + +The Is_Valid function from a previous section might be coded to look like this, + +function Is_Valid (S : String) + + return Boolean is + +Result : Boolean := True; + +begin + +for I in S'Range loop +case S(I) is + +when 'a'..'z' | 'A'..'Z' => + null; +when others => + Result := False; + exit; + +end case; + +end loop; +return Result; + +end Is_Valid; + +6.2.2 Function Examples + +-- 1 Default mode is in for type String +-- 2 Boolean defined in package Standard +-- 3 Return type named Result as local variable +-- 4 Begin the handled-sequence of statements +-- 5 I takes the index type of String: Positive +-- 6 Examine a single character from the String +-- 7 Check both upper and lower case +-- 8 No break statement is required +-- 9 others required if not all options are covered +-- 10 Simple assignment of Boolean value +-- 11 exit leaves the loop. all indices are reset +-- 12 Every control structure requires terminator +-- 13 Ends the scope of the loop including, I +-- 14 Compiler requires a return statement +-- 15 Scope terminator for the function. Required. + +The next program is an example of an Ada function. This function simply evaluates the greater of two +values in a parameter list and returns it. Every function must have at least one return statement. + +function Largest (L, R : Integer) return Integer is +begin + +if L > R then +return L; + +else + +return R; + +end if; +end Largest; + +-- 1 Parameterized function declaration; 6.3 +-- 2 Begins sequence of algorithmic statements; +-- 3 Compare L to R +-- 4 function must return a value of return type +-- 5 If the comparison is false +5.3 +-- 6 Another return; would a single return be better? +-- 7 Every if must have a corresponding end if. +6.3 +-- 8 Scope terminator with name of unit + +6.3 + +6.3 + +5.3 + +To call this function you will use an assignment statement. + +with Largest; +procedure Hrothgar (Y, Z : in Integer; X : out Integer) is +begin + +X := Largest(L => Y, R => Z); + +end Hrothgar; + +-- 1 with is permitted for library unit function +-- 2 Note the modes of the parameter list +-- 3 +-- 4 Named Association syntax +-- 5 As usual, include the name with the end statement + +6.3 + +Line 4 shows named association syntax. In this case, L and R name the formal parameters. Y and Z name +the actual parameters. The arrow, in the form of =>, associates the actual paramter with the formal. This is +a powerful feature, unique to Ada, that makes source code more readable and more maintainable. + +Suppose we have a record type called Stack. It contains two components. Every type ... is record +declaration must contain an end record statement. In the Stack record, shown below, there is also a +component of an array type. This is a constrained array of type Stack_Data. + +type Stack_Data is array(1..1000) of Integer; +type Stack is record + +-- 1 Constrained array type definition for Integers +-- 2 Record type format + +Page 43 of 113 + + + Ada Distilled + +by Richard Riehle + +Data : Stack_Data; +Top : Natural := 0; + +end record; + +-- 3 Array component within a record +-- 4 Natural data type; note the initialization +-- 5 Every record structure requires an end record + +Here is a function that returns a boolean value for a record type, Stack, that contains a component, Top + +function Is_Empty (S : Stack) return Boolean is + +Result : Boolean := False; + +begin + +if S.Top = 0 then -- Equality test + +else + +Result := True; + +Result := False; + +end if; +return Result; + +end Is_Empty; + +-- 1 Parameterized function declaration; 6.3 +-- 2 A locally declared result variable +-- 3 Begins sequence of algorithmic statements; +-- 4 Syntax for an if statement; then is required +-- 5 Assignment statement based on true path +-- 6 An else takes the false path +-- 7 Another assignment +-- 8 An if requires an end if; checked by compiler +-- 9 A function must contain at least one return +-- 10 Scope terminator with name of unit 6.3 + +6.3 + +Would it be better to have coded the Is_Empty function as, + +function Is_Empty (S : Stack) return Boolean is +begin + +return S.Top = 0; + +end Is_Empty; + +-- 1 Parameterized function declaration; 6.3 +-- 2 Begins sequence of algorithmic statements; 6.3 +-- 3 Compare S.Top to Zero True or False +-- 4 Scope terminator with name of unit + +6.3 + +Function parameters modes are only allowed to be in or access. The default mode is always in. An in +parameter is the equivalent of a constant to the function. One may never assign a value to an in mode +parameter value. Consider enumerated type, Month, and cycle through the months, returning to January +when you reach December. Consider, + +type Month is (January, February, March, April, May, June, July, August, September, October, November, December); + +function Next (Value : Month) return Month is +begin + +if Value = Month’Last then +return Month’First; + +else + +return Month’Succ(Value); + +end if; +end Next; + +-- 1 Declare a parameterized function +-- 2 No other declarations +-- 3 Month'Last is December +-- 4 Month'First is January +-- 5 The usual behavior of else +-- 6 Month'Succ(June) is July +-- 7 End Scope of if statement +-- 8 End scope of function + +Consider another type, Vector, defined as an unconstrained array: + +type Vector is array (Positive range <>) of Float; -- An unconstrained array; must be constrained when used + +with an exception defined in a visible package specification as: + +Range_Imbalance : exception; -- An exception declaration, visible somewhere in the design + + -- Note: an exception is not a data type + +function “+” (L, R : Vector) return Vector is + +Result : Vector (L’Range) := (others => 0.0); + +begin + +if L’Length /= R’Length then +raise Range_Imbalance; + +end if; +for Index in L’Range + +loop + +Result (Index) := L(Index) + R(Index); + +end loop; +return Result; + +end “+”; + +-- 1 Overloading an infix operator +-- 2 Constrain and initialize the result array +-- 3 +-- 4 Ensure R and L are of the same length +-- 5 Raise user-defined exception shown above. +-- 6 We never reach this point if exception is raised +-- 7 The 'Range attribute generalizes the Index +-- 8 Index only lives the scope of the loop +-- 9 Index is a constant in the loop +-- 10 The end of scope for the loop +-- 11 No exception handler. The exception is propogated +-- 12 to the calling subprogram. Looks for handler. + +Page 44 of 113 + + Ada Distilled + +by Richard Riehle + +If the exception is not handled locally, the RTE will unwind through the calling stack searching for a +handler. If none is found, the program will crash and burn. You might want to have a function with an +access parameter. This has potential side effects. Consider the following record definition, + +type Data is record + +Value : Integer := 0; +Description : String(1..20); + +end record; +type Ref is access all Data; + +-- 1 Define a record type with a name +-- 2 Initialize the values when possible +-- 3 Probably should be initialized +-- 4 Scope terminator for the record data +-- 5 Define a pointer to the record + +You could have a function, + +function Is_Zero (The_Data : access Data) return Boolean is +begin + +return The_Data.Value = 0; + +end Is_Zero; + +-- 1 Note access parameter +-- 2 Of course, by now you know this +-- 3 Return result of equality test +-- 4 Scope terminator for the function + +The Ada compiler will reject the following code, + +function Fix_It_A (The_Data : access Data) return Ref is + Fix_It_Data : Ref := new Data'(some initial values); +begin + +The_Data := Fix_It_Data; -- illegal, illegal, illegal +return The_Data; + +end Fix_It_A; + +-- 1 Access parameter and access result +-- 2 Declare some initialized access object +-- 3 Of course, by now you know this +-- 4 No assignment allowed to parameter value +-- 5 Will never get to this; will not compile +-- 6 Scope terminator for the function + +but the compiler will accept the following modification of a componenent of an access object, + +function Fix_It_B (The_Data : access Data) return Ref is + Fix_It_Data : Integer := 25; +begin + +The_Data.Value := Fix_It_Data; +return The_Data; + +end Fix_It_B; + +-- 1 Access parameter and access result +-- 2 Declare initialized Integer object +-- 3 +-- 4 Assignment allowed to component +-- 5 Yes. Returns updated value for The_Data +-- 6 Always include the name of the function + +This is one of Ada's rare weaknesses vis a vis C++. In C++ we can declare a function as const or a +parameter as const. This may be fixed in a future ISO Ada so the access parameter can be constant. + +One of the useful algorithmic capabilities of modern programming languages is recursion. For a recursive +solution, the subprogram must include a way to terminate before it runs out of memory. The following +academic example for a recursive function, is seldom a practical in real progamming applications. + +function Factorial (N : Natural ) + + return Positive is + +begin + +if N <= 1 then +return 1; + +else + +return N * Factorial (N - 1); + +end if; +end Factorial; + +-- 1 +-- 2 Must have a return type +-- 3 Start of algorithmic part +-- 4 Less than or equal to ... +-- 5 Lowest positive value +-- 6 Alternative path +-- 7 The recursive call; function calls itself +-- 8 Terminate if statement +-- 9 Scope of the recursive function + +Many sort routines, tree searching routines, and other algorithms use recursion. It is possible to do this in +Ada because every subprogram call is re-entrant. Each internal call of itself puts a result in a stack frame. +When the algorithm reaches a stopping point, based on the if statement, it unwinds itself from the stack +frame entries with a final result of the computation. The following program will work to test the Factorial +program, + +Page 45 of 113 + + Ada Distilled + +by Richard Riehle + +with Factorial; +with Ada.Integer_Text_IO; +with Ada.Text_IO; +use Ada; +procedure Test_Factorial is + Data : Natural := 0; +begin + Text_IO.Put("Enter Positive Integer: "); + +Integer_Text_IO.Get(Data); +Integer_Text_IO.Put(Factorial(Data)); + +end Test_Factorial; + +-- 1 Yes, you may with a subprogram +-- 2 I/O for Standard Integer +-- 3 Character and String I/O +-- 4 Make Ada visible; not a problem +-- 5 Specification with "is" +-- 6 In scope up to end of procedure +-- 7 You know what this means by now +-- 8 Display a prompt on the screen +-- 9 Get an integer from the keyboard +-- 10 Display an integer on the screen +-- 11 End of declarative region for procedure + +Note: Although this is the usual +example given in textbooks to +illustrate recursion, it is not always +the best way to accomplish +factorial computation. + +It is important to understand that recusion can result in a Storage_Error (see package Standard). Also, +intelligent use of Ada's visibility rules can often prevent accidental, infinite recursion. + +A function can be compiled by itself in the library. Even more interesting is that a function specification +can be compiled into the library by itself. When the specification is compiled it must be completed later +with an implementation. This is identical to the procedure example, Simple_2, in 6.1.2 above. + +6.3 Subprograms in A Package + +An Ada package specification may group a set of subprogram declarations. No implementation code is +permitted in the specification. The implementation will be in the package body. This is more fully +covered in Chapter 7, below. Here is a simple package specification with a corresponding body. First the +specification: + +package Kia_Ora is + procedure Kia_Menemene; + +function Menemene return Boolean; + +end Kia_Ora; + +-- 1 Hello in Maori,early language of New Zealand +-- 2 Be happy, in Maori +-- 3 Are you happy? +-- 4 end of pacakge specification + +Then a package body highlighting separate compilation: + +package body Kia_Ora is + procedure Kia_Menemene is separate; + function Menemene return Boolean is separate; +end Kia_Ora; + +-- 1 Now includes the word, body +-- 2 Defer actual implementation for the subprograms +-- 3 to separate compilation units. +-- 4 + +The separately compiled procedure could be coded: + +separate (Kia_Ora) +procedure Kia_Menemene is +begin + +-- some implementation code here + +end Kia_Menemene; + +-- 1 Note absence of semicolon +-- 2 Makes maintenance much easier in small chunks +-- 3 +-- 4 Any standard Ada algorithmic code here + +Page 46 of 113 + + + + Ada Distilled + +by Richard Riehle + +7. Package Design + +At the beginning of this book, we showed an example of an Ada package. Most Ada programs are +designed with packages. In fact, a single program is usually composed of many packages. A package is a +module for collecting related information and services. It can be thought of as a contract for services. +The user of that contract may be thought of as a client. In this sense, a client may us some of the services +but not want to use all of those services. Ada allows a client to indentify only those services needed, +through its visibility rules, even though all services might be in scope and potentially visible. + +The services are in the form of type definitions, data declarations, and subprograms. A well-designed +package will rarely have data declarations as part of the contract. Instead, references to data should be +through a call to some subprogram. + +7.1 A Simple Package + +We revise the specification for the earlier Messenger package. + +package Messenger is + +type Message is private; +function Null_Message return Message; +function Create (S : String) return Message; +function Get return Message; +procedure Put (M : in Message); +procedure Clear (M : in out Message); +function Text (M : Message) return String; +function Length (M : Message) return Natural; + +private + +type Message is record + +Data : String(1..200) := (others => ' '); +Len : Natural := 0; + +end record; +end Messenger; + +-- 1 An Ada Module +-- 2 A partial definition of message +-- 3 Gives a null message +-- 4 Make a message from a String +-- 5 Get message from keyboard +-- 6 Put Message to Screen +-- 7 Set message to null message +-- 8 The string portion of message +-- 9 How many of characters +-- 10 Begin private part of package +-- 11 Full definition of message +-- 12 Message content; initialized +-- 13 Message size; initialized +-- 14 End of message definition +-- 15 End of the specification + +Public Part + +Private + +Notice there is no algorithmic code in a package specification. Ada lets you declare all the subprograms in +the specification. The implementation is in another compilation unit called the package body but the +specification and body are both part of the same library unit. The specification is a contract with a client. +It tells what it will do, not how it will be done. Ada is forbids algorithmic code in the specification part. + +A client of package Messenger is only able to see lines 1 through 9 of the specification. The rest (lines 10 +through 14) is only in the specification to satisfy the requirements of the Ada compiler. We call lines 1 +through 9 the public part of the specification and lines 10 through 14, the private part. The private part of +an Ada package specification is somewhat analogous to a C++ class protected part. A child library unit +may have some visibility to private part just as C++ derived class has visibility to a protected part of its +parent class. We examine these visibility issues later. + +The package Messenger exports some services as subprograms. The algorithmic (procedural) part of these +subprograms must be coded someplace. Ada forbids algorithms in the package specification. Algorithms +must be coded in the package body. Subprogram declarations in the specification require a corresponding +implementation in the body. The package body depends on successful compilation of its fully conforming +package specification. The Ada compiler checks this dependency through compilation unit date and time +stamps. The package body is an integral part of the library unit. The package body never needs to with +the package specification because both are part of the same library unit. + +Page 47 of 113 + + Ada Distilled + +by Richard Riehle + +7.2 Package Body + +Not every package needs a package body. In practice, only packages that declare public subprograms need +a body. Now and then a package may require a body even if it does not export a subprogram. This would +be the exception rather than the rule. This exception to the rule is also rigorously managed by the +compiler. + + Here is a package body for Messenger. + +package body Messenger is + +function Create (S : String) return Message is +begin + +-- algorithm to create object of type Message +-- must have at least one return statement + +end Create; +function Get return Message is +begin + +-- algorithm to Get a message from some container or input device +-- must have at least one return statement + +; + +end Get +procedure Put (M : in Message) is +begin + +-- algorithm goes here + +end Put; +procedure Clear (M : in out Message) is +begin + +-- algorithm to clear the Message + +end Clear; +function Text (M : Message) return String is +begin + +-- algorithm, if necessary +-- must have at least one return statement + +end Text; +function Length (M : Message) return Natural is +begin + +-- algorithm to get length of Message Text +-- must have at least one return statement + +end Length; +end Messenger; + +-- 1 +-- 2 +-- 3 +-- 4 +-- 5 +-- 6 +-- 7 +-- 8 +-- 9 +-- 10 +-- 11 +-- 12 +-- 13 +-- 14 +-- 15 +-- 16 +-- 17 +-- 18 +-- 19 +-- 20 +-- 21 +-- 22 +-- 23 +-- 24 +-- 25 +-- 26 +-- 27 +-- 28 +-- 29 +-- 30 + +An acceptable variation on this body +would be to code each subprogram with +the reserved word separate. For +example, + +procedure Put + (M : in Message) is separate; + +This would cause a stub for a subunit to +be created in the library for the completed +code corresponding to procedure Put. +This technique is useful when one wants +to divide the implementation of a package +over a team of several people, or preserve +the confidentiality of a particular piece of +source code. + +Neither a client or child of package Messenger ever has visibility to the package body. We say that the +implementation (always in a package body) is encapsulated. + +7.3 More Simple Package Examples + +7.3.1 Monetary Conversion Package + +Here is another simple package specification. An implementation would convert currencies. + +package Conversions is + +type Money is delta 0.0001 digits 12; +type Yen is new Money; +type Dollars is new Money; +function Convert (Y : Yen; Rate : Money) return Dollars; +function Convert (D : Dollars; Rate : Money) return Yen; +Conversion_Error : exception; + +end Conversions; + +-- 1 +-- 2 a decimal fixed-point type +-- 3 derive from Money +-- 4 derive from Money +-- 5 declare a function specification +-- 6 declare a function specification +-- 7 declare an exception +-- 8 + +package body Conversions is + +function Convert (Y : Yen; Rate : Money) return Dollars is + +Result : Dollars := 0.0; + +-- 1 +-- 2 +-- 3 declare result of return type + +Page 48 of 113 + + Ada Distilled + +by Richard Riehle + +begin + +return Result; + +end Convert; +function Convert (D : Dollars; Rate : Money) return Yen is + +Result : Yen := 0.0; + +begin + +return Result; + +end Convert; +end Conversions; + +-- 4 stub out the function temporarily +-- 5 after algorithm to do conversion +-- 6 +-- 7 +-- 8 declare result of return type +-- 9 temporarily stub out the begin..end part +-- 10 after algorithm to do conversion +-- 11 +-- 12 + +The technique here is to stub out a function. Notice we must first declare a Result of the return type. Then +we can code the return statement in the begin..end part. A procedure can be stubbed out with the reserved +word, null. A function must have at least one return statement. This technique satisfies that requirement. + +7.3.2 Simple Statistics Package + +Here is another kind of package. This package provides a simple set of statistical services. + +package Statistics is + +type Data is array (Positive range <>) of Float; +function Mean (The_Data : Data) return Float; -- 3 Computes the statistical Mean +function Mode (The_Data : Data) return Float; -- 4 Computes the statistical Mode +function Max (The_Data : Data) return Float; +function Min (The_Data : Data) return Float; -- 6 Computes Minimum Value of array +function Variance (The_Data : Data) return Float; -- 7 Computes Statistical Variance +function StdDev (The_Data : Data) return Float; -- 8 Computes Standard Deviation + +-- 5 Computes Maximum Value of arrray + +end Statistics; + +-- 9 Package specification requires end + +-- 1 Specification declaration +-- 2 An unconstrained array. + +The following procedure is a client of the Statistics package. + +with Statistics; +with Ada.Float_Text_IO; +use Ada; +procedure Compute_Statistics is + Stat_Data : Statistics.Data(1..100); +begin + for Index in Stat_Data'Range + +loop + +Float_Text_IO.Get(Stat_Data(Index)); + +end loop; + +-- 1 Put Statistics library unit in scope +-- 2 Library unit for floating point I/O +-- 3 Makes Ada visible; discussed later +-- 4 A stand-alone procedure +-- 5 An array of float; note the constraint +-- 6 Starts the algorithmic part of procedure +-- 7 Specification of a for loop; more later +-- 8 Every loop must have the word loop +-- 9 Fill the array with data +-- 10 Every loop must have an end loop +-- 11 Call Statistics.Mean and output result + + Float_Text_IO.Put(Statistics.Mean(Stat_Data)); + Float_Text_IO.Put(Statistics.StdDev(Stat_Data)); -- 12 Call Statistics.StdDev and output result +end Compute_Statistics; + +-- 13 End of the procedure scope + +The with statement on Line 1 puts the resources of the Statistics package in scope. The Variance function +may be called by referencing Statistics.Variance. Line 2 puts the language-defined library unit, +Ada.Float_Text_IO in scope. Line 3 makes the parent of Float_Text_IO directly visible. Therefore, the +Get operation of Float_Text_IO on Line 9 is legal. Program declarations are between the is on Line 4 and +the begin on Line 6. On Line 5, the declaration is for data of the array type Statistics.Data. Since +Statistics.Data is declared with no actual range in the Statistics package, the programmer must specify +beginning and ending index values. Ada allows starting indexes other than zero. The defined index for an +array type may even include a range of negative values. + +The expression, Stat_Data'Range in the loop specification, indicates that the loop will traverse the entire +array, beginning with the first value through the last value. The loop index, Index, will start with the first +value in the Range and proceed to the end. The Get operation on Line 9 is defined in the package +Ada.Float_Text_IO. Because we have a use clause for Ada on Line 3, we may reference it as shown. +The same is true for the Put operations on Lines 11 and 12. We call the Mean and StdDev functions from +Statistics. These functions take a parameter of type Data and return a floating point value. + +Page 49 of 113 + + + + + + + Ada Distilled + +by Richard Riehle + +7.4 Simple Mathematics Packages + +Ada has a rich set of capabilities for numeric algorithms. One of the key packages is Ada.Numerics. This +package has some child packages. The most important are Ada.Numerics.Generic_Elementary_Functions, +Ada.Numerics.Float_Random, and Ada.Numerics.Discrete_Random. It also defines, in Annex G, a model +for strict and relaxed mode for floating point values. (Also see the discussion on attributes in this book.) + +7.4.1 Example without Numerics Library + +This example will compile and execute. However, it is better to use the language-defined libraries. + +with Ada.Text_IO; +with Ada.Float_Text_IO; +procedure Pi_Symbol is + +Pi : constant Float := 3.1415; +Radius : Float := 12.0; +Area : Float := 0.0; + +begin + +Area := Pi * Radius ** 2; +Ada.Float_Text_IO.Put(Area); + +end Pi_Symbol; + +7.4.2 Using Numerics Library + +10.1.2, A.10 +A.10.9/33 +6.3 + +-- 1 Put Text_IO library unit in scope; +-- 2 Predefined in Annex A +-- 3 Parameterless declaration; +-- 4 Should have used Ada.Numerics for this +-- 5 Ordinary Floating point initialized +-- 6 I somtimes initialize all variables; not required here +-- 7 Begins sequence of algorithmic statements; +-- 8 Pi is also pre-defined in Ada.Numerics +-- 9 Dot notation makes Put visible +-- 10 Scope terminator with name of unit + +6.3 + +A.10.6 +6.3 + +A better approach to declaring Pi and and using Ada for number crunching is to use the language-defined +numerics libraries. The following program illustrates some ideas from this set of libraries. + +with Ada.Text_IO; +with Ada.Float_Text_IO; +with Ada.Numerics.Generic_Elementary_Functions; +use Ada; +procedure Compute_Trigs is + +package Compute is new Ada. + +Numerics. +Generic_Elementary_Functions +(Float_Type => Float); + +Pi : Float := Ada.Numerics.Pi; +Radius : Float := 12.0; +Area : Float := 0.0; +SQRT_Result : Float := 0.0; + +begin + +Area := Pi* Radius ** 2; +Ada.Float_Text_IO.Put(Area); +Sqrt_Result := Compute.Sqrt(Area); + +end Compute_Trigs; + +8.4 + +-- 1 Put Text_IO library unit in scope; 10.1.2, A.10 +-- 2 A.10.9/33 +-- 3 A.5.1 +-- 4 Gives direct visibility to all of package Ada +-- 5 Parameterless declaration; +6.3 +-- 6 A.2 A new instance with a new name +-- 7 A.5 Root package for numerics +-- 8 A.5.1 Contains Trig and other functions +-- 9 A.1/25 for definition of type Float +-- 10 Pi is defined in Ada.Numerics +-- 11 Ordinary Floating point initialized +-- 12 Not everyone agrees that initialization is a good idea! +-- 13 For our Square root computation +-- 14 Begins sequence of algorithmic statements; 6.3 +-- 15 Compute the area of the circle +-- 16 dot notation makes Put visible +-- 17 Note use of Compute with dot notation +-- 18 Scope terminator with name of unit + +A.10.6 + +6.3 + +Note: Not everyone agrees with line 12, above. Some developers prefer not to initialize variables because they might contribute to unexpected errors during maintenance. + +Page 50 of 113 + + Ada Distilled + +by Richard Riehle + +7.4.3 Precompile Numerics Library + +Sometimes it is useful to precompile a generic library package for a frequently used data type. The math +library is one such package, especially if you are using the same floating point type over and over in your +application. + +Suppose you have declared the following example somewhere in your design, + +package Defined_Types is + type Real is digits 7 range -2.0 ** 32 .. 2.0 ** 32; +end Defined_Types; + +Ada allows you to precompile the generic elementary functions package for this type so it could be brought +into scope through a simple "with" clause. For example, + +with Ada.Numerics.Generic_Elementary_Functions; +with Defined_Types; +package Real_Functions is new Ada.Numerics. + Generic_Elementary_Functions(Defined_Types.Real); + +This fragment of code can actually be compiled as a new library unit that can be +referenced in a context clause through a with clause + +Now, you can access this package easily by "with Real_Functions" in a context clause. + +7.4.4 Mathematical Expressions + +The following examples demonstrate the use of the generic mathematics package with calls to some of the +functions in that package. Note that the default type for trigonometric functions is in Radians. + +with Defined_Types; +with Real_Functions; +with Generic_Utilities; +procedure Test_Math_Functions is + subtype Degree is Defined_Types.Real range 1.0..360.0; + subtype Radian is Defined_Types.Real range 0.0..2.0 * 3.14; + function To_Degrees is new Generic_Utilities.To_Degrees(Degree => Degree, Radian => Radian); + function To_Radians is new Generic_Utilities.To_Radians(Degree => Degree, Radian => Radian); + R1, R2, R3, R4 : Radian := 0.0; + D1 : Degree := 90.0; + D2 : Degree := 360.0; +begin + +R1 := To_Radians(D1); +R2 := Real_Functions.Sin(X => R1); +R2 := Real_Functions.Sin(X => R1, Cycle => D2); +R2 := Real_Functions.ArcSinh(X => R1); +R3 := Real_Functions.ArcCot(X => R1, Cycle => 40.0); +R4 := Real_Functions.Cos(X => R1, Cycle => D2); +R1 := To_Radians(D2); +R3 := Real_Functions.Tan(X => R1); +D2 := To_Degrees(R2); + +end Test_Math_Functions; + +-- 1 +-- 2 +-- 3 +-- 4 +-- 5 +-- 6 +-- 7 +-- 8 +-- 9 +-- 10 +-- 11 +-- 12 +-- 13 +-- 14 +-- 15 +-- 16 +-- 17 +-- 18 +-- 19 +-- 20 +-- 21 +-- 22 + +The package Generic_Utilities is not described in this book. It is one the program files in the ZIP file of +programs available with this book. For functions with no cycle parameter, assume a natural cycle of 2 Pi, +which means all calculations are done in radians. Lines 17 shows that you can provide other parameter +values for the cycle parameter. + +Page 51 of 113 + + Ada Distilled + +by Richard Riehle + +8. Child Library Units + +An Ada package may have a child. The child may be another package or a subprogram. A subprogram +may not have a child. Most of the time, design child library units as packages so they can be extended. A +child package specification is just like any other package specification. + +P + +package P is + type T1 is tagged private; + procedure Make(X : in out T1); + function OK (X : in T1) return Boolean; +private + type T1 is tagged record .. end record; +end P; + +Public part of child has +direct visibility to +. +public part of parent + +private part of child has +direct visibility to private +and public part of parent + +P.Q + +package P.Q is + type T2 is new T1 with private; + procedure Make(X : in out T2); + function OK (X : in T2) return Boolean; +private + type T2 is new T1 with record .... +end P.Q; + +client has no direct visibility +to any unit referred to in a +context clause + +with P.Q; +procedure P_Q_Client is + ... +begin + ... +end P_Q_Client; + +8.1 Kinds of Child Library Units + +package body P is + procedure Make(X : in out T1) is ... end Make; + function OK (X : in T1) return Boolean is ... end OK; +end P; + +private part of child has direct visibility to private and public part of parent + +package body P.Q is + procedure Make(X : in out T2) is ... end Make; + function OK (X : in T2) return Boolean is ... end OK; +end P.Q; + +There is direct upward visibility from a child to its parent. The +private part and body of a child can see the private and public +part of a parent or grandparent. No unit ever has direct +visibility to a package body. A client never has direct visibility +to any other unit. The client must use one of the visibility +mechanisms to get direct visibility. The direct visibility of child +units continues all through the parent child hierarchy. +Grandchildren bodies can see grandparent private parts. + +Child library units may be packages, functions, or procedures. A function or procedure may not have +additional children. Ada supports both public and private child library units. A child unit is public, by +default. The identifier of a public child may appear in any context clause anywhere in a system. A +private child may only appear in a context clause for a body (e.g., package body) when that body is for a +specification with the same root (parent, grandparent, etc.). It is also possible to have generic child library +units. However, children of generic units must also be generic. + +8.2 Visibility Rules + +Visibility Rules are a little tricky +but easy once you grok them. + +8.3 Root Packages + +Public part of child has direct visibility to public part of parent + +a) +b) Private part of child has direct visibility to private and public part of parent. +c) Body of child has direct visibility to private and public part of parent. +d) Grandchild units have direct visibility that corresponds to child units. +e) + +Private unit can only be with'ed by body of unit if both have same root package. + +Sometimes we want to design a root package that is the home node for a hierarchy or subsystem of other +library units. A root package can vary greatly in its form. Sometimes a root package contains nothing bu +a few exception declarations. Other times it is more complex, possibly with specialized type declarations. +the general rule is to keep the root package as simple as possible Here is one possible root package + +Page 52 of 113 + + Ada Distilled + +by Richard Riehle + +package Root is + +Bad_Bad_Bad : exception; +No_No_No : exception; +type Number is private; +function "+" (N : Number) return Number; +function "-" (N : Number) return Number; +function Set (To : Integer) return Number; +function Integer_Is(N : Number) return Integer; + +private + +type Number is range -2**31..2**31-1; + +end Root; + +-- 1 Declare a root package specification +-- 2 An exception declaration which will be +-- 3 visible throughout the entire hierarchy. +-- 4 A partial definition for a type +-- 5 Overloading equivalent to i++ +-- 6 Overloading equivalent to i-- +-- 7 Set number to a value +-- 8 Convert number to an Integer +-- 9 Begin the private part of package +-- 10 Full definition of the private type +-- 11 End of scope for package specification + +This package illustrates a possible design for a root package. This is simply a model, not a suggested +design approach. Here is a simple child package of the preceding Root package. + +package Root.Application is + +type Application_Type is private; -- partial definition of type +procedure Create (A : in out Application_Type); +function Is_Empty(A : Application_Type) return Boolean; +-- more operations(modifier and query methods) + +private + +type Application_Type is ... ; -- full definition of private type + +end Root.Application; + +8.4 Classwide Child Package + +Dot notation to signifies that Application is a +child unit of package Root. Public part of +Application has direct visibility to public part +of package Root. Private part of Application +has direct visibility to private and public part +of package Root. + +Earlier in this book we had a root package that resembled the following, + +package Abstract_Machinery is + +type Machine is abstract tagged private; +type Reference is access all Machine'Class; +function Create (Desc : String) + + return Machine'Class; +procedure Turn_On (M : in out Machine); +procedure Turn_Off (M : in out Machine); +function Is_On (M : in Machine) return Boolean; + +private + +type Machine is abstract tagged record + +Turned_On : Boolean := False; +Description : String(1..120); + +end record; + +end Abstract_Machinery; + +Visible +part of +contract. + +Not +visible +to client +of +contract. + +-- Package specification; requires body +-- Specifies the visible part of the data type; +-- Tagged type should have classwide access +-- Parameter for Create +-- Tagged return type should be classwide +-- procedure specification; modifier method +-- procedure specification; modifier method +-- function specification; query method +-- private part hidden from a client of contract +-- full definition of the publicly declared type +-- component of the type; OOP attribute +-- Constrained array component +-- scope terminator for the component +-- scope terminator for the specification + +This could be a base (root) package for machines that can be turned on and off. The data type, Machine, +is declared abstract. That means no instances of it are allowed. One could create some child packages for +this, combining child library units and inheritance. + +package Abstract_Machinery.Classwide is +type FIFO_Container(Size : Positive) + + is limited private; + +procedure Put(CM : in out FIFO_Container; + + Data : access Machine'Class); + +procedure Get(CM : in out FIFO_Container) + Data : access Machine'Class); + +private + +type Machine_Data is array + + (Positive range <>) of Reference; +type FIFO_Container(Size : Positive) is + +record + +Current : Natural; +Data : Machine_Data(1..Size); + +end record; + +end Abstract_Machinery.Classwide; + +-- 1 Package specification; requires body +-- 2 Parameterized type; make it any size +-- 3 No assignment ever allowed for limited view of a type +-- 4 Put into the next available Container location +-- 5 Any member of class, Machine +-- 6 Get, destructively, first item, from the Container +-- 7 Any member in derivation tree for Machine'class +-- 8 Start hidden part of the package; never visible to a client +-- 9 Define an unconstrained array +-- 10 The array is pointers to Machine'Class +-- 11 Full definition of parameterized type; parameter call a discriminant +-- 12 in the format of a record +-- 13 What is the current item; this is an index into the array +-- 14 Pointer array to Machine derivations +-- 15 Terminate scope of the record +-- 16 scope terminator for the child library unit specification + +Page 53 of 113 + + + + + + Ada Distilled + +by Richard Riehle + +The preceding classwide child package lets you put any object of type Machine'Class into a container. +This is quite a handy thing to be able to do. This is a heterogeneous container for different kinds of +machines. + +8.5 Private Child Library Unit + +Sometimes we want to promote the internals of a package to the level of a separate package. The +following example is a simplified contrivance that is intended to show how one might refactor a design into +better abstractions. We use the language feature called, private child units to accomplish this. + +Consider a package that has a simple specification where the public methods are implemented in package +body. The example package, shown below, does some simple message handling, but also stores and +retrieves each message from a fixed size array. A real application would be more designed to more robust +standards, but we keep this simple to illustrate our central point. + +8.5.3.1 Root Package for Application + +package Application is + type Assertion is access function return Boolean; -- 2 A pointer (access type) to a function + Precondition_Error + Postcondition_Error + Invariant_Error +end Application; + +Three exceptions, each of which will be +directly visible throughout the entire +hierarchy of child library units. + +-- 1 Declare a root package for application + +: exception; +: exception; +: exception; + +-- 3 +-- 4 +-- 5 +-- 6 + +This style of package should be self-evident by now. It is nothing more than an access type to a +parameterless function and few exceptions that can be raised throughout the parent-child hierarchy. + +8.5.3.2 Child of Application Root + +This is a child package of the root package in 8.1.2.1. It has direct visibility to all of the public features of +the parent package. The pre- and post-conditions are access parameters in the procedures. This is possible +because the functions are at the same accessibility level as the access type in package Application. + +package Application.Messenger is +type Message is tagged private; + + type Reference is access all Message'Class; + function Require_Not_Empty return Boolean; + function Ensure_Length_Adjusted return Boolean; + function Require_Not_Full return Boolean; + +procedure Make(M : in out Message; S : String); +procedure Get + + (M : out Message; + Precondition : Assertion := Require_Not_Empty'Access; + Postcondition : Assertion := Ensure_Length_Adjusted'Access); + +procedure Put + + (M : in Message; + + Precondition : Assertion := Require_Not_Full'Access; + Postcondition : Assertion := Ensure_Length_Adjusted'Access); + function Len (M : in Message) return Natural; +private + +type Message is tagged record +Text : String(1..120); +Length : Natural := 0; + + end record; +end Application.Messenger; + +-- 1 A child of package Application +-- 2 A simple tagged type; public view +-- 3 Always include a classwide access +-- 4 Pre-condition +-- 5 Post-condition +-- 6 Pre-condition +-- 7 +-- 7.1 +-- 8 +-- 9 Access to a subprogram +-- 10 Access to subprogram +-- 10.1 +-- 11 +-- 12 Access to subprogram +-- 13 Access to subprogram +-- 14 +-- 15 +-- 16 Full definition of tagged type +-- 17 +-- 18 +-- 19 +-- 20 + +Page 54 of 113 + + + + Ada Distilled + +by Richard Riehle + +8.5.3.3 A Private Child Package + +Important Ada Design Feature + +A private child library unit may only be referenced by a context clause in the body of a unit rooted at the +same level as the direct parent unit. In this case, Messenger is the parent of this private package. + +Note + +private package Application.Messenger.Storage is -- Note the word private and dot notation + +type Message_Container is private; +procedure Insert_At_End (Into : in out Message_Container; M : in Message'Class); + procedure Get_First_Message (From : in out Message_Container; M : out Message'Class); + private + +type Container is array(1..500) of Reference; +type Message_Container is + +record + +Data : Container; -- An array of 500 Message'Class access values + + Count : Natural := 0; + +end record; + +end Application.Messenger.Storage; + +8.5.3.4 The Body of Application.Messenger + +This private package +specification is a +child of Messenger +which is a child of +Application. + +-- 1 +-- 2 +-- 3 +-- 4 +-- 5 +-- 6 +-- 7 +-- 8 +-- 9 +-- 10 +-- 11 +-- 12 + +This exmple, by itself, is not to be taken too seriously. You will enjoy designing your own version when +you study this feature in more depth. It does compile and execute. However, we have not defined all the +algorithms and functionality that you might want for a robust application. + + (From => The_Storage_Container, M => M); -- Note use of named association + +if Require_Not_Empty then +Application.Messenger.Storage.Get_First_Message + + end Get; + function Len (M : in Message) return Natural is + begin + +end if; +if Ensure_Length_Adjusted then + null; -- This would usually contain actual code but we stubbed it out. +end if; + +with Application.Messenger.Storage; -- Context clause only permitted in body of sibling package +package body Application.Messenger is + The_Storage_Container : Application.Messenger.Storage. + Message_Container; + procedure Get (M : out Message; + Precondition : Assertion := Require_Not_Empty'Access; + Postcondition : Assertion := Ensure_Length_Adjusted'Access) is + begin + +-- 1 +-- 2 +-- 3 +-- 4 +-- 5 +-- 6 +-- 7 +-- 8 +-- 9 +-- 10 +-- 11 +-- 12 +-- 13 +-- 14 +-- 15 +-- 16 +-- 17 +-- 18 +-- 19 +-- 20 +-- 21 +-- 22 +-- 23 +-- 24 +-- 25 +-- 26 +-- 27 +-- 28 +-- 29 +-- 30 +-- 31 +-- 32 +-- 33 +Application.Messenger.Storage.Insert_At_End(Into => The_Storage_Container, M => M); -- 34 + + end Len; + procedure Make (M : in out Message; S : String) is + begin + if S'Length > M.Text'Length then + raise PreCondition_Error; + else + M.Text(1..S'Length) := S(S'Range); + end if; + M.Length := S'Length; + end Make; + procedure Put (M : in Message; + Precondition : Assertion := Require_Not_Full'Access; + Postcondition : Assertion := Ensure_Length_Adjusted'Access) is + begin + +return M.Length; + +Page 55 of 113 + + + + + + + + + + + + + + + Ada Distilled + +by Richard Riehle + + end Put; + function Require_Not_Empty return Boolean is + Result : Boolean := False; + begin + return Result; + end Require_Not_Empty; + function Ensure_Length_Adjusted return Boolean is + Result : Boolean := False; + begin + return Result; + end Ensure_Length_Adjusted; + function Require_Not_Full return Boolean is + Result : Boolean := False; + begin + return Result; + end Require_Not_Full; +end Application.Messenger; + +8.5.3.5 Body for The Private Child Unit + +-- 35 +-- 36 +-- 37 +-- 38 +-- 39 +-- 40 +-- 41 +-- 42 +-- 43 +-- 44 +-- 45 +-- 46 +-- 47 +-- 48 +-- 49 +-- 50 +-- 51 + +This example is all done except for the body of the private child unit. Notice that we have stubbed out +some of the implementation. This also compiles and executes. You will want to expand on the +functionality. You might even want to change the implementation of the container to something more +generic. + +with Ada.Unchecked_Deallocation; +package body Application.Messenger.Storage is + +procedure Free is new Ada.Unchecked_Deallocation + (Object => Message'Class, + + Name => Reference); + +procedure Get_First_Message (From : in out Message_Container; M : out Message'Class) is + +Work_Container : Container := From.Data; + + begin + +M := From.Data(1).all; +Free(From.Data(1)); +From.Data(1..From.Count ) := From.Data(2..From.Data'Last); + +end Get_First_Message; +procedure Insert_At_End (Into : in out Message_Container; M : in Message'Class) is +begin + +if Into.Count < Container'Length then +Into.Count := Into.Count + 1; + Into.Data(Into.Count) := new Message'(M); +else + raise PreCondition_Error; +end if; + + end Insert_At_End; +end Application.Messenger.Storage; + +8.6 Summary + +-- 1 +-- 2 +-- 3 +-- 4 +-- 5 +-- 6 +-- 7 +-- 8 +-- 9 +-- 10 +-- 11 +-- 12 +-- 13 +-- 14 +-- 15 +-- 16 +-- 17 +-- 18 +-- 19 +-- 20 +-- 21 +-- 22 + +Child library units are one of the most powerful features of the current Ada standard. When you have +practised with them long enough, you will find many ways to use them to factor your designs into smaller, +more compact, and more maintainable compilation units. + +Page 56 of 113 + + + + + + + + + + + + + Ada Distilled + +by Richard Riehle + +9. Object-Oriented Programming With Packages + +Ada includes support for extensible inheritance, polymorphism, and dynamic binding. These are three key +features of object-oriented programming (OOP). Ada enables this through the extensible tagged type. + +9.1 An Object-Oriented Type + +An Ada package is not a first-class object. You cannot +create instances of a package, unless it is a generic package. + +Consider this package containing a tagged type. Every instance of a tagged type contains an internal tag. +A tagged type may be extended with additional components. + +package Machinery is + +type Machine is tagged private; +type Reference is access all Machine’Class; +procedure Turn_On (M : in out Machine); +procedure Turn_Off (M : in out Machine); +function Is_On (M : Machine) return Boolean; + +private + +type Machine is tagged record + +Is_On : Boolean := False; + +end record; +end Machinery; + +9.2 A Possible Client of the Type + +A client of package Messenger might be set up as, + +-- 1 An Ada Module +-- 2 A tagged partial definition of message +-- 3 A classwide access type +-- 5 Turn on the machine +-- 6 Turn off the Machine +-- 7 Is the Machine turned on? +-- 8 Begin private part of package +-- 9 Full tagged definition of message +-- 10 Machine content; initialized +-- 11 End of machine definition +-- 12 End of the package specification + +with Messenger; +procedure Messenger_Processor ... end Messenger_Processor; + +-- 1 A context clause +-- 2 Three dots are not legal Ada + +The context clause, with Messenger, makes package Messenger and all its public services available, but +not directly visible, to Messenger_Processor. Public services can be made visible through a use clause, a +use type clause, renaming of the operations, or simple dot notation. + +9.3 Inheritance and Extension + +The Machinery package specification, with its tagged type, Machine, illustrates some important ideas in +Ada. A tagged type may be extended. Therefore, one could have a client package, Rotating_Machinery, + +with Machinery; +package Rotating_Machinery is + +type Rotational is new Machinery.Machine with private; +procedure Turn_On (M : in out Rotational); +procedure Turn_Off (M : in out Rotational); +procedure Set_Speed(M : in out Rotational; S : in Positive); + +private + +type Rotational is new Machinery.Machine + +with record + RPM : Natural := 0; +end record; + +end Rotating_Machinery; + +-- 1 +-- 2 +-- 3 Inherits Machine methods & data +-- 4 Overrides Machinery.Turn_On +-- 5 Overrides Machinery.Turn_Off +-- 6 New primitive operation +-- 7 +-- 8 +-- 9 +-- 10 New component in derivation +-- 11 +-- 12 + +The Rotating_Machinery package declares a data type that extends the content of the parent type. The +type, Rotational now contains two components. It has the one originally included in Machine plus the one +we added in the type derivation statement. + +Page 57 of 113 + + Ada Distilled + +by Richard Riehle + +9.4 Dynamic Polymorphism + +Essential for true object-oriented programming + +9.4.1 Dispatching with Primitive Operations + +The operations Turn_On, Turn_Off, Is_On, and Set_Speed are called primitive operations. They can be +called dynamically, depending on the tag of the object. The following procedure demonstrates one way to +do this. Note: the actual procedure to be called cannot be determined until run-time in this example. + +with Machinery, Rotating_Machinery; +with Ada.Integer_Text_IO; +procedure Dynamic_Binding_Example_1 is +Data : array (1..2) of Machinery.Reference := + (1 => new Machinery.Machine, + 2 => new Rotating_Machinery.Rotational); + +Index : Natural range 1..2 := 0; + +begin + +Ada.Integer_Text_IO.Get(Index); +Machinery.Turn_On(Data(Index).all); + +end Dynamic_Binding_Example_1; + +-- 1 Context clause +-- 2 Enables the input of the array index +-- 3 Specification for the example procedure +-- 4 Anonymous array of access objects +-- 5 Dynamically allocate new Object +-- 6 Dynamically allocate new Object +-- 7 Use this to index into the array +-- 8 +-- 9 Get the index for the next statement +-- 10 Dynamically call one of the Turn_On methods +-- 11 + +Dynamic Binding + +The next example does essentially what the previous example did. However, this example illustrates how +to code a classwide procedure. Once again, the version of Turn_On to choose is known only at run-time. + +with Machinery, Rotating_Machinery; +with Ada.Integer_Text_IO; +procedure Dynamic_Binding_Example_2 is + +Data : array (1..2) of Machinery.Reference := + + (1 => new Machinery.Machine, + 2 => new Rotating_Machinery.Rotational); + +Index : Natural range 0..2 := 0; +procedure Start(M : Machine’Class) is +begin + +Machinery.Turn_On(M); + +end Start; + +begin + +Ada.Integer_Text_IO.Get(Index); +Start(M => Data(Index).all)); +end Dynamic_Binding_Example_2; + +Dynamic Binding + +-- 1 With both packages; no use clause required +-- 2 Enables the input of the array index +-- 3 Specification for the example procedure +-- 4 Anonymous array of access objects +-- 5 Dynamically allocate new Object +-- 6 Dynamically allocate new Object +-- 7 Use this to index into the array +-- 8 Procedure with classwide parameter +-- 9 +-- 10 Turn_On is dynamically determined via the tag +-- 11 +-- 12 +-- 13 Get the index for the next statement +-- 14 Call the classwide procedure +-- 15 + +Here is still one more example that illustrates the usefulness of a function that returns a classwide value.. + +with Machinery, Rotating_Machinery; +with Ada.Integer_Text_IO; +procedure Dynamic_Binding_Example_3 is + +Index : Natural range 0..2 := 0; +function Get (The_Index : Natural) return Machine’Class is + +Data : array (1..2) of Machinery.Reference := + (1 => new Machinery.Machine, + + 2 => new Rotating_Machinery.Rotational); + +begin + +return Data(Index).all)); + +end Get; + +begin + +Ada.Integer_Text_IO.Get(Index); +declare + +The_Machine : Machine’Class := Get(Index); + +begin + +Turn_On(The_Machine); + +end; + +end Dynamic_Binding_Example_3; + +Dynamic Binding + +-- 1 No use clause is required for this example +-- 2 Enables the input of the array index +-- 3 Specification for the example procedure +-- 4 Use this to index into the array +-- 5 Procedure with classwide parameter +-- 6 Anoymous array of access objects +-- 7 Dynamically allocate new Object +-- 8 Dynamically allocate new Object +-- 9 +-- 10 return the data access by Data(Index) +-- 11 +-- 12 +-- 13 Get the index for the next statement +-- 14 Start a local declare block +-- 15 Declare and constrain classwide variable +-- 16 +-- 17 Call classwide procedure +-- 18 +-- 19 + +Page 58 of 113 + + + + Ada Distilled + +by Richard Riehle + +9.4.2 Dynamic Binding with Mixed Library Units + +Ada makes the dynamic binding (dispatching) feature really easy to use in your object-oriented +programming design. The following example shows a derivation class with a combined set of base +packages, child units, and client units. Notice that the base type is an abstract type. This means there can +be no instances of Machine, only of non-abstract derivations from Machine. Abstract type Machine is the +base type of Machine'Class. + +important + +-- 1 +package Machinery is +-- 2 + type Machine is abstract tagged private; +-- 3 + type Reference is access all Machine'Class; +-- 4 + type Machine_Set is + array (Positive range <>) of Reference; +-- 5 + procedure Turn_On (M : in out Machine) is abstract; -- 6 + procedure Turn_Off (M : in out Machine) is abstract; -- 7 +-- 8 + function Is_On (M : in Machine) return Boolean; +-- 9 +private +-- 10 + type Machine is abstract tagged record +-- 11 + Is_On : Boolean := False; +-- 12 + end record; +-- 13 +end Machinery; + +This package defines an abstrract +type (class root) along with two +abstract methods. Note the +important access type Reference that +can access any type derived from the +abstract type. + +Followed by a child library unit specification with an extended type; direct visibility; dot notation unnecessary. + +package Machinery.Rotating is + type Rotator_Type is new Machine with private; + type Rotator_Type _Reference is access + Rotator_Type_Type'Class; + procedure Turn_On (R : in out Rotator_Type); + procedure Turn_Off (R : in out Rotator_Type); + procedure Set_RPM (R : in out Rotator_Type; + Speed : Natural); + function RPM_Is (R : in Rotator_Type) + return Natural; +private + type Rotator_Type is new Machine with record + RPM : Natural := 0; + end record; +end Machinery.Rotating; + +-- 1 +-- 2 +-- 3 +-- 4 +-- 5 +-- 6 +-- 7 +-- 8 +-- 9 +-- 10 +-- 11 +-- 12 +-- 13 +-- 14 +-- 15 + +This is a child package of Machinery. +It contains a derivation from the +abstract type, Machine. Note that dot +notation is unnecessary because the +child unit has direct visibility to the +public part of the parent unit. + +We inherit the Is_On function and +override the Turn_On and Turn_Off +methods. + +Followed by a client library unit specification with an extended type; no direct visibility; dot notation is necessary. + +-- 1 +with Machinery; +-- 2 +package Oscillator is +-- 3 + type Oscillator_Type is +-- 4 + new Machinery.Machine with private; +-- 5 + type Reference is +-- 6 + access all Oscillator_Type'Class; +-- 7 + procedure Turn_On (OS : in out Oscillator_Type); + procedure Turn_Off (OS : in out Oscillator_Type); +-- 8 + procedure Set_Frequency(OS : in out Oscillator_Type; -- 9 +-- 10 + To : in Float); +-- 11 + function Frequency_Is(OS : Oscillator_Type) +-- 12 + return Float; +-- 13 +private +-- 14 + type Oscillator_Type is new Machinery.Machine +-- 15 + with record +-- 16 + Frequency : Float := 0.0; +-- 17 + end record; +-- 18 +end Oscillator; + +This is a client package of Machinery. +It contains a derivation from the +abstract type, Machine. Note that dot +notation is required because the client +unit has no direct visibility to the +public part of the parent unit. + +We inherit the Is_On function and +override the Turn_On and Turn_Off +methods. We also add another method +to set Frequency and one to get the +current value of Frequency + +Followed by a client procedure; client never has direct visibility; dot notation is necessary to achieve visibility. + +with Ada.Text_IO; +with Ada.Integer_Text_IO; +use Ada; +with Machinery.Rotating; +with Oscillator; +procedure Test_Machinery is + package Rotating renames Machinery.Rotating; + The_Machinery : Machinery.Machine_Set(1..2):= + +-- 1 pre-defined library unit +-- 2 pre-defined library unit +-- 3 parent of pre-defined library units +-- 4 context clause for library unit +-- 5 client unit +-- 6 +-- 7 shorter name with renames clause +-- 8 array instance, constrained + +Page 59 of 113 + + + Ada Distilled + +by Richard Riehle + + (1 => new Oscillator.Oscillator_Type, + 2 => new Rotating.Rotator_Type); + Input : Integer; +begin + loop + Text_IO.Put("Enter either a 1 or a 2 "); + Integer_Text_IO.Get(Input); + exit when Input not in 1..2; + Machinery.Turn_On(The_Machinery(Input).all); + end loop; +end Test_Machinery; + +-- 9 dynamic storage allocator +-- 10 dynamic storage allocator +-- 11 will be used as array index +-- 12 +-- 13 +-- 14 get the array index +-- 15 +-- 16 Notice membership test here +-- 17 dynamic binding +-- 18 +-- 19 + +This version of Test_Machinery demonstrates two kinds of dynamic binding. In one case the derived type, +Rotator_Type is declared in a child library unit. In the other, the derived type is declared in a client unit. +There are no visibility clauses in Test_Machinery for any of the Machinery'Class types. When we all +Machinery.Turn_On or Machinery.Turn_Off, on the abstract type, Machine, we dynamically call the +correct version of Turn_On or Turn_Off because of the tag of the actual parameter in the call. + +We could easily extend this design with more client packages. The dynamic dispatching model will +remain in operational regardless of how many library units are added. + +Dispatching works because of the tag. For each derivation of a tagged type the compiler creates a dispatch +table on the primitive operations for that type. The dispatch table is linked into the combined set of library +units so it is easily reachable from anywhere in the program. + +The tag binds each object directly to its dispatch table. Anytime a primitive operation is called, the tag +directs the call to the dispatch table which, in -turn, references the appropriate primitive operation. This +works well when you have the objects heterogenuously stored in a list, array, or table. This property is +called object persistence. Ada allows object persistence even in files. + +9.4.3 Parameter Lists With Multiple Tagged Types + +It is illegal to have a primitive operation with more than one definite tagged type in the parameter list. +However, a primitive operation may have one definite type and multiple classwide tagged types. The +following example demonstrates this. First we have the package specification. + +package Double_Tagged_Type is +type T1 is tagged private; + + type T1_Reference is access all T1'Class; + type T1_Class is array (Positive range <>) of T1_Reference; + type T2 is new T1 with private; + procedure Process(D1 : in out T1; D2 : in out T1'Class); + procedure Process(D2 : in out T2; D1 : in out T1'Class); + procedure Process_All(D1 : in out T1'Class; D2 : in out T1'Class); +private + +type T1 is tagged null record; + + type T2 is new T1 with null record; +end Double_Tagged_Type; + +-- 1 +-- 2 +-- 3 +-- 4 +-- 5 +-- 6 +-- 7 +-- 8 +-- 9 +-- 10 +-- 11 +-- 12 + +primitive operation +must be primitive +for only one type; +the other parameter +may be classwide + +line 8 is not actually +necessary; it is +simply here to +illustrate a point +about classwide +methods. + +This package contains a root type at Line 2 and a derivation at Line 5. The two procedures named Process +are overloaded and disambiguated because of the definite tagged type as a first parameter. Both versions +of Process are primitive methods (and will dispatch) for the named definite types. One of the types in each +parameter list must be classwide or this would not compile. Next we look at the package body. + +with Ada.Text_IO; +use Ada; +package body Double_Tagged_Type is + procedure Process (D1 : in out T1; D2 : in out T1'Class) is + begin + Text_IO.Put("D1 is concrete T1 and D2 is classwide" ); + end Process; + +-- 1 +-- 2 +-- 3 +-- 4 +-- 5 +-- 6 +-- 7 + +Call this with one definite +type and another that is +classwide. It does not +matter which comes first +since we have a primitive +for both versions. + +Page 60 of 113 + + + Ada Distilled + +by Richard Riehle + + procedure Process (D2 : in out T2; + + D1 : in out T1'Class) is + + begin + Text_IO.Put("D2 is concrete T1 and D1 is classwide" ); + end Process; + procedure Process_All (D1 : in out T1'Class; + D2 : in out T1'Class) is + begin + Process(D1, D2); + end Process_All; +end Double_Tagged_Type; + +-- 8 definite parameter +-- 9 classwide parameter +-- 10 +-- 11 +-- 12 +-- 13 classwide parameter +-- 14 classwide parameter +-- 15 +-- 16 +-- 17 +-- 18 + +We could easily have designed this without the Process_All procedure, but including it demonstrates the +power of a fully classwide subprogram. For example, it is often useful to have a function that returns a +classwide type. In this package, we might have wanted to create a function such as, + +function Get (From : T1_Class; Index : Integer) return + +T1'Class; + +Primitive method that +includes a classwide +formal parameter. + +The Get function would be espcially useful if we had a lot of derivations from T1 and a large array of such +objects. In the Double_Tagged_Type example, Get is unnecessary. Keep it in mind for future designs. +The following client of this package demonstrates how dynamic dispatching might take form. + +with Double_Tagged_Type; +with Ada.Text_IO; +with Ada.Integer_Text_IO; +use Ada; +procedure Test_Double_Tagged_Type is + Data : Double_Tagged_Type.T1_Class (1..6) := + (1 => new Double_Tagged_Type.T1, + 2 => new Double_Tagged_Type.T2, + 3 => new Double_Tagged_Type.T1, + 4 => new Double_Tagged_Type.T2, + 5 => new Double_Tagged_Type.T1, + 6 => new Double_Tagged_Type.T2); + Input_1, Input_2 : Integer := 0; +begin + loop + Text_IO.Put("Enter First Integer Value: "); + Integer_Text_IO.Get(Input_1); + exit when Input_1 not in 1..6; + Text_IO.New_Line; + Text_IO.Put("Enter First Integer Value: "); + Integer_Text_IO.Get(Input_2); + exit when Input_2 not in 1..6; + Double_Tagged_Type.Process_All(Data(Input_1).all, + Data(Input_2).all); + Text_IO.New_Line; + end loop; +end Test_Double_Tagged_Type; + +9.4.4 Dispatching on File Data + +array of access objects; +dyanamically allocated in +a ragged array + +-- 1 package defined above +-- 2 package from Ada Annex A +-- 3 Library instantiation +-- 4 OK occurrence of visibility clause +-- 5 +-- 6 +-- 7 +-- 8 +-- 9 +-- 10 dynamic allocation of instances +-- 11 +-- 12 +-- 13 for indices into the array +-- 14 +-- 15 +-- 16 prompt for keyboard input +-- 17 get index value for array +-- 18 early loop exit +-- 19 carriage-return/line-feed +-- 20 prompt for keyboard input +-- 21 get index value for array +-- 22 +-- 23 +-- 24 +-- 25 +-- 26 +-- 27 + +dynamic dispatching on method +Process_All; this could avoid the +Process_All call and call Process +directly; this will also +dynamically dispatch. + +If you have your Ada Language Reference Manual handy, look for a package called +Ada.Streams.Stream_IO in Annex A. This is a special input-output package that lets you store a tagged +type with its tag intact. You can design Stream_IO files that are sequential or using some direct access +method. Because the tag is stored in the file, each object is virtually bound to its set of operations. We +start with the following package of tagged types and their associated methods: + +package Machinery_For_Streams is + type Machine is abstract tagged private; + +-- 1 An Ada Module +-- 2 Tagged partial definition + +Page 61 of 113 + + Ada Distilled + +by Richard Riehle + + type Reference is access all Machine'Class; +-- 3 Classwide access type +-- 4 Array of access values + type Machine_Set is array(Positive range <>) + of Reference; +-- 5 +-- 6 Turn on the machine + procedure Turn_On (M : in out Machine) +-- 7 Turn off the Machine + procedure Turn_Off (M : in out Machine); +-- 8 Is the Machine turned on? + function Is_On (M : Machine) return Boolean; +-- 9 + type Machine_1 is new Machine with private; + function Create(S : String := "Machn_1") return Machine_1; -- 10 +-- 11 Turn on the machine_1 + procedure Turn_On (M1 : in out Machine_1); +-- 12 Turn off the Machine_1 + procedure Turn_Off (M1 : in out Machine_1); + type Machine_2 is new Machine with private; +-- 13 + function Create(S : String := "Machn_2") return Machine_2; -- 14 +-- 15 Turn on the machine_2 + procedure Turn_On (M2 : in out Machine_2); +-- 16 Turn off the Machine_2 + procedure Turn_Off (M2 : in out Machine_2); + type Machine_3 is new Machine_1 with private; +-- 17 + function Create(S : String := "Machn_3") return Machine_3; -- 18 +-- 19 Turn on the machine_3 + procedure Turn_On (M3 : in out Machine_3); +-- 20 Turn off the Machine_3 + procedure Turn_Off (M3 : in out Machine_3); + type Machine_4 is new Machine_3 with private; +-- 21 + function Create(S : String := "Machn_4") return Machine_4; -- 22 + procedure Turn_On (M4 : in out Machine_4); + procedure Turn_Off (M4 : in out Machine_4); + function Get (From : Machine_Set) + return Machine'Class; +private + type Machine is tagged + record + Identifier : String(1..7) := (others => ' '); + Is_On : Boolean := False; + end record; + type Machine_1 is new Machine with null record; + type Machine_2 is new Machine with null record; + type Machine_3 is new Machine_1 with null record; + type Machine_4 is new Machine_3 with null record; +end Machinery_For_Streams; + +-- 23 Turn on the machine_4 +-- 24 Turn off the Machine_4 +-- 25 +-- 26 +-- 27 Begin private part +-- 28 Full tagged definition +-- 29 +-- 30 Machine content +-- 31 Machine content; +-- 32 End of machine definition +-- 33 +-- 34 +-- 35 +-- 36 +-- 37 End of specification + +Now we can create two procdedures using Stream_IO to store the items of type Machine'Class in a file +along with their tags. This permits the program to dispatch automatically on each item as it is retrieved +from the file and called by the Turn_On operation. The first procedure will output the data with its tag to +the Stream_IO file. + +Allocate some data +in an array for the +stream file. + +with Ada.Streams.Stream_Io; +use Ada.Streams; +with Ada.Text_IO; +use Ada; +with Machinery_For_Streams; +procedure Stream_Output_For_Machine is + The_File : Stream_Io.File_Type; + Reference : Stream_Io.Stream_Access; + Data : Machinery_For_Streams.Machine_Set(1..9) + := (1 => new Machinery_For_Streams.Machine_1, + 2 => new Machinery_For_Streams.Machine_2, + 3 => new Machinery_For_Streams.Machine_3, + 4 => new Machinery_For_Streams.Machine_3, + 5 => new Machinery_For_Streams.Machine_2, + 6 => new Machinery_For_Streams.Machine_1, + 7 => new Machinery_For_Streams.Machine_4, + 8 => new Machinery_For_Streams.Machine_1, + 9 => new Machinery_For_Streams.Machine_4); + File_Name : String(1..80) := (others => ' '); + File_Name_Length : Natural; +begin + Text_IO.Put("Enter File Name: "); + Text_IO.Get_Line(File_Name, File_Name_Length); + Stream_Io.Create(File => The_File, + Mode => Stream_IO.Out_File, + Name => File_Name(1..File_Name_Length)); + Reference := Stream_Io.Stream(The_File); + +-- 1 Stream_IO from Annex A +-- 2 +-- 3 +-- 4 +-- 5 +-- 6 +-- 7 +-- 8 Access value +-- 9 +-- 10 Dynamic allocation of instances +-- 11 +-- 12 +-- 13 +-- 14 +-- 15 +-- 16 +-- 17 +-- 18 End of dynamic allocation +-- 19 External file name +-- 20 +-- 21 +-- 22 + +Create a Stream_IO output file. + +-- 23 +-- 24 +-- 25 +-- 26 +-- 27 + +Page 62 of 113 + + Ada Distilled + +by Richard Riehle + + for I in Data'Range + loop + Machinery_For_Streams. + Machine'Class'Output(Reference, Data(I).all); + end loop; + Stream_Io.Close(The_File); +end Stream_Output_For_Machine; + +Output data +from array to +stream file. + +-- 28 +-- 29 +-- 31 +-- 32 +-- 33 +-- 34 +-- 35 + +The preceding procedure will put tagged data in the output file. The next procedure will retrieve the data +and call, polymorphically, the Turn_On method. + +with Ada.Streams.Stream_Io; +use Ada.Streams; +with Ada.Text_IO; +use Ada; +with Machinery_For_Streams; +procedure Stream_Input_For_Machine is + The_File : Stream_Io.File_Type; + Reference : Stream_Io.Stream_Access; + File_Name : String(1..80) := (others => ' '); + File_Name_Length : Natural; +begin + Text_IO.Put("Enter File Name: "); + Text_IO.Get_Line(File_Name, File_Name_Length); + Stream_Io.Open(File => The_File, + Mode => Stream_IO.In_File, + Name => File_Name(1..File_Name_Length)); + Reference := Stream_Io.Stream(The_File); + while not Stream_Io.End_Of_File (The_File) + loop + declare + Data : Machinery_For_Streams.Machine'Class + := Machinery_For_Streams. + Machine'Class'Input(Reference); + begin + Machinery_For_Streams.Turn_On(Data); + end; + end loop; + Stream_Io.Close(The_File); +end Stream_Input_For_Machine; + +Get tagged +data from a +Stream file. + +-- 1 +-- 2 +-- 3 +-- 4 +-- 5 +-- 6 +-- 7 +-- 8 +-- 9 +-- 10 +-- 11 +-- 12 +-- 13 +-- 14 +-- 15 +-- 16 +-- 17 Sequential Stream File +-- 18 +-- 19 +-- 20 +-- 21 Data in a declare block +-- 22 so it can be dynamically +-- 23 initialized as classwide +-- 24 +-- 25 Dispatching Call +-- 26 +-- 27 +-- 28 +-- 29 + +This is a sequential Stream_IO file. It would be perfectly OK to create a random access file using some +kind of key processing algorithm such as a B-Tree. In this case, you would be able to create a true object- +oriented database system. People have used Ada for object-oriented database applications successfully. + +On Lines 21 through 23 we initialize the declaration Data when it is declared. A tagged object must +always be initialized when it is declared. Initialization may be static or dynamic. In this example, the +initialization is dynamic. This is analogous to an unconstrained array such as a type String, where the size +of the object is not known until run-time. The earlier example of a Get function also illustrates a +technique for dynanically constraining a tagged object declaration. + +Page 63 of 113 + + Ada Distilled + +by Richard Riehle + +10. Using Standard Libraries and Annexes + +The Ada language standard is published in two parts: Core language and Annexes. The Annexes, labled +A through H, include some standard libary units. One library unit in Annex A, package Standard, is +always in scope and always visible. Other libraries support special needs such as real-time system +development, platform-specific systems programming, distributed systems, and safety and security. Still +other units support input-output, string handling, and mathematical functions. + +The most commonly used and misused library unit is package Standard, where all the predefined types +(Boolean, Integer, Float, Character and String) are declared. Never use Standard numeric types for +production software. Annexes A through K constitute some implementations of the language, but never +define new syntax or semantics. Annex K defines the equivalent of intrinsic functions, called Attributes in +Ada, that enable portability of algorithmic design. + +10.1 Attributes for Portability (ALRM Annex K) + +The term, attribute, was used in Ada before it became a term in OOP. +It has a different meaning from that in the OOP communiity. + +Attributes enhance your ability to create flexibile, portable, and easy to read code. Many attributes behave +like built-in functions. The format for an attribute is a prefix of a type or object name along followed by +an apostrophe, followed by the attribute itself. Attributes may have parameters. The format is, + +Q'Attribute +Q'Attribute(parameter) + +where there is no parameter for the attribute +where there is a parameter for the attibute + +In an expression, an attribute might be coded as, + +The apostrophe is +pronounced, "tic." In +this example we would +say, Q tic Attribute + +X := Q'Attribute +X := Q'Attribute(parameter) +for X use Y'Attribute + +where there is no parameter for the attribute +where there is a parameter for the attibute +in a representation specification clause + +along with other kinds of attribute expressions such as conditional and declarative statements. +The prefix Q, shown above can be replaced by any of a number of Ada entities. When the attributes are +defined in Annex K, they are shown with a prefix that indicates what kind of prefix is required. The +possibilities are summarized in the following table. + +Legend for Attribute Prefixes + +P +X +S +E +T +R +A + +Subprogram +an object or varible name +type or subtype identifier +entry or exception +task +record (component is R.C') +array + +Examples of attributes are: + +Integer'Last +Float'Last +T1'Callable +Vector'Last +Vector'Length +Byte'Size +X'Valid + +The last value in the set for predefined type Integer +The last value for predefined type Float +Is task T1 still callable +Where Vector is an array, Last is largest index value +Where Vector is an array, Length is the length of the array +The number of bits in type Byte +Is instance named X a valid representation of its own type? + +Page 64 of 113 + + + + + Ada Distilled + +by Richard Riehle + +Each attribute is rigorously defined for its associated entity. As mentioned above, many attibutes make +floating point operations more precise. Other attributes allow one to generalize an algorithm so it can be +used for any type in a given set of types. Consider the following generic function, Next. + +generic + +type Element is (<>); + +function Next (Data : Element) return Element; + +function Next (Data : Element) return Element is +begin + +if Data = Element'Last then +return Element'First; + +else + +return Element'Succ(Data); + +end if; +end Next; + +-- 1 +-- 2 +-- 3 + +-- 1 +-- 2 +-- 3 +-- 4 +-- 5 +-- 6 +-- 7 +-- 8 + +Generic formal discrete type parameter + +Body of generic function; note the use +of attributes 'Last, 'First, and 'Succ +to generalize the algorithm. + +Notice how we are able to use attibutes in this algorithm to generalize the code. Element can be +instantiated with any discrete type. This is a powerful feature of Ada. It has been copied by other +languages, but never quite as well as originally designed in Ada. + +10.1.1 Classification of Attributes + +Some attributes are unique to specific types. Others are for machine/platform representation specifications. +Still others are specific to object-oriented programming. The following charts organize some common +attributes according to typical usage. + +Floating Point Attributes (where S is name of a floating point type) + +S'Adjacent +S'Ceiling +S'Compose +S'Copy_Sign +S'Denorm +S'Digits +S'Exponent +S'Floor + +S'Fraction +S'Leading_Part +S'Machine +S'Machine_Emax +S'Machine_Emin +S'Machine_Mantissa S'Model_Small +S'Machine_Overflows S'Remainder +S'Machine_Radix + +S'Machine_Radix +S'Model +S'Model_Emin +S'Model_Epsilon +S'Model_Mantissa + +S'Rounding + +S'Safe_First +S'Safe_Last +S'Scaling +S'Signed_Zeros +S'Unbiased_Rounding + +The floating-point attributes are particularly useful for numerically intensive applications such as scientific, +engineering, and analytical programs. In particular, note the presence of both Machine number and Model +number attributes. These can be used to create highly portable numerical software. Some of the above +attributes also apply to fixed-point types. + +The following attributes are available for any scalar type. As a reminder, scalar types include all integers, +enumeration types, floating-point types and fixed-point types. Scalar does not include records, arrays, +access types, private types, task types, or protected types. + +Scalar Type Attributes (where S is name of a scalar type) + +S'Base +S'First +S'Image +S'Last +S'Max +S'Min + +S'Pred +S'Range +S'Succ +S'Valid +S'Value + +S'Wide_Image +S'Wide_Value +S'Wide_Width +S'Width + +Page 65 of 113 + + + Ada Distilled + +by Richard Riehle + +Of special interest in this list are the attributes, S'Base and S'Valid. The 'Base attribute encompasses the +range supported for every unconstrained object of the type. It enables the designer to create functions that +ensure there will be no constraint_error due to overflow of intermediate range overflow. The Valid +attribute is used to test the result of an expression before using that result in another expression. It is also +useful in avoiding exceptions when using features such as unchecked_conversion. + +Array Type/Object Attributes (where A is name of a array type or array object) + +A'First +A'First(N) +A'Last + +A'Last(N) +A'Range + +A'Range(N) +A'Length +A'Length(N) + +A'Constrained +A'Component_Size + +Array attributes are among the most useful in developing portable Ada code. The are especially good to +use in generic reusable components designed as unconstrained array types. + +Object-Oriented Progamming Attributes (Using legend from beginning of this section) + +S'Class +S'Output +X'Tag + +S'Class'Input +S'Class'Read +S'Class'Write + +S'Input +S'Read +S'Write + +S'Class'Output +S'Tag + +Actually, all the attributes in the preceding box in which the class does not appear are used for Stream +operations. We include them together because they are most often used in conjunction with an object- +oriented programming design. + +Other Attributes (where prefix is as shown in the legend box at the beginning of this section) + +P'Access +S'Bit_Order +E'Count +T'Identity +D'Partition_ID +S'Size +T'Terminated +P'Version + +X'Access +P'Body_Version +S'Definite +R.C'First_Bit +S'Pos +X'Size +S'Val +X'Unchecked_Access + +S'Aft +T'Callable +S'Delta +R.C'Last_Bit +R.C'Position +S'Small +T'Storage_Size + +X'Alignment +E'Caller +E'Identity +S'Modulus +S'Scale +S'Storage_Pool +S'Storage_Size + +The above attributes cover everything from tasks to exceptions. As with other attributes, these can be +used to make a design more portable. We have described the attributes declared in Annex K of the Ada +Language Reference Manual. A compiler publisher is permitted to add attributes for their specific +compiler. For example, the GNAT compiler has a special attribute, Unrestricted_Access, which +corresponds to Unchecked_Access for subprogram access objects. + +10.2 String Examples + +String handling is a simple idea that often becomes complicated in some programming environments. In +particular, C, C++, and COBOL have made string handling more difficult than it needs to be. Ada is +especially handy for string manipulation. Not only is an Ada string easy to declare and process, the +predefined libraries (in Annex A) support most of the operations one might want to do on strings. Ada +supports three kinds of strings: fixed strings, bounded strings, and unbounded strings. One kind of string +may easily be converted into another kind of string. Fixed strings are those defined in package Standard. + +The following program illustrates several additional features of the language. Notice the syntax for +declaring a constant. On line 3, if the string variable is declared with a range constraint, the initializing +string must have exactly the same number of characters. On line 4, if there is no range constraint, the index + +Page 66 of 113 + + Ada Distilled + +by Richard Riehle + +of the first character is 1 and the index of the last character is whatever the character count might be, in this +case 9. Line 15 “slides” a string slice from one string into a slice in another string using the assignment +operator and parenthetical notation to designate the source and target slices. + +with Ada.Text_IO; +procedure Bon_Jour is + +Hello : String (1..5) := “Salut”; +Howdy : String := “Howdy Joe”; +Bon_Jour : constant String := “Bon Jour”; + +begin + +Ada.Text_IO.Put(Hello); +Ada.Text_IO.Set_Col(20); +Ada.Text_IO.Put_Line(Hello); +Ada.Text_IO.Put(Howdy); +Ada.Text_IO.Set_Col (20); +Ada.Text_IO.Put(Howdy); +Ada.Text_IO.New_Line(2); +Ada.Text_IO.Put_Line(Bon_Jour); +Howdy(7..9) := Bon_Jour(1..3); +Ada.Text_IO.Put_Line (Howdy); + +end Bon_Jour; + +Ada Language Reference Manual +10.1.2, A.10 +6.3 +4.1, A.1/37 + +-- 1 Put Ada.Text_IO library unit in scope; +-- 2 Parameterless declaration; +-- 3 Number of characters must match range; +-- 4 Compiler determines constraint from string; 2.6, 3.3.1/13 +3.3.1/5-6 +-- 5 A true constant; cannot be altered; +6.3 +-- 6 Begins sequence of algorithmic statements; +A.10.6 +-- 7 Put a string with no carriage return; +-- 8 On same line, position cursor at column 20; A.10.5 +-- 9 Put a string with a carriage return / line feed; A.10.7 +-- 10 Put a string with no carriage return; +A.10.7 +-- 11 Set the cursor to column 20 / line feed; +A.10.5 +-- 12 Put a string with no carriage return / line feed; A.10.7 +-- 13 Position cursor to a new line; double space; A.10.5 +A.10.7 +-- 14 Put a constant to the screen with CR/LF; +4.1.2 +-- 15 Slide (assign) one string slice into another; +A.10.7 +-- 16 Put the modified string with CR/LF; +6.3 +-- 17 Note the label for the enclosing procedure; + +There are better alternatives for String handling in a set of packages in Annex A.4 Here is a simple +example of one of the packages. This is easier than string slicing and other low-level code. + +10.2.1 Using the Fixed Strings Package + +ALRM A.4.4.4 package Ada.Strings.Fixed + +with Ada.Text_IO; +with Ada.Strings.Fixed; +use Ada; +procedure Ni_Hao_Ma is +Greeting : String(1..80); +Farewell : String(1..120); + +begin + +-- 1 Put Ada.Text_IO library unit in scope; +-- 2 A language defined string package +-- 3 Makes all of package Ada visible +-- 4 Hello in Mandarin Chinese +-- 5 80 character string; String defined in package Standard ALRM A.1 +-- 6 120 character string +-- 7 Start sequence of statements + +10.1.2, A.10 +A.4.1, A.4.3 + +6.3 + +Ada.Strings.Fixed.Move(Greeting, Farewell); -- 8 Move shorter string to longer string; may also move longer to shorter +-- 9 End of procedure scope. + +end Ni_Hao_Ma; + +10.2.2 Bounded Strings + +ALRM A.4.4.4 package Ada.Strings.Bounded + +It is also possible to do operations on Bounded and Unbounded_Strings. Bounded strings are those with +a fixed size at compilation time through a generic instantiation. + +10.2.3 Unbounded Strings + +ALRM A.4.4.4 package Ada.Strings.UnBounded + +Unbounded strings are those which can be of any size, mixed size, etc. Many compilers will do +automatic garbage collection of unbounded strings. If you want to try these two features of the language, +they are defined in Annex A.4 of the Ada Language Reference Manual. + +Consider the following program that lets you concatenate data to an unbounded string, convert that string +to a standard fixed string, and then print it out to the screen. + +This is useful when you have a need for an input buffer of unknown size. + +with Ada.Strings.Unbounded; +with Ada.Text_IO; +use Ada; use Strings; +procedure Unbounded_String_Demonstration is + +Input : Character := ' '; +Output : String (1..80) := (others => ' '); + +Unbounded strings are usually +implemented with automatic +garbage collection. This saves +storage but any kind of +automatic garbage collection is +time non-determinate. + +-- 1 +-- 2 +-- 3 +-- 4 +-- 5 Element of the buffer +-- 6 Fixed length output string + +Page 67 of 113 + + + + + Ada Distilled + +by Richard Riehle + +Buffer : Unbounded.Unbounded_String; +Length : Natural; + +begin + +loop + +Text_IO.Put("Enter a character: "); +Text_IO.Get(Input); +exit when Input = '~'; +Unbounded.Append(Source => Buffer, New_Item => Input); + +end loop; +Length := Unbounded.Length(Buffer); +Output(1..Length) := Unbounded.To_String(Buffer); +Text_IO.Put_Line(Output(1..Length)); + +end Unbounded_String_Demonstration; + +-- 7 An unbounded buffer of input +-- 8 Size of the buffer +-- 9 +-- 10 +-- 11 +-- 12 Get the character +-- 13 Exit when tilde is entered +-- 14 Add the character to the buffer +-- 15 +-- 16 How big is the buffer? +-- 17 Copy the buffer to a String +-- 18 Output the entire string +-- 19 + +10.2.4 Other String Operations + +There are many other facilities for string handling in Ada. We show here an example from another useful +library, package Ada.Characters. Here is a little package that converts lower case letters to upper case. + +with Ada.Text_IO; +with Ada.Characters.Handling; +use Ada; +procedure Arirang is + Data : String := "arirang"; +begin + Text_IO.Put(Characters.Handling.To_Upper(Data)); +end Arirang; + +-- 1 Put Ada.Text_IO library unit in scope; 10.1.2, A.10 +-- 2 Character Handling Operations +-- 3 Makes package Ada visible +-- 4 Famous Korean love song +-- 5 initialized lower case character string +-- 6 Start sequence of statements +-- 7 Convert output to upper case characters and print it +-- 8 End of procedure scope. + +A.3.2 + +6.3 + +10.3 Converting Strings to Other Types + +Sometimes it is necessary to represent a string value in some other format. Other times we need to convert +some other type to a string representation. One could easily write a small generic subprogram to +accomplish this. Also, Ada provides an unchecked conversion capability. Unchecked features are seldom +used since they circumvent the fundamental philosophy of Ada: every construct should be, by default, +safe. + +10.3.1 Converting a String to an Scalar Type + +Note the many variations possible + +The following procedure demonstrates many of the features of the language for converting a string to an integer, a +string to a floating point, a string to an unsigned number, and a string to an enumerated value. + +with Ada.Text_IO; +with Ada.Integer_Text_IO; +with Ada.Float_Text_IO; +use Ada; -- It is generally OK to use a visibility clause for package Ada +procedure String_To_Scalar_Demonstration is + type Spectrum is (Red, Orange, Yellow, Green, Blue, Indigo, Violet); + type Unsigned is mod 2**8; -- Reminder: mod defines an unsigned integer type + Num : Integer := 0; + FNum : Float := 0.0; + Color : Spectrum := Blue; + MNum : Unsigned := 0; + Text : String(1..10); + Text_Integer : String := "451"; + Text_Float : String := "360.0"; + Text_Color : String := "Orange"; + Text_Unsigned : String := "42"; + Integer_Last : Natural; + Float_Last : Natural; + Spectrum_Last : Natural; + Modular_Last : Natural; + package SIO is new Text_IO.Enumeration_IO(Enum => Spectrum); + +Good use of named +association + +Instantiate IO +packages for each +data type that needs +conversion to or from +a string. + +-- 1 +-- 2 +-- 3 +-- 4 +-- 5 +-- 6 +-- 7 +-- 8 +-- 9 +-- 10 +-- 11 +-- 12 +-- 13 +-- 14 +-- 15 +-- 16 +-- 17 +-- 18 +-- 19 +-- 20 +-- 21 + +Page 68 of 113 + + + + + + + + + + + + + Ada Distilled + +by Richard Riehle + +Preamble stuff so reader can +see how the program +corresponds to reality + +-- 22 + package MIO is new Text_IO.Modular_IO (Num => Unsigned); +-- 23 + package IIO is new Text_IO.Integer_IO (Num => Integer); +-- 24 + package FIO is new Text_IO.Float_IO (Num => Float); +-- 25 +begin +-- 26 + Text_IO.Put_Line("The String Values are: "); +-- 27 + Text_IO.Put("Orange for Enumerated Type "); +-- 28 + Text_IO.Put_Line("451 for Integer Type "); +-- 29 + Text_IO.Put("360.0 for Float Type "); +-- 30 + Text_IO.Put_Line("42 for Unsigned Type "); +-- 31 + Text_IO.New_Line; + -- Example 1; using the Value attribute +-- 32 +-- 33 + Text_IO.New_Line; +-- 34 + Text_IO.Put_Line(" >>> Example 1; Using 'Value Attribute <<< "); +-- 35 + Color := Spectrum'Value(Text_Color); + Num := Integer'Value(Text_Integer); -- See Annex K for meaning of 'Value +-- 36 +-- 37 + FNum := Float'Value(Text_Float); +-- 38 + MNum := Unsigned'Value(Text_Unsigned); + SIO.Put(Color); Text_IO.New_Line; -- I/O for Spectrum data type +-- 39 + IIO.Put(Num); Text_IO.New_Line; -- I/O for Integer data type +-- 40 + FIO.Put(Fnum); Text_IO.New_Line; -- I/O for Float data type +-- 41 + MIO.Put(MNum); Text_IO.New_Line; -- I/O for Modular data type +-- 42 +-- 43 + Text_IO.New_Line; + -- Example 2; using the procedures of pre-instantiated packages +-- 44 +-- 45 + Text_IO.Put_Line(" >>>> Example 2; using pre-instantiated packages << " ); + Integer_Text_IO.Get(From => Text_Integer, Item => Num,Last => Integer_Last); -- 46 +-- 47 + Float_Text_IO.Get(From => Text_Float, Item => FNum,Last => Float_Last); +-- 48 + Integer_Text_IO.Put(Num); Text_IO.New_Line; +-- 49 + Float_Text_IO.Put (FNum, Fore => 3, Aft => 3, Exp => 0); +-- 50 + Text_IO.New_Line(2); + -- Example 3; using your own instantiated packages +-- 51 +-- 52 + Text_IO.Put_Line(" >>>> Example 3; Using own instantiations <<<< "); +-- 53 + Text_IO.New_Line; +-- 54 + SIO.Get(From => Text_Color, Item => Color, Last => Spectrum_Last); +-- 55 + MIO.Get(From => Text_Unsigned, Item => MNum, Last => Modular_Last); +-- 56 + IIO.Get(From => Text_Integer, Item => Num, Last => Integer_Last); +-- 57 + FIO.Get(From => Text_Float, Item => FNum, Last => Float_Last); + -- Now Write the Results to the Screen +-- 58 +-- 59 + SIO.Put(Item => Color); Text_IO.New_Line; +-- 60 + IIO.Put(Item => Num); Text_IO.New_Line; +-- 61 + FIO.Put(Item => FNum, Fore => 3, Aft => 3, Exp => 0); +-- 62 + Text_IO.New_Line; +-- 63 + MIO.Put(Item => MNum); +-- 64 + Text_IO.New_Line(2); +-- 65 + Text_IO.Put_Line(" **** End of String_To_Scalar_Demonstration **** "); +-- 66 +end String_To_Scalar_Demonstration; + +Named association + +Named association + +10.3.2 Converting a Scalar to a String + +Note the many variations possible + +This program is the opposite of the one in 10.2.2, above. We can convert almost any kind of scalar value +to a string. The package, Ada.Text_IO contains nested generic packages that make it easy to convert any +kind of number to a string. The programmer may also use the X'Image attribute (See ALRM Annex K) +from an internal (machine based) representation to a string. + +with Ada.Text_IO, Ada.Integer_Text_IO, Ada.Float_Text_IO; +use Ada; +procedure Scalar_To_String_Demonstration is + type Spectrum is (Red, Orange, Yellow, Green, Blue, Indigo, Violet); + type Unsigned is mod 2**8; + Num : Integer := 451; + FNum : Float := 360.0; + Color : Spectrum := Blue; + MNum : Unsigned := 42; + Text : String(1..10); + package SIO is new Text_IO.Enumeration_IO(Enum => Spectrum); + package MIO is new Text_IO.Modular_IO (Num => Unsigned); + package IIO is new Text_IO.Integer_IO (Num => Integer); + package FIO is new Text_IO.Float_IO (Num => Float); +begin + Text_IO.Put_Line(" Example 1; Using 'Image Attribute "); + +-- 1 +-- 2 May safely use Ada +-- 3 Convert a string to a scalar object +-- 4 Enumerated type +-- 5 Unsigned modular type +-- 6 Combustion point of paper in farenheit +-- 7 Don't go off on a tangent +-- 8 Hmmmm. "You don't look bluish." +-- 9 Life, the Universe, and Everything +-- 10 +-- 11 Instantiate IO for enumerated type +-- 12 Instantiate IO for modular type +-- 13 Instantiate IO for predefined Integer +-- 14 Instantiate IO for predefined Float +-- 15 +-- 17 Example 1; using the image attribute + +Page 69 of 113 + + Ada Distilled + +by Richard Riehle + + Text_IO.Put_Line(Spectrum'Image(Color)); + Text_IO.Put_Line(Unsigned'Image(MNum)); + Text_IO.Put_Line(Integer'Image(Num)); + Text_IO.Put_Line(Float'Image(FNum)); + Text_IO.New_Line; + Text_IO.Put_Line(" Example 2; using pre-instantiated packages " ); + Integer_Text_IO.Put(Num); Text_IO.New_Line; + Float_Text_IO.Put (FNum, Fore => 3, Aft => 3, Exp => 0); + Text_IO.New_Line(2); + Text_IO.Put_Line(" Example 3; Using own instantiations "); + SIO.Put(Color); Text_IO.New_Line; + MIO.Put(MNum); Text_IO.New_Line; + IIO.Put(Num); Text_IO.New_Line; + FIO.Put(FNum, Fore => 3, Aft => 3, Exp => 0); + Text_IO.New_Line(2); + -- Example 4; convert to text and then print + Text_IO.Put_Line("Example 4; Convert to text, then print "); + SIO.Put(To => Text, Item => Color); + Text_IO.Put_Line(Text); + MIO.Put(To => Text, Item => MNum); + Text_IO.Put_Line(Text); + IIO.Put(To => Text, Item => Num); + Text_IO.Put_Line(Text); + FIO.Put(To => Text, Item => FNum, Aft => 3, Exp => 0); + Text_IO.Put_Line(Text); + Text_IO.New_Line; + Text_IO.Put_Line("End of Image_Demonstration "); +end Scalar_To_String_Demonstration; + +Output using the 'Image attributes from +Annex K. Leading space for positive +values. Leading sign for negative values. + +-- 18 +-- 19 +-- 20 +-- 21 +-- 22 +-- 24 Example 2; pre-instantiated packages +-- 25 +-- 26 Named association for parameters +-- 27 Example 3; own instantiated packages +-- 29 +-- 30 Two statements on single line +-- 31 +-- 32 +-- 33 Named association for parameters +-- 34 +-- 35 +-- 36 +-- 37 Named association for parameters +-- 38 +-- 39 +-- 40 +-- 41 +-- 42 +-- 43 +-- 44 +-- 45 +-- 46 +-- 47 + +Convert each value to a String +and then print it. This is built-in +to Ada.Text_IO. Don't write +your own version of this. + +10.4 Wide Strings +Both Ada and Java are designed to support international (Unicode) character sets. Ada calls this wide- +strings. We will add some examples of wide string processing in a future edition of Ada Distilled. + +Page 70 of 113 + + Ada Distilled + +by Richard Riehle + +11. Exception Management + +Ada 83 was one of the first languages to include exception management. Nearly all modern programming +languages now have this feature. Exceptions are an essential feature of typed-languages that support +encapsulation. Think of an exception handler as a kind of software circuit-breaker. Just a real circuit- +breaker prevents your house from catching fire, the software circuit-breaker can prevent your program +from aborting at uncontrollable points during execution. + +Ada 95 has four predefined exceptions and allows the programmer to declare exceptions specific to the +problem being solved. Predefined exceptions from package Standard (Annex A.1) are: + +Constraint_Error, Storage_Error, Program_Error, Tasking_Error + +Input/output errors in package IO_Exceptions (Annex A.13) are, + +Status_Error, Mode_Error, Name_Error, Use_Error, Device_Error, +End_Error, Data_Error, Layout_Error + +Other Annex packages define other kinds of exceptions. Exceptions appear in library packages from +various software repositories, including the many freeware packages available from Internet sources. + +11.1 Handling an Exception (ALRM 11.4) + +An exception handler must appear in a begin...end sequence. Therfore it might appear as, + +function Ohm (Volt, Amp : Float) return Float is + +Result : Float := 0.0; + +begin + +Result := Volt / Amp; + + return Result; +exception + +when Constraint_Error => + +Text_IO.Put_Line(“Divide by Zero”); +raise; + +end Ohm; + +6.3 + +6.3 + +-- 1 Parameterized function declaration; +-- 2 Initialized local variable +-- 3 Begins algorithmic statements; +-- 4 Simple division; cannot divide by zero +-- 5 +-- 6 If we try to divide by zero, land here. +-- 7 Raised on divide-by-zero; handle it here. +-- 8 Display the error on the console +-- 9 Re-raises the exception after handling it. +-- 10 Scope terminator with name of unit + +6.3 + +Reminder: + +Every Ada program +body can be viewed +in terms of the Ada +comb even if one +tooth of the comb is +not shown. + +It is better not to return an invalid value from a function so it is useful to raise an exception. Sometimes +you want a begin ... exception ... end sequence in-line in other code. To call the function Ohm from a +procedure, we would want another exception handler. Since the handler re-raised the same exception (on +line 8), we need another handler in the calling subprogram. + +with Ada.Exceptions; with Ohm; +with Ada.Text_IO; use Ada; +procedure Electric (Amp, Volt : in Float; + Resistance : out Float) is + +function MSG (X :.Exceptions.Exception_Occurrence) + +return String +renames Exceptions.Exception_Message; + +begin + +Resistance := Ohm(Amp => Amp, Volt => Volt); + +exception + +when Electric_Error: + Constraint_Error => +Text_IO.Put_Line(MSG(Electric_Error)); +Exceptions.Reraise_Occurrence(Electric_Error); + +end Electric; + +6.3 + +-- 1 Chapter 11.4.1 ALRM; also, see the end of this chapter +-- 2 OK for use clause on package Ada +-- 3 In mode parameters +-- 4 Out mode parameter; +-- 5 Profile for Exception_Message function +-- 6 Return type for Exception_Message +-- 7 Rename it to three character function name +-- 8 Begins sequence of algorithmic statements; +-- 9 Simple division operation; cannot divide by zero +-- 10 If we try to divide by zero, land here. +-- 11 data type is Ada.Exceptions.Exception_Occurrence +-- 12 This error is raised on divide-by-zero; handle it here. +-- 13 See lines 5-7; renamed Exception_Message function +-- 14 Procedure for re-raising the exception by occurrence name +-- 15 Scope terminator with name of unit + +6.3 + +6.3 + +Page 71 of 113 + + Ada Distilled + +by Richard Riehle + +11.2 Declaring your Own Exceptions + +Ada allows user-defined exceptions. These can be declared and raised by the designer. + +with Ada.Exceptions; use Ada; +package Exception_Manager is +Overflow : exception; +Underflow : exception; +Divide_By_Zero : exception; +type Exception_Store is tagged limited private; +type Reference is access all Exception_Store’Class; +procedure Save ... + procedure Log ... + +ellipses are not part of Ada + +procedure Display ... + +private + +type Exception_Set is array (1..100) + +of Exceptions.Exception_Occurrence_Access; + +type Exception_Store is tagged + +record + +Current_Exception : Natural := 0; +Exception_Set; + +end record; +end Exception_Manager; + +with Exception_Manager; +package Application_With_Exception is +type Application_Type is private; +procedure Start (Data : in out Application_Type); +procedure Restart (Data : in out Application_Type); +procedure Stop (Data : in out Application_Type); +procedure Cleanup (Data : in out Application_Type); +procedure Finalization (Data : in out Application_Type); +Application_Exception : exception; + +private + type Application_Type is ... -- requires full definition of type +end Application_With_Exception; + +-- 1 Chapter 11.4.1 ALRM +-- 2 A typical exception/error management package +-- 3 Own named exception; User-defined exception +-- 4 Ada exception is not a first class object +-- 5 This could be handy for some applications +-- 6 A place to store exception occurrences +-- 7 In case you need to reference this in another way +-- 8 Saves an exception to Exception_Store +-- 9 Logs an exception +-- 10 Displays and exception +-- 11 Useful to have more operations before this +-- 12 Array of access values to Exception_Occurrence +-- 13 Exception_Occurrence_Access is an access type +-- 14 A record containing an array of exceptions +-- 15 +-- 16 And index over the Exception_Set +-- 17 Instance of type from Lines 12-13 +-- 18 +-- 19 Package scope terminator + +-- 1 Put Exception_Manager package in scope +-- 2 +-- 3 Private here is partial definition of type +-- 4 Create and initialize the application +-- 5 If there is an exception, you may need to restart +-- 6 Stop the application; may be able to restart +-- 7 When there is an error, call this procedure +-- 8 Not be confused with Ada.Finalization +-- 9 Your locally defined exception for this package +-- 10 Nothing is public from here forward +-- 11 Full definition of the private type +-- 12 End of the specification unit. Needs a body. + +In the Application_With_Exception package, any one of the subprograms defined might raise an +Application_Exception or some other kind of exception. Since we have not used any of the resources of +Exception_Manager, it would be better to defer its context clause (put it in scope) in the package body. + +with Exception_Manager; +package body Application_With_Exception is + -- Implementation code for the package body +end Application_With_Exception; + +-- 1 Localize the context clause to package body +-- 2 +-- 3 +-- 4 + +11.3 Raising Exceptions + +Exceptions should indicate a strange event that cannot be handled with the usual coding conventions. Ada +95 includes an attribute, X'Valid, to help the developer avoid exceptions on scalar types. Consider this +program that uses X'Valid. + +First an exception should be visible for the user. + +Given: the following visible declaration: + Compound_Data_Error : exception; + +procedure Test_The_Valid_Attribute is + type Real is digits 7; + type Number is range 0..32_767; + type Compound is + +record + + Weight : Real := 42.0; + Height : Number; + +-- 1 +-- 2 +-- 3 +-- 4 +-- 5 +-- 6 +-- 7 + +Page 72 of 113 + +Scalar types declared within the record +definition. X'Valid will not work on a +record but can be used on scalar +components. + + + Ada Distilled + +by Richard Riehle + + Width : Number; +end record; + + Data : Compound := (80.0, 64, 97); +begin + if Data.Weight'Valid then + +null; + + elsif Data.Height'Valid then + +null; + + elsif Data.Width'Valid then + +null; + + else + +raise Compound_Data_Error; + + end if; +end Test_The_Valid_Attribute; + +-- 8 +-- 9 +-- 10 Record initilialized with aggregate +-- 11 +-- 12 Test the Weight to see if it is valid +-- 13 Usually some sequence of statements +-- 14 Test the Height to see if it is valid +-- 15 Usually some sequence of statements +-- 16 Test the Widht to see if it is valid +-- 17 Usually some sequence of statements +-- 18 An else part is usually a good idea +-- 19 Failed all around; raise an exception +-- 20 +-- 21 + +Not all Ada designers agree with the above example. It is your responsibility to decide whether this +appropriate in designing your software. The important consideration is that you may define and raise +exceptions when you determine they are necessary. Object-oriented programming has a strong bias toward +encapsulation. When encapsulation is strong, an exception may be the appropriate technique to notify a +client that something has gone wrong within the software object they are using. + +11.4 Package Ada.Exceptions + +Ada 95 only + +The following language-defined can be useful for some kinds of applications. + +package Ada.Exceptions is -- This is an Ada language defined package + +type Exception_Id is private; +Null_Id : constant Exception_Id; +function Exception_Name(Id : Exception_Id) return String; +type Exception_Occurrence is limited private; +type Exception_Occurrence_Access is access all Exception_Occurrence; +Null_Occurrence : constant Exception_Occurrence; +procedure Raise_Exception(E : in Exception_Id; Message : in String := ""); +function Exception_Message(X : Exception_Occurrence) return String; +procedure Reraise_Occurrence(X : in Exception_Occurrence); +function Exception_Identity(X : Exception_Occurrence) return Exception_Id; -- 11 Exception_ID from line 2 +function Exception_Name(X : Exception_Occurrence) return String; + +-- 1 ALRM 11.4.1 +-- 2 predefined assignment +-- 3 this is called a deferred constant +-- 4 Associate a string with an exception +-- 5 no assignment operation +-- 6 useful for a limited private type +-- 7 Deferred constant; no assignment +-- 8 Exception_ID from line 2 +-- 9 String value of exception message +-- 10 After handling, raise it again + +-- Same as Exception_Name(Exception_Identity(X)). + +function Exception_Information(X : Exception_Occurrence) return String; +procedure Save_Occurrence(Target : out Exception_Occurrence; + Source : in Exception_Occurrence); +function Save_Occurrence(Source : Exception_Occurrence) + return Exception_Occurrence_Access; + +private + +... -- not specified by the language + +end Ada.Exceptions; + +-- 12 String value of Exception_Occurrence +-- 13 +-- 14 Some compilers give lots of info here +-- 15 A lot like a copy or assignment +-- 16 +-- 17 Create an access object +-- 18 +-- 19 +-- 20 +-- 21 + +One can design with this package so objects of type Exception_Occurrence are stored in a volatile data +structure (list, array, etc) and held for later processing. Any exception can be converted into a text format +and stored as text in a log file. This feature is useful for non-stop systems that require handling exceptions +but cannot stop executing for each processing anomaly. Even though standard Ada exceptions are not +first-class objects, the availability of Exception_ID and Exception_Occurrence gives the designer an +opportunity to promote them to first-class objects. This enables the efficiency associated with ordinary +Ada exceptions with the less efficient option of designing them as objects. + +Page 73 of 113 + + + + + Ada Distilled + +by Richard Riehle + +12. Generic Components + +12.1 Generic Subprograms + +In Ada, an algorithm or object can be designed as type-independent, and can be put in the library as a +generic reusable component. There are huge libraries of generic Ada components already in place such as +the Public Ada Library. Here are examples of simple generic subprograms. The first example is a +generalization of the Next function shown earlier. First we are required to define the generic specification. + +generic + +type Item is (<>); -- Any discrete type +function Next (Value : Item ) return Item; + +-- 1 Reserved word for defining templates +-- 2 Generic formal Parameter (GFP) +-- 3 Specification for generic subprogram + +We would not be allowed to code a generic specification with an is such as, + +generic + +type Item is (<>); + +Illegal + +function Next (Value : Item ) return Item is + ... +end Next; + +-- 1 As in line 1, above +-- 2 As in line 2, above +-- 3 Illegal; Specification required +-- 4 body of function +-- 5 before implementation + +because any generic subprogram must be first specified as a specification. The specification may actually +be compiled or may be declared in the specification of a package. + +Then we code the actual algorithm. Notice that the algorithm does not change at all for the earlier version +of function Next, even though we may now use it for any discrete data type. + +function Next (Value : Item) return Item is +begin + +if Item’Succ(Value) = Item’Last then + +return Item’First; + +else + +return Item’Succ(Value); + +end if; +end Next; + +-- 1 Item is a generic formal parameter +-- 2 No local declarations for this function +-- 3 A good use of attribute; see ALRM K/104 +-- 4 ALRM 6.3 +-- 5 ALRM 5.3 +-- 6 Note two returns; may not be good idea +-- 7 ALRM 5.3 +-- 8 Always include the function identifier + +This function can be instantiated for any discrete data type. Given the following types, write a few little +procedures to cycle through the types, + +type Month is (January, Februrary, March, April, May, June, July, August, September, October, November, December); +type Color is (Red, Orange, Yellow, Green, Blue, Indigo, Violet) ; -- our friend, Roy G. Biv. +type Day is (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday); +type Priority is (Very_Low, Low, Sorta_Medium, Medium, Getting_Higher, High, Very_High, The_Very_Top); + +The next generic subprogram is the famous, but simple Swap procedure. Recall that every non-limited +type including a private type has the predefined operators, =, /=, and operation := (assignment). Therefore, +we can instantiate the Swap procedure with any non-limited type in Ada. That is, a private generic formal +parameter, as defined on line 2 below, can be associated with any non-limited type + +generic + +type Element (<>) is private; + +procedure Swap (Left, Right : in out Element) ; + +-- 1 Start the generic formal parameters +-- 2 Unconstrained generic parameter +-- 3 Usually compiled separately + +First we code the the specification then the actual implementation. Notice the algorithm in the +implementation works for any non-limited data type. + +Page 74 of 113 + + Ada Distilled + +by Richard Riehle + +procedure Swap (Left, Right : in out Element) is + +Temp : Element := Left; + +begin + +Left := Right; +Right := Temp; + +end Swap; + +-- 1 Compile separately in its own body file +-- 2 Must be constrained in declaration +-- 3 +-- 4 First part of exchange +-- 5 Second part of exchange +-- 6 + +An algorithm does not get much easier than the Swap procedure. What should be clear from seeing +generic Swap is that you can use this technique to generalize hundreds of other algorithms on your own +projects. You can also use this technique to easily share algorithmic code with your colleagues. + +Sometimes it is useful to collect generic subprogram specifications some common properties in one +package. For example, using those already described, + +package Utilities is + +generic + +-- A handy package at the project-specific level + +type Item is private; + +-- A constrained generic formal parameter + +procedure Swap(L, R : in out Item); + +generic + +type Item is (<>); + +function Next (Data : Item) return Item; + +generic + +type Item is (<>); + +-- A discrete type generic formal parameter + +-- A discrete type generic formal parameter + +function Prev (Data : Item) return Item; +-- more generic subprograms as appropriate to your particular project needs + +end Utilities; + +Tip: +You can create +this kind of +utility package +for algorithms +unique to your +team's project. + +The Utilities package can be used to collect common generic algorithms. This set of small reusable +components is useful for creating larger components; i.e., build new generics from existing generics. + +12.2 Other Generic Formal Parameters + +A generic formal type parameter is possible for any type; e.g., access types, derived types, array types, and +even limited types. For limited types, the designer must include a corresponding set of generic formal +operations. Even for other types, generic formal operations are often useful. Consider this private type. + +generic + +type Item is private; -- Predefined assignment and equality +with function ">" (L, R : Item ) return Boolean; +with function "<" (L, R : Item) return Boolean; + +package Doubly_Linked_Ring_1 is + +-- Specification of a Doubly_Linked_Ring data strructure + +end Doubly_Linked_Ring_1; + +-- Start the generic formal parameters +-- Generic formal type parameter +-- Generic formal ">" operator +-- Generic formal "<" operator +-- Generic reusable data structure +-- +-- + +In the example for the Doubly_Linked_Ring_1, we know that implementation requires some operations +beyond simple test for equality. The only operator predefined for a private type is test for equality. +Consequently, we may include parameters for other operators. These "own code" operators are instantiated +by the client of the package. Before looking at the instantiation of this example, study the following +example that is preferred by many designers of resuable generic data structure components. + +generic + +-- 1 start a generic package specification +-- 2 Predefined assignment and equality +type Item is private; +-- 3 general access type formal parameter +type Item_Reference is access all Item; +-- 4 formal parameter for own code "=" +with function Is_Equal (L, R : Item ) return Boolean; +-- 5 formal parameter for own code "<" +with function Is_Less_Than (L, R : Item) return Boolean; +with function Is_Greater_Than (L, R : Item) return Boolean; -- 6 formal paramter for own code ">" + +Many Ada designers +prefer to use function +names instead of +function operators for +generic formal +subprogram +parameters. + +package Doubly_Linked_Ring_2 is +type Ring is limited private; + +-- 7 Generic reusable data structure +-- 8 limited private type for container + +Page 75 of 113 + + Ada Distilled + +by Richard Riehle + +-- Specification of a Doubly_Linked_Ring data strructure + +end Doubly_Linked_Ring_2; + +-- 9 more operations on limited type +-- 10 end of package specification + +Although test for equality is predefined for a private type, the test is on the binary value of the data not on +its selected components. If the actual parameter is a record or constrained array, a pure binary comparison +may not give the intended result. Instead, by associating a generic actual operation with a generic formal +operation, the client of the generic package can ensure the structure behaves according to a given record +key definition. Also, by including an access type for the generic formal private type, the client may have +lists of lists, trees of queues, lists of rings, etc. This example instantiates the Doubly_Linked_Ring_2. + +with Doubly_Linked_Ring_2 ; +procedure Test_Doubly_Linked_Ring_2 is + +type Stock is record + +Stock_Key : Positive; +Description : String (1..20); + +The Key is only one field of the record. + +end record; +type Stock_Reference is access all Stock; +function Is_Equal (L, R : Stock) return Boolean is +begin + +return L.Key = R.Key; + +Compare only the record Key, not the whole record. + +end Is_Equal; +function ">" ... -- Overload ">" Implement using the same rules as in function Is_Equal, above +function "<" ... +package Stockkeeper is new Doubly_Linked_Ring_2( Item => Stock, + + Item_Reference => Stock_Reference, + Is_Equal => Is_Equal, + Is_Less_Than => "<", + Is_Greater_Than => ">"); + +The Ring : Stockkeeper.Ring; +The_Data : Stock; + +begin + +-- Insert and remove stuff from the Ring + +end Test_Doubly_Linked_Ring_2; + +Notice the use of +named association; +good for readability +and documentation + +Sometimes it is convenient to combine a set of generic formal parameters into a signature package. A +signature package can be reused over and over to instantiate many different kinds of other generic +packages. A signature package will often have nothing in it except the generic parameters. It must be +instantiated before it can be used. This is an advanced topic. Here is one small oversimplified example, +derived and extended into a fully coded program, adapted from the Ada 95 Language Rationale. + +package Mapping_Example is -- Begin the enclosing package specification + generic + type Mapping_Type is private; + type Key is limited private; + type Value is limited private; + with procedure Add (M : in out Mapping_Type; K : in Key; V : in Value); + with procedure Remove (M : in out Mapping_Type; K : in Key; V : in Value); + with procedure Apply (M : in out Mapping_Type; K : in Key; V : in Value); + package Mapping is end Mapping; + -- Now declare the specification for the generic procedure in the same package + generic + with package Mapping_Operations is new Mapping (<>); + use Mapping_Operations; + -- This is a generic formal package parameter instead of a generic formal subprogram -- 13 + procedure Do_Something(M : in out Mapping_Type; K : in Key; V : in Value); + +Note the generic +formal parameters +for the signature +package, Mapping. +The package +contains no other +operations. This is +legal and handy + +-- 1 +-- 2 +-- 3 +-- 4 +-- 5 +-- 6 +-- 7 +-- 8 +-- 9 Signature package +-- 9.1 +-- 10 +-- 11 See line 9, above +-- 12 Use clause OK here + +-- 14 Generic procedure +-- 15 + + end Mapping_Example; -- End of the enclosing package specification + +Page 76 of 113 + + Ada Distilled + +by Richard Riehle + +Lines 2 through 9 define the generic formal signature that will become our generic formal pacakage +parameter for the Do_Something procedure. This model has no specification and therefore will not have a +body. It is typical of a generic formal signature to be a set of parameters for later instantiation. The code +on Line 11 is the syntax for a generic formal package parameter. The parenthetical box (<>) may have the +formal parameters associated with actual parameters if any are visible at this point. + +The code beginning on Line 13 is a generic procedure declaration. By making it a simple procedure with +its own formal parameters we keep this example simple. The package body for Mapping_Example will +simply implement the procedure Do_Something. + +package body Mapping_Example is + procedure Do_Something(M : in out Mapping_Type; + +K : in Key; +V : in Value) is + + begin -- Do_Something + Mapping_Operations.Add(M, K, V); + end Do_Something; +end Mapping_Example; + +-- 1 Implementation of the +-- 2 generic formal program +-- 3 +-- 4 +-- 5 +-- 6 +-- 7 +-- 8 + +The comment the begin statement on Line 5 to emphasizes that it belongs to Do_Something. The call on +Line 6 is to the Add procedure in the generic formal parameter list for Mapping_Operations. Dot notation +makes clear the referencing of the formal parameter name, not the “is new” name. Here is an example of +how to instantiate the units in Mapping_Example: + +with Mapping_Example; +procedure Test_Mapping_Example is + Map_Key : Integer := 0; + Map_Data : Character := 'A'; + Map_Value : Integer := Map_Key; + procedure Add (M : in out Character; K : Integer; V : Integer) is + +begin + +null; -- Stubbed out; usually is the algorithmic part of the code + + end Add; + procedure Remove (M : in out Character; K : Integer; V : Integer) is + +begin + +null; -- Stubbed out; usually is the algorithmic part of the code + + end Remove; + procedure Apply (M : in out Character; K : Integer; V : Integer) is + +begin + +null; -- Stubbed out; usually is the algorithmic part of the code + + end Apply; + + package Character_Mapping is new Mapping_Example.Mapping + + (Mapping_Type => Character, + Key => Integer, + Value => Integer, + Add => Add, + Remove => Remove, + Apply => Apply); + + procedure Do_Something_To_Map + + is new Mapping_Example.Do_Something + (Mapping_Operations => Character_Mapping); +begin + Do_Something_To_Map(M => Map_Data, +K => Map_Key, +V => Map_Value); + +end Test_Mapping_Example; + +-- 1 +-- 2 +-- 3 +-- 4 +-- 5 +-- 6 +-- 7 +-- 8 +-- 9 +-- 10 +-- 11 +-- 12 +-- 13 +-- 14 +-- 15 +-- 16 +-- 17 + +-- 18 + +-- 19 + +-- 20 +-- 21 +-- 22 +-- 23 +-- 24 +-- 25 + +-- 26 + +-- 27 +-- 28 +-- 29 +-- 30 +-- 31 +-- 32 +-- 33 + +Page 77 of 113 + +Implementation +of procedures +intended to be +used as generic +formal +subprogram +parameters. + +Instantiation of +the signature +package using +generic actual +parameters. + +Instantiation of +generic package +associating the +generic formal +package +parameter with +generic actual +from lines 19-25. + + + + + + + + + + Ada Distilled + +by Richard Riehle + +12.3 Longer Generic Code Example + +Just as you can create simple generic subprograms, as shown above, you can also generalize entire +packages. This book has some examples of how to do this. Here is an example of a generic container +package which corresponds to some of the the generic packages you will see when programming with Ada. + +This package is a managed FIFO Queue_Manager which includes an iterator. A managed data structure +is one which includes some kind of automatic garbage collection. An iterator is a mechanism by which +you may non-destructively visit every node of a data structure. There are two fundamental kinds of +iterators, active and passive. A passive iterator is somewhat safer than an active iterator. Also, a passive +iterator requires less work from the client. We show a package with an active iterator. + +with Ada.Finalization; +use Ada; +generic + type Element is tagged private; -- A more robust design might derive Element + + -- from Ada.Finalization.Controlled + + with function Is_Valid(Data : Element) return Boolean; +package Queue_Manager_1 is + +type List is limited private; +type List_Reference is access all List; +type List_Item is new Element with private; +type Item_Reference is access all List_Item'Class; + +-- A classwide access type permitting a heterogenuous queue + +procedure Clear (L : in out List); +procedure Insert_At_Head (L : in out List; I : in List_Item'Class); +procedure Insert_At_Head (L : access List; I : access List_Item'Class); + +-- A more complete design would include added options for the Insert operation + +procedure Copy (Source : in List; Target : in out List); +function Remove_From_Tail (L : access List) return List_Item'Class; + +-- A more complete design would include added options for the Remove operation + +-- + +function "=" (L, R : List) return Boolean; +function Node_Count (L : access List) return Natural; +function Is_Empty (L : access List) return Boolean; + =============== Define the Active Iterator ===================== +type Iterator is private; +procedure Initialize_Iterator(This : in out Iterator; The_List : access List); +function Next(This : in Iterator) return Iterator; +function Get (This : in Iterator) return List_Item'Class; +function Get (This : in Iterator) return Item_Reference; +function Is_Done(This : in Iterator) return Boolean; + +Iterator_Error : exception; + +private + +use Ada.Finalization; +type List_Node; +type Link is access all List_Node; +type Iterator is new Link; +type List_Item is new Element with null record; +type List_Node is new Controlled with -- Derived from a controlled type + +record + +Data : Item_Reference; +Next : Link; +Prev : Link; + +end record; + +type List is new Limited_Controlled with -- Derived from limited controlled type + +record + +Count : Natural := 0; +Head : Link; +Tail : Link; +Current : Link; + +-- 1 +-- 2 Make package Ada visible +-- 3 +-- 4 +-- 5 +-- 6 +-- 7 +-- 8 No assignment possible +-- 9 We can assign access values +-- 10 Inherits from Element +-- 11 Classwide access type +-- 12 +-- 13 Empties the list +-- 14 Self-documenting Method +-- 15 +-- 16 +-- 17 +-- 18 Self-documenting Method +-- 19 +-- 20 Overload Equality Test +-- 21 Self-documenting Query +-- 22 Self-documenting Query +-- 23 +-- 24 +-- 25 +-- 26 +-- 27 +-- 28 +-- 29 +-- 30 +-- 31 +-- 32 +-- 33 Use clause OK here +-- 34 Incomplete type definition +-- 35 Reference to List_Node +-- 36 Derive Iterator from Link +-- 37 Empty tagged record +-- 38 Define a List_Node +-- 39 +-- 40 Item is a pointer +-- 41 List_Node pointer +-- 42 List_Node pointer +-- 43 +-- 44 +-- 45 +-- 46 +-- 47 List_Node pointer +-- 48 List_Node pointer +-- 49 List_Node pointer + +Page 78 of 113 + + Ada Distilled + +by Richard Riehle + +end record; + +procedure Finalize(One_Node : in out List_Node); +procedure Finalize(The_List : in out List); + +end Queue_Manager_1; + +-- 50 +-- 51 reclaim Node storage +-- 52 reclaim List storage +-- 53 + +An active iterator would require the client to write a loop which successively calls the Next function +followed by a Get function. An active iterator is not quite as safe as a passive iterator, but it can be an +effective building block for contructing passive iterators. Since the list is potentially heterogenuous, the +Get returns a classwide type. This can be used in conjuction with dispatching operations. Here is an +annotated package body for the above specification. This is a long set of source code but it should be +useful to the student because of its near completeness. It also serves as a model for creating other data +structures. This package body was compiled using the GNAT Ada compiler. + +package +body + +with Text_IO; +with Ada.Exceptions; +with Unchecked_Deallocation; +package body Queue_Manager_1 is + +-- This instantiation enables destruction of unreferenced allocated storage + + procedure Free_Node is new Unchecked_Deallocation + (Object => List_Node, + Name => Link); + +-- This instantiation enables destruction of unreferenced Data items + procedure Free_Item is new Unchecked_Deallocation + (Object => List_Item'Class, + Name => Item_Reference); + +-- We override Ada.Finalizaion for a single node + + procedure Finalize(One_Node : in out List_Node) is + begin + +Free_Item (One_Node.Data); + Free_Node (One_Node.Next); + end Finalize; + +-- When the list goes out of scope, this is called to clean up the storage + + procedure Finalize(The_List : in out List) is + begin + +-- Use the Iterator to traverse the list and call Free_Item; add this code yourself + + Free_Node (The_List.Current); + Free_Node (The_List.Tail); + Free_Node (The_List.Head); + end Finalize; + +-- The name says what it does. Note the allocation of a temp. Finalization will +-- occur to ensure there is no left over storage. + + procedure Insert_At_Head (L : in out List; + I : in List_Item'Class) is + Temp_Item : Item := new List_Item'(I); + Temp : Link := new List_Node'(Controlled with + Data => Temp_Item, + Next => null, + Prev => null); + begin + if Is_Empty(L'Access) + then + L.Head := Temp; + L.Tail := Temp; + else + L.Head.Prev := Temp; + Temp.Next := L.Head; + L.Head := Temp; + end if; + L.Count := L.Count + 1; + end Insert_At_Head; + +-- This is implemented in terms of the non-access version. Simply makes it convenient +-- to call this with access to object values, general or storage-pool access values. + + procedure Insert_At_Head (L : access List; + I : access List_Item'Class) is + +Page 79 of 113 + +-- 1 +-- 2 +-- 3 +-- 4 +-- 5 +-- 6 +-- 7 +-- 8 + +-- 9 +-- 10 +-- 11 +-- 12 + +-- 13 +-- 14 +-- 15 +-- 16 +-- 17 +-- 18 + +-- 19 +-- 20 +-- 21 +-- 22 +-- 23 +-- 24 +-- 25 +-- 26 + +-- 27 +-- 28 +-- 29 +-- 30 +-- 31 +-- 32 +-- 33 +-- 34 +-- 35 +-- 36 +-- 37 +-- 38 +-- 39 +-- 40 +-- 41 +-- 42 +-- 43 +-- 44 +-- 45 +-- 46 +-- 47 + +-- 48 +-- 49 +-- 50 +-- 51 + + Ada Distilled + +by Richard Riehle + + begin + Insert_At_Head(L => L.all, + I => I.all); + end Insert_At_Head; + +-- We implement this as a function instead of a procedure with in out modes +-- because this can be used in an expression to constrain a classwide variable +-- For example, X : List_Item’Class := Remove(L); + + function Remove_From_Tail (L : access List) + return List_Item'Class is + Result : Item := L.Tail.Data; + begin + L.Tail := L.Tail.Prev; + L.Count := L.Count - 1; + +Free_Item(L.Tail.Next.Data); +Free_Node(L.Tail.Next); + + return Result.all; + end Remove_From_Tail; + +-- You might want a more robust “=“. For example, it might be better to traverse +-- each list, node by node, to ensure that each element is the same. + + function "=" (L, R : List) return Boolean is + begin + return L.Count = R.Count; + end "="; + +-- The name says it. Simply returns how many nodes in this list. + + function Node_Count (L : access List) return Natural is + begin + return L.Count; + end Node_Count; + +-- This will not be correct unless you keep careful count of the inserted and deleted nodes. + + function Is_Empty(L : access List) return Boolean is + +begin + + return L.Count = 0; + +end Is_Empty; + +-- We made List a limited private to prevent automatic assignment. Instead, we design +-- this “deep copy” procedure to ensure there will be two separate copies of the data + + procedure Copy (Source : in List; + Target : in out List) is + type Item_Ref is access all List_Item'Class; + Temp : Link := Source.Tail; + Local_Data : Item_Reference; + +begin + + Clear(Target); -- Be sure the target is initialized before copying. + +loop + +exit when Temp = null; +Local_Data := new List_Item'(Temp.Data.all); +declare + Local_List_Item + : List_Item'Class := Local_Data.all; +begin + Insert_At_Head(Target, Local_List_Item); +end; +Temp := Temp.Prev; + +end loop; + +end Copy; + +-- This is pretty simple. It is also an important part of the overall design. +procedure Clear (L : in out List) is +begin + +L.Head := null; +L.Tail := null; +L.Current := null; +L.Count := 0; + +end Clear; + +Also need to free data storage in this routine + +procedure Initialize_Iterator(This : in out Iterator; + The_List : access List) is + +Page 80 of 113 + +-- 52 +-- 53 +-- 54 +-- 55 + +-- 56 +-- 57 +-- 58 +-- 59 +-- 60 +-- 61 +-- 62 +-- 63 +-- 64 +-- 65 +-- 66 +-- 67 +-- 68 + +-- 69 +-- 70 +-- 71 +-- 72 +-- 73 +-- 74 + +-- 75 +-- 76 +-- 77 +-- 78 +-- 79 + +-- 80 +-- 81 +-- 82 +-- 83 +-- 84 + +-- 85 +-- 86 +-- 87 +-- 88 +-- 89 +-- 90 +-- 91 +-- 92 +-- 93 +-- 94 +-- 95 +-- 96 +-- 97 +-- 98 +-- 99 +-- 100 +-- 101 +-- 102 +-- 103 +-- 104 +-- 105 + +-- 106 +-- 107 +-- 108 +-- 109 +-- 110 +-- 111 +-- 112 +-- 113 + +-- 114 +-- 115 + + Ada Distilled + +by Richard Riehle + +begin + +This := Iterator(The_List.Head); + +end Initialize_Iterator; + +function Next(This : access Iterator) return Iterator is +begin + +return Next(This.all); + +end Next; + +function Next (This : Iterator) return Iterator is +begin + +return Iterator(This.Next); + +end Next; + +function Get (This : in Iterator) + + return List_Item'Class is + +begin + +return This.Data.all; + +end Get; + +function Get (This : in Iterator) return Item_Reference is +begin + +return This.Data; + +end Get; + +function Is_Done(This : in Iterator) return Boolean is +begin + +return This = null; + +end Is_Done; + +function Is_Done(This : access Iterator) + + return Boolean is + +begin + +return Is_Done(This.all); + +end Is_Done; + +end Queue_Manager_1; + +12.3 Generics and Software Reuse + +-- 116 +-- 117 +-- 118 + +-- 119 +-- 120 +-- 121 +-- 122 + +-- 123 +-- 124 +-- 125 +-- 126 + +-- 127 +-- 128 +-- 129 +-- 130 +-- 131 + +-- 132 +-- 133 +-- 134 +-- 135 + +-- 136 +-- 137 +-- 138 +-- 139 + +-- 140 +-- 141 +-- 142 +-- 143 +-- 144 +-- 145 + +Be sure to let the others on your project know about generic components you might design. Watch for +opportunities during design and code walkthroughs to promote code that is specific to one type into a +generic component. This is a project manager's responsibility. + +Don't write code that already exists in libraries. Most data structures and common algorithms are already +written and residing in component repositories. Some of those repositories are commercial. Others are +open source and free. For military systems, there are libraries of classified components that can be helpful +in building weapon systems. + +One strategy is to define, at the beginning of the project, what kinds of components are likely to be needed. +Select a dozen or so of these from existing libraries. Require that everyone on the project, during the early +stages of development, use the same generic linked-list, sort routine, etc. This will help you produce a +working solution early. Later, if there is a need for refinement, you can search for components that more +closely accomplish your needs. + +Page 81 of 113 + + Ada Distilled + +by Richard Riehle + +13. New Names from Old Ones + +Renaming is sometimes controversial. Some people like it. Others hate it. The important things to +understand are: + +1. Renaming does not create new data space. It simply provides compiler with a new name for an existing entity. + +2. Don't rename same item over and over with new names; you will confuse colleagues -- and yourself. + +3. Use renaming to simplify your code. A new name can sometimes make the code easier to read. + +13.1 Making a Long Name Shorter + +This section demonstrates some useful ideas such as renaming long package names, commenting the begin +statement, getting a line of data from a terminal using Get_Line, and catenating two strings. Also, note that +a string may be initialized to all spaces using the others => aggregate notation. + +with Text_IO, Ada.Integer_Text_IO; +procedure Gun_Aydin is + +package TIO renames Text_IO; +package IIO renames Ada.Integer_Text_IO; +Text_Data : String (1..80) := (others => ‘ ‘); -- 5 others => ‘ ‘ iniitalizes string to spaces; +Len : Natural; +begin -- Hello_2 + +4.3.3 +A.10.7 + +-- 1 Put Text_IO library unit in scope; +-- 2 "Good morning" in Turkish; +-- 3 Shorten a long name with renaming; +-- 4 Shorter name is same as full name to compiler; + +A.10.8/21 +6.1 +8.5.3 + +8.5.3 + +-- 4 To be used as parameter in Get_Line; +-- 6 Good idea to comment every begin statement; 2.7/2 +-- 7 Put a string prompt with no carriage return; A.10 +-- 8 After cursor, get a line of text with its length; A.10 +-- 9 Convert number to text and print it; +-- 10 Put catenated string with carriage return; +-- 11 end Label same as procedure name; + +A.10 and line 4 +4.4.1 +6.3 + +TIO.Put(“Enter Data: “); +TIO.Get_Line(Text_Data, Len); +IIO.Put (Len); +TIO.Put_Line(“ “ & Text_Data(1..Len)); + +end Gun_Aydin; + +13.2 Renaming an Operator ALRM 8.5 + +Sometimes an operator for a type declared in a with'ed package is in scope but not visible . The rules of +Ada require that no entity in scope is directly visible to a client until it is explicitly made visible. An +operator is one of the symbol-based operations such as "+", "/", ">", or "=". A use clause makes +operators directly visible, but a use clause also makes too many other entities directly visible. You can +selectively makeoperators visible through renaming as shown on lines 6 and 8 below. + +Renaming makes a specific operator visible without making all other operators visible. In the following +procedure, which draws a text-based diamond on the screen, we rename the packages to make their names +shorter and rename the “+” and “-” operators for Text_IO.Count to make them explicitly visible. + +with ada.text_io; +with ada.integer_text_Io; +procedure diamond1 is + +package TIO renames ada.text_io; +package IIO renames ada.integer_text_io; +function "+" (L, R : TIO.Count) return TIO.Count + renames TIO."+"; +function "-" (L, R : TIO.Count) return TIO.Count + renames TIO."-"; + + Center : constant TIO.Count := 37; + Left_Col, Right_Col : TIO.Count := Center; + +Symbol : constant Character := 'X'; +Spacing : TIO.Count := 1; +Increment : TIO.Count := 2; + +begin -- Diamond1 + TIO.Set_Col(Center); + +Page 82 of 113 + +-- 1 A.10; context clause. +-- 2 A.10.8/21 +-- 3 Parameterless procedure +-- 4 Rename a library unit; 8.5.3 +-- 5 Renames; 8.5.3 +-- 6 Rename Operator; 8.5.4 +-- 7 Makes the operators directly +-- 8 visible for "+" and "-" to avoid +-- 9 the need for a "use" clause. +-- 10 type-specific constant; named number +-- 11 type-specific variables +-- 12 a character type constant +-- 13 Local variables for counting +-- 14 Initialize the variable +-- 15 Always declare comment at begin +-- 16 Set the column on the screen + + + + + + + + + Ada Distilled + +by Richard Riehle + + TIO.Put(Symbol); +for I in 1..8 loop + +TIO.New_Line(Spacing); +Left_Col := Left_Col - Increment; +Right_Col := Right_Col + Increment; +TIO.Set_Col(Left_Col); +TIO.Put(Symbol); +TIO.Set_Col(Right_Col); +TIO.Put(Symbol); + +end loop; +for I in 9..15 loop + +TIO.New_Line(Spacing); +Left_Col := Left_Col + Increment; +Right_Col := Right_Col - Increment; +TIO.Set_Col(Left_Col); +TIO.Put(Symbol); +TIO.Set_Col(Right_Col); +TIO.Put(Symbol); + +end loop; + + TIO.Set_Col(Center); + TIO.Put(Symbol); +end Diamond1; + +-- 17 Put a single character to video display +-- 18 begin a for loop with constants +-- 19 Advance one line at a time +-- 20 See lines 8 & 9, above +-- 21 Data type and operator visibility +-- 22 +-- 23 +-- 24 +-- 25 +-- 26 +-- 27 +-- 28 +-- 29 Increment the Left Column by 1 +-- 30 Increment the Right Column by 1 +-- 31 Set the column +-- 32 Print the symbol +-- 33 Set the column +-- 34 Print the symbol +-- 35 Loop requires an end loop +-- 36 Set the column for final character output +-- 37 The last character for the diamond +-- 38 End of scope and declarative region + +Always plan ahead to ease operator usage through careful package design. In the following example, the +operators are renamed in a nested package which can be made visible with a use clause. + +Could also be +designed as a +child package + +package Nested is + +type T1 is private; -- this is called a partial view of the type +type Status is (Off, Low, Medium, High, Ultra_High, Dangerous); -- 3 Enumerated type; full set +-- operations on T1 and Status +package Operators is + +-- 1 Package specification +-- 2 Only =, /=, and := + +function “>=“ (L, R : Status) return Boolean + renames Nested.”>=“; +function “=“ (L, R : Status) return Boolean + renames Nested.” =“; + +end Operators; + +private + +type T1 is ... + +end Nested; + +-- 4 of infix operators is available +-- 5 A nested package specification +-- 6 Profile for a function and +-- 7 renames for the >= operator +-- 8 Profile for an = function and +-- 9 renames of the = operator +-- 10 Nested specifcation requires end +-- 11 Private part of package +-- 12 Full definition of type from line 2 +-- 13 Always include the identifier + +The above package can be accessed via a “with Nested;” context clause followed by a “use +Nested.Operators;” to make the comparison operators explicitly visible. Not everyone will approve of this +approach, but it has been employed in many Ada designs to simplify the use of infix operators because it +eliminates the need for localized renaming. We caution you to use this technique with discretion. + +with Nested; +procedure Test_Nested is +use Nested.Operators; +X, Y : Nested.Status := Nested.Status'First; + +begin -- Test_Nested + +-- Get some values for X, and Y +if X = Nested.Status'Last then +-- Some statements here + +end if; + +end Test_Nested; + +-- 1 Always include the identifier +-- 2 A simple procedure body +-- 3 Use clause for nested package +-- 4 Declare some Status objects +-- 5 Always include Identifier +-- 6 This code is commented +-- 7 = is made directly visible at line 3 +-- 8 +-- 9 Of course. End if required +--10 Always use identifier with end + +The code just shown illustrates a technique for letting the client make the selected operators directly visible +via a use clause on the nested package specification. I prefer this solution to use type (ALRM 8.4/4) +because it only makes a restricted set of operators visible. The downside of this is that it requires the +designer to think ahead. Thinking ahead is all too rare for package designers. + +Page 83 of 113 + + + + + + + + + + + + + + + + + + + + Ada Distilled + +by Richard Riehle + +13.3 Renaming an Exception + +Sometimes it is useful to rename an exception locally to where it will be used. For example, + +with Ada.IO_Exceptions; +package My_IO is + +-- various IO services +-- Data_Error : exception renames Ada.IO_Exceptions.Data_Error; +... + +end My_IO; + +13.4 Renaming a Component + +One of the most frequently overlooked features of Ada renaming is the option of giving a component of a +composite type its own name. + +with Ada.Text_IO; +package Rename_A_Variable is + +-- various IO services +-- Record_Count : renames Ada.Text_IO.Count; +... + +end Rename_A_Variable; + +13.4.1 Renaming an Array Slice + +Suppose you have the following string, + +Name : String(1..60); -- A String is a special kind of array that must be constrained + +where 1..30 is the last name, 31..59 is the first name and 60 is the middle initial. You could do the +following. + +declare +: String renames Name(1..30); +Last +: String renames Name(31.29); +First +Middle : String renames Name(60.60); + +begin + +Ada.Text_IO.Put_Line(Last); +Ada.Text_IO.Put_Line(First); +Ada.Text_IO.Put_Line(Middle); + +end; + +In this example, you can rename slices of +arrays including String arrays. Each slice +will have its own name that can be used in +any statement where that type is legal. + +where each Put_Line references a named object instead of a range of indices. Notice that the object still +holds the same indices. Also, the renamed range constrains the named object. No new space is declared. +The renaming simply gives a new name for existing data. + +13.4.2 Renaming a Record Component + +Consider the following definitions, + +subtype Number_Symbol is Character range '0'..'9'; +subtype Address_Character is Character range Ada.Characters.Latin_1.Space + .. Ada.Characters.Latin_1.LC_Z; -- Notice continuation of range to this line +type Address_Data is array(Positive range <>) of Address_Character; +type Number_Data is array(Positive range <>) of Number_Symbol; + +Page 84 of 113 + + + Ada Distilled + +type Phone_Number is record + +Country_Code : Number_Data(1..2); +Area_Code : Number _ Data (1..3); +Prefix : Number_ Data (1..3); +Last_Four : Number_ Data (1..4); + +end record; +type Address_Record is + +The_Phone : Phone_Number; +Street_Address_1 : Address_Data(1..30); +Street_Address_2 : Address_Data(1..20); +City : Address_Data (1..25); +State : Address_Data(1..2); +Zip : Number _ Data (1..5); +Plus_4 : Number_ Data (1..4); + +end record; + +One_Address_Record : Address_Record; + +by Richard Riehle + +Inner record + +Inner record +contained in +outer record + +Now you can rename an inner component for direct referencing in your program. For example, to rename +the Area_Code in a declare block, + +declare + +AC : Number_ Data renames One_Address_Record .The_Phone.Area_Code; + +begin + +-- Operations directly on variable AC + +end; + +Nested data + +The declaration of AC does not create any new data space. Instead, it localizes the name for the +component nested more deeply within the record. If the record had deeply nested components that you +needed in an algorithm, this renaming could be a powerful technique for simplifying the names within that +algorithm. + +13.5 Renaming a Library Unit + +Suppose you have a package in your library that everyone on the project uses. Further, suppose that +package has a long name. You can with that library unit, rename it, and compile it back into the libaray +with the new name. Anytime you with the new name, it is the same as withing the original. + +-- The following code compiles a renamed library unit into the library +with Ada.Generic_Elementary_Functions; +package Elementary_Functions renames Ada.Generic_Elementary_Functions; + +with Graphics.Common_Display_Types; +package CDT renames Graphics.Common_Display_Types; + +Take care when doing this kind of thing. You don't want to confuse others on the project by making up +new names that no one knows about. Also, renaming can be a problem when the renamed entity is too far +from its original definition or description. + +13.6. Renaming an Object or Value + +This can be especially troublesome when done too often. I recall a project where the same value was +renamed about seven times throughout a succession of packages. Each new name had meaning within the +context of the new package but was increasingly untraceable the further one got from its original value. + +Page 85 of 113 + + Ada Distilled + +by Richard Riehle + +package Messenger is + +type Message is tagged private; +type Message_Pointer is access all Message'Class; +procedure Create(M : in out Message; + S : in String); +procedure Clear (M : in out Message); +function Message_Text (M : Message) return String; +function Message_Length(M : Message) return Natural; + +private + +type String_Pointer is access all String; +type Message is tagged record + +Data : String_Pointer; +Length : Natural; + +end record; +end Messenger; + +-- 1 Specification Declaration +-- 2 Partial definition , tagged type +-- 3 Classwide access type (pointer) +-- 4 Operation on the type +-- 5 Second parameter for Operation +-- 6 Clear all fields of the Message +-- 7 Return the Data of Message +-- 8 Return the Length of Message +-- 9 Private part of specification +-- 10 Private pointer declaration +-- 11 Full definition of type Message type +-- 12 Component of Message record +-- 13 Component of Message record +-- 14 Ends scope of Message record +-- 15 End scope of specification + +Full private +type definition + +13.7. Renaming a Type or Subprogram + +The rename option does not apply to a type declaration. However, if you look back at the example of +subtypes elsewhere in this book, you will see that a subtype can be used any place its parent type can +be used. John English, in his JEWL package for Windows development, use this capability in clever +ways. + +Professor English first declares certain types using the spelling of his native England and they renames +them for his cousins across the pond. Consider the following examples from JEWL. + +Original type declaration + +type Colour is ... + +Renamed by a subtype declaration; + +subtype Color is Color; + +Original subprogram declaration. + +function Centre return Alignment_Type; + +Renamed by a renaming declaration; + +function Center return Alignment_Type renames Centre; + +13.8. Notes on renaming + +Ada developers are cautious about renaming. If the renames is used to localize the effect of an entity, or +clarify the understanding of that entity, it can be a good thing. If the renaming is applied to a global +variable or to some entity at a great distance from its original declaration, readability might be reduced. + +Use renaming to improve readability, understandability, and maintainability for the programmer who will +need to update your program after you are finished with it. Array renaming, while often handy, can +introduce confusion for a programmer unaccustomed to it. + +Page 86 of 113 + + Ada Distilled + +by Richard Riehle + +14. Concurrency with Tasking + +Ada is unique among general purpose programming languages in its support for concurrency. There are +two models for Ada concurrency: multitasking, and distributed objects. The latter, distributed objects is +beyond the scope of this book. We focus this discussion on multitasking. In Ada this is simply called +tasking. Tasking is implemented using standard Ada language syntax and semantics along with two +additional types: task types and protected types. The syntax and semantics of task types and protected +types is described in Chapter 9 of the Ada Language Reference Manual (ALRM). The semantics are +augmented in Annex D and Annex C of the ALRM. + +Each task is a sequential entity that may operate concurrently with, and communicate with, other tasks. A +task object may be either an anonymous type or an object of a task type. + +14.1 Fundamental Ideas + +Tasks are concurrent active objects. The word active in that sentence is important. An active object is +called a task in Ada. Once it is created and activated a task is in one of two states: executing or +suspended. Ada can support multiple active tasks. In a single processor implementation, only one task +can be executing at any instant. In this environment, other active tasks are suspended. When one active +task begins to execute, all other tasks are suspended. Tasks can be assigned priorities so they can enter +suspended/executing states according to a scheduling algorithm. The underlying Ada Run-time Executive +(it comes with every Ada compiler) has a scheduler that controls tasks according to a scheduling model. +The scheduling model may vary according the the needs of the execution environment. + +Tasks may be designed so they communicate with each other. The communication is called a rendezvous. +One task communicates with another by placing requests for rendezvous in the entry queue of the called +task. The calling task goes into a suspended state until the entry (request for rendezvous) in the queue is +consumed and and processed by the called task. The called task does not know its caller. + +14.2 A Keyboard Entry Example + +Task may be anonymous or instances of task types. The following tasks are anonymous. They are +concurrently active. Only one executes while the others are suspended. + +package Set_Of_Tasks is + +task T1; +task T2 is + +entry A; +entry B; + +end T2; +task T3 is + +entry X(I : in Character); +entry Y(I : out Character); + +end T3; + +end Set_Of_Tasks; + +-- 1 object of anonymous task type +-- 2 communicating object +-- 3 entry point to task +-- 4 entry point to task +-- 5 end of task specification +-- 6 communicating task object +-- 7 parameterized entry point +-- 8 parameterized entry point +-- 9 end of task specification +-- 10 end of package specification + +A task has two parts: specification and body. A task may not be a library unit and cannot be compiled by +itself. A task must be declared inside some other library unit. In the example, above, there are three task +specifications within a package specification. The body of each task will be within the body of the +package. For example, + +Page 87 of 113 + + Ada Distilled + +by Richard Riehle + +-- 1 Context clause +with Ada.Text_IO; +-- 2 For referencing special characters +with Ada.Characters.Latin_1; +-- 3 Make package Ada visible +use Ada; +-- 4 Make package Characters visible +use Characters; +-- 5 Enclosing scope for the task bodies +package body Set_Of_Tasks is +-- 6 Implement task T1 + task body T1 is +-- 7 Local variable + Input : Character; +-- 8 Local variable + Output : Character; +-- 9 Could be Text_IO.Positive_Count + Column : Positive := 1; +-- 10 + begin +-- 11 + loop +-- 12 Input character with no return key entry + Text_IO.Get_Immediate (Input); +-- 13 If the character is a tilde, exit the loop + exit when Input = '~'; +-- 14 Put entry in queue for T3.X; suspend + T3.X(Input); +-- 15 Put entry in queue for T2.A; suspend + T2.A; +-- 16 Put entry in queue for T2B; suspend + T2.B; +-- 17 Put entry in queue for T3.Y; suspend + T3.Y(Output); +-- 18 No more than 40 characters per line + if Column > 40 then +-- 19 Start the character count over from 1 + Column := 1; +-- 20 and then start a new line + Text_IO.New_Line; +-- 21 + else +-- 22 Increment the character per line count + Column := Column + 1; + end if; +-- 23 + Text_IO.Set_Col(Text_IO.Positive_Count(Column)); + Ada.Text_IO.Put(Output); + end loop; + end T1; + + task body T2 is + begin + loop + select + accept A; + accept B; + or + terminate; -- 36 Taken only when nothing can call this anymore + end select; + end loop ; + end T2; + +-- 25 Print the character on the screen; echo +-- 26 +-- 27 End of task T1 implementation +-- 28 +-- 29 Implement body of task T2 +-- 30 +-- 31 +-- 32 Select this alternative or terminate when done +-- 33 Rendezvous point; corresponds to entry in +-- 34 task specification. These are sequential here. +-- 35 The alternative to selecting accept A; + +-- 24 Note type conversion here + + task body T3 is + Temp : Character := Latin_1.Nul; + begin + loop + select + accept X (I : in Character ) do + + Temp := I; + + end X; + or + accept Y (I : out Character ) do + I := Temp; + Temp := Latin_1.Nul; + end Y; + or + terminate; + end select; + end loop; + end T3; +end Set_Of_Tasks; + +-- 37 +-- 38 +-- 39 +-- 40 +-- 41 Implement task T3 body +-- 42 Local variable +-- 43 +-- 44 Choose rendezvous altenative +-- 45 Another selective accept statement +-- 46 Begins critical region for rendezvous +-- 47 Calling task is suspended until end statement +-- 48 Rendezvous complete. Caller is not suspended +-- 49 or this next altenative +-- 50 Critical region begins with do statement +-- 51 Caller is suspended at this point +-- 52 The non-printing nul character +-- 53 Rendezvous complete at this point +-- 54 or the terminate alternative which will only +-- 55 be taken if no other task can call this one +-- 56 end of scope for the select statement +-- 57 +-- 58 +-- 59 + +We apologize for the length of this example. It does serve to show a lot of interesting issues related to +tasking. You can key it in and it will work. We also suggest you experiment with it by little alterations. + +Each task is coded as a loop. Task T1 simply gets a character from the keyboard, sends that character to +T3, gets it back from T3, and prints it to the screen. T3 does nothing with the character, but it could have + +Page 88 of 113 + + + + Ada Distilled + +by Richard Riehle + +more logic for examining the character to see if it is OK. You could modify this program to behave as a +simple data entry application. We recommend you do this as an exercise. + +Here is a simple little test program you can use with this package. + +with Set_Of_Tasks; +procedure Test_Set_Of_Tasks is +begin + null; +end Test_Set_Of_Tasks; + +The tasks, in package Set_Of_Tasks, will begin +executing as soon as the null statement is +executed. It is not necessary to call the tasks. + +Some tasks will have one or more entry specifications. In Ada, an entry is unique because it implies an +entry queue. That is, a call to an entry simply places an entry into a queue. An entry call is not a request +for immediate action. If there are already other entries in that queue, the request for action will have to +wait for the entries ahead of it to be consumed. Entries disappear from the queue in one of several ways. +The most common is for them to complete the rendezvous request. + +Each task body has a begin statement. Two of the tasks, T2 and T3, have local variables. The accept +statements in the bodies of T2 and T3 correspond to the entry statements in their specifications. A task +body may have more than one accept statement for each entry. When an accept statement includes a do +part, everything up to the end of accept statement is called the critical region. A calling task is suspended +until the critical region is finished for its entry into the task queue. + +Now we examine the details of the program example. Each task in this package specification is an +anonymous task. We know this because the word type does not appear in the specification. Task T1 is not +callable because it has no entries. Task T2 is callable, but has no parameters in the entry. T3 is callable +and includes a parameter list in each entry. A call to an entry is simply placemes a request for action in an +entry queue. This is more like message passing than subprogram calling. + +The body of this package contains the bodies of the corresponding task specifications. Task body T1 is +implemented as a loop. This is not a good model for task design. In fact, it is a bad design. However, it +does give us an introductory point into understanding. A better design would permit interrupts to occur +and be handled as they occur rather than within the confines of a loop. We show an example of this kind in +the next example. + +Line 14 is an entry call to T3.X. It includes a parameter of type Character. This entry call puts a request +for action in the T3.X queue. There are, potentially, other entries already in that queue. The default, in +Ada, is that the entries will be consumed in a FIFO order. This default may be overridden by the designer +when deemed appropriate. At Line 14, Task T1 is suspended while waiting for the completion of its +request for action. Task T1 will resume once that request is completed. + +Lines 15 and 16 are do nothing entry calls. We include them in this example for educational purposes, not +because they add anything to the design or performance. If we were to reverse Lines 15 and 16, this +program would deadlock. Each task is a sequential process. The two accept statements in task T2 are +sequential. Entry B cannot be processed until Entry A is processed. This is an important feature of Ada, +and almost all models for communicating sequential processes that operate concurrently. + +On line 32 in task T2 and line 45 of task T3, we show the start of a select statement. This construct allows +the task to take a choice of accept alternatives, depending on which entry is called. The accept statements +in task T3 are not sequential. That is, entry X is not dependent on entry Y and entry Y is not dependent on +entry X. The corresponding accept statements may proceed regardless of which is called first. + +Lines 36 and 56 have the terminate alternative within a select statement. This alternative will never be +taken unless no other task can call one of the other entries. The Ada run-time will take the terminate path +for every task that has reached the state where it cannot be called, cannot call any other task, and has no + +Page 89 of 113 + + Ada Distilled + +by Richard Riehle + +other tasks currently dependent on it. This is a graceful way to for a task to die. There is no need for a +special shutdown entry. Terminate should be used for most service tasks. + +If you do not understand the mechanisms associated with an entry queue, you will not understand +communicating tasks. It is a rule that, when a task puts an entry into the queue of another task, that entry +remains in the queue until it is consumed or otherwise is removed from the queue. The task that puts the +entry is suspended until the request for action is completed. The calling task may request, as part of the +call, that the request remain in the queue for a limited period, after which it is removed from the queue. + +Task T3 cannot identify who called which entry. It cannot purge its own queue. It can determine how +many entries are in each queue. That is, we could have a statement that gets X'Count or Y'Count within +task T3. + +Lines 47-48 and 52-53 are the procedural statements within an accept statement. Every statement between +the word do and the corresponding end is in the critical region, mentioned earlier. Statement 47 must +occur before statement 48. Task T1, when it makes a call, T3.Input(...), is suspended until the entire +critical region is finished. T3.Input will consume an entry from its own queue, process that entry in the +critical region, and finish. Once it is finished with the statements in the critical region, task T1 is released +from its suspended state and may continue. + +In tasks T2 and T3, the loop serves a slightly different purpose than in task T1. Here the loop is more of a +semantic construct to prevent the task from doing one set of actions and then terminating. That is, the loop +guarantees the task will remain active for as long as it is needed. + +14.3 Protecting Shared Data + +It has been traditional for a design in which concurrent threads share access to the same resource to use +some kind of Semaphore. Semaphores come in many different varieties. The two most common are the +counting semaphore and the binary semaphore. The latter is sometimes called a Mutex. A Semaphore is a +low-level mechanism that exposes a program to many kinds of potential hazards. Ada uses a different +mechanism, the protected object, which allows the programmer to design encapsulated, self-locking objects +where the data is secure against multiple concurrent updates. + +Protected types are a large topic. Therefore, we show only one simple version in this book. The reader is +encouraged to study this in greater depth if they need to develop Ada software using the tasking model. +The following example illustrates all of three operators of a protected object. There a lot of reasons why +you would not want to design a task-based application in exactly the way this one is designed. There are +some inherent inefficiencies in the design but it does illustrate some fundamental ideas you should know. + +with Ada.Text_IO; +procedure Protected_Variable_Example is + package TIO renames Ada.Text_IO; + +task T1; +task T2; + + protected Variable is + +procedure Modify(Data : Character); +function Query return Character ; +entry Display(Data : Character; T : String); + + private + +Shared_Data : Character := '0'; + +end Variable; + +Although this will work with +Text_IO, it is not a good idea to use +protected types with Text_IO in this +way. We do this only for +pedagogical purposes. + +-- 1 +-- 2 +-- 3 +-- 4 +-- 5 +-- 6 Could have been a type definition +-- 7 Object is locked for this operation +-- 8 Read-only. May not update data +-- 9 An entry has a queue +-- 10 +-- 11 All data is declared here +-- 12 + +protected body Variable is + +-- 13 No begin end part in protected body + +Page 90 of 113 + + + + + + + + + + Ada Distilled + +by Richard Riehle + +entry Display(Data : Character; T : String) + +when Display'Count > 0 is + + begin + +TIO.Put(T & " "); +TIO.Put(Data); +TIO.New_Line; + +end Display; +procedure Modify (Data : Character) is +begin + +end Modify; +function Query return Character is +begin + +return Shared_Data; + +end Query; + +end Variable; +task body T1 is + +Local : Character := 'a'; + Output : Character; + + begin + + loop + +TIO.Get_Immediate(Local); +exit when Local not in '0'..'z'; + + Variable.Modify(Local); + + Output := Variable.Query; + + Variable.Display(Output, "T1 "); + end loop; + end T1; +task body T2 is + +Local : Character :='a'; +Output : Character; + + begin + loop + +TIO.Get_Immediate(Local); +exit when Local not in '0'..'z'; +Variable.Modify(Local); + + Output := Variable.Query; + +Variable.Display(Output, "T2 "); + + end loop; + end T2; + +begin + null; +end Protected_Variable_Example; + +When a procedure is executed, the object is locked +for update only. It is performed in mutual exclusion. +No other updates can be performed at the same time. +-- 23 +Any other calls to modify must wait for it to be the +protected object to be unlocked. + +The object is locked for read-only. No updates can +be performed. A function is not allowed to update +the encapsulated data. + +It does not matter how many tasks are trying to +update the data. Only one can do so at any time. +This task, and its corresponding task will update +the protected variable in mutual exclusion. + +-- 14 A queue and a required barrier that +-- 15 acts like a pre-condition +-- 16 +-- 17 +-- 18 +-- 19 +-- 20 +-- 21 +-- 22 +Shared_Data := Data; +-- 24 +-- 25 +-- 26 +-- 27 +-- 28 +-- 29 +-- 30 +-- 31 +-- 32 +-- 33 +-- 34 +-- 35 +-- 36 +-- 37 +-- 38 +-- 39 +-- 40 +-- 41 +-- 42 +-- 43 +-- 44 +-- 45 +-- 46 +-- 47 +-- 48 +-- 49 +-- 50 +-- 51 +-- 52 +-- 53 +-- 54 +-- 55 +-- 56 + +Every operation in a protected object is performed in mutual exclusion. The object is locked for update +only during the modification operations. It is locked for read only during query operations. It is +impossible for both update and query to occur at the same time. A function is read-only. During function +calls, the object is locked for read-only. An entry, as with a task, has a queue. Every entry is controlled +by a boolean pre-condition that must be satisfied before it can be entered. + +Think of the difference between a semaphore and a protected type in terms of an airplane lavatory. If you +were to enter the lavatory and depend on the flight attendendant to set the lock when you enter and remove +the lock to let you out, that would be analogous to a semaphore. In a protected type, once you enter the +lavatory, you set the lock yourself. Once you are finished with your business in the lavatory, you unlock it +yourself, and it is now free for someone else to use. A protected object knows when it is finished with its +work and can unlock itself so another client can enter. + +Page 91 of 113 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Ada Distilled + +by Richard Riehle + +A. Annexes, Appendices and Standard Libraries + +Reserved Word List + +abort +abs +abstract +accept +access +aliased +all +and +array +at + +begin +body + +case +constant + +for +function + +declare +delay +delta +digits +do + +else +elsif +end +entry +exit + +generic +goto + +if +in +is + +limited +loop + +mod + +new +not +null + +of +or +others +out + +package +pragma +private +procedure +protected + +raise +range +record +rem +renames +requeue +return +reverse + +select +separate +subtype + +tagged +task +terminate +then +type + +until +use + +when +while +with + +xor + +The reserved words in blue are for concurrency and tasking. +Be careful when using the words in red. + +Every language has reserved words, +sometimes called keywords. Notice that, +among Ada’s 69 reserved words, there are +no explicit data types. Instead, pre-defined +types are declared in package Standard. + +Sometimes people will try to evaluate a +language by counting the number of +reserved words. This is a silly metric and +the intelligent student will select more +substantive criteria. + +Some Ada reserved words are overloaded +with more than one meaning, depending on +context. The compiler will not let you make +a mistake in the use of a reserved word. + +A.1 Package Standard + +package Standard is always in scope. Every entity is directly visible to every part of an Ada +program. Think of it as the root parent of every other package in any Ada program. + +package Standard is -- This package is always visible and never needs a with clause or use clause + +pragma Pure(Standard); +type Boolean is (False, True); -- An enumerated type; and ordered set; False is less than True +-- The predefined relational operators for this type are as follows: +-- function "=" (Left, Right : Boolean) return Boolean; +-- function "/=" (Left, Right : Boolean) return Boolean; +-- function "<" (Left, Right : Boolean) return Boolean; +-- function "<=" (Left, Right : Boolean) return Boolean; +-- function ">" (Left, Right : Boolean) return Boolean; +-- function ">=" (Left, Right : Boolean) return Boolean; + +Package Standard is the implied +parent of every other Ada package. +It does not need a with clause or a +use clause. Every element of +package Standard is always visible +to every part of every Ada +program. + +This package defines the types, +Integer, Boolean, Float, Character, +String, Duration. It also defines +two subtypes, Natural and Positive. + +All numeric types are +implementation dependent. +Therefore, do not use predefined +numeric types in your Ada +program designs. Instead, define +your own numeric types with +problem-based constraints. + +Note: Parameter and return types +are Integer'Base rather than Integer. + +-- The predefined logical operators and the predefined logical +-- negation operator are as follows: +-- function "and" (Left, Right : Boolean) return Boolean; +-- function "or" (Left, Right : Boolean) return Boolean; +-- function "xor" (Left, Right : Boolean) return Boolean; +-- function "not" (Right : Boolean) return Boolean; + +-- The integer type root_integer is predefined; The corresponding universal type is universal_integer. + +type Integer is range implementation-defined; +subtype Natural is Integer range 0 .. Integer'Last; +subtype Positive is Integer range 1 .. Integer'Last; + +-- The predefined operators for type Integer are as follows: + +-- function "=" (Left, Right : Integer'Base) return Boolean; +-- function "/=" (Left, Right : Integer'Base) return Boolean; +-- function "<" (Left, Right : Integer'Base) return Boolean; +-- function "<=" (Left, Right : Integer'Base) return Boolean; +-- function ">" (Left, Right : Integer'Base) return Boolean; +-- function ">=" (Left, Right : Integer'Base) return Boolean; + +-- function "+" (Right : Integer'Base) return Integer'Base; +-- function "–" (Right : Integer'Base) return Integer'Base; +-- function "abs" (Right : Integer'Base) return Integer'Base; +-- function "+" (Left, Right : Integer'Base) return Integer'Base; +-- function "–" (Left, Right : Integer'Base) return Integer'Base; +-- function "*" (Left, Right : Integer'Base) return Integer'Base; +-- function "/" (Left, Right : Integer'Base) return Integer'Base; + +Page 92 of 113 + + Ada Distilled + +by Richard Riehle + +-- function "rem" (Left, Right : Integer'Base) return Integer'Base; +-- function "mod" (Left, Right : Integer'Base) return Integer'Base; + +-- function "**" (Left : Integer'Base; Right : Natural) return Integer'Base; + +-- The floating point type root_real is predefined; The corresponding universal type is universal_real. + +type Float is digits implementation-defined; + +-- The predefined operators for this type are as follows: +-- function "=" (Left, Right : Float) return Boolean; +-- function "/=" (Left, Right : Float) return Boolean; +-- function "<" (Left, Right : Float) return Boolean; +-- function "<=" (Left, Right : Float) return Boolean; +-- function ">" (Left, Right : Float) return Boolean; +-- function ">=" (Left, Right : Float) return Boolean; + +-- function "+" (Right : Float) return Float; +-- function "–" (Right : Float) return Float; +-- function "abs" (Right : Float) return Float; +-- function "+" (Left, Right : Float) return Float; +-- function "–" (Left, Right : Float) return Float; +-- function "*" (Left, Right : Float) return Float; +-- function "/" (Left, Right : Float) return Float; + +Warning: +Do not use predefined Float from +package Standard in your production +programs. This type is useful for +student programs but is not well-suited +to portable software targeted to some +actual production application. + +-- function "**" (Left : Float; Right : Integer'Base) return Float; + +-- In addition, the following operators are predefined for the root numeric types: +function "*" (Left : root_integer; Right : root_real) return root_real; +function "*" (Left : root_real; Right : root_integer) return root_real; +function "/" (Left : root_real; Right : root_integer) return root_real; +-- The type universal_fixed is predefined. +-- The only multiplying operators defined between fixed point types are: + +Note: Fixed point arithmetic on root types +and universal fixed-point types is defined +here. See also ALRM 4.5.5/16-20 + +function "*" (Left : universal_fixed; Right : universal_fixed) + +return universal_fixed; + +function "/" (Left : universal_fixed; Right : universal_fixed) + +return universal_fixed; + +-- The declaration of type Character is based on the standard ISO 8859-1 character set. +-- There are no character literals corresponding to the positions forcontrol characters. +-- They are indicated in italics in this definition. See 3.5.2. + +type Character is + +(nul, soh, stx, etx, eot, enq, ack, bel, + bs, ht, lf, vt, ff, cr, so, si, + dle, dc1, dc2, dc3, dc4, nak, syn, etb, + can, em, sub, esc, fs, gs, rs, us, +' ', '!', '"', '#', '$', '%', '&', ''', +'(', ')', '*', '+', ',' ,'-', '.', '/', +'0', '1', '2', '3', '4', '5', '6', '7', +'8', '9', ':', ';', '<', '=', '>', '?', +'@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', +'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', +'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', +'X', 'Y', 'Z', '[', '\', ']', '^', '_', +'`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', +'h', 'I', 'j', 'k', 'l', 'm', 'n', 'o', +'p', 'q', 'r', 's', 't', 'u', 'v', 'w', +'x', 'y', 'z', '{', '|', '}', '~', del, +reserved_128, reserved_129, bph, nbh, +reserved_132, nel, ssa, esa, +hts, htj, vts, pld, plu, ri, ss2, ss3, +dcs, pu1, pu2, sts, cch, mw, spa, epa, +sos, reserved_153, sci, csi, +st, osc, pm, apc, +' ', '¡', '¢', '£', '¤', '¥', '¦', '§' +'¨', '©', 'ª', '«', '¬', '-', '®', '¯' +'°', '±', '²', '³', '´', 'µ', '¶', '·' +'¸','¹', 'º', '»', '¼', '½', '¾', '¿' + +Page 93 of 113 + +See also: +package Ada.Characters +package Ada.Characters.Latin_1 +package Ada.Characters.Handling + +-- 0 (16#00#) .. 7 (16#07#) +-- 8 (16#08#) .. 15 (16#0F#) +-- 16 (16#10#) .. 23 (16#17#) +-- 24 (16#18#) .. 31 (16#1F#) +-- 32 (16#20#) .. 39 (16#27#) +-- 40 (16#28#) .. 47 (16#2F#) +-- 48 (16#30#) .. 55 (16#37#) +-- 56 (16#38#) .. 63 (16#3F#) +-- 64 (16#40#) .. 71 (16#47#) +-- 72 (16#48#) .. 79 (16#4F#) +-- 80 (16#50#) .. 87 (16#57#) +-- 88 (16#58#) .. 95 (16#5F#) +-- 96 (16#60#) .. 103 (16#67#) +-- 104 (16#68#) .. 111 (16#6F#) +-- 112 (16#70#) .. 119 (16#77#) +-- 120 (16#78#) .. 127 (16#7F#) +-- 128 (16#80#) .. 131 (16#83#) +-- 132 (16#84#) .. 135 (16#87#) +-- 136 (16#88#) .. 143 (16#8F#) +-- 144 (16#90#) .. 151 (16#97#) +-- 152 (16#98#) .. 155 (16#9B#) +-- 156 (16#9C#) .. 159 (16#9F#) +-- 160 (16#A0#) .. 167 (16#A7#) +-- 168 (16#A8#) .. 175 (16#AF#) +-- 176 (16#B0#) .. 183 (16#B7#) +-- 184 (16#B8#) .. 191 (16#BF#) + +Characters beyond +the normal 7 bit +ASCII format now +use 8 bits. Also see +Wide-Character + + + Ada Distilled + +by Richard Riehle + +'À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Æ', 'Ç' +'È', 'É', 'Ê', 'Ë', 'Ì', 'Í', 'Î', 'Ï' +'Ð', 'Ñ', 'Ò', 'Ó', 'Ô', 'Õ', 'Ö', '×' +'Ø', 'Ù', 'Ú', 'Û', 'Ü', 'Ý', 'Þ', 'ß' +'à', 'á', 'â', 'ã', 'ä', 'å', 'æ', 'ç' +'è', 'é', 'ê', 'ë', 'ì', 'í', 'î', 'ï' +'ð', 'ñ', 'ò', 'ó', 'ô', 'õ', 'ö', '÷' +'ø', 'ù', 'ú', 'û', 'ü', 'ý', 'þ', 'ÿ' + +-- 192 (16#C0#) .. 199 (16#C7#) +-- 200 (16#C8#) .. 207 (16#CF#) +-- 208 (16#D0#) .. 215 (16#D7#) +-- 216 (16#D8#) .. 223 (16#DF#) +-- 224 (16#E0#) .. 231 (16#E7#) +-- 232 (16#E8#) .. 239 (16#EF#) +-- 240 (16#F0#) .. 247 (16#F7#) +-- 248 (16#F8#) .. 255 (16#FF#) + +-- The predefined operators for the type Character are the same as for any enumeration type. +-- The declaration of type Wide_Character is based on the standard ISO 10646 BMP character set. +-- The first 256 positions have the same contents as type Character. See 3.5.2. + +type Wide_Character is (nul, soh ... FFFE, FFFF); + +package ASCII is ... end ASCII; -- Obsolescent; see J.5 + +This is equivalent to Unicode. Can be used for +internationalization of a language implementation. + +-- Predefined string types: + +type String is array(Positive range <>) of Character; +pragma Pack(String); +-- The predefined operators for this type are as follows: +-- function "=" (Left, Right: String) return Boolean; +-- function "/=" (Left, Right: String) return Boolean; +-- function "<" (Left, Right: String) return Boolean; +-- function "<=" (Left, Right: String) return Boolean; +-- function ">" (Left, Right: String) return Boolean; +-- function ">=" (Left, Right: String) return Boolean; + +Strings of with the same constraint can +take advantage of these operators. + +-- function "&" (Left: String; Right: String) return String; +-- function "&" (Left: Character; Right: String) return String; +-- function "&" (Left: String; Right: Character) return String; +-- function "&" (Left: Character; Right: Character) return String; +type Wide_String is array(Positive range <>) of Wide_Character; +pragma Pack(Wide_String); + +This operator is used to catenate +arrays to arrays, arrays to +components, etc. It is defined for +any kind of array as well as for +predefined type Strring + +-- The predefined operators for Wide_String correspond to those for String + +type Duration is delta implementation-defined range implementation-defined; + +-- The predefined operators for the type Duration are the same as forany fixed point type. + +Used in delay statements in tasking. See +data types in package Calendar, ALRM 9.6 + +-- The predefined exceptions: + +Constraint_Error: exception; +Program_Error : exception; +Storage_Error : exception; +Tasking_Error : exception; + +end Standard; + +A.2 The Package Ada + +package Ada is + +pragma Pure(Ada); + +end Ada + +These exceptions are predefined in this package. A designer may define more +exceptions. Note the absence of Numeric_Error, which is now obsolescent in the +current standard. + +package Ada is the parent package for many of the library units. It has no type +definitions and no operations. It is nothing more than a placeholder packge that +provides a common root (common ancestor) for all of its descendants. As you learn +more about parent and child packages, you will understand the value for having one +package that is a common root. + +The expression, pragma Pure (Ada), is a compiler directive. Pragmas are compiler +directives. This directive is of little interest to you at this stage of your study. It will be +very important when you being developing larger software systems, especially those +that require the Distributed Systems Annex (Annex E). + +Page 94 of 113 + + Ada Distilled + +by Richard Riehle + +package Numerics + +This is the root package for a variety of numerics packages. + +package Ada.Numerics is + +pragma Pure(Numerics); +Argument_Error : exception; +Pi : constant := 3.14159_26535_89793_23846_26433_83279_50288_41971_69399_37511; +e : constant := 2.71828_18284_59045_23536_02874_71352_66249_77572_47093_69996; + +end Ada.Numerics; + +A.5.1 Elementary Functions + +Elementary functions are defined as a generic package. This means it must be instantiated before it can be +used. Note also that trigonometric functions are in radians. Also, the function "**" is an operator that +must be made directly visible before it can be used. We recommend renaming it in the scope where it is +required. Also, note that the parameters and return type are Float_Type'Base. This reduces any overflow +problems associated with intermediate results in extended expressions. + +generic + +type Float_Type is digits <>; + +package Ada.Numerics.Generic_Elementary_Functions is + +pragma Pure(Generic_Elementary_Functions); +function Sqrt (X : Float_Type'Base) return Float_Type'Base; +function Log (X : Float_Type'Base) return Float_Type'Base; +function Log (X, Base : Float_Type'Base) return Float_Type'Base; +function Exp (X : Float_Type'Base) return Float_Type'Base; +function "**" (Left, Right : Float_Type'Base) return Float_Type'Base; + +-- Trigonometric functions default in Radians +function Sin (X : Float_Type'Base) return Float_Type'Base; +function Sin (X, Cycle : Float_Type'Base) return Float_Type'Base; +function Cos (X : Float_Type'Base) return Float_Type'Base; +function Cos (X, Cycle : Float_Type'Base) return Float_Type'Base; +function Tan (X : Float_Type'Base) return Float_Type'Base; +function Tan (X, Cycle : Float_Type'Base) return Float_Type'Base; +function Cot (X : Float_Type'Base) return Float_Type'Base; +function Cot (X, Cycle : Float_Type'Base) return Float_Type'Base; +function Arcsin (X : Float_Type'Base) return Float_Type'Base; +function Arcsin (X, Cycle : Float_Type'Base) return Float_Type'Base; +function Arccos (X : Float_Type'Base) return Float_Type'Base; +function Arccos (X, Cycle : Float_Type'Base) return Float_Type'Base; +function Arctan (Y : Float_Type'Base; + + X : Float_Type'Base := 1.0) return Float_Type'Base; + +function Arctan (Y : Float_Type'Base; + X : Float_Type'Base := 1.0; + Cycle : Float_Type'Base) return Float_Type'Base; +function Arccot (X : Float_Type'Base; + Y : Float_Type'Base := 1.0) return Float_Type'Base; +function Arccot (X : Float_Type'Base; + Y : Float_Type'Base := 1.0; + Cycle : Float_Type'Base) return Float_Type'Base; +function Sinh (X : Float_Type'Base) return Float_Type'Base; +function Cosh (X : Float_Type'Base) return Float_Type'Base; +function Tanh (X : Float_Type'Base) return Float_Type'Base; +function Coth (X : Float_Type'Base) return Float_Type'Base; +function Arcsinh (X : Float_Type'Base) return Float_Type'Base; +function Arccosh (X : Float_Type'Base) return Float_Type'Base; +function Arctanh (X : Float_Type'Base) return Float_Type'Base; +function Arccoth (X : Float_Type'Base) return Float_Type'Base; + +end Ada.Numerics.Generic_Elementary_Functions; + +Log default base is +natural (e). The base +may be other than e. + +For the ** function, +you may have a +visibility problem. +You can solve it by +renaming it locally +after instantiating the +package. + +If cycle is not +supplied, the default +is in radians. + +Float_Type'Base +permits an +unconstrained result +that will not raise a +constraint error +during intermediate +operations. This +eliminates spurious +range constraint +violations in complex +expressions. + +Page 95 of 113 + + Ada Distilled + +by Richard Riehle + +A.10 Ada.Text_IO (Annotated) + +Text_IO enables machine-readable data to be formatted as human-readable data and human-readable data to +be conveted to machine-readable. For character and string types, no conversion from internal to external +format is required. For all other types, transformations should be done with Text_IO; Some operations are +overloaded. Overloading is most common when there are two file destinations for an action: a named file or +default standard file. + +with Ada.IO_Exceptions; -- Declared in Annex A of the Ada Language Reference Manual +package Ada.Text_IO is -- Converts human-readable text to machine-readable as well as standard input/output +type File_Type is limited private; -- Internal file handle for a program +type File_Mode is (In_File, Out_File, Append_File); -- Controls direction of data flow +type Count is range 0 .. implementation-defined; -- An integer data type; see Positive_Count +subtype Positive_Count is Count range 1 .. Count'Last; -- May be used with type Count +Unbounded : constant Count := 0; -- line and page length +subtype Field is Integer range 0 .. implementation-defined; -- Varies by platform. +subtype Number_Base is Integer range 2 .. 16; -- Only use: 2, 8, 10 and 16 + +type Type_Set is (Lower_Case, Upper_Case); -- Use this for enumerated types +-- File Management +procedure Create (File : in out File_Type; -- Program refers to this parameter + Mode : in File_Mode := Out_File; -- Almost always an output file + Name : in String := ""; -- The external name for the file + Form : in String := ""); -- Usage not defined by the language +procedure Open (File : in out File_Type; + Mode : in File_Mode; -- May be opened for input or for append + Name : in String; + Form : in String := ""); -- Form is rarely used in Ada 95. Compiler dependent. + +Note overloading of +subprogram names +from this point on. + +procedure Close (File : in out File_Type); -- Pretty much what youwould think this would do +procedure Delete (File : in out File_Type); +procedure Reset (File : in out File_Type; Mode : in File_Mode); -- Resets the mode of the file +procedure Reset (File : in out File_Type); -- Resets the mode of the file +function Mode (File : in File_Type) return File_Mode; -- Query the mode of a file +function Name (File : in File_Type) return String; -- Query the external name of a file +function Form (File : in File_Type) return String; -- Varies by compiler implementation + +function Is_Open(File : in File_Type) return Boolean; -- Query the open status of a file +-- Control of default input and output files +procedure Set_Input (File : in File_Type); -- Set this file as the default input file; must be open +procedure Set_Output(File : in File_Type); -- Set this file as the default ouput file; must be open +procedure Set_Error (File : in File_Type); -- Use this as the standard error file; must be open +function Standard_Input return File_Type; -- Standard input is usually a keyboard +function Standard_Output return File_Type; -- Standard output is usually a video display terminal +function Standard_Error return File_Type; + +function Current_Input return File_Type; -- Usually the same as Standard Input +function Current_Output return File_Type; +function Current_Error return File_Type; +type File_Access is access constant File_Type; -- Enable a pointer value to a file handle +function Standard_Input return File_Access; +function Standard_Output return File_Access; +function Standard_Error return File_Access; + +Access to File_Type has been added to Ada 95 version +of Text_IO. This turns out to be quite useful for many +situations. Since file type is a limited private type, +assignment is impossible. However, assignment is +possible on an access value (value of an access type). + +function Current_Input return File_Access; +function Current_Output return File_Access; +function Current_Error return File_Access; +-- Buffer control +procedure Flush (File : in out File_Type); -- Flushes any internal buffers +procedure Flush; -- Flush synchronizes internal file with external file by Flushing internal buffers +-- Specification of line and page lengths +procedure Set_Line_Length(File : in File_Type; To : in Count); +procedure Set_Line_Length(To : in Count); + +procedure Set_Page_Length(File : in File_Type; To : in Count); +procedure Set_Page_Length(To : in Count); +function Line_Length(File : in File_Type) return Count; +function Line_Length return Count; +function Page_Length(File : in File_Type) return Count; +function Page_Length return Count; +-- Column, Line, and Page Control + +Note: You may use Count instead of +Positive_Count but be careful of potential +constraint error. + +Page 96 of 113 + + Ada Distilled + +by Richard Riehle + +procedure New_Line (File : in File_Type; -- Carriage return/Line Feed for a File + Spacing : in Positive_Count := 1); -- Default to 1 unless otherwise called +procedure New_Line (Spacing : in Positive_Count := 1); -- CR/LF on the default output device +procedure Skip_Line (File : in File_Type; -- Discard characters up to line terminator + Spacing : in Positive_Count := 1); -- single line by default +procedure Skip_Line (Spacing : in Positive_Count := 1); -- single line by default +function End_Of_Line(File : in File_Type) return Boolean; -- Are we at the end of a line? +function End_Of_Line return Boolean; -- Are we at the end of a line? + +procedure New_Page (File : in File_Type); -- Terminate current page with page terminator +procedure New_Page; +procedure Skip_Page (File : in File_Type); -- Discard characters to end of page +procedure Skip_Page; +function End_Of_Page(File : in File_Type) return Boolean; -- Is this the end of a page? +function End_Of_Page return Boolean; +function End_Of_File(File : in File_Type) return Boolean; -- Is this the end of file? +function End_Of_File return Boolean; + +procedure Set_Col (File : in File_Type; To : in Positive_Count); -- Cursor to designated col +procedure Set_Col (To : in Positive_Count); -- Do not set this to a number less than current Col +procedure Set_Line(File : in File_Type; To : in Positive_Count); -- Cursor to designated line +procedure Set_Line(To : in Positive_Count); -- Must be value greater than current Line +function Col (File : in File_Type) return Positive_Count; -- What column number in file? +function Col return Positive_Count; -- What column number? +function Line(File : in File_Type) return Positive_Count; -- What line number in file? +function Line return Positive_Count; -- What line number? + +function Page(File : in File_Type) return Positive_Count; -- What page number in file? +function Page return Positive_Count; -- What page number? +-- Character Input-Output +procedure Get(File : in File_Type; Item : out Character); -- Gets single character from file +procedure Get(Item : out Character); -- Gets single character from keyboard +procedure Put(File : in File_Type; Item : in Character); -- Put single character; no CR/LF +procedure Put(Item : in Character); -- Put never emits CR/LF + +procedure Look_Ahead (File : in File_Type; -- Item set to next character without + Item : out Character; -- consuming it. + End_Of_Line : out Boolean); -- True if End of Line/End of Page/End of File +procedure Look_Ahead (Item : out Character; -- What is next character; don't get it yet + End_Of_Line : out Boolean); +procedure Get_Immediate(File : in File_Type; -- Get the next character without CR/LF + Item : out Character); -- Wait until character is available +procedure Get_Immediate(Item : out Character); -- Wait until character is available + +procedure Get_Immediate(File : in File_Type; -- Only get character if it is available + Item : out Character; + Available : out Boolean); -- False if character is not available +procedure Get_Immediate(Item : out Character; + Available : out Boolean); -- False if character is not available +-- String Input-Output +procedure Get(File : in File_Type; Item : out String); -- Get fixed sized string +procedure Get(Item : out String); -- Must enter entire string of size specified + +procedure Put(File : in File_Type; Item : in String); -- Output string; no CR/LF +procedure Put(Item : in String); -- No carriage return/line feed character +procedure Get_Line(File : in File_Type; -- String will vary in size based on value of Last + Item : out String; -- Must be large enough to hold all characters of input + Last : out Natural); -- Number of characters up to line terminator (CR/LF) +procedure Get_Line(Item : out String; Last : out Natural); +procedure Put_Line(File : in File_Type; Item : in String); +procedure Put_Line(Item : in String); -- Include carriage return/line feed character + +Following is a set of generic input/output packages nested within Ada.Text_IO + +-- Generic packages for Input-Output of any type of signed integer + +Page 97 of 113 + + Ada Distilled + +by Richard Riehle + +-- Consider Ada.Integer_Text_IO for standard Integer; you can with that package and get the same result for type Integer. +generic + +type Num is range <>; -- Generic formal paramter for sikgned numeric integer type + +package Integer_IO is -- Conversion between human-readable text and internal number format. + +Default_Width : Field := Num'Width; -- How big is the number going to be? +Default_Base : Number_Base := 10; -- See the options for number base in beginning of Text_IO +procedure Get(File : in File_Type; + Item : out Num; -- Corresponds to generic formal parameter, above + Width : in Field := 0); -- May specify exact number of input characters. +procedure Get(Item : out Num; + Width : in Field := 0); -- Should usually leave this as zero + +procedure Put(File : in File_Type; -- The file type is the internal handle for the file + Item : in Num; -- Corresponds to generic formal parameter, above + Width : in Field := Default_Width; -- Ordinarily, don't change this + Base : in Number_Base := Default_Base); +procedure Put(Item : in Num; + Width : in Field := Default_Width; + Base : in Number_Base := Default_Base); +procedure Get(From : in String; -- 10.2.1 of this book, line 56 for an example of this + Item : out Num; -- The actual numeric value of the string + Last : out Positive); -- Index value of last character in From +procedure Put(To : out String; -- 10.2.2 of this book, line 41 for an example of this + Item : in Num; -- Can raise a data error, or other IO_Error. Check this first. + Base : in Number_Base := Default_Base); -- Consider output in other than base ten. + +end Integer_IO; + +generic + +type Num is mod <>; -- Generic formal paramter for unsigned numeric type. See ALRM 3.5.4/10 + +package Modular_IO is + +Default_Width : Field := Num'Width; +Default_Base : Number_Base := 10; +procedure Get(File : in File_Type; + Item : out Num; + Width : in Field := 0); +procedure Get(Item : out Num; Width : in Field := 0); + +Modular_IO is new to Ada 95 and applies +to a new Modular data type. + +A Modular type is unsigned and has +wraparound arithmetic semantics. It is +especially useful for array indexes instead +of a signed integer type. + +procedure Put(File : in File_Type; + Item : in Num; + Width : in Field := Default_Width; + Base : in Number_Base := Default_Base); +procedure Put(Item : in Num; + Width : in Field := Default_Width; + Base : in Number_Base := Default_Base); +procedure Get(From : in String; + Item : out Num; + Last : out Positive); +procedure Put(To : out String; + Item : in Num; -- Get a string from an float type; convert float type to string + Base : in Number_Base := Default_Base); + +end Modular_IO; + +-- Generic packages for Input-Output of Real Types + +generic + +type Num is digits <>; -- Generic formal paramter for floating point numeric type; ALRM 3.5.7 + +package Float_IO is + +Default_Fore : Field := 2; -- Number of Positions to left of decimal point +Default_Aft : Field := Num'Digits–1; -- Number of Positions to right of decimal point +Default_Exp : Field := 3; -- For scientific notation; often zero is OK +procedure Get(File : in File_Type; + Item : out Num; + Width : in Field := 0); -- May specify exact width; usually don't; leave as zero +procedure Get(Item : out Num; + Width : in Field := 0); + +procedure Put(File : in File_Type; + Item : in Num; + Fore : in Field := Default_Fore; -- Number of Positions to left of decimal point + Aft : in Field := Default_Aft; -- Number of Positions to right of decimal point + +Page 98 of 113 + + Ada Distilled + +by Richard Riehle + + Exp : in Field := Default_Exp); -- Set this to zero if you don't want scientific notation +procedure Put(Item : in Num; + Fore : in Field := Default_Fore; -- Number of Positions to left of decimal point + Aft : in Field := Default_Aft; -- Number of Positions to right of decimal point + Exp : in Field := Default_Exp); -- Set this to zero if you don't want scientific notation + +-- Use these procedures to convert a floating-point value to a string or a string to a floating-point value +procedure Get(From : in String; -- Get floating point value from a string value + Item : out Num; -- Converts a valid floating point string to a float value + Last : out Positive); + +procedure Put(To : out String; -- Write a floating point value into an internal string + + Item : in Num; -- Converts a floating point value to a variable of type String + Aft : in Field := Default_Aft; -- Number of Positions to right of decimal point + Exp : in Field := Default_Exp); -- Set this to zero if you don't want scientific notation + +end Float_IO; + +generic + +type Num is delta <>; -- Generic formal paramter for fixed point numeric types + +package Fixed_IO is -- Input/Output of fixed point numeric types + +Default_Fore : Field := Num'Fore; +Default_Aft : Field := Num'Aft; -- Number of Positions to right of decimal point +Default_Exp : Field := 0; +procedure Get(File : in File_Type; + Item : out Num; + Width : in Field := 0); +procedure Get(Item : out Num; + Width : in Field := 0); +procedure Put(File : in File_Type; + Item : in Num; + Fore : in Field := Default_Fore; -- Number of Positions to left of decimal point + Aft : in Field := Default_Aft; -- Number of Positions to right of decimal point + Exp : in Field := Default_Exp); -- Set this to zero if you don't want scientific notation +procedure Put(Item : in Num; + Fore : in Field := Default_Fore; -- Number of Positions to left of decimal point + Aft : in Field := Default_Aft; -- Number of Positions to right of decimal point + Exp : in Field := Default_Exp); -- Set this to zero if you don't want scientific notation +-- Use these procedures to convert a fixed-point value to a string or a string to a fixed-point value +procedure Get(From : in String; + Item : out Num; + Last : out Positive); +procedure Put(To : out String; + Item : in Num; + Aft : in Field := Default_Aft; -- Number of Positions to right of decimal point + Exp : in Field := Default_Exp); -- Set this to zero if you don't want scientific notation + +end Fixed_IO; + +generic + +type Num is delta <> digits <>; -- Generic formal paramter for decimal numeric type + +package Decimal_IO is -- Decimal types are used for financial computing. + +Default_Fore : Field := Num'Fore; +Default_Aft : Field := Num'Aft; +Default_Exp : Field := 0; +procedure Get(File : in File_Type; + Item : out Num; + Width : in Field := 0); +procedure Get(Item : out Num; + Width : in Field := 0); +procedure Put(File : in File_Type; + Item : in Num; + Fore : in Field := Default_Fore; + Aft : in Field := Default_Aft; + Exp : in Field := Default_Exp); +procedure Put(Item : in Num; + Fore : in Field := Default_Fore; + Aft : in Field := Default_Aft; + Exp : in Field := Default_Exp); +-- Use these procedures to convert a decimal value to a string or a string to a decimal value + +See: ALRM Annex F + ALRM 3.5.9/4, ALRM 3.5.9/16 + +A decimal type is a special kind of fixed-point +type in which the delta must be a power of ten. +This is unlike a normal fixed point type where +the granluarity is a power of two. + +Decimal types are more accurate for monetary +applications and others that can be best served +using power of ten decimal fractions. + +Page 99 of 113 + + Ada Distilled + +by Richard Riehle + +procedure Get(From : in String; + Item : out Num; + Last : out Positive); +procedure Put(To : out String; + Item : in Num; + Aft : in Field := Default_Aft; -- see type defined above + Exp : in Field := Default_Exp); -- see type defined above + +end Decimal_IO; + +-- Generic package for Input-Output of Enumeration Types +generic + +type Enum is (<>); -- Actual must be a discrete type + +package Enumeration_IO is + +An enumerated type is an ordered set of +values for a named type. Example: + +type Color is (Red, Yellow, Blue); +type Month is (Jan, Feb,.., Dec) + ... is not legal Ada +type Day is (Monday, Tuesday, ...); +type Priority is (Low, Medium, High); + +Default_Width : Field := 0; +Default_Setting : Type_Set := Upper_Case; +procedure Get(File : in File_Type; + Item : out Enum); +procedure Get(Item : out Enum); +procedure Put(File : in File_Type; + Item : in Enum; + Width : in Field := Default_Width; + Set : in Type_Set := Default_Setting); +procedure Put(Item : in Enum; + Width : in Field := Default_Width; + Set : in Type_Set := Default_Setting); +-- Use these procedures to convert a enumerated value to a string or a string to a enumerated value +procedure Get(From : in String; + Item : out Enum; + Last : out Positive); +procedure Put(To : out String; + Item : in Enum; + Set : in Type_Set := Default_Setting); -- see type defined above + +end Enumeration_IO; + +-- Input-Output Exceptions +Status_Error : exception renames IO_Exceptions.Status_Error; +Mode_Error : exception renames IO_Exceptions.Mode_Error; +Name_Error : exception renames IO_Exceptions.Name_Error; +Use_Error : exception renames IO_Exceptions.Use_Error; -- from package IO_Exceptions +Device_Error : exception renames IO_Exceptions.Device_Error; +End_Error : exception renames IO_Exceptions.End_Error; +Data_Error : exception renames IO_Exceptions.Data_Error; +Layout_Error : exception renames IO_Exceptions.Layout_Error; + +private + +... -- not specified by the language + +end Ada.Text_IO; + +Page 100 of 113 + + Ada Distilled + +by Richard Riehle + +Ada.Streams.Stream_IO + +with Ada.IO_Exceptions; +package Ada.Streams.Stream_IO is + +Permits input/ouput of data in terms of System.Storage_Unit. +Use this with attributes: S'Input, S'Output, S'Read, S'Write. +This package makes it possible to store a tag of a tagged type +along with the rest of the data in the object. + +type Stream_Access is access all Root_Stream_Type'Class; +type File_Type is limited private; +type File_Mode is (In_File, Out_File, Append_File); +type Count is range 0 .. implementation-defined; +subtype Positive_Count is Count range 1 .. Count'Last; + +Note the consistency of this package +with other input-output packages + +-- Index into file, in stream elements. +procedure Create (File : in out File_Type; + Mode : in File_Mode := Out_File; + Name : in String := ""; + Form : in String := ""); +procedure Open (File : in out File_Type; + Mode : in File_Mode; + Name : in String; + Form : in String := ""); +procedure Close (File : in out File_Type); +procedure Delete (File : in out File_Type); +procedure Reset (File : in out File_Type; Mode : in File_Mode); +procedure Reset (File : in out File_Type); +function Mode (File : in File_Type) return File_Mode; +function Name (File : in File_Type) return String; +function Form (File : in File_Type) return String; +function Is_Open (File : in File_Type) return Boolean; +function End_Of_File (File : in File_Type) return Boolean; +function Stream (File : in File_Type) return Stream_Access; +-- Return stream access for use with T’Input and T’Output +-- Read array of stream elements from file + +procedure Read (File : in File_Type; + Item : out Stream_Element_Array; + Last : out Stream_Element_Offset; + From : in Positive_Count); +procedure Read (File : in File_Type; + Item : out Stream_Element_Array; + Last : out Stream_Element_Offset); +-- Write array of stream elements into file +procedure Write (File : in File_Type; + Item : in Stream_Element_Array; + To : in Positive_Count); +procedure Write (File : in File_Type; + Item : in Stream_Element_Array); +-- Operations on position within file +procedure Set_Index(File : in File_Type; To : in Positive_Count); +function Index(File : in File_Type) return Positive_Count; +function Size (File : in File_Type) return Count; +procedure Set_Mode(File : in out File_Type; Mode : in File_Mode); +procedure Flush(File : in out File_Type); +-- Exceptions +Status_Error : exception renames IO_Exceptions.Status_Error; +Mode_Error : exception renames IO_Exceptions.Mode_Error; +Name_Error : exception renames IO_Exceptions.Name_Error; +Use_Error : exception renames IO_Exceptions.Use_Error; +Device_Error : exception renames IO_Exceptions.Device_Error; +End_Error : exception renames IO_Exceptions.End_Error; +Data_Error : exception renames IO_Exceptions.Data_Error; + +private + +... -- not specified by the language + +end Ada.Streams.Stream_IO; + +Supports direct_IO on +stream items + +Ada.Calendar -- ALRM 9..6 (also See ALRM, Annex D.8 for Ada.Real-Time calendar package) + +Page 101 of 113 + + Ada Distilled + +by Richard Riehle + +package Ada.Calendar is +type Time is private; +subtype Year_Number is Integer range 1901 .. 2099; +subtype Month_Number is Integer range 1 .. 12; +subtype Day_Number is Integer range 1 .. 31; +subtype Day_Duration is Duration range 0.0 .. 86_400.0; +function Clock return Time; +function Year (Date : Time) return Year_Number; +function Month (Date : Time) return Month_Number; +function Day (Date : Time) return Day_Number; +function Seconds(Date : Time) return Day_Duration; + +procedure Split (Date : in Time; + Year : out Year_Number; + Month : out Month_Number; + Day : out Day_Number; + Seconds : out Day_Duration); + function Time_Of(Year : Year_Number; + + Month : Month_Number; + Day : Day_Number; + Seconds : Day_Duration := 0.0) return Time; + +function "+" (Left : Time; Right : Duration) return Time; +function "+" (Left : Duration; Right : Time) return Time; +function "–" (Left : Time; Right : Duration) return Time; +function "–" (Left : Time; Right : Time) return Duration; + + function "<" (Left, Right : Time) return Boolean; +function "<="(Left, Right : Time) return Boolean; +function ">" (Left, Right : Time) return Boolean; +function ">="(Left, Right : Time) return Boolean; +Time_Error : exception; + +private + +... -- not specified by the language + +end Ada.Calendar; + +-- 1 +-- 2 Encapsulated; use public methods +-- 3 Ada has always been Y2K compliant +-- 4 +-- 5 +-- 6 Total number of seconds in one day +-- 7 Gets the current clock time +-- 8 +-- 9 +-- 10 +-- 11 + +type Duration is defined in +package Standard + +-- 12 +-- 13 +-- 14 +-- 15 +-- 16 +-- 17 +-- 18 +-- 19 +-- 20 +-- 21 +-- 22 +-- 23 +-- 24 +-- 25 +-- 26 +-- 27 +-- 28 +-- 29 +-- 30 +-- 31 +-- 32 +-- 33 + +Page 102 of 113 + + Ada Distilled + +by Richard Riehle + +System Description Package + +Also see: System.Storage_Elements + System.Address_To_Access_Conversion + System.Storage Pools + +package System is + +: constant Address; +: constant := implementation-defined; +: constant := implementation-defined * Storage_Unit; +: constant := implementation-defined; + +: constant := root_integer'First; +: constant := root_integer'Last; +: constant := implementation-defined; +: constant := implementation-defined; +: constant := root_real'Digits; +: constant := implementation-defined; +: constant := implementation-defined; +: constant := implementation-defined; +: constant := implementation-defined; + +pragma Preelaborate(System); +type Name is implementation-defined-enumeration-type; +System_Name : constant Name := implementation-defined; +-- System-Dependent Named Numbers: +Min_Int +Max_Int +Max_Binary_Modulus +Max_Nonbinary_Modulus +Max_Base_Digits +Max_Digits +Max_Mantissa +Fine_Delta +Tick +-- Storage-related Declarations: +type Address is implementation-defined; +Null_Address +Storage_Unit +Word_Size +Memory_Size +Address Comparison +function "<" (Left, Right : Address) return Boolean; +function "<="(Left, Right : Address) return Boolean; +function ">" (Left, Right : Address) return Boolean; +function ">="(Left, Right : Address) return Boolean; +function "=" (Left, Right : Address) return Boolean; +-- function "/=" (Left, Right : Address) return Boolean; + +-- 1 Required for every compiler +-- 2 Elaborate at compile time +-- 3 Look this up for your compiler +-- 4 +-- 5 +-- 6 root integer is base type +-- 7 for all integers in this system +-- 8 +-- 9 +-- 10 +-- 11 +-- 12 +-- 13 +-- 14 +-- 15 +-- 16 Usually a private type +-- 17 +-- 18 +-- 19 +-- 20 +-- 21 +-- 22 +-- 23 +-- 24 +-- 25 +-- 26 +-- 27 +-- 28 +-- "/=" is implicitly defined +-- 29 +pragma Convention(Intrinsic, "<"); +-- 30 +... -- and so on for all language-defined subprograms in this package +-- 31 +-- Other System-Dependent Declarations: +-- 32 Big-endian/Little-endian +type Bit_Order is (High_Order_First, Low_Order_First); +-- 33 +Default_Bit_Order : constant Bit_Order; +-- 34 +-- Priority-related declarations (see D.1): +-- 35 Used for tasking +subtype Any_Priority is Integer range implementation-defined; +subtype Priority is Any_Priority range Any_Priority'First .. implementation-defined; + -- 36 +subtype Interrupt_Priority is Any_Priority range Priority'Last+1 .. Any_Priority'Last; -- 37 +-- 38 +Default_Priority : constant Priority := (Priority'First + Priority'Last)/2; +-- 39 +-- 40 +-- 41 + +... -- not specified by the language + +end System; + +private + +Arithmetic operators for type +Address are defined in package +System.Storage_Elements + +An implementation may add more specifications and declarations to this +package to make it conformant with the underlying system platform. + +Page 103 of 113 + + Ada Distilled + +by Richard Riehle + +Annex L Pragmas - Language-defined Compiler Directives + +Pragmas are Ada compiler directives. The word pragma has the same root as the word, pragmatic. It orginates in a +Greek word which, roughly translated, means “Do this.” Some pragmas affect the process of compilation. Others tell +the compiler about what elements belong in the Run-time Environment (RTE), and others restrict or expand the role +of of some language feature. + +pragma +pragma +pragma +pragma +pragma + +All_Calls_Remote[(library_unit_name)]; +Asynchronous(local_name); +Atomic(local_name); +Atomic_Components(array_local_name); +Attach_Handler(handler_name, expression); + +pragma +pragma + +Controlled(first_subtype_local_name); +Convention([Convention =>] convention_identifier,[Entity =>] local_name); + +pragma +pragma + +Discard_Names[([On => ] local_name)]; +Elaborate(library_unit_name{, library_unit_name}); + +— See E.2.3. +— See E.4.1. +— See C.6. +— See C.6. +— See C.3.1. + +— See 13.11.3. + +— See B.1. +— See C.5. +— See 10.2.1. + +Elaborate_All(library_unit_name{, library_unit_name}); +Elaborate_Body[(library_unit_name)]; +Export( [Convention =>] convention_identifier, [Entity =>] local_name [, [External_Name =>] string_expression] +[, [Link_Name =>] string_expression]); + +— See 10.2.1. +— See 10.2.1. + +— See B.1. + +Import( [Convention =>] convention_identifier, [Entity =>] local_name [, [External_Name =>] string_expression] +[, [Link_Name =>] string_expression]); +Inline(name {, name}); +Inspection_Point[(object_name {, object_name})]; + +— See B.1. +— See 6.3.2. +— See H.3.2. + +pragma +pragma +pragma + +pragma + +pragma +pragma + +pragma +pragma +pragma +pragma +pragma + +pragma +pragma +pragma +pragma +pragma +pragma + +pragma +pragma +pragma +pragma +pragma + +pragma +pragma +pragma +pragma +pragma + +Interrupt_Handler(handler_name); +Interrupt_Priority[(expression)]; +Linker_Options(string_expression); +List(identifier); +Locking_Policy(policy_identifier); + +Normalize_Scalars; +Optimize(identifier); +Pack(first_subtype_local_name); +Page; +Preelaborate[(library_unit_name)]; +Priority(expression); + +Pure[(library_unit_name)]; +Queuing_Policy(policy_identifier); +Remote_Call_Interface[(library_unit_name)]; +Remote_Types[(library_unit_name)]; +Restrictions(restriction{, restriction}); + +Reviewable; +Shared_Passive[(library_unit_name)]; +Storage_Size(expression); +Suppress(identifier [, [On =>] name]); +Task_Dispatching_Policy(policy_identifier ); + +pragma +pragma + +Volatile(local_name); +Volatile_Components(array_local_name); + +Page 104 of 113 + +— See C.3.1. +— See D.1. +— See B.1. +— See 2.8. +— See D.3. + +— See H.1. +— See 2.8. +— See 13.2. +— See 2.8. +— See 10.2.1. +— See D.1. + +— See 10.2.1. +— See D.4. +— See E.2.3. +— See E.2.2. +— See 13.12. + +— See H.3.1. +— See E.2.1. +— See 13.3. +— See 11.5. +— See D.2.2. + +— See C.6. +— See C.6. + + Ada Distilled + +by Richard Riehle + +Windows 95/98/NT/XP/ME/2000 Programming + +Not a part of Ada, but a useful package for many +simple programs where formatting is required. + +NT_Console Package + +This package can be used to format a window with colors, place a cursor wherever you wish, +and create character-based graphics on a Microsoft Windows console screen. You +can access all of the control characters, and you can print the characters defined in Annex A, +package Ada.Characters.Latin_1. This package is required form implementing the tasking problems +shown in this book. + +----------------------------------------------------------------------- +-- +-- File: nt_console.ads +-- Description: Win95/NT console support +-- Rev: 0.1 +-- Date: 18-jan-1998 +-- Author: Jerry van Dijk Mail: jdijk@acm.org +-- +-- Copyright (c) Jerry van Dijk, 1997, 1998 +-- Billie Holidaystraat 28 2324 LK LEIDEN THE NETHERLANDS tel int + 31 71 531 43 65 +-- +-- Permission granted to use for any purpose, provided this copyright remains attached and unmodified. +-- +-- THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, +-- WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +-- PURPOSE. +----------------------------------------------------------------------- + +package NT_Console is + +-- TYPE DEFINITIONS -- + + subtype X_Pos is Natural range 0 .. 79; + subtype Y_Pos is Natural range 0 .. 24; + + type Color_Type is (Black, Blue, Green, Cyan, Red, Magenta, Brown, Gray, + Light_Blue, Light_Green, Light_Cyan, Light_Red, + Light_Magenta, Yellow, White); + +-- CURSOR CONTROL -- + + function Where_X return X_Pos; + function Where_Y return Y_Pos; + + procedure Goto_XY (X : in X_Pos := X_Pos'First; + Y : in Y_Pos := Y_Pos'First); + +-- COLOR CONTROL -- + + function Get_Foreground return Color_Type; + function Get_Background return Color_Type; + + procedure Set_Foreground (Color : in Color_Type := Gray); + procedure Set_Background (Color : in Color_Type := Black); + +-- SCREEN CONTROL -- + + procedure Clear_Screen (Color : in Color_Type := Black); + +-- SOUND CONTROL -- + + procedure Bleep; + +-- INPUT CONTROL -- + +Page 105 of 113 + + Ada Distilled + +by Richard Riehle + + function Get_Key return Character; + function Key_Available return Boolean; + +-- EXTENDED PC KEYS -- Provides access to upper eight bit scan-code on a PC + -- Defined here is a list of special function keys available in + -- Microsoft Operating Systems. The full list is in the package specification + -- but we do not include here since they are seldom used. + +Each keypress on a standard PC keyboard generates a scan-code. The scan-code is contained in an eight bit format that uniquely +identifies the format of the keystroke. The scan code is interpreted by the combination of press and release of a keystroke. The +PC's ROM-BIOS sees an Interrupt 9 which triggers the call of an interrupt handling routine. The Interrupt handling routine reads +Port 96 (Hex 60) to decide what keyboard action took place. The interrupt handler returns a 2 byte code to the BIO where a +keyboard service routine examines low-order and high order bytes of a sixteen bit value. The scan code is in the high-order byte. + +Certain scan code actions are buffered in a FIFO queue for reading by some application program. Others trigger some immediate +action such as reboot instead of inserting them into the queue. + +The special keys in this list are those that can be queued rather than those that trigger an immediate operating system action. + +Console_IO + +This package is designed by Jerry Van Dijk and Richard Riehle. The package body is written by Jerry. It +is a more extensive and more robust version of NT_Console. It is included with the software provided +with this book. Console_IO is excellent for doing I/O on predefined types, formatting a screen, using +color, positioning a cursor exactly where you want it, and accessing low-level scan codes in your program. + +CLAW + +This is a powerful set of libraries for programming in Microsoft Windows. Available at: + http://www.rrsoftware.com + +GWindows + + This is a set of freeware windows development libraries created by David Botton. You can get this from: + http://www.adapower.com + +JEWL + +By far the easiest library for developing elementary Windows programs. This will not take you far in +building industrial strength Windows software, but it will get you started. This library set is particularly +useful if you are completely new to Windows programming. + +GtkAda + +A portable GUI development environment. You can target Windows, Linux, Unix, and many other +variations on Windowing operating systems. This is not an easy product to use, but it is powerful enough +that you can do almost anything you might want to do. However, if you are only targeting Microsoft, +consider CLAW or GWindows. + +Page 106 of 113 + + Ada Distilled + +by Richard Riehle + +C. Bibliography + +Books Related to Ada + +Ada 95 - The Language Reference Manual ANSI/ISO/IEC 8652:1995 + +Ada 95 Rationale, The Language and Standard Libraries, Ada Joint Program Office (with Intermetrics) + +Beidler, John, Data Structures and Algorithms, An Object-Oriented Approach Using Ada 95, +Springer-Verlag 1997, New York, ISBN 0-387-94834-1 + +Barnes, John G. P., Programming in Ada 95, Addison-Wesley, 1998, Second Edition + +Be sure you get the second edition; many improvements over the first edition + +Ben-Ari, Moti, Understanding Programming Languages, John Wiley & Sons, 1996 + +Ben-Ari, Moti, Ada for Professional Software Engineers, John Wiley & Sons, 1998 + +Booch, Grady, Doug Bryan, Charles Petersen, Software Engineering with Ada, Third Edition + Benjamin/Cummings, 1994 (Ada 83 only) + +Booch, Grady, Object Solutions, Managing the Object-Oriented Project, Addison-Wesley, 1996 + +Burns, Alan; Wellings, Andy; Concurrency in Ada, Cambridge University Press, 1995 + +Burns, Alan; Wellings, Andy; Real-Time Systems and Programming Languages, Addison-Wesley, 1997 + +Bryan, Doulass & Mendal, Geoffrey, Exploring Ada (2 vols), Prentice-Hall, 1992 + +Cohen, Norman, Ada As A Second Language, , Second Edition, McGraw-Hill, 1996 + +Coleman, Derek, et al Object-Oriented Development; The Fusion Method, Prentice-Hall, 1994 + +Culwin, Fintan, Ada, A Developmental Approach, , Second Edition, 1997, Prentice-Hall + +English, John, Ada 95, The Craft of Object-Oriented Programming, Prentice-Hall, 1997 + (Now available for FTP download on the World Wide Web) + +Fayad, Mohammed; Schmidt, Douglas; “Object-Oriented Application Frameworks”, Communications of the ACM, +October 1997 (Frameworks theme issue of CACM) + +Feldman, Michael, Software Construction and Data Structures with Ada 95, Addison-Wesley, 1997 + +Feldman, M.B, and E.B. Koffman, Ada95: Problem Solving and Program Design, Addison-Wesley, 1996 + +Finklestein A. and Fuks S. (1989) “Multi-party Specification”, Proceedings of 5th International Workshop on +Software Specification and Design, Pittsburgh, PA , pp 185-95 + +Fowler, Martin and Kendall Scott, UML Distilled, Addison-Wesley Longman, 1997 + +Gamma, Erich; Helm, Richard; Johnson, Ralph; Vlissides, John; Design Patterns, Elements of Resuable Object- +Oriented Software, Addison-Wesley, 1995 + +Gonzalez, Dean , Ada Programmer’s Handbook, Benjamin/Cummings, 1993 (Ada 83 version only) + +Jacobson, Ivar, Object-Oriented Software Engineering: A Use Case Driven Approach, Addison-Wesley, 1994 + +Page 107 of 113 + + Ada Distilled + +by Richard Riehle + +Johnston, Simon, Ada 95 for C and C++ Programmers, Addison-Wesley, 1997 + +Kain, Richard Y., Computer Architecture, Prentice-Hall, 1989 (because software examples are in Ada) + +Loftus, Chris (editor), Ada Yearbook - 1994, IOS Press, 1994 + +Meyer, Bertrand, Object-Oriented Software Construction, 2nd Editon, Prentice-Hall PTR, 1997 + (Not friendly to Ada, but an excellent treatment of object-oriented programming) + +Naiditch, David, Rendezvous with Ada 95, John Wiley & Sons, 1995 (0-471-01276-9) + +Rosen, Jean Pierre, ……………………….. HOOD ………………….. + +Pressman, Roger, Software Engineering, A Practitioner’s Approach, Fourth Edition, McGraw-Hill, 1997 + +Salus, Peter H, Handbook of Programming Languages, Vol 1, Object-Oriented Programming Languages, + MacMillan Technical Publishing, 1998 , ISBN 1-57870-009-4 + +Sigfried, Stefan, Understanding Object-Oriented Software Engineering, IEEE Press, 1995 + +Skansholm, Jan, Ada From The Beginning, , Third Edition, Addison-Wesley, 1997 + +Smith, Michael A., Object-Oriented Software in Ada 95, Thomson Computer Press, 1996 + +Sommerville, Ian, Software Engineering, Addison-Wesley, 1992 (an Ada friendly book on this topic) + +Stroustrop, Bjarne, The C++ Programming Language, 3rd Edition, Addison-Wesley, 1997 + +Szyperski, Clemens, + Component Software: Beyond Object-Oriented Programming, Addison-Wesley, 1998 + (Not an Ada book since it favors Java. But it is chock full of good ideas about components) + +Taylor, David A, Object-Oriented Technology; A Manager's Guide, Addison-Wesley, 1992 + +Wheeler, David, A, Ada 95, The Lovelace Tutorial, Springer-Verlag, New York, 1997 +[ Other Books to be added ] + +Page 108 of 113 + + Ada Distilled + +by Richard Riehle + +Recommended Periodicals & Other Current Information + +Most popular programming periodicals are staffed by editors who have little knowledge or interest in +software engineering. Those who do care about software engineering seem woefully ignorant about Ada. +Some of this ignorance reflects the general ignorance in the software community about Ada. Some of the +following sources are listed for their general interest rather than their attention to serious software issues. + +Ada Letters, A Bimonthly Publication of SIGAda, the ACM Special Interest Group on Ada + +(ISSN 1094-3641) +A good and reliable source of accurate information regarding Ada + +JOOP, Journal of Object-Oriented Programming, SIGS Publications, (Now out of business) + +Publishes articles and columns with positive perspective on Ada + +C++ Report, (especially the Column, Obfuscated C++), SIGS Publications + +If you want to be frightened about just how dangerous C++ really is, go to this source! + +Embedded Systems Programming, Miller-Freeman Publications + +Good Ada articles from time to time. Other good articles of interest to Ada practitioners + +Dr. Dobbs Journal, Miller-Freeman + +Generally misinformed about Ada. Editors, however, are open-minded about learning more accurate information + +Internet Usenet Forum: comp.lang.ada + +Internet Ada Advocacy ListServe: team-ada@acm.org + +Internet AdaWorks Web Site: http://www.adaworks.com + +Internet Ada Resources Association Web Site: http://www.adapower.com and http://www.adaic.org/ + +Microsoft Windows Programming in Ada. + There are several good options. The easiest to learn is JEWL from + John English. The FTP is: ftp://ftp.brighton.ac.uk/pub/je/jewl/. + +Caution: Do not depend on any information from +www.adahome.com. It is unreliable and out-of-date. + + A commercial library, for serious Windows developers is CLAW from RR Software. This has + a price tag but is worth every penny if you need industrial strength Ada Windows programs. + http://www.rrsoftware.com + + The adapower.com site lists other options for those who want to program in Windows + +Portable Windows and Graphics Programming + +Check out the Gtk+ and OpenGL bindings available free on the Web. The GtkAda binding is a powerful +set of tools that allows you to build graphical user interfaces (GUI) and leverage the power of the portable +graphics development toolset, OpenGL. With these tools, you can build Ada applications that will be +second to none in usability, efficiency, and portability. + +Page 109 of 113 + + Ada Distilled + +by Richard Riehle + +Index + +A +abs · 7, 131, 132 +abstract · 19, 78, 79, 87, 89, 131 +access · 8, 10, 18, 19, 21, 23, 24, 25, 26, 30, 45, 47, 48, 49, 50, 51, 52, 53, 54, 55, 61, 64, 65, 66, 75, 78, + +79, 80, 84, 85, 86, 87, 88, 89, 91, 101, 102, 104, 107, 108, 111, 112, 114, 115, 116, 122, 128, 131, 137, +143, 148, 152, 154, 155, 156, 157, 158, 161, 164, 165 + +Ada Core Technologies · 2, 5, 113, 168 +aliased · 48, 49, 50, 51, 131, 148, 160 +all · 9, 10, 12, 13, 18, 19, 24, 25, 26, 30, 32, 33, 40, 42, 44, 47, 48, 49, 50, 51, 54, 55, 59, 62, 65, 69, 74, +76, 78, 80, 82, 83, 84, 85, 86, 87, 88, 89, 91, 92, 94, 100, 101, 103, 104, 105, 108, 111, 112, 114, 115, +116, 117, 118, 122, 128, 129, 131, 139, 143, 146, 160, 161, 164 + +and · 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 25, 26, 27, 28, 29, 30, 31, 32, + +33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 44, 46, 47, 49, 50, 51, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, +66, 69, 70, 73, 74, 75, 77, 79, 80, 81, 82, 83, 84, 85, 87, 89, 90, 92, 93, 94, 95, 96, 98, 99, 100, 101, 102, +103, 104, 105, 106, 107, 108, 109, 111, 113, 115, 117, 118, 119, 120, 122, 124, 125, 126, 127, 128, 129, +130, 131, 132, 135, 136, 137, 139, 143, 146, 149, 150, 151, 152, 153, 154, 155, 156, 158, 159, 160, 162, +164, 166, 167, 168 + +array · 18, 19, 22, 24, 30, 31, 32, 33, 37, 38, 52, 54, 63, 64, 72, 73, 79, 80, 81, 85, 86, 87, 88, 89, 90, 93, + +101, 102, 104, 107, 108, 121, 131, 134, 143, 149, 151, 152, 153, 156, 162, 163 + +at · 2, 5, 13, 14, 15, 16, 17, 18, 26, 32, 34, 36, 40, 43, 44, 48, 49, 51, 54, 55, 59, 60, 62, 63, 70, 71, 72, 80, + +81, 86, 90, 94, 95, 100, 105, 106, 107, 109, 118, 124, 126, 130, 131, 138, 146, 149, 151 + +B +begin · 5, 9, 10, 14, 15, 16, 18, 20, 28, 29, 31, 32, 33, 36, 37, 38, 39, 41, 42, 43, 44, 45, 46, 48, 49, 50, 51, + +52, 53, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 68, 70, 71, 72, 73, 74, 75, 82, 83, 85, 86, 87, 88, 90, +91, 93, 94, 95, 96, 97, 98, 100, 101, 103, 105, 106, 108, 109, 110, 111, 113, 114, 115, 116, 117, 118, +119, 120, 121, 125, 126, 127, 129, 130, 131 + +body · 8, 9, 10, 11, 12, 13, 14, 15, 19, 20, 26, 57, 67, 70, 71, 77, 78, 79, 81, 82, 83, 102, 105, 106, 109, + +113, 119, 125, 126, 127, 129, 131, 148, 149, 156 + +C +C++ · 10, 22, 28, 29, 36, 66, 70, 93, 167, 168 +case · 6, 18, 36, 39, 40, 42, 43, 44, 62, 63, 81, 89, 94, 95, 96, 101, 131, 148 +Child Library Units · 11, 77, 79 +COBOL · 93 +code blocks · 15, 16, 43, 44, 45, 46, 59, 112, 159 +Compilation Unit · 8, 9, 10, 11, 14, 35, 55, 56, 67, 69, 70, 71, 77, 80, 82, 83, 90, 95, 102, 110, 113, 125, + +149, 160, 162 + +Compilation Units · 9, 10, 11, 14, 67, 69, 70, 83, 95, 149, 160, 162 +Compiler Publishers · 5 +constant · 9, 16, 21, 34, 36, 38, 44, 60, 64, 65, 66, 74, 94, 103, 104, 118, 131, 135, 136, 137, 146, 147, 150 +D +declarations · 8, 9, 12, 14, 15, 16, 20, 24, 27, 29, 31, 44, 45, 51, 54, 57, 58, 64, 67, 69, 70, 73, 78, 105, 146 +declare · 7, 13, 14, 15, 16, 24, 44, 45, 46, 49, 50, 51, 54, 66, 69, 70, 71, 72, 86, 93, 100, 109, 115, 118, + +120, 121, 131 + +declare block · 14, 15, 16, 44, 45, 46, 49, 86, 121 +delay · 18, 131 +delta · 24, 71, 131, 134, 141, 148, 150 + +Page 110 of 113 + + Ada Distilled + +by Richard Riehle + +digits · 24, 27, 29, 53, 71, 75, 103, 131, 132, 135, 140, 141, 148, 150, 153, 154, 158 +Dynamic Binding · 87, 89, 90, 113 +E +elaboration · 14 +else · 7, 15, 22, 40, 41, 42, 60, 62, 63, 64, 66, 82, 83, 93, 103, 105, 114, 125, 130, 131 +elsif · 40, 41, 103, 131 +end · 5, 7, 9, 10, 11, 12, 13, 15, 16, 18, 20, 24, 25, 26, 27, 28, 29, 31, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, +43, 44, 45, 46, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, +72, 73, 74, 75, 76, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 93, 94, 95, 96, 97, 99, 100, 101, +102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 123, +124, 125, 126, 127, 128, 129, 130, 131, 134, 135, 136, 138, 140, 141, 142, 144, 145, 147 + +entry · 5, 12, 14, 34, 40, 124, 125, 126, 127, 128, 129, 130, 131, 149, 150 +exceptions · 8, 18, 22, 44, 80, 100, 101, 102, 103, 104, 134 +exit · 39, 40, 45, 53, 56, 59, 60, 62, 89, 91, 95, 115, 125, 129, 130, 131 +F +for · 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, +33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, +61, 62, 63, 64, 65, 66, 67, 69, 70, 72, 73, 74, 75, 76, 77, 78, 79, 80, 82, 83, 84, 85, 86, 89, 90, 91, 92, 93, +94, 95, 96, 97, 98, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 117, 118, 119, 120, +121, 122, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 138, 139, 140, 141, 142, 143, +144, 146, 147, 148, 149, 151, 153, 154, 155, 157, 158, 159, 160, 161, 164, 166, 167, 168 + +Fortran · 32 +function · 8, 9, 11, 12, 14, 18, 19, 21, 22, 25, 27, 28, 29, 34, 41, 42, 43, 44, 45, 47, 50, 51, 52, 53, 54, 55, + +56, 58, 59, 61, 62, 63, 64, 65, 66, 67, 69, 70, 71, 72, 73, 75, 77, 78, 79, 80, 81, 82, 83, 84, 86, 87, 88, 90, +93, 100, 101, 103, 104, 105, 106, 107, 108, 111, 112, 114, 115, 116, 118, 119, 122, 129, 130, 131, 132, +133, 134, 135, 136, 137, 138, 143, 144, 145, 146, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, +159, 160, 164, 165 + +G +generic · 15, 18, 28, 75, 77, 83, 93, 95, 96, 105, 106, 107, 108, 109, 110, 111, 131, 135, 139, 140, 141, 142 +goto · 18, 131 +GtkAda · 2, 5 +I +Implementation · 8, 9, 10, 11, 14, 35, 55, 56, 67, 69, 70, 71, 77, 80, 82, 83, 90, 102, 110, 113, 125, 126, + +127, 129, 130 + +J +Java · 5, 47, 99, 167 +L +Libraries · 3, 5, 7, 15, 16, 17, 18, 22, 24, 27, 28, 29, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 42, 44, 45, 46, + +47, 48, 49, 52, 53, 55, 57, 58, 59, 60, 62, 66, 67, 72, 73, 74, 75, 83, 85, 86, 88, 90, 91, 92, 93, 94, 95, 96, +97, 98, 99, 100, 101, 103, 104, 113, 117, 118, 120, 121, 122, 125, 126, 129, 131, 134, 135, 136, 137, +139, 142, 146, 147, 148, 149, 158, 166 + +Library Unit · 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19, 20, 21, 22, 24, 25, 26, 27, 28, 29, 31, 34, 36, 37, +38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, +66, 67, 69, 70, 71, 72, 73, 74, 75, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 93, 94, 95, 96, +98, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, +122, 127, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 148, +149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 164, 165 + +Library Units · 2, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 30, 31, 32, + +34, 35, 38, 39, 41, 43, 44, 45, 46, 47, 48, 49, 51, 52, 53, 54, 55, 56, 57, 58, 59, 61, 62, 64, 65, 66, 67, 69, +70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, +100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 113, 117, 118, 119, 120, 122, 124, 125, 127, +129, 131, 134, 135, 136, 139, 140, 141, 142, 143, 144, 146, 148, 152, 156, 157, 160, 161, 162, 163, 164, +165, 168 + +Linux Programming · 3, 5, 113, 168 + +Page 111 of 113 + + Ada Distilled + +by Richard Riehle + +M +methods · 7, 8, 15, 19, 20, 35, 49, 51, 57, 67, 69, 70, 90, 102, 105, 107, 111, 146 +mod · 7, 30, 96, 98, 131, 132, 140 +N +null · 18, 20, 42, 48, 56, 62, 69, 72, 82, 90, 103, 110, 112, 113, 115, 116, 121, 127, 130, 131, 153, 160, 161 +O +Object Modeling · 166, 167 +Object-Oriented Programming · 24, 25, 34, 35, 78, 79, 80, 81, 84, 87, 89, 90, 91, 101, 102, 111, 122, 131, + +149, 151, 159 +OC Systems · 2, 5 +P +package · 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 19, 20, 22, 23, 24, 25, 26, 27, 28, 31, 32, 34, 35, 39, 44, 46, +47, 48, 49, 53, 54, 55, 56, 59, 62, 64, 67, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, +87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, +113, 117, 118, 119, 120, 122, 124, 125, 127, 129, 131, 134, 135, 136, 139, 140, 141, 142, 143, 144, 146, +160, 164, 165 + +Package Design · 7, 8, 9, 10, 11, 12, 13, 14, 19, 20, 22, 23, 26, 27, 29, 34, 54, 57, 64, 67, 69, 70, 71, 72, +73, 77, 78, 79, 80, 84, 89, 102, 105, 106, 108, 109, 113, 119, 120, 122, 123, 124, 125, 126, 127, 148, +149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 165 + +Parameters · 9, 10, 21, 22, 25, 30, 33, 38, 39, 47, 50, 51, 52, 53, 54, 55, 57, 58, 59, 60, 61, 62, 63, 64, 65, + +66, 73, 76, 86, 89, 90, 92, 101, 105, 106, 107, 108, 109, 110, 117, 122, 127, 136, 137, 139, 158 + +pragma · 14, 15, 31, 34, 35, 131, 134, 135, 146, 162, 163 +private · 8, 9, 10, 11, 12, 23, 24, 25, 26, 27, 41, 47, 69, 70, 77, 78, 79, 80, 81, 83, 84, 85, 87, 88, 89, 90, + +101, 102, 103, 104, 106, 107, 108, 109, 111, 112, 115, 119, 122, 129, 131, 136, 142, 143, 144, 145, 146, +147, 149 + +procedure · 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 18, 19, 20, 21, 24, 25, 26, 27, 28, 29, 31, 36, 37, 38, 39, 40, + +42, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 63, 66, 67, 69, 70, 71, 72, 73, 74, 75, +77, 78, 79, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 94, 95, 96, 98, 100, 101, 102, 103, 104, 106, 107, +108, 109, 110, 111, 112, 113, 114, 115, 117, 118, 119, 122, 127, 129, 131, 136, 137, 138, 139, 140, 141, +142, 143, 144, 155, 156, 157, 161, 165 + +protected · 15, 23, 30, 70, 124, 128, 129, 130, 131, 150 +R +raise · 18, 41, 61, 64, 82, 83, 100, 102, 103, 104, 131, 140 +rem · 7, 131, 132 +rename · 17, 117, 118, 120, 121, 122 +requeue · 131 +Reserved Words · 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, + +28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, +56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, +84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, +109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, +130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, +151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168 + +return · 8, 9, 11, 12, 19, 21, 25, 27, 33, 38, 40, 41, 42, 43, 44, 45, 47, 50, 51, 52, 53, 54, 55, 56, 58, 61, 62, +63, 64, 65, 66, 67, 69, 70, 71, 72, 73, 78, 79, 80, 81, 82, 83, 84, 86, 87, 88, 90, 93, 94, 100, 101, 103, +104, 105, 107, 108, 111, 112, 114, 115, 116, 117, 118, 119, 122, 125, 129, 131, 132, 133, 134, 135, 136, +137, 138, 139, 143, 144, 145, 146, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 164, +165 + +Reusable components · 15, 18, 28, 75, 77, 83, 93, 95, 96, 105, 106, 107, 108, 109, 110, 111, 131, 135, 139, + +140, 141, 142 + +reverse · 37, 128, 131 +S +Scope and Visibility · 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 19, 20, 24, 25, 27, 28, 34, 36, 38, 39, 40, 41, +44, 45, 46, 49, 50, 51, 52, 55, 57, 58, 59, 62, 64, 65, 66, 67, 69, 70, 71, 72, 73, 74, 75, 78, 79, 80, 89, 92, +94, 95, 96, 102, 113, 117, 118, 119, 123, 124, 125, 126, 135 + +Page 112 of 113 + + Ada Distilled + +by Richard Riehle + +select · 18, 126, 128, 131 +separate · 11, 14, 61, 67, 80, 115, 131 +Subprogram · 2, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19, 20, 21, 22, 24, 25, 26, 27, 28, 29, 30, 31, 34, + +36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, +64, 65, 66, 67, 69, 70, 71, 72, 73, 74, 75, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 93, 94, +95, 96, 98, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, +118, 119, 122, 127, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, +146, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 164, 165 + +subprograms · 7, 17, 61, 80, 85, 90, 144 +subtype · 27, 28, 29, 60, 75, 121, 131, 132, 136, 143, 144, 147, 148, 149, 150, 151, 152, 153, 154, 155, + +156, 157, 158, 159, 160, 161, 162, 163, 164 + +Supported Operating Systems · 2, 3, 5, 164, 165, 168 +T +tagged · 24, 25, 34, 35, 78, 79, 80, 81, 84, 87, 89, 90, 91, 101, 102, 111, 122, 131, 149, 151, 159 +task · 14, 15, 23, 30, 57, 93, 124, 125, 126, 127, 128, 129, 130, 131, 149, 150, 152, 158, 159 +Tasking · 5, 12, 14, 34, 40, 124, 125, 126, 127, 128, 129, 130, 131, 149, 150 +terminate · 18, 66, 126, 128, 131 +then · 7, 39, 40, 41, 42, 45, 58, 60, 61, 62, 63, 64, 66, 70, 82, 83, 93, 95, 98, 103, 105, 106, 114, 119, 125, + +128, 131, 149, 152, 156, 157 + +type · 6, 8, 9, 10, 11, 12, 13, 15, 16, 17, 18, 19, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, +37, 38, 41, 42, 47, 48, 49, 50, 51, 52, 53, 54, 55, 58, 59, 60, 61, 62, 63, 64, 65, 66, 69, 70, 71, 72, 73, 74, +75, 78, 79, 80, 81, 84, 85, 87, 88, 89, 90, 91, 92, 93, 96, 98, 101, 102, 103, 104, 105, 106, 107, 108, 109, +111, 112, 113, 115, 117, 118, 119, 120, 121, 122, 124, 126, 127, 129, 130, 131, 132, 133, 134, 135, 136, +137, 139, 140, 141, 142, 143, 144, 146, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, +161, 164 + +Type definitions · 9, 11, 12, 18, 19, 22, 23, 24, 25, 26, 27, 30, 31, 32, 33, 34, 35, 37, 38, 41, 52, 54, 63, 64, +65, 69, 72, 73, 79, 80, 81, 84, 85, 86, 87, 88, 89, 90, 93, 101, 102, 103, 104, 107, 108, 112, 121, 122, +123, 131, 134, 143, 148, 149, 151, 152, 153, 156, 158, 162, 163 + +type safety · 22 +U +until · 10, 38, 39, 50, 85, 117, 124, 126, 127, 128, 131, 138, 149 +use · 2, 6, 11, 12, 13, 15, 18, 19, 28, 29, 30, 31, 33, 35, 36, 37, 40, 43, 44, 46, 47, 48, 49, 52, 53, 54, 59, 61, +62, 66, 67, 69, 73, 74, 75, 80, 83, 84, 86, 87, 88, 90, 91, 93, 94, 95, 96, 98, 101, 105, 106, 111, 112, 117, +118, 119, 120, 125, 127, 128, 130, 131, 136, 143, 144, 164 + +V +Visibility Rules · 7, 9, 10, 12, 13, 17, 19, 20, 36, 45, 46, 51, 67, 69, 70, 71, 74, 80, 89, 96, 118 +W +when · 2, 6, 15, 16, 22, 28, 29, 31, 37, 39, 40, 42, 43, 44, 45, 46, 48, 49, 50, 52, 53, 54, 56, 59, 62, 64, 65, +77, 82, 89, 90, 91, 95, 100, 101, 103, 111, 115, 122, 124, 125, 126, 127, 128, 129, 130, 131, 149, 150, +152, 153, 157, 158, 159 + +while · 38, 39, 127, 131 +WinTel Programming · 3, 5, 164, 168 +with · 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19, 21, 22, 23, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, +35, 36, 37, 38, 39, 40, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, +67, 69, 70, 72, 73, 74, 75, 76, 77, 78, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 98, 100, +101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 117, 118, 119, 120, 122, 124, 125, +126, 127, 128, 129, 130, 131, 135, 136, 137, 138, 139, 143, 148, 149, 150, 151, 152, 153, 154, 155, 156, +157, 158, 159, 160, 161, 164, 166, 167, 168 + +X +xor · 7, 31, 131, 132 + +Page 113 of 113 + + \ No newline at end of file