Presentation is loading. Please wait.

Presentation is loading. Please wait.

Morning Ada Concepts - 1Introduction to Ada Pyrrhus Software Enduring Solutions Introduction to Ada 95 Joyce L Tokar, PhD

Similar presentations


Presentation on theme: "Morning Ada Concepts - 1Introduction to Ada Pyrrhus Software Enduring Solutions Introduction to Ada 95 Joyce L Tokar, PhD"— Presentation transcript:

1 Morning Ada Concepts - 1Introduction to Ada Pyrrhus Software Enduring Solutions Introduction to Ada 95 Joyce L Tokar, PhD

2 Morning Ada Concepts - 2Introduction to Ada Pyrrhus Software Enduring Solutions Acknowledgements Eugene Binque, Ph. D. David A. Cook, Ph. D. Leslie (Les) Dupaix

3 Morning Ada Concepts - 3Introduction to Ada Pyrrhus Software Enduring Solutions Course Outline History and Overview Large Scale View of Ada Conventional Features Packages and Overloading Object Oriented Features Real-Time Features Systems Features Tools and Environments

4 Morning Ada Concepts - 4Introduction to Ada Pyrrhus Software Enduring Solutions Morning Session This part of the course will present the basic structure of the Ada programming language. It will cover data abstraction, information hiding, localization, and modularity. The features that enable you to build reliable software systems from the building blocks in the language.

5 Morning Ada Concepts - 5Introduction to Ada Pyrrhus Software Enduring Solutions Afternoon Session Look into the support for Systems programming, Real-Time programming, and Object-Oriented programming. Look at the tools and environments available for developing Ada applications.

6 Morning Ada Concepts - 6Introduction to Ada Pyrrhus Software Enduring Solutions History and Overview High-level programming language initially developed to address the software crisis as recognized as early as 1974 by the US Department of Defense. Evolution to continue to incorporate improvements in software engineering and programming. Large scale and critical programs that require high-reliability.

7 Morning Ada Concepts - 7Introduction to Ada Pyrrhus Software Enduring Solutions Mission of Ada 95 Revise Ada based on User Need Effect a Smooth Transition Correct Minor Flaws in Ada 83 Improve the Facilities for Real-Time Programming Introduce Enhancements to Enter Other Programming Domains

8 Morning Ada Concepts - 8Introduction to Ada Pyrrhus Software Enduring Solutions Ada Revision Process User Requirements – Oct 88 Requirements – Dec 90 Mapping – Dec 94 ISO Approval – Feb 95 ANSI/FIPS Approval – Feb 95 Technical Corrigenda – Jul 2000 Addendums – 2005+

9 Morning Ada Concepts - 9Introduction to Ada Pyrrhus Software Enduring Solutions Structure of Ada 95 CORE OOP Features Systems Programming Real-Time Features Interfaces Systems Programming Real-Time Systems Distributed Systems Information Systems Numerics Safety & Security ANNEXES

10 Morning Ada Concepts - 10Introduction to Ada Pyrrhus Software Enduring Solutions Goals of Ada Readability Strong Typing Programming in the Large Exception Handling Data Abstraction Object Oriented Programming Tasking Generic Units Interfacing

11 Morning Ada Concepts - 11Introduction to Ada Pyrrhus Software Enduring Solutions Software Engineering Reuse of software components. Established methods of developing software.

12 Morning Ada Concepts - 12Introduction to Ada Pyrrhus Software Enduring Solutions What distinguishes Ada? Strong typing leads to the early detection of errors. Integrity is maintained across compilation unit boundaries. All identifiers are declared before they are used. Clearly identified links to other program modules through the use of context clauses. Program libraries to encapsulate program units for reuse. Ada is Reliable ….

13 Morning Ada Concepts - 13Introduction to Ada Pyrrhus Software Enduring Solutions Strong Typing Each object has a clearly defined set of values. Prevents confusion between logically distinct concepts. Thus, many errors are detected by the compiler.

14 Morning Ada Concepts - 14Introduction to Ada Pyrrhus Software Enduring Solutions Ada Library All interfaces between modules can be checked during the compilation phase. Often this capability is left to the debugger in many other languages. Early detection by the Ada compiler increases the development of reliable code and decreases the cost of development.

15 Morning Ada Concepts - 15Introduction to Ada Pyrrhus Software Enduring Solutions Strong Typing procedure TESTER is type APPLES is range ; -- an integer type type ORANGES is range ; --ditto A : APPLES := 100;- - initialized during declaration B : APPLES; O : ORANGES; begin B := A; O := A;- - ERROR, incompatible types end TESTER; --> A and O are not compatible. Ada enforces “name equivalence” not structural equivalence.

16 Morning Ada Concepts - 16Introduction to Ada Pyrrhus Software Enduring Solutions Overall Program Structure Library of program components Program is composed of library units Services are available in packages stored in a libraries.

17 Morning Ada Concepts - 17Introduction to Ada Pyrrhus Software Enduring Solutions The Library Model Subunit Compiler The Library The subunit is developed with calls to library units The compiler compiles the subunit checking all interfaces with library units. Resolves ambiguities. Places the subunit into the library. The library maintains a record of all compiled subunits and their interfaces.

18 Morning Ada Concepts - 18Introduction to Ada Pyrrhus Software Enduring Solutions Overall Program Structure Context clause -- used to introduce packages and program units into the current compilation. Library Access -- the compiler will check to make sure that the procedure call signature (number of parameters, order & type) is correct. with Sqrt; with Simple_IO; -- context clause procedure Print_Square_Root is begin Simple_IO.Put( Sqrt(2.5) ); -- library access end Print_Square_Root;

19 Morning Ada Concepts - 19Introduction to Ada Pyrrhus Software Enduring Solutions Basics of Ada Once you understand the strong typing and library features of Ada, the basics of the language resembles many other high-level languages. Once we have covered the basic features of the language, we will have the building blocks to construct many highly reliable systems.

20 Morning Ada Concepts - 20Introduction to Ada Pyrrhus Software Enduring Solutions Overall Program Structure Syntax and Lexical Forms Scalar Types Composite Types Data Declarations Expressions Statements Control Statements Subprograms Input/Output

21 Morning Ada Concepts - 21Introduction to Ada Pyrrhus Software Enduring Solutions Syntax and Lexical Forms Lexical Elements: the alphabet A - Z the digits special characters “ # & ‘ ( ) * +, - / : ; _ | the space character identifier ::= identifier_letter {[underline] letter_or_digit} Age : Integer := 21; Pi : constant Real := _26536; Nineteen_Hundred : Year := 19E2; CPI : Real := 2#101.11#; -- equals 5.75 base 10 Still_Thirty : Integer := 16#30#; -- equals 48 base 10

22 Morning Ada Concepts - 22Introduction to Ada Pyrrhus Software Enduring Solutions Scalar & Composite Types Scalar type objects are single values. Composite objects contain components. INTEGER ENUMERATED FIXED FLOAT DISCRETE REAL SCALAR TYPES COMPOSITE TYPES ARRAYRECORDTAGGEDPROTECTED

23 Morning Ada Concepts - 23Introduction to Ada Pyrrhus Software Enduring Solutions Declarations procedure Monkey_Test is type A_String is access String; type A_String_Array is array (Positive range <>) of A_String; function "+" (S : String) return A_String is begin return new String'(S); end "+"; Zoo : constant A_String_Array := (+"Aardvark", +"Baboon", +"Cougar", +“Monkey", +"Elephant", +"Ferret", +"Giraffe", +"Horse"); Monkey : A_String := +"Monkey"; Zoo_Monkeys : Boolean := False;

24 Morning Ada Concepts - 24Introduction to Ada Pyrrhus Software Enduring Solutions Expressions, Statements, Control Statements, and I/O begin -- Monkey_Test for Animal in Zoo’Range loop Zoo_Monkeys := Monkey.all = Zoo( Animal ).all; exit when Zoo_Monkeys; end loop; if Zoo_Monkeys then Put( “There are monkeys in the Zoo” ); end if; end Monkey_Test; Ada 95

25 Morning Ada Concepts - 25Introduction to Ada Pyrrhus Software Enduring Solutions Program Units Subprograms -- Functions and Procedures -- Main Program -- Abstract Operations Tasks -- Parallel Processing -- Real-time Programming -- Interrupt Handling Packages -- Encapsulation -- Information Hiding -- Abstract Data Types Generics -- Templates

26 Morning Ada Concepts - 26Introduction to Ada Pyrrhus Software Enduring Solutions Specifications and Bodies A program unit or subunit "with” ing the package Ada subunit Specification Body Data

27 Morning Ada Concepts - 27Introduction to Ada Pyrrhus Software Enduring Solutions Subprogram Units Specification Body ABSTRACTION "What" the program unit does All the user of the program unit needs to know INFORMATION HIDING "How" the program unit does what it does The details of implementation are inaccessible to the user

28 Morning Ada Concepts - 28Introduction to Ada Pyrrhus Software Enduring Solutions Ada Subprograms Procedures  Perform some action  Call always appears as a statement. Functions  Returns a value  Call always appears in an expression. Specification Body visible hidden

29 Morning Ada Concepts - 29Introduction to Ada Pyrrhus Software Enduring Solutions Parameter Modes The direction in which the value associated with the formal parameter is passed. Three modes  in-- Parameter is initialized by the actual parameter; -- Parameter may only be read  out-- Parameter is an uninitialized variable; -- Parameter may be updated and read  in out-- parameter is initialized by the actual parameter; -- it permits both reading and updating of the -- value of the associated actual parameter. Functions may only have in parameters. The default parameter mode is in.

30 Morning Ada Concepts - 30Introduction to Ada Pyrrhus Software Enduring Solutions Ada Procedures -- Procedure Specification procedure SWAP (PRE, POST: in out INTEGER); -- Procedure Body procedure SWAP (PRE, POST: in out INTEGER) is TEMP: INTEGER := PRE; begin PRE := POST; POST:= TEMP; end SWAP; -- Procedure Calls SWAP (MY_COUNT, YOUR_COUNT); SWAP (MY_COUNT, POST => YOUR_COUNT); SWAP (PRE => MY_COUNT, POST => YOUR_COUNT);

31 Morning Ada Concepts - 31Introduction to Ada Pyrrhus Software Enduring Solutions Specification vs. Body Required to allow calling of procedure SWAP prior to writing the actual body The specification allows the compiler to check the interface for units that call SWAP This is a COMPILABLE unit! -- Procedure Specification procedure SWAP (PRE, POST: in out INTEGER);

32 Morning Ada Concepts - 32Introduction to Ada Pyrrhus Software Enduring Solutions Specification vs. Body This is required before the compiled unit can actually run. NOTE: if you are not making SWAP a library unit (i.e. if it is embedded inside another unit) the separate specification is not required. A body always implies a specification. -- Procedure Body procedure SWAP (PRE, POST: in out INTEGER) is TEMP: INTEGER := PRE; begin PRE := POST; POST:= TEMP; end SWAP;

33 Morning Ada Concepts - 33Introduction to Ada Pyrrhus Software Enduring Solutions Ada Functions -- Function Specification function SQRT (ARG: FLOAT) return FLOAT; -- Function Body function SQRT (ARG: FLOAT) return FLOAT is RESULT: FLOAT; begin -- algorithm for computing RESULT goes here return RESULT; end SQRT; -- Function Call -- (Assumes STANDARD_DEV and VARIANCE are of type FLOAT) STANDARD_DEV := SQRT (VARIANCE);

34 Morning Ada Concepts - 34Introduction to Ada Pyrrhus Software Enduring Solutions Ada Packages The PACKAGE is the primary means of “extending” the Ada language. The PACKAGE hides information in the body thereby enforcing the abstraction represented by the specification. Operations (subprograms, functions, etc.) whose specification appear in the package specification must have their body in the package body. Other units (subprograms, functions, etc. ) as well as other types, objects, etc. may also appear in the package body. If so, they are not visible outside of the package body. object operation Specification Body

35 Morning Ada Concepts - 35Introduction to Ada Pyrrhus Software Enduring Solutions Ada Packages -- Package Specification package RUBIK is type CUBE is private; procedure GET (C : out CUBE); procedure SOLVE (C : in out CUBE); procedure DISPLAY (C : in CUBE); BAD_CUBE : exception; private type CUBE is also considered “hidden” end RUBIK; -- Package Body package body RUBIK is -- all bodies of subprograms found in the package spec go here -- along with any other local declarations that should be -- kept "hidden“ from the user procedure GET (C : out CUBE) is... procedure SOLVE (C : in out CUBE) is... procedure DISPLAY (C : in CUBE) is... end RUBIK;

36 Morning Ada Concepts - 36Introduction to Ada Pyrrhus Software Enduring Solutions Package Usage with RUBIK; with Ada.Text_IO; procedure MAIN is MY_CUBE : RUBIK.CUBE; begin RUBIK.GET(MY_CUBE); RUBIK.SOLVE(MY_CUBE); RUBIK.DISPLAY(MY_CUBE); exception when RUBIK.BAD_CUBE => Ada.Text_IO.PUT_LINE("You've got a bad one"); end MAIN; CUBE GET SOLVE DISPLAY BAD_CUBE RUBIK MAIN

37 Morning Ada Concepts - 37Introduction to Ada Pyrrhus Software Enduring Solutions Direct Visibility package MEASURES is type AREA is private; type LENGTH is private; function "+" (LEFT, RIGHT : LENGTH) return LENGTH; function "*" (LEFT, RIGHT : LENGTH) return AREA; private type LENGTH is range ; type AREA is range ; end MEASURES; with MEASURES; use MEASURES; --direct visibility procedure MEASUREMENT is SIDE1,SIDE2 : LENGTH; FIELD : AREA; begin …... FIELD := SIDE1 * SIDE2; -- Infix notation of -- user-defined operation end MEASUREMENT;

38 Morning Ada Concepts - 38Introduction to Ada Pyrrhus Software Enduring Solutions Indirect Visibility package MEASURES is type AREA is private; type LENGTH is private; function "+" (LEFT, RIGHT : LENGTH) return LENGTH; function "*" (LEFT, RIGHT : LENGTH) return AREA; private type LENGTH is range ; type AREA is range ; end MEASURES; with MEASURES; --NOTE: No “use”clause procedure MEASUREMENT is SIDE1,SIDE2 : MEASURES.LENGTH; FIELD : MEASURES.AREA; begin …... FIELD := MEASURES."+"(SIDE1, SIDE2); end MEASUREMENT;

39 Morning Ada Concepts - 39Introduction to Ada Pyrrhus Software Enduring Solutions Use Type with MEASURES; -- NOTE: No “use”clause procedure MEASUREMENT is use MEASURES.LENGTH; -- direct visibility of the type LENGTH use MEASURES.AREA; -- direct visibility of the type AREA SIDE1,SIDE2 : LENGTH; FIELD : AREA; begin …... FIELD := SIDE1+ SIDE2; end MEASUREMENT;

40 Morning Ada Concepts - 40Introduction to Ada Pyrrhus Software Enduring Solutions “use” considered harmful Leads to problems during maintenance. Makes debugging difficult. Pollutes the name space. use

41 Morning Ada Concepts - 41Introduction to Ada Pyrrhus Software Enduring Solutions “Use” clause considered harmful package MEASURES is type AREA is private; type LENGTH is private; function Add_Length (LEFT, RIGHT : LENGTH) return LENGTH; function Calc_Area (LEFT, RIGHT : LENGTH) return AREA; private type LENGTH is range ; type AREA is range ; end MEASURES;

42 Morning Ada Concepts - 42Introduction to Ada Pyrrhus Software Enduring Solutions “Use” considered harmful with MEASURES; -- NOTE: no “use” clause procedure MEASUREMENT is SIDE1, SIDE2 : MEASURES.LENGTH; FIELD : MEASURES.AREA; begin …... FIELD := MEASURES.Calc_Area (Side1, Side2); end MEASUREMENT;

43 Morning Ada Concepts - 43Introduction to Ada Pyrrhus Software Enduring Solutions Ada Tasks The TASK concept in Ada provides a model of parallelism which encompasses: multicomputers multiprocessors interleaved execution. In Ada, the method of communication between tasks is known as "rendezvous“. Ada "draws up" into the language certain capabilities previously performed only by the operating system. Specification Body entry

44 Morning Ada Concepts - 44Introduction to Ada Pyrrhus Software Enduring Solutions Ada Tasks Each task is a separately executing unit (with it’s own stack) A task starts running as soon as the parent starts executing Once a task starts, it runs independently of the parent If desired, a task can communicate with other running units by passing data. Data is passed via “Entry Points” using rendezvous (calls)

45 Morning Ada Concepts - 45Introduction to Ada Pyrrhus Software Enduring Solutions Task Communication -- Task Specifications task TASK_1; -- no entries task TASK_2 is entry PASS_DATA (N : INTEGER); end TASK_2; -- Task Bodies task body TASK_1 is TASK_2.PASS_DATA(17); -- an entry call end TASK_1; task body TASK_2 is accept PASS_DATA (N : INTEGER) do -- statements to be executed during rendezvous end PASS_DATA; end TASK_2;

46 Morning Ada Concepts - 46Introduction to Ada Pyrrhus Software Enduring Solutions Ada Tasks Simplest form: no communication Tasks always belong to someone (MAIN) MAIN can not end until all dependent tasks end Execution of T1 and T2 begins here procedure MAIN is task T1; task T2; task body T1 is -- local declarations begin -- do something end T1; task body T2 is -- local declarations begin -- do something end T2; begin -- MAIN null; end MAIN;

47 Morning Ada Concepts - 47Introduction to Ada Pyrrhus Software Enduring Solutions Tasks are Operating System Independent OS TRADITIONAL Model Of Concurrency OS The Ada Concurrency Model

48 Morning Ada Concepts - 48Introduction to Ada Pyrrhus Software Enduring Solutions Buffer Manager task Buffer is entry Put( X : Item ); entry Get( Z : out Item ); end Buffer; task body Buffer is Full : Boolean := False; Y : Item; begin loop select when not Full => accept Put( X : Item ) do Y := X; Full := True; end; or when Full => accept Get( Z : out Item ) do Z := Y; Full := False; end; or terminate; end select; end loop; end Buffer;

49 Morning Ada Concepts - 49Introduction to Ada Pyrrhus Software Enduring Solutions Buffer Manager in Use task body Producer is X : Item; begin loop -- Produce X Buffer.Put( X ); end loop; end Producer; task body Consumer is Z : Item; begin loop Buffer.Get( Z ); -- Consume Z end loop; end Consumer; ProducerConsumer Buffer Put(X)Get(X)

50 Morning Ada Concepts - 50Introduction to Ada Pyrrhus Software Enduring Solutions Typing

51 Morning Ada Concepts - 51Introduction to Ada Pyrrhus Software Enduring Solutions Ada Types An Ada type is a template for objects  a set of values which are meaningful  a set of operations on the objects (values) All objects must be declared and objects of different types cannot be implicitly mixed in operations TYPES are not operated upon directly, objects can be operated upon. Types are a means of declaring OBJECTS (instances of the type). type object

52 Morning Ada Concepts - 52Introduction to Ada Pyrrhus Software Enduring Solutions Classes of Ada Types Ada Types Scalar Composite Private Task Access Objects are single values Objects contain components Objects point to other objects & subprograms Objects are abstract Objects are parallel processes

53 Morning Ada Concepts - 53Introduction to Ada Pyrrhus Software Enduring Solutions Predefined Types In Ada, most types SHOULD be defined by the user.  Model real-world values  Matches the “abstraction” being programmed  Limits and ranges are detected early Nevertheless, there are some predefined types.

54 Morning Ada Concepts - 54Introduction to Ada Pyrrhus Software Enduring Solutions Predefined Types INTEGER ENUMERATED FIXED FLOAT DISCRETE REAL SCALAR TYPES Integer Natural Positive Mod Long_Integer Short_Integer Boolean Character Wide_Character Duration Decimal Float

55 Morning Ada Concepts - 55Introduction to Ada Pyrrhus Software Enduring Solutions Integer Types An Integer type characterizes a set of whole number values and a set of operations on whole numbers. type DEPTH is range ; type ROWS is range 1.. 8; type LINES is range ; subtype TERMINAL is LINES range ; ROW_COUNT : ROWS; LINE_COUNT : LINES := 1; CRT : TERMINAL := 16; FATHOMS : constant DEPTH := -100;

56 Morning Ada Concepts - 56Introduction to Ada Pyrrhus Software Enduring Solutions Enumeration Types Enumeration types are defined by enumeration of their values. type COLOR is (WHITE, RED, YELLOW, GREEN, BLUE); type LIGHT is (RED, AMBER, GREEN); type GEAR_POSITION is (UP, DOWN, NEUTRAL); type SUITS is (CLUBS, DIAMONDS, HEARTS, SPADES); subtype MAJORS is SUITS range HEARTS..SPADES; type BOOLEAN is (FALSE, TRUE); -- predefined type HUE : COLOR; SHIFT : GEAR_POSITION := GEAR_POSITION'last; T : constant BOOLEAN := TRUE; HIGH : MAJORS := CLUBS; -- invalid

57 Morning Ada Concepts - 57Introduction to Ada Pyrrhus Software Enduring Solutions Character Type The predefined type Character is a character type whose values correspond to the 256 code positions of Row 00 (also known as Latin-1) of the ISO Basic Multilingual Plane (BMP). type ROMAN_DIGIT is ('I', 'V', 'X', 'L', 'C', 'D', 'M'); type VOWELS is ('A', 'E', 'I', 'O', 'U'); subtype FORTRAN_CONVENTION is CHARACTER range 'I'.. 'N'; INDEX : FORTRAN_CONVENTION :='K'; ROMAN_100 : constant ROMAN_DIGIT := 'C'; MY_CHAR : CHARACTER;

58 Morning Ada Concepts - 58Introduction to Ada Pyrrhus Software Enduring Solutions Composite Types Building block for complex types. Combination of any types. COMPOSITE TYPES ARRAYRECORDTAGGEDPROTECTED

59 Morning Ada Concepts - 59Introduction to Ada Pyrrhus Software Enduring Solutions Constrained Arrays type TABLE is array(INTEGER range 1.. 5) of FLOAT; MY_LIST : TABLE := (3.7, 14.2, -6.5, 0.0, 1.0); MY_LIST (4) := 7.3; MY_LIST 7.3

60 Morning Ada Concepts - 60Introduction to Ada Pyrrhus Software Enduring Solutions Constrained Arrays type DAYS is (SUN, MON, TUE, WED, THU, FRI, SAT); type WEEK_ARRAY is array (DAYS) of BOOLEAN; MY_WEEK : WEEK_ARRAY := (MON.. FRI => TRUE, others => FALSE); if MY _WEEK (THU) = TRUE then... if MY_WEEK (THU) then … SUNFALSE MONTRUE TUETRUE WEDTRUE THUTRUE FRITRUE SATFALSE MY_WEEK

61 Morning Ada Concepts - 61Introduction to Ada Pyrrhus Software Enduring Solutions Unconstrained Arrays Index type and component type bound to array type. Index range bound to objects, not type. Allows for general purpose subprograms. Includes the Ada String type. type SAMP is array (INTEGER range <>) of FLOAT; LARGE : SAMP (1.. 5) := (2.5, 3.4, 1.0, 0.0, 4.4); SMALL : SAMP (2.. 4) := (others => 5.0); LARGE SMALL

62 Morning Ada Concepts - 62Introduction to Ada Pyrrhus Software Enduring Solutions Record Type type DATE is record DAY : INTEGER range ; MONTH : MONTH_TYPE; YEAR : INTEGER range ; end record; TODAY : DATE; DAY MONTH DAY

63 Morning Ada Concepts - 63Introduction to Ada Pyrrhus Software Enduring Solutions Record Types Record components may have default initial values for their components. type DATE is record DAY : INTEGER range := 15; MONTH : MONTH_TYPE := January; YEAR : INTEGER range := 1968; end record; FDJ_BDAY : DATE; TODAY : DATE := (7, December, 2003); -- an aggregate FDJ_BDAY.DAY = 15 FDJ_BDY.MONTH = January FDJ_BDY.YEAR = 1968 if FDJ_BDAY /= (15, December, 1968) then …

64 Morning Ada Concepts - 64Introduction to Ada Pyrrhus Software Enduring Solutions Ada Strings type STRING is array (Positive range <>) of Character; -- predefined type STR_5 : STRING (1.. 5); STR_7 : STRING (1.. 6) := "SIGAda"; WARNING : constant STRING := "DANGER"; STR_6 WARNING subtype TEN_LONG is STRING(1.. 10); FIRST_TEN : TEN_LONG := "Header "; FIRST_TEN SIGAda DANGER Header

65 Morning Ada Concepts - 65Introduction to Ada Pyrrhus Software Enduring Solutions Access Types Also known as “pointers” type NODE; type PTR is access NODE; type NODE is record FIELD1 : SOME_TYPE; FIELD2 : SOME_TYPE; FIELD3 : PTR; end record; TOP : PTR; -- an access object TOP : PTR := new NODE; -- an allocator TOP.FIELD3 := new NODE; -- another allocator

66 Morning Ada Concepts - 66Introduction to Ada Pyrrhus Software Enduring Solutions Access Types Pool-specific access types General access types Access parameters Access discriminants Access to subprograms Storage pool management

67 Morning Ada Concepts - 67Introduction to Ada Pyrrhus Software Enduring Solutions General Access Types type Int_Ptr is access all Integer; IP : Int_Ptr; type Const_Int_Ptr is access constant Integer; CIP : Const_Int_Ptr; I : aliased Integer; C : aliased constant Integer := 1815; J : Integer := 6; … IP := I’Access; -- access to a variable CIP := C’Access; -- access to a constant IP.all := CIP.all; CIP.all := I; -- cannot assign to a constant CIP := I’Access; -- read only access to a variable IP := J’Access; -- illegal, J is not marked as aliased

68 Morning Ada Concepts - 68Introduction to Ada Pyrrhus Software Enduring Solutions Private Types Actual type description is "hidden" The type is primarily known through its operations Private types are always implemented by packages Private types protect data from erroneous access Two types  Private -- assignment, (in) equality and all explicitly declared operations  Limited Private -- assignment and all explicitly declared operations

69 Morning Ada Concepts - 69Introduction to Ada Pyrrhus Software Enduring Solutions Private Types Password is “hidden” in that the compiler will enforce limitations on what you can do with any variable of type PASSWORD_TYPE. If you “with PASSWORD_CHECK” in another unit, you have access to PASSWORD_TYPE and the two procedures, but you may NOT perform ANY other operations on a variable of type PASSWORD_TYPE. package PASSWORD_CHECK is type PASSWORD_TYPE is limited private; procedure GET_PASSWORD (Value : in out PASSWORD_TYPE); procedure VERIFY_PASSWORD (Value : in out PASSWORD_TYPE); private type PASSWORD_TYPE is STRING(1..20); end PASSWORD_CHECK;

70 Morning Ada Concepts - 70Introduction to Ada Pyrrhus Software Enduring Solutions Ada Statements := null procedure call return declare SEQUENTIAL if then elsif else case CONDITIONAL delay entry call abort accept select requeue then abort TASKING loop exit for while ITERATIVE raise goto OTHERS All block statements are terminated with an end (end if, … )

71 Morning Ada Concepts - 71Introduction to Ada Pyrrhus Software Enduring Solutions If Statements if TODAY = (29, May, 2004) then MOMS_YEARS := MOMS_YEARS + 1; GET (BIRTHDAY_CARD); end if; if IS_ODD (NUMBER) then ODD_TOTAL := ODD_TOTAL + 1; else EVEN_TOTAL := EVEN_TOTAL + 1; end if; if SCORE >= 90 then GRADE := 'A'; elsif SCORE >= 80 then GRADE := 'B'; elsif SCORE >= 70 then GRADE := 'C'; elsif SCORE >= 60 then GRADE := 'D'; else GRADE := 'F'; end if;

72 Morning Ada Concepts - 72Introduction to Ada Pyrrhus Software Enduring Solutions Case Statements procedure SWITCH (HEADING : in out DIRECTION) is begin case HEADING is when NORTH => HEADING := SOUTH; when EAST => HEADING := WEST; when SOUTH => HEADING := NORTH; when WEST => HEADING := EAST; end case; end SWITCH; case NUMBER is when 2 => DO_SOMETHING; when 3 | 7 | 8 => DO_SOMETHING_ELSE; when => DO_SOMETHING RADICAL; when others => PUNT; end case;

73 Morning Ada Concepts - 73Introduction to Ada Pyrrhus Software Enduring Solutions Loop Statements loop GET_SAMPLES; exit when EXHAUSTED; PROCESS_SAMPLES; end loop; for i in 1..5 loop GET_IT(NUMBER); MODIFY_IT (NUMBER); end loop; while DATA_REMAINS loop end loop;

74 Morning Ada Concepts - 74Introduction to Ada Pyrrhus Software Enduring Solutions What makes an Ada loop unique? You can only increment or decrement (no skipping values). The Loop Control Variable (LCV) is implicitly declared inside the loop. The LCV disappears when the loop terminates.

75 Morning Ada Concepts - 75Introduction to Ada Pyrrhus Software Enduring Solutions Loop Control Variable …. COUNTER : Integer := 0; … for Counter in loop if My_Array(Counter) = SOME_VALUE then exit; end if; end loop; Put (Counter); -- Prints out the value of Counter QUESTION - what is the value of Counter for the Put operation?

76 Morning Ada Concepts - 76Introduction to Ada Pyrrhus Software Enduring Solutions Sample Program -- To exemplify some of the Ada statements, let's look -- at the implementation of a 'wrap-around’ successor -- function for type DAYS. procedure TEST is type DAYS is (SUN, MON, TUE, WED, THU, FRI, SAT); TODAY : DAYS;-- Object declarations TOMORROW : DAYS; function WRAP (D : DAYS) return DAYS is... begin... TOMORROW := WRAP(TODAY);... end TEST;

77 Morning Ada Concepts - 77Introduction to Ada Pyrrhus Software Enduring Solutions Sample Program function WRAP (D : DAYS) return DAYS is begin case D is when SUN => return MON; when MON => return TUE; when TUE => return WED; when WED => return THU; when THU => return FRI; when FRI => return SAT; when SAT => return SUN; end case; end WRAP ;

78 Morning Ada Concepts - 78Introduction to Ada Pyrrhus Software Enduring Solutions Sample Program function WRAP (D : DAYS ) return DAYS is begin return DAYS'SUCC(D); exception when CONSTRAINT_ERROR => return DAYS'FIRST; end WRAP;

79 Morning Ada Concepts - 79Introduction to Ada Pyrrhus Software Enduring Solutions Exceptions Hndling all pssible erros! There are four errors in this slide!

80 Morning Ada Concepts - 80Introduction to Ada Pyrrhus Software Enduring Solutions Exceptions Exceptions are a mechanism to allow the programmer to identify and handle errors within the program without calling system error routines. There are two kinds of exceptions:  predefined exceptions Standard (Constraint, Program, Storage, Tasking ) IO (Status, Mode, Name, Data, Layout, End, Device, Use).  user-defined exceptions Allows the designer to define and raise non-standard exceptions.

81 Morning Ada Concepts - 81Introduction to Ada Pyrrhus Software Enduring Solutions Exceptions Exception handlers handle the exception.  You can also name and save an exception externally.  An exception can be re-raised and it can be propagated.

82 Morning Ada Concepts - 82Introduction to Ada Pyrrhus Software Enduring Solutions Exception Example procedure Check_Data is My_Error : exception; AGE : Positive; DATA : string (1..10) := (others => ‘ ‘); begin Get_AGE (Age); Get_DATA (DATA); if DATA = “David Cook” then raise My_Error; end if; exception when Constraint_Error => …. when My_Error => …. end;

83 Morning Ada Concepts - 83Introduction to Ada Pyrrhus Software Enduring Solutions Generics Generics define a template or mold for program units.

84 Morning Ada Concepts - 84Introduction to Ada Pyrrhus Software Enduring Solutions Generics Dictionary Definition: a. Relating to, or characteristic of, a whole group or class b. General Defines a template for a general purpose program units. The template must be instantiated prior to use in a program. Strong typing makes generics a necessary language feature.

85 Morning Ada Concepts - 85Introduction to Ada Pyrrhus Software Enduring Solutions Generics Generics are like high-level macros. We parameterize them at compile time because we can't pass types or subprograms at execution time. Parameters to generics can be types subprograms Different objects Different Types values Types (Ordered) With a generic, we can pass a type and reuse one general routine or algorithm or a variety of data types. Generic Sort

86 Morning Ada Concepts - 86Introduction to Ada Pyrrhus Software Enduring Solutions Swap Routine procedure Integer_Swap(Integer_1 : in out Integer; Integer_2 : in out Integer) is Temp : Integer := 0; begin Temp := Integer_1; Integer_1 := Integer_2; Integer_2 := Temp; end Integer_Swap;

87 Morning Ada Concepts - 87Introduction to Ada Pyrrhus Software Enduring Solutions Generic Swap Routine generic type ELEMENT is private; procedure SWAP(ITEM_1 : in out ELEMENT; ITEM_2 : in out ELEMENT); procedure SWAP(ITEM_1 : in out ELEMENT; ITEM_2 : in out ELEMENT) is TEMP : ELEMENT; begin TEMP := ITEM_1; ITEM_1 := ITEM_2; ITEM_2 := TEMP; end Swap; spec body Generic parameter Generic formal part

88 Morning Ada Concepts - 88Introduction to Ada Pyrrhus Software Enduring Solutions Using Generics with Swap; procedure Example is procedure Int_Swap is new Swap(Integer); procedure Chr_Swap is new Swap(Character); Num1, Num2 : Integer; Char1, Char2 : Character; begin Num1 := 10; Num2 := 3; Int_Swap (Num1, Num2); Char1 := 'A'; Char2 := 'Z'; Chr_Swap (Char1, Char2); end Example;

89 Morning Ada Concepts - 89Introduction to Ada Pyrrhus Software Enduring Solutions Summary of Morning Subprograms Packages Tasks Types and Objects Statements Exception Handlers Generics

90 Morning Ada Concepts - 90Introduction to Ada Pyrrhus Software Enduring Solutions Thank You


Download ppt "Morning Ada Concepts - 1Introduction to Ada Pyrrhus Software Enduring Solutions Introduction to Ada 95 Joyce L Tokar, PhD"

Similar presentations


Ads by Google