ada 95 - programming in the large

90
1 http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License Franco Gasperoni [email protected] http://libre.act-europe.fr

Upload: gneuromante-canaladaorg

Post on 01-Nov-2014

1.416 views

Category:

Technology


1 download

DESCRIPTION

Author: Franco Gasperoni. License: GFDL

TRANSCRIPT

Page 1: Ada 95 - Programming in the large

1http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Franco [email protected]://libre.act-europe.fr

Page 2: Ada 95 - Programming in the large

2http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Copyright NoticeCopyright Notice

• © ACT Europe under the GNU Free Documentation License

• Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; provided its original author is mentioned and the link to http://libre.act-europe.fr/ is kept at the bottom of every non-title slide. A copy of the license is available at:

• http://www.fsf.org/licenses/fdl.html

Page 3: Ada 95 - Programming in the large

3http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Page 4: Ada 95 - Programming in the large

4http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

What is the largest What is the largest software systemsoftware systemyou have built ?you have built ?

(in SLOC)(in SLOC)

Page 5: Ada 95 - Programming in the large

5http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Small Software SystemsSmall Software Systems

• Understandable by 1 person

• Can throw away & replace it to– repair / extend– port to new platform

• Anything is OK for small systems (< 10 Ksloc)

Page 6: Ada 95 - Programming in the large

6http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Medium/Large SystemsMedium/Large Systems

• Team of people• No 1 person knows all its aspects• Long life-span (> 10 years)• CANNOT throw away & replace it to

– repair / extend– port to new platform

• Requires organization, discipline & right tools

Page 7: Ada 95 - Programming in the large

7http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• Programming in the Large– specification & implementation– privacy– abstract data types– hierarchical packages

Page 8: Ada 95 - Programming in the large

8http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Separate CompilationSeparate CompilationTHE

PROBLEM

Compiler objectCODE

Compiler objectCODE

Compiler objectCODE

Linker

executable

libraries

Page 9: Ada 95 - Programming in the large

9http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Problem with this approachProblem with this approach

CODE

CODE

CODE

• No structure

• To write your own code– YOU MUST understand

everybody else’s code

CODE

CODE

Page 10: Ada 95 - Programming in the large

10http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

IdeaIdea

SPECIFY WHAT EACH

MODULESHOULD DO

Page 11: Ada 95 - Programming in the large

11http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

SPECIFICATION

Software module

????

BODY

Page 12: Ada 95 - Programming in the large

12http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• Programming in the Large– specification & implementation

• specification• implementation• specification rules in Ada

Page 13: Ada 95 - Programming in the large

13http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

A Specification is a ...A Specification is a ...

CONTRACTCONTRACTCONTRACTCONTRACTImplementorof themodule

Users/clientsof themodule

• On the SERVICES provided by the module

Page 14: Ada 95 - Programming in the large

14http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• SPEC = list of services provided

• BODY = implementation of the services (hidden)

Software module

Service_1Service_2Service_3

Service_1implementation

Service_2implementation

Service_3implementation

Page 15: Ada 95 - Programming in the large

15http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

SPECIFICATION

????BODY

Page 16: Ada 95 - Programming in the large

16http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

ExampleExample

• Create a Queue module that can– Add an Integer to the Queue– See the First integer in the Queue– Get the first integer in the Queue– Test whether the Queue is Empty

Page 17: Ada 95 - Programming in the large

17http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package Queue isprocedure Add (Element : Integer);

function First return Integer;function Get return Integer;

function Empty return Boolean;end Queue;

package Queue isprocedure Add (Element : Integer);

function First return Integer;function Get return Integer;

function Empty return Boolean;end Queue;

queue.adsqueue.ads

Page 18: Ada 95 - Programming in the large

18http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package Queue

????

BODY

package Queue isprocedure Add (Element : Integer);

function First return Integer;function Get return Integer;

function Empty return Boolean;end Queue;

package Queue isprocedure Add (Element : Integer);

function First return Integer;function Get return Integer;

function Empty return Boolean;end Queue;

queue.adsqueue.ads

Page 19: Ada 95 - Programming in the large

19http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Using package QueueUsing package Queue

with Queue;procedure Client is

Queue_Error : exception;X : Integer;

beginQueue.Add (3);Queue.Add (4);

if not Queue.Empty thenX := Queue.Get;

elseraise Queue_Error;

end if;end Client;

client.adb

Page 20: Ada 95 - Programming in the large

20http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Specifications Reduce ComplexitySpecifications Reduce Complexity

SPECSPEC

SPECSPEC

SPECSPECSPECSPEC

• To write your own code– only need to understand

specs for the services you need

Page 21: Ada 95 - Programming in the large

21http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package Queue????

package Queue isprocedure Add (Element : Integer);

function First return Integer;function Get return Integer;

function Empty return Boolean;end Queue;

package Queue isprocedure Add (Element : Integer);

function First return Integer;function Get return Integer;

function Empty return Boolean;end Queue;

queue.adsqueue.ads

To write Client only need to look atTo write Client only need to look at

Page 22: Ada 95 - Programming in the large

22http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Aside: use clauseAside: use clause

with Queue; use Queue;procedure Client is

Queue_Error : exception;X : Integer;

beginQueue. Add (3);Queue. Add (4);

if not Queue. Empty thenX := Queue. Get;

elseraise Queue_Error;

end if;end Client;

Page 23: Ada 95 - Programming in the large

23http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• Programming in the Large– specification & implementation

• specification• implementation• specification rules in Ada

Page 24: Ada 95 - Programming in the large

24http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

ONEONEONEONEONEONEONEONE possible possible implementationimplementation

of packageof packageQueueQueue

This implementationThis implementationraises Constraint_Errorraises Constraint_Errorif more than Max_Sizeif more than Max_Size

elements are put in the Queue.elements are put in the Queue.

Page 25: Ada 95 - Programming in the large

25http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Circular BufferCircular Buffer

Q

0 1 Max_Size - 1

Q_Last Q_First

Page 26: Ada 95 - Programming in the large

26http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package body Queue isMax_Size : constant := 100;

type Q_Index is mod Max_Size;Q : array (Q_Index range 0 .. Max_Size - 1) of Integer;

Q_First : Q_Index := Q ’ First;Q_Last : Q_Index := Q_First;Size : Natural range 0 .. Max_Size;

procedure Add (Element : Integer) isbegin

Q (Q_Last) := Element;Q_Last := Q_Last + 1;Size := Size + 1;

end Add;...

end Queue;

queue.adb

Page 27: Ada 95 - Programming in the large

27http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package body Queue is...function First return Integer isbegin

return Q (Q_First);end First;

function Get return Integer isbegin

Q_First := Q_First + 1;Size := Size - 1;return Q (Q_First - 1);

end Get;

function Empty return Boolean isbegin

return Size = 0;end Empty;

end Queue;

queue.adb

Page 28: Ada 95 - Programming in the large

28http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

ANOTHERANOTHERANOTHERANOTHERANOTHERANOTHERANOTHERANOTHER possible possible implementationimplementation

of packageof packageQueueQueue

Page 29: Ada 95 - Programming in the large

29http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Linked ListLinked List

Q_LastQ_First

Free

Page 30: Ada 95 - Programming in the large

30http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package body Queue istype Queue_Element;type Element_Ptr is access Queue_Element;

type Queue_Element is recordVal : Integer;Next : Element_Ptr;

end record;

Q_First : Element_Ptr;Q_Last : Element_Ptr;

Free : Element_Ptr := new Queue_Element;...

end Queue;

queue.adb

Page 31: Ada 95 - Programming in the large

31http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package body Queue is...procedure Add (Element : Integer) isbegin

if Q_First = null thenQ_First := Free;

elseQ_Last.Next := Free;

end if;Q_Last := Free;Free := Free.Next;Q_Last.all := (Element, null);if Free = null then

Free := new Queue_Element;end if;

end Add;...

end Queue;

queue.adb

Page 32: Ada 95 - Programming in the large

32http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package body Queue is...function Get return Integer is

Tmp : Element_Ptr := Q_First;begin

Q_First := Q_First.Next;if Q_First = null then

Q_Last := null;end if;Tmp.Next := Free;Free := Tmp;return Tmp.Val;

end Get;...

end Queue;

queue.adb

Page 33: Ada 95 - Programming in the large

33http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package body Queue is...function First return Integer isbegin

return Q_First;end First;

function Empty return Boolean isbegin

return Q_First = null;end Empty;

end Queue;

queue.adb

Page 34: Ada 95 - Programming in the large

34http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

A Spec can have several A Spec can have several implementationsimplementations

• Can change implementation

• WITHOUT having to change ANY of the client’s code

package Queue isprocedure Add (Element : Integer);

function First return Integer;function Get return Integer;

function Empty return Boolean;end Queue;

package Queue isprocedure Add (Element : Integer);

function First return Integer;function Get return Integer;

function Empty return Boolean;end Queue;

queue.adsqueue.ads

firstimplement.

firstimplement.

secondimplement.second

implement.

Page 35: Ada 95 - Programming in the large

35http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• Programming in the Large– specification & implementation

• specification• implementation• specification rules in Ada

Page 36: Ada 95 - Programming in the large

36http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

In AdaIn Ada

• Spec always checked against implementation

• Must with the specs that you are going to use (not in C)

• Packages provide multiple name spaces

Page 37: Ada 95 - Programming in the large

37http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Spec is checked against its bodySpec is checked against its body

package Queue isprocedure Add (Element : Integer);...

end Queue;

package Queue isprocedure Add (Element : Integer);...

end Queue;

package body Queue is...procedure Add (Element : Integer; X : Float) is

...end Add;...

end Queue;

package body Queue is...procedure Add (Element : Integer; X : Float) is

...end Add;...

end Queue;

Compilationerror

Page 38: Ada 95 - Programming in the large

38http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Must with Specs usedMust with Specs used

with Queue;procedure Client is

...begin

Queue.Add (3);...

end Client;

Compilationerror

Page 39: Ada 95 - Programming in the large

39http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Multiple Name SpacesMultiple Name Spacespackage Queue is

procedure Add (E : Integer);...

end Queue;

package Queue isprocedure Add (E : Integer);...

end Queue;

package Set isprocedure Add (E : Integer);...

end Set;

package Set isprocedure Add (E : Integer);...

end Set;

with Queue;with Set;procedure Client isbegin

Queue.Add (3);

Set.Add (99);end Client;

Page 40: Ada 95 - Programming in the large

40http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Use Clause and AmbiguitiesUse Clause and Ambiguitiespackage Queue is

procedure Add (E : Integer);...

end Queue;

package Queue isprocedure Add (E : Integer);...

end Queue;

package Set isprocedure Add (E : Integer);...

end Set;

package Set isprocedure Add (E : Integer);...

end Set;

with Queue; use Queue;with Set; use Set;procedure Client isbegin

Add (123);end Client;

Compilationerror

ambiguity

Page 41: Ada 95 - Programming in the large

41http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

But … Ada has overloadingBut … Ada has overloadingpackage Queue is

procedure Add (E : Integer);procedure Add (E : Float);...

end Queue;

package Queue isprocedure Add (E : Integer);procedure Add (E : Float);...

end Queue;

with Queue; use Queue;

procedure Client isbegin

Add (123);

Add (3.141);end Client;

Page 42: Ada 95 - Programming in the large

42http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• Programming in the Large– specification & implementation– privacy– abstract data types– hierarchical packages

Page 43: Ada 95 - Programming in the large

43http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package Queues istype Queue is …;

procedure Add (Q : Queue; Element : Integer);function First (Q : Queue) return Integer;function Get (Q : Queue) return Integer;function Empty (Q : Queue) return Boolean;

end Queues;

package Queues istype Queue is …;

procedure Add (Q : Queue; Element : Integer);function First (Q : Queue) return Integer;function Get (Q : Queue) return Integer;function Empty (Q : Queue) return Boolean;

end Queues;

Having Several QueuesHaving Several Queues

Page 44: Ada 95 - Programming in the large

44http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package Queues is

type Queue is …;

end Queues;

package Queues is

type Queue is …;

end Queues;

!!! WARNING !!!!!! WARNING !!!

Use Different names

Page 45: Ada 95 - Programming in the large

45http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Using Several QueuesUsing Several Queues

with Queues; use Queues;procedure Client is

Q1 : Queue;Q2 : Queue;

beginAdd (Q1, 123);

Add (Q2, 3);

Add (Q2, Get (Q1));end Client;

Page 46: Ada 95 - Programming in the large

46http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

One possible implementation ...One possible implementation ...

package Queues istype Queue is …;

procedure Add (Q : Queue; Element : Integer);function First (Q : Queue) return Integer;function Get (Q : Queue) return Integer;function Empty (Q : Queue) return Boolean;

end Queues;

type Q_Element;type Element_Ptr is

access Queue_Element;

type Queue_Element is recordVal : Integer;Next : Element_Ptr;

end record;

type Queue is recordFirst : Element_Ptr;Last : Element_Ptr;

end record;

type Q_Element;type Element_Ptr is

access Queue_Element;

type Queue_Element is recordVal : Integer;Next : Element_Ptr;

end record;

type Queue is recordFirst : Element_Ptr;Last : Element_Ptr;

end record;

Page 47: Ada 95 - Programming in the large

47http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Client code allowed to depend Client code allowed to depend on the implementation !on the implementation !

with Queues; use Queues;procedure Client is

Q1 : Queue;Q2 : Queue;

beginAdd (Q1, 123);Add (Q2, 3);

Q2.Last := null;

end Client;

OK

Page 48: Ada 95 - Programming in the large

48http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Another implementation ...Another implementation ...

package Queues istype Queue is …;

procedure Add (Q : Queue; Element : Integer);function First (Q : Queue) return Integer;function Get (Q : Queue) return Integer;function Empty (Q : Queue) return Boolean;

end Queues;

Max_Size : constant := 100;type Q_array (Natural range <>)

of Integer;

type Queue is recordQ : Q_Array (0 .. Max_Size);First : Natural;Last : Natural;Size : Natural;

end record;

Max_Size : constant := 100;type Q_array (Natural range <>)

of Integer;

type Queue is recordQ : Q_Array (0 .. Max_Size);First : Natural;Last : Natural;Size : Natural;

end record;

Page 49: Ada 95 - Programming in the large

49http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

… breaks client code !… breaks client code !

with Queues; use Queues;procedure Client is

Q1 : Queue;Q2 : Queue;

beginAdd (Q1, 123);Add (Q2, 3);

Q2.Last := null;

end Client;

Compilationerror

Page 50: Ada 95 - Programming in the large

50http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Even without changing Even without changing the implementationthe implementationthere is a there is a PROBLEMPROBLEM

with Queues; use Queues;procedure Client is

Q1 : Queue;Q2 : Queue;

beginAdd (Q1, 123);Add (Q2, 3);

Q2.Last := null;

end Client;

Q2 is in aninconsistent

state

33FirstLast

null

Q2:

Page 51: Ada 95 - Programming in the large

51http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

You need PRIVACYYou need PRIVACY

• Exposing your data structures is risky

– Client code may manipulate the structures directly without using your own services

– Client code is hard to change

Page 52: Ada 95 - Programming in the large

52http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

ThinkThink BIGBIG

what if what if the the QueuesQueues package package

is is used byused by 1000s1000s of other of other packagespackages

Page 53: Ada 95 - Programming in the large

53http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• If there is a bug concerning a Queue, you may have to look at 1000s of packages to find the bug

• If you change the implementation you may have to update 1000s of packages

Page 54: Ada 95 - Programming in the large

54http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• Programming in the Large

– privacy• private types• private types & discriminants• limited private types

Page 55: Ada 95 - Programming in the large

55http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Private typesPrivate types

package Queues istype Queue is private;

procedure Add (Q : Queue; Element : Integer);function First (Q : Queue) return Integer;function Get (Q : Queue) return Integer;function Empty (Q : Queue) return Boolean;

privatetype Queue is …;

end Queues;

package Queues istype Queue is private;

procedure Add (Q : Queue; Element : Integer);function First (Q : Queue) return Integer;function Get (Q : Queue) return Integer;function Empty (Q : Queue) return Boolean;

privatetype Queue is …;

end Queues;

Page 56: Ada 95 - Programming in the large

56http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

In any implementation ...In any implementation ...package Queues is

type Queue is private;procedure Add (Q : Queue; Element : Integer);function First (Q : Queue) return Integer;function Get (Q : Queue) return Integer;function Empty (Q : Queue) return Boolean;

privatetype Queue_Element;type Element_Ptr is access Queue_Element;type Queue_Element is record

Val : Integer;Next : Element_Ptr;

end record;type Queue is record

First : Element_Ptr;Last : Element_Ptr;

end record;end Queues;

Page 57: Ada 95 - Programming in the large

57http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

… private types are PRIVATE… private types are PRIVATE

with Queues; use Queues;procedure Client is

Q1 : Queue;Q2 : Queue;

beginAdd (Q1, 123);Add (Q2, 3);

Q2.Last := null;

end Client;

Compilationerror

Page 58: Ada 95 - Programming in the large

58http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Advantages of Advantages of privateprivate typestypes

• Enforces the contract of a specification

• No client code can corrupt your data structures

• Can change implementation without changing client code

Page 59: Ada 95 - Programming in the large

59http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Why is the private part in the spec ?Why is the private part in the spec ?package Queues is

type Queue is private;procedure Add (Q : Queue; Element : Integer);function First (Q : Queue) return Integer;function Get (Q : Queue) return Integer;function Empty (Q : Queue) return Boolean;

privatetype Queue_Element;type Element_Ptr is access Queue_Element;type Queue_Element is record

Val : Integer;Next : Element_Ptr;

end record;type Queue is record

First : Element_Ptr;Last : Element_Ptr;

end record;end Queues;

Page 60: Ada 95 - Programming in the large

60http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

… because we still need to … because we still need to compile the clients codecompile the clients code

with Queues; use Queues;

procedure Client is

Q1 : Queue;

beginAdd (Q1, 123);

end Client;

Page 61: Ada 95 - Programming in the large

61http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

… but you can make a private type … but you can make a private type quite privatequite private

package Queues istype Queue is private;procedure Add (Q : Queue; Element : Integer);function First (Q : Queue) return Integer;function Get (Q : Queue) return Integer;function Empty (Q : Queue) return Boolean;

privatetype Queue_Info;type Queue is access Queue_Info;

end Queues;

Page 62: Ada 95 - Programming in the large

62http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package body Queues is type Queue_Element;type Element_Ptr is access Queue_Element;

type Queue_Element is recordVal : Integer;Next : Element_Ptr;

end record;type Queue_Info is record

First : Element_Ptr;Last : Element_Ptr;

end record;...

end Queues;

Page 63: Ada 95 - Programming in the large

63http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• Programming in the Large

– privacy• private types• private types & discriminants• limited private types

Page 64: Ada 95 - Programming in the large

64http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package Queues istype Queue (Max_Size : Natural) is private;

procedure Add (Q : Queue; Element : Integer);function First (Q : Queue) return Integer;function Get (Q : Queue) return Integer;function Empty (Q : Queue) return Boolean;

privatetype Q_array (Natural range <>) of Integer;

type Queue (Max_Size : Natural) is recordQ : Q_Array (0 .. Max_Size);First : Natural;Last : Natural;Size : Natural;

end record;end Queues;

Page 65: Ada 95 - Programming in the large

65http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

with Queues; use Queues;procedure Client is

Q1 : Queue (100);

Q2 : Queue (250);

beginAdd (Q1, 123);

Add (Q2, 3);end Client;

Q1 can have up to 100 elmts

Q2 can have up to 250 elmts

Page 66: Ada 95 - Programming in the large

66http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• Programming in the Large

– privacy• private types• private types & discriminants• limited private types

Page 67: Ada 95 - Programming in the large

67http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

with Queues; use Queues;procedure Client is

Q1 : Queue;Q2 : Queue;

X : Integer;

beginAdd (Q1, 123);Add (Q1, 3);

Q2 := Q1;

X := Get (Q2);end Client;

Does this affect Q1 ?

Page 68: Ada 95 - Programming in the large

68http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

it depends on … the it depends on … the implementation !implementation !

• If a Queue is implemented with

– a pointer then Get (Q2) MODIFIES Q1

– a record containing an array then Get (Q2) does NOT modify Q1

Page 69: Ada 95 - Programming in the large

69http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

with Queues; use Queues;procedure Client is

Q1 : Queue;Q2 : Queue;X : Integer;

beginAdd (Q1, 123);

Add (Q1, 3);

Q2 := Q1;

X := Get (Q2);X := Get (Q2);end Client;

123123Q1: 33

Q2:

123123Q1:

123123Q1: 33

Q2:

Pointer implementation

Pointer Pointer implementationimplementation

Page 70: Ada 95 - Programming in the large

70http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

limited private typeslimited private types

• NO assignment :=

• NO equality comparison =

Page 71: Ada 95 - Programming in the large

71http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package Queues istype Queue is limited private;

procedure Add (Q : Queue; Element : Integer);function First (Q : Queue) return Integer;function Get (Q : Queue) return Integer;function Empty (Q : Queue) return Boolean;

privatetype Queue is …;

end Queues;

package Queues istype Queue is limited private;

procedure Add (Q : Queue; Element : Integer);function First (Q : Queue) return Integer;function Get (Q : Queue) return Integer;function Empty (Q : Queue) return Boolean;

privatetype Queue is …;

end Queues;

Page 72: Ada 95 - Programming in the large

72http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

with Queues; use Queues;procedure Client is

Q1 : Queue;Q2 : Queue;

X : Integer;

beginAdd (Q1, 123);Add (Q1, 3);

Q2 := Q1;

end Client;

COMPILATIONERROR

:= forbidden

Page 73: Ada 95 - Programming in the large

73http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• Programming in the Large– specification & implementation– privacy– abstract data types– hierarchical packages

Page 74: Ada 95 - Programming in the large

74http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

QueueQueue is an is an Abstract Data TypeAbstract Data Type

(ADT)(ADT)

• Set of abstract values (data domain)

• collection of abstract Operations(routines that manipulate the values)

ADTADTADTADT ====

Page 75: Ada 95 - Programming in the large

75http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Queue Queue is an ADTis an ADT

package Queues istype Queue is limited private;

procedure Add (Q : Queue; Element : Integer);function First (Q : Queue) return Integer;function Get (Q : Queue) return Integer;function Empty (Q : Queue) return Boolean;

privatetype Queue is …;

end Queues;

package Queues istype Queue is limited private;

procedure Add (Q : Queue; Element : Integer);function First (Q : Queue) return Integer;function Get (Q : Queue) return Integer;function Empty (Q : Queue) return Boolean;

privatetype Queue is …;

end Queues;operations

values

Page 76: Ada 95 - Programming in the large

76http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Objects & VariablesObjects & Variables

• OBJECT = instance of an ADT– piece of memory containing values of the ADT

• VARIABLE = name of a specific object

• not all objects have a name

Page 77: Ada 95 - Programming in the large

77http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

My_Q : Queue;

Memory

Queueobject

an object

name of the object

Page 78: Ada 95 - Programming in the large

78http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

type Queue_Ptr is access Queue;

Ptr : Queue_Ptr;

Ptr := new Queue;

Memory

Queueobject

not all objects have a namenot all objects have a name

object has no namePtr is just a pointer to the object

Page 79: Ada 95 - Programming in the large

79http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

CLASS = ADT+ inheritance

Page 80: Ada 95 - Programming in the large

80http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

operation

PrivatePrivatePrivatePrivatedatadatadatadata

operation

operation

ENCAPSULATION

invoke some operationin the SPECto use ADTservices

Page 81: Ada 95 - Programming in the large

81http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

The object modelThe object model

Page 82: Ada 95 - Programming in the large

82http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

The C modelThe C model

C module C module C module

GLOBAL DATA

Page 83: Ada 95 - Programming in the large

83http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

• Programming in the Large– specification & implementation– privacy– abstract data types– hierarchical packages

Page 84: Ada 95 - Programming in the large

84http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

To add functionality ...To add functionality ...

package Queues istype Queue is private;procedure Add (Q : Queue; Element : Integer);function First (Q : Queue) return Integer;function Get (Q : Queue) return Integer;function Empty (Q : Queue) return Boolean;

privatetype Queue is …;

end Queues;

function Last (Q : Queue) return Integer;

function Last (Q : Queue) return Integer;

Must add it to

QueuesQueue is a private type

Page 85: Ada 95 - Programming in the large

85http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

But ...But ...

• Every time you change a spec you must recompile all its clients

• Every time you change a module you must RETEST the whole module

Page 86: Ada 95 - Programming in the large

86http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Solution: use child unitsSolution: use child units

package Queues istype Queue is private;procedure Add (Q : Queue; Element : Integer);function First (Q : Queue) return Integer;function Get (Q : Queue) return Integer;function Empty (Q : Queue) return Boolean;

privatetype Queue is …;

end Queues;

queues.ads

function Queues . Last (Q : Queue) return Integer;queues-last.ads

Child subprogram

Page 87: Ada 95 - Programming in the large

87http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Child Units RulesChild Units Rules

• The body or private part of a child unit can see the private part of all of its parents

• The spec of a child unit does NOT

Page 88: Ada 95 - Programming in the large

88http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

Using a child unitUsing a child unit

with Queues; use Queues;with Queues.Last;procedure Client is

Q : Queue;X : Integer;

beginAdd (Q, 123);Add (Q, 3);

X := Queues.Last (Q);end Client;

Page 89: Ada 95 - Programming in the large

89http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

package Queues istype Queue is private;procedure Add (Q : Queue; Element : Integer);function First (Q : Queue) return Integer;function Get (Q : Queue) return Integer;function Empty (Q : Queue) return Boolean;

privatetype Queue is …;

end Queues;

queues.ads

package Queues . New_Functionality isfunction Last (Q : Queue) return Integer;

end Queues . New_Functionality

queues-new_functionality.ads

Child package

Page 90: Ada 95 - Programming in the large

90http://libre.act-europe.fr © ACT Europe under the GNU Free Documentation License

with Queues; use Queues;with Queues.New_Functionality;procedure Client is

Q : Queue;X : Integer;

beginAdd (Q, 123);Add (Q, 3);

X := Queues.New_Functionality.Last (Q);end Client;