Program Realisation 2 Todays Topics Pixels, coordinates, and - - PowerPoint PPT Presentation

program realisation 2 today s topics pixels coordinates
SMART_READER_LITE
LIVE PREVIEW

Program Realisation 2 Todays Topics Pixels, coordinates, and - - PowerPoint PPT Presentation

Program Realisation 2 Todays Topics Pixels, coordinates, and rectangles in Delphi http://www.win.tue.nl/hemerik/2IP20/ Event mechanism in Delphi Lecture 9 Kees Hemerik Memory leaks, dangling references Tom Verhoe ff UML:


slide-1
SLIDE 1

Program Realisation 2 http://www.win.tue.nl/˜hemerik/2IP20/ Lecture 9 Kees Hemerik Tom Verhoeff Technische Universiteit Eindhoven Faculteit Wiskunde en Informatica Software Engineering & Technology Feedback to T.Verhoeff@TUE.NL

c 2006, T. Verhoeff @ TUE.NL 1 Program Realization 2: Lecture 9

Today’s Topics

  • Pixels, coordinates, and rectangles in Delphi
  • Event mechanism in Delphi
  • Memory leaks, dangling references
  • UML: Unified Modeling Language
  • (Automated unit testing)
  • Conclusion

c 2006, T. Verhoeff @ TUE.NL 2 Program Realization 2: Lecture 9

Pixels and coordinates

x=0 1 2 3 y=0 1 2 y=0 1 2 3 x=0 1 2 3 4

  • Pixels are not points, but little squares
  • Coordinate options:

through pixel centers , between pixels

  • For many purposes, coordinates between pixels are preferred

c 2006, T. Verhoeff @ TUE.NL 3 Program Realization 2: Lecture 9

Rectangles

  • FillRect(XL, YL, XH, YH) colors the points x, y such that

XL ≤ x < XH ∧ YL ≤ y < YH

  • It has width XH - XL and height YH - YL
  • FillRect(X0, YL, X1, YH) and FillRect(X1, YL, X2, YH) are horizon-

tally adjacent: they touch, but do not overlap

  • FillRect(X, YL, X, YH) is empty
  • Scales well: FillRect(0, 0, XH, YH) vs. FillRect(0, 0, 2*XH, 2*YH)
  • Edsger W. Dijkstra. Why numbering should start at zero. EWD 831,
  • Aug. 1982

c 2006, T. Verhoeff @ TUE.NL 4 Program Realization 2: Lecture 9

slide-2
SLIDE 2

Framed Rectangles

y=0 1 2 3 x=0 1 2 3 4 4 5 6 7

  • Rectangle(0, 0, 5, 4) ; Rectangle(4, 0, 7, 4)
  • These share a 1-pixel boundary, or each has 0.5-pixel boundary
  • In final assignment, we want Rectangle(0.5, 0.5, 4.5, 3.5) and

Rectangle(4.5, 0.5, 6.5, 3.5)

c 2006, T. Verhoeff @ TUE.NL 5 Program Realization 2: Lecture 9

Event Mechanism in Delphi: Main Event Loop

1 program PrDirTreeView; 2 3 uses 4

Forms,

5

Main in ’Main.pas’ {Form1};

6 7 begin 8

Application.Initialize;

9

Application.CreateForm(TForm1, Form1);

10

Application.Run;

11 end.

Application.Run starts the main event loop

The main event loop waits for an event and calls its event handler

c 2006, T. Verhoeff @ TUE.NL 6 Program Realization 2: Lecture 9

Event Mechanism in Delphi: Event

  • Events are triggered by user actions and object state changes
  • Events are implemented as private fields with method pointers,

e.g. of type TNotifyEvent

  • TNotifyEvent = procedure (Sender: TObject) of object;
  • Procedure types
  • Cannot be accommodated in the static call graph

c 2006, T. Verhoeff @ TUE.NL 7 Program Realization 2: Lecture 9

Memory Leaks and Dangling References Memory leak : after p := q or New(p) or p := ...Create(...) , (old p)ˆ is possibly still allocated but unreachable

  • The amount of usable memory has diminished (leaked away)
  • GetHeapStatus reports some memory statistics (e.g. TotalAllocated)

Dangling references : after

Dispose(p) or p.Free , other references

could still point to (old p)ˆ

  • References to disposed/freed objects are called ‘dangling’
  • The objects pointed to are no longer valid and are unreliable

c 2006, T. Verhoeff @ TUE.NL 8 Program Realization 2: Lecture 9

slide-3
SLIDE 3

Memory Leaks and Dangling References: Examples See final assignment, Main.pas:

1 // invariants --------------------------------------------------------- 2

...

3 // I2: (FFocus = nil) or (FFocus is a node in the tree with root FRoot) 4

...

5 procedure TForm1.ChangeRoot(APath: String); 6 begin 7

FFocus := nil;

8

FRoot.Free;

9

FRoot := MakeDirTree(APath);

10 end;

Omitting FRoot.Free causes a memory leak after line 9 Omitting FFocus := nil, makes FFocus a dangling reference after line 8 (violating I2)

c 2006, T. Verhoeff @ TUE.NL 9 Program Realization 2: Lecture 9

UML: Unified Modeling Language

  • Standard graphical language for modeling object-oriented software
  • Syntax: Diagrams, but also textual representation (OCL)
  • Semantics: somewhat controversial
  • For (static) structure and (dynamic) behavior
  • Extensible, still evolving
  • Not a design method

c 2006, T. Verhoeff @ TUE.NL 10 Program Realization 2: Lecture 9

Automated Unit Testing Framework

  • For each unit U, write a separate unit test driver Test_U
  • Test_U uses U, makes various calls of U, and checks the results
  • DUnit for Delphi, or FPCUnit for FreePascal/Lazarus
  • XUnit test framework provides classes for organizing test cases,

and checking and reporting of results

c 2006, T. Verhoeff @ TUE.NL 11 Program Realization 2: Lecture 9

Main Theme: Modular Structure

  • Manage Complexity
  • Separation of Concerns
  • Divide and Conquer
  • Design by Contract

c 2006, T. Verhoeff @ TUE.NL 12 Program Realization 2: Lecture 9

slide-4
SLIDE 4

Why Modular Structure?

  • Correct construction

Manage complexity

  • Team construction

Reduce development time

  • Verification

Errors are inevitable

  • Adaptation

Change is inevitable

  • Reuse

Reduce budget

c 2006, T. Verhoeff @ TUE.NL 13 Program Realization 2: Lecture 9

How to Design During design, many decisions must be made Design guidelines, principles, and methods needed Modular structure: how to ‘find’ the modules

  • Top-down design , stepwise refinement, functional decomposition
  • Bottom-up design

Functionality versus data as basis for modularity

c 2006, T. Verhoeff @ TUE.NL 14 Program Realization 2: Lecture 9

Main Themes

  • Abstract Data Types and object-oriented programming
  • Recursion , both in control and in data
  • Event-driven, interactive Graphical User Interfaces
  • Borland Delphi with Object Pascal

c 2006, T. Verhoeff @ TUE.NL 15 Program Realization 2: Lecture 9

What Next?

  • 2IA20: Ontwerp van Algoritmen 2
  • 2IF10: Componenten 1

c 2006, T. Verhoeff @ TUE.NL 16 Program Realization 2: Lecture 9