Aspect-Oriented Programming

Slides:



Advertisements
Similar presentations
Prof. Muhammad Saeed. Procedure-Driven Programming Event-Driven Programming Events Messages Event Handlers GUI Windows and Multitasking Queues ( System.
Advertisements

Aspect Oriented Programming. AOP Contents 1 Overview 2 Terminology 3 The Problem 4 The Solution 4 Join point models 5 Implementation 6 Terminology Review.
QT – Introduction C++ GUI Programming with Qt 4 Blanchette and Summerfield, Ch. 1 Miller, 2004.
Introduction to Windows Programming. First Windows Program This program simply displays a blank window. The following code is the minimum necessary to.
IN-LAB # 1 - GETTING STARTED - BUT FIRST: 1.Project ideas - watch the scope!!! 2.Check accounts 3. Either myself or the TA need to check you off BEFORE.
Simple Gui in C++. Project Selecting So Far Registering the window class Creating the window.
Aspect Oriented Programming Written by Michael Beder.
First Windows Program Hello Windows. 2 HELLOWIN.C #include LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; int WINAPI WinMain (HINSTANCE hInstance,
Intro to Windows Programming Basic Ideas. Program Entry Point Int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
AOP and Aspect C++ presentation by Igor Kotenkov based on presentation by Andreas Gal, Daniel Lohmann and Olaf Spinczyk.
GVE, Hallym University, Song, Chang Geun, Ph.D. 컴퓨터 그래픽스 응용 한림대학교 컴퓨터공학부 송 창근.
Outline Introduction Problem Statement Object-Oriented Design Aspect-Oriented Design Conclusion Demo.
Win32 API Programming Event-driven, graphics oriented Example: User clicks mouse over a program’s window area (an event) -- – Windows decodes HW signals.
Mining Function Usage Patterns to Find Bugs Chadd Williams.
Prepared by Fareeha Lecturer DCS IIUI 1 Windows API.
Overview of Previous Lesson(s) Over View  Visual C++ provides us with 3 basic ways of creating an interactive Windows application  Using the Windows.
Chapter 1: Hello, MFC Windows Programming Model Department of Digital Contents Sang Il Park.
Chapter 1: Hello, MFC Your first MFC Application Department of Digital Contents Sang Il Park.
Overview of Previous Lesson(s) Over View  Microsoft Foundation Classes (MFC)  A set of predefined classes upon which Windows programming with Visual.
Microsoft Foundation Classes. What is MFC? Set of C++ classes written by MS Simplifies writing complex programs Covers many areas: – GUI – I/O – O/S interfaces.
Aspect Oriented Programming Gülşah KARADUMAN.
CSE3AGT Paul Taylor Stupid Conventions! l = Long p = Pointer h = handle g = global wnd = Windows WM = Windows Message d3d = Direct3D hr = HRESULT.
Winsock Programming Blocking and Asynchronous Sockets for Windows.
Further games and graphics concepts COSE50581 Introduction to Module and Recap Bob Hobbs Faculty of Computing, Engineering and Technology Staffordshire.
BZUPAGES.COM Visual Programming Lecture – 2 Miss. SADAF MAJEED SIAL Computer Science Department Bahauddin Zakariya University Multan.
Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals.
Design Rules for Increasing Modularity with CaesarJ Carlos Eduardo Pontual Advisor: Paulo Borba 17/06/2010.
AOP-1 Aspect Oriented Programming. AOP-2 Aspects of AOP and Related Tools Limitation of OO Separation of Concerns Aspect Oriented programming AspectJ.
1 A Brief Introduction to Design Patterns Based on materials from Doug Schmidt 1.
GUI-Based Programming ECE 417/617: Elements of Software Engineering Stan Birchfield Clemson University.
Dialog boxes Modal and modeless dialog boxes Displaying about dialog box: case WM_COMMAND : switch (LOWORD (wParam)) { case IDM_APP_ABOUT : DialogBox (hInstance,
Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01.
1 Programming and Software Engineering. 2 Software Development We can define two main classes of software: User-written software solve a particular problem,
The WM_NCHITTEST Message  This is an internal message used by Windows for generating all the other mouse messages.  Though it almost never needs to be.
Introduction to OpenGL
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
Learning Programming Windows API Morpheus Feb-28, 2008.
Our good old first Win32 programme of lecture 8 #include int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
Introduction To GDI GDI Definition : It is a interface present in windows which provide function and related structures that an application can use to.
Lecture 8: Discussion of papers OpenGL programming Lecturer: Simon Winberg Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)
Software Architecture and Design BITS C461/IS C341 Software Engineering First Semester Aditya P. Mathur Department of Computer Science Purdue.
Windows Programming Lecture 14. WM_PAINT message Whenever an application receives the WM_PAINT message, whether the entire window needs repainting? WM_PAINT.
Modern Programming Tools And Techniques-I
Windows Programming Lecture 10.
A Brief Introduction to Design Patterns
Part II – AspectC++ Language
Windows Programming Lecture 09.
Design Patterns Damian Gordon.
Window.
Aspect-Oriented Generation of the API Documentation for AspectJ
Chapter 1 Hello, MFC.
Windows Programming Model
Object-Oriented Programming
Aspect-Oriented Programming
Chapter 6: Using Design Patterns
Windows Programming using Dev C++
Advanced Programming Behnam Hatami Fall 2017.
Queued and Nonqueued Messages
The program in traditional OS
null, true, and false are also reserved.
Windows Controls & Concepts
Windows Programming Lecture 12
Windows Programming Lecture 13
CS 1253 Visual Programming Unit I Windows Environment
Fundaments of Game Design
Windows Development Dynadata Copyright, 2014 © DynaData S.A. 1/10.
Aspect Oriented Software Design
Graphics Laboratory Korea University
Binding 10: Binding Programming C# © 2003 DevelopMentor, Inc.
Presentation transcript:

Aspect-Oriented Programming with AspectC++ Part IV – Examples

AspectC++ in Practice - Examples Applying the observer protocol Example: a typical scenario for the widely used observer pattern Problem: implementing observer requires several design and code transformations Errorhandling in legacy code Example: a typical Win32 application Problem: errorhandling often “forgotten” as too much of a bother

Observer Pattern: Scenario DigitalClock AnalogClock ClockTimer Draw () Draw () GetHour () : int SetTime ( in h : int , in m : int , in s : int ) Tick () update on change

Observer Pattern: Implementation IObserver observers ISubject update ( in s : ISubject ) updateAll () DigitalClock AnalogClock ClockTimer Draw () Draw () GetHour () : int update ( in s ) update ( in s ) SetTime ( in h : int , in m : int , in s : int ) Tick ()

Observer Pattern: Problem The 'Observer Protocol' Concern... ...crosscuts the module structure IObserver observers ISubject update ( in s : ISubject ) updateAll () DigitalClock AnalogClock ClockTimer Draw () Draw () GetHour () : int update ( in s ) update ( in s ) SetTime ( in h : int , in m : int , in s : int ) Tick ()

Solution: Generic Observer Aspect aspect ObserverPattern { ... public: struct ISubject {}; struct IObserver { virtual void update (ISubject *) = 0; }; pointcut virtual observers() = 0; pointcut virtual subjects() = 0; pointcut virtual subjectChange() = execution( "% ...::%(...)" && !"% ...::%(...) const" ) && within( subjects() ); advice observers () : slice class : public ObserverPattern::IObserver; advice subjects() : slice class : public ObserverPattern::ISubject; advice subjectChange() : after () { ISubject* subject = tjp->that(); updateObservers( subject ); } void updateObservers( ISubject* subject ) { ... } void addObserver( ISubject* subject, IObserver* observer ) { ... } void remObserver( ISubject* subject, IObserver* observer ) { ... }

Solution: Generic Observer Aspect aspect ObserverPattern { ... public: struct ISubject {}; struct IObserver { virtual void update (ISubject *) = 0; }; pointcut virtual observers() = 0; pointcut virtual subjects() = 0; pointcut virtual subjectChange() = execution( "% ...::%(...)" && !"% ...::%(...) const" ) && within( subjects() ); advice observers () : slice class : public ObserverPattern::IObserver; advice subjects() : slice class : public ObserverPattern::ISubject; advice subjectChange() : after () { ISubject* subject = tjp->that(); updateObservers( subject ); } void updateObservers( ISubject* subject ) { ... } void addObserver( ISubject* subject, IObserver* observer ) { ... } void remObserver( ISubject* subject, IObserver* observer ) { ... } Interfaces for the subject/observer roles

Solution: Generic Observer Aspect aspect ObserverPattern { ... public: struct ISubject {}; struct IObserver { virtual void update (ISubject *) = 0; }; pointcut virtual observers() = 0; pointcut virtual subjects() = 0; pointcut virtual subjectChange() = execution( "% ...::%(...)" && !"% ...::%(...) const" ) && within( subjects() ); advice observers () : slice class : public ObserverPattern::IObserver; advice subjects() : slice class : public ObserverPattern::ISubject; advice subjectChange() : after () { ISubject* subject = tjp->that(); updateObservers( subject ); } void updateObservers( ISubject* subject ) { ... } void addObserver( ISubject* subject, IObserver* observer ) { ... } void remObserver( ISubject* subject, IObserver* observer ) { ... } abstract pointcuts that define subjects/observers (need to be overridden by a derived aspect)

Solution: Generic Observer Aspect virtual pointcut defining all state-changing methods. (Defaults to the execution of any non-const method in subjects) derived aspect) aspect ObserverPattern { ... public: struct ISubject {}; struct IObserver { virtual void update (ISubject *) = 0; }; pointcut virtual observers() = 0; pointcut virtual subjects() = 0; pointcut virtual subjectChange() = execution( "% ...::%(...)" && !"% ...::%(...) const" ) && within( subjects() ); advice observers () : slice class : public ObserverPattern::IObserver; advice subjects() : slice class : public ObserverPattern::ISubject; advice subjectChange() : after () { ISubject* subject = tjp->that(); updateObservers( subject ); } void updateObservers( ISubject* subject ) { ... } void addObserver( ISubject* subject, IObserver* observer ) { ... } void remObserver( ISubject* subject, IObserver* observer ) { ... }

Solution: Generic Observer Aspect aspect ObserverPattern { ... public: struct ISubject {}; struct IObserver { virtual void update (ISubject *) = 0; }; pointcut virtual observers() = 0; pointcut virtual subjects() = 0; pointcut virtual subjectChange() = execution( "% ...::%(...)" && !"% ...::%(...) const" ) && within( subjects() ); advice observers () : slice class : public ObserverPattern::IObserver; advice subjects() : slice class : public ObserverPattern::ISubject; advice subjectChange() : after () { ISubject* subject = tjp->that(); updateObservers( subject ); } void updateObservers( ISubject* subject ) { ... } void addObserver( ISubject* subject, IObserver* observer ) { ... } void remObserver( ISubject* subject, IObserver* observer ) { ... } Introduction of the role interface as additional baseclass into subjects / observers

Solution: Generic Observer Aspect aspect ObserverPattern { ... public: struct ISubject {}; struct IObserver { virtual void update (ISubject *) = 0; }; pointcut virtual observers() = 0; pointcut virtual subjects() = 0; pointcut virtual subjectChange() = execution( "% ...::%(...)" && !"% ...::%(...) const" ) && within( subjects() ); advice observers () : slice class : public ObserverPattern::IObserver; advice subjects() : slice class : public ObserverPattern::ISubject; advice subjectChange() : after () { ISubject* subject = tjp->that(); updateObservers( subject ); } void updateObservers( ISubject* subject ) { ... } void addObserver( ISubject* subject, IObserver* observer ) { ... } void remObserver( ISubject* subject, IObserver* observer ) { ... } After advice to update observers after execution of a state-changing method

Solution: Putting Everything Together Applying the Generic Observer Aspect to the clock example aspect ClockObserver : public ObserverPattern { // define the participants pointcut subjects() = "ClockTimer"; pointcut observers() = "DigitalClock"||"AnalogClock"; public: // define what to do in case of a notification advice observers() : slice class { void update( ObserverPattern::ISubject* s ) { Draw(); } };

Observer Pattern: Conclusions Applying the observer protocol is now very easy! all necessary transformations are performed by the generic aspect programmer just needs to define participants and behaviour multiple subject/observer relationships can be defined More reusable and less error-prone component code observer no longer “hard coded” into the desing and code no more forgotten calls to update() in subject classes Full source code available at www.aspectc.org

Errorhandling in Legacy Code: Scenario LRESULT WINAPI WndProc( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { HDC dc = NULL; PAINTSTRUCT ps = {0}; switch( nMsg ) { case WM_PAINT: dc = BeginPaint( hWnd, &ps ); ... EndPaint(hWnd, &ps); break; }} int WINAPI WinMain( ... ) { HANDLE hConfigFile = CreateFile( "example.config", GENERIC_READ, ... ); WNDCLASS wc = {0, WndProc, 0, 0, ... , "Example_Class"}; RegisterClass( &wc ); HWND hwndMain = CreateWindowEx( 0, "Example_Class", "Example", ... ); UpdateWindow( hwndMain ); MSG msg; while( GetMessage( &msg, NULL, 0, 0 ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } return 0; A typical Win32 application

Errorhandling in Legacy Code: Scenario LRESULT WINAPI WndProc( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { HDC dc = NULL; PAINTSTRUCT ps = {0}; switch( nMsg ) { case WM_PAINT: dc = BeginPaint( hWnd, &ps ); ... EndPaint(hWnd, &ps); break; }} int WINAPI WinMain( ... ) { HANDLE hConfigFile = CreateFile( "example.config", GENERIC_READ, ... ); WNDCLASS wc = {0, WndProc, 0, 0, ... , "Example_Class"}; RegisterClass( &wc ); HWND hwndMain = CreateWindowEx( 0, "Example_Class", "Example", ... ); UpdateWindow( hwndMain ); MSG msg; while( GetMessage( &msg, NULL, 0, 0 ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } return 0; These Win32 API functions may fail!

Win32 Errorhandling: Goals Detect failed calls of Win32 API functions by giving after advice for any call to a Win32 function Throw a helpful exception in case of a failure describing the exact circumstances and reason of the failure Problem: Win32 failures are indicated by a “magic” return value magic value to compare against depends on the return type of the function error reason (GetLastError()) only valid in case of a failure

Detecting the Failure: Generic Advice bool isError(ATOM); advice call(win32API ()) : after () { if (isError (*tjp->result())) // throw an exception } bool isError(BOOL); bool isError(HANDLE); bool isError(HWND); ...

Error Reporting: Generative Advice template <int I> struct ArgPrinter { template <class JP> static void work (JP &tjp, ostream &s) { ArgPrinter<I–1>::work (tjp, s); s << “, “ << *tjp. template arg<I-1>(); } }; advice call(win32API ()) : after () { // throw an exception ostringstream s; DWORD code = GetLastError(); s << “WIN32 ERROR ” << code << ... << win32::GetErrorText( code ) << ... << << tjp->signature() << “WITH: ” << ...; ArgPrinter<JoinPoint::ARGS>::work (*tjp, s); throw win32::Exception( s.str() ); }

Error Reporting LRESULT WINAPI WndProc( HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam ) { HDC dc = NULL; PAINTSTRUCT ps = {0}; switch( nMsg ) { case WM_PAINT: dc = BeginPaint( hWnd, &ps ); ... EndPaint(hWnd, &ps); break; }} int WINAPI WinMain( ... ) { HANDLE hConfigFile = CreateFile( "example.config", GENERIC_READ, ... ); WNDCLASS wc = {0, WndProc, 0, 0, ... , "Example_Class"}; RegisterClass( &wc ); HWND hwndMain = CreateWindowEx( 0, "Example_Class", "Example", ... ); UpdateWindow( hwndMain ); MSG msg; while( GetMessage( &msg, NULL, 0, 0 ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } return 0;

Win32-Errorhandling: Conclusions Easy to apply errorhandling for Win32 applications previously undetected failures are reported by exceptions rich context information is provided Uses advanced AspectC++ techniques error detection by generic advice context propagation by generative advice Full source code available at www.aspectc.org