Aspect-Oriented Programming with C++ and AspectC++ AOSD 2007 Tutorial. Part V Examples. Examples V/1

Similar documents
AspectC++ An AOP Extension for C++

On Typesafe Aspect Implementations in C++

AspectC++ A Language Overview

The Design and Implementation of AspectC++

We display some text in the middle of a window, and see how the text remains there whenever the window is re-sized or moved.

Advances in AOP with AspectC++ 1

Programming in graphical environment. Introduction

Window programming. Programming

Windows and Messages. Creating the Window

Chapter 15 Programming Paradigm

Windows Programming. 1 st Week, 2011

Advantech Windows CE.net Application Hand on Lab

Alisson Sol Knowledge Engineer Engineering Excellence June 08, Public version

Getting Started. 1 st Week, Sun-Jeong Kim. Computer Graphics Applications

Hands-On Lab. Multitouch Gestures - Native. Lab version: Last updated: 12/3/2010

Task Toolkit Manual for InduSoft Web Studio v6.1+sp3

Game Programming I. Introduction to Windows Programming. Sample Program hello.cpp. 5 th Week,

Computer Programming Lecture 11 이윤진서울대학교

LSN 4 GUI Programming Using The WIN32 API

MOBILE COMPUTING Practical 1: Graphic representation

An Introduction to Windows Programming Using VC++ Computer Graphics. Binghamton University. EngiNet. Thomas J. Watson

Tutorial 7: Mouse Input

Course 3D_OpenGL: 3D-Graphics with C++ and OpenGL Chapter 1: Moving Triangles

IFE: Course in Low Level Programing. Lecture 5

Aspect-Oriented Programming with C++ and AspectC++

Qno.2 Write a complete syantax of "Getparents" functions? What kind of value it return and when this function is use? Answer:-

Windows Programming in C++

Creating a DirectX Project

Hands-On Lab. Multi-Touch WMTouch - Native. Lab version: Last updated: 12/3/2010

Using DAQ Event Messaging under Windows NT/95/3.1

Input and Interaction

Review. Designing Interactive Systems II. The Apple Macintosh

hinstance = ((LPCREATESTRUCT)lParam)->hInstance obtains the program's instance handle and stores it in the static variable, hinstance.

CS410 Visual Programming Solved Online Quiz No. 01, 02, 03 and 04. For Final Term Exam Preparation by Virtualians Social Network

Detecting USB Device Insertion and Removal Using Windows API

uvi ... Universal Validator Interface Software Developers Kit Revision /29/04 Happ Controls

/*********************************************************************

We take a look at implementing some file menu options: New, Open, Save, Save As and Exit.

Introduction to Computer Graphics (CS602) Lecture No 04 Point

Tutorial 9:Child Window Controls

Question No: 1 (Marks: 1) - Please choose one For showing Dialog we can use "ShowWindow(...)" function.

CS410. Note: VuGujranwala.com is not responsible for any solved solution, but honestly we are trying our best to Guide correctly.

HANNAH HOWARD #ABOUTME

Graphics Software. Interaction schemes. Input handling. Graphics application interfaces. Output handling. General scheme of CG applications

Graphics Software. Interaction schemes. General scheme of CG applications. Balázs Csébfalvi

Based on the following Circuit Cellar Articles. Developing a Mobile Phone Application. The story Spark podcast 49. The articles

Lecture 4. Drawing - coordinate systems Advanced drawing - bitmaps, regions, clipping, metafiles

Modern GUI applications may be composed from a number of different software components.

Lambda Expression & Concurrency API. 김명신부장 Principal Technical Evangelist

int fnvgetconfig(handle h, UINT32 id, const void *cfg, size_t sz);... 4

Multithreading Multi-Threaded Programming. Prof. Lin Weiguo Copyleft 2009~2017, School of Computing, CUC

Different Ways of Writing Windows Programs

Call DLL from Limnor Applications

Developing a Prototype Window s Mobile Application

. DRAFT version 0.681

Bria 3 for Windows API

Win32 Multilingual IME Overview for IME Development

DirectX Programming #1. Hyunna Lee Computer Graphics, 2010 Spring

Socket I/Os in Windows. Dae-Ki Kang

4 th (Programmatic Branch ) Advance Windows Programming class

C Windows 16. Visual C++ VC Borland C++ Compiler BCC 2. Windows. c:\develop

Designing Interactive Systems II

Customizable Toolbar: Implementing a toolbar combo button

Introduction to. Bruno Harbulot. ESNW, the University of Manchester.

SUSI-SQFlash. User s Manual

2. On the main menu, click File -> New... or File -> New -> Other... Windows Fundamentals

Windows Socket Message-Driven/WSAAsyncSelect Model. Prof. Lin Weiguo Copyleft 2009~2015, College of Computing, CUC

CSCE : Computer Systems Homework #1 Part 1 (25 pts) Due date: 1/24/19

Tutorial 3: A Simple Window

27 Designing Your Own Program

Data Structures and Other Objects Using C++

CSE 143. Linked Lists. Linked Lists. Manipulating Nodes (1) Creating Nodes. Manipulating Nodes (3) Manipulating Nodes (2) CSE 143 1

Mobile Application Development (Part 1)-Program a Sound-Monitoring Prototype

File System Watcher. Gregory Adam 2015

Key Switch Control Software Windows driver software for Touch Panel Classembly Devices

Developer Documentation

// handle of dialog box

Analyzing effect of Aspect Oriented concepts in design and implementation of design patterns with case study of Observer Pattern

64 Bit Delphi What does it all mean?

Client and Server (DirectX)

Documentation: AC++ Compiler Manual

Game Programming Genesis Part II : Using Resources in Win32 Programs by Joseph "Ironblayde" Farrell

CS410 Visual Programming Solved Objective Midterm Papers For Preparation of Midterm Exam

CIS 488 Programming Assignment 14 Using Borland s OWL

The Observer Design Pattern

Front Panel: Free Kit for Prototyping Embedded Systems on Windows

CC Pilot XS. Backlight interface description

An overview of how to write your function and fill out the FUNCTIONINFO structure. Allocating and freeing memory.

Revision: 09/21/ East Main Pullman, WA (509) Voice and Fax

Message-Passing Interface for Microsoft Windows 3.1

Introduction. Overview

Windows Printer Driver User Guide for NCR Retail Printers. B Issue G

Konfigurierbare Systemsoftware (KSS)

C++ Templates. David Camp

Chapter 32. Aspect-Oriented Software Development (AOSD) Ian Sommerville 2006 Software Engineering. Chapter 32 Slide 1

G52CPP C++ Programming Lecture 16

DirectX Programming #1. Kang, Seong-tae Computer Graphics, 2007 Fall

Visualization Of Hard Disk Geometry And Master Boot Record

Topics. bool and string types input/output library functions comments memory allocation templates classes

User Guide for Sign Language Video Chat for Deaf People. User Guide. For End Users ( or Deaf people) For Developers ( or Programmers) FAQs

Transcription:

Aspect-Oriented Programming with C++ and AspectC++ AOSD 2007 Tutorial Part V V/1

AspectC++ in Practice - 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 V/2

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

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

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

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 ) { ; V/6

Solution: Generic Observer Aspect aspect ObserverPattern { public: struct ISubject {; struct IObserver { virtual void update (ISubject *) = 0; ; Interfaces for the subject/observer roles 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 ) { ; V/7

Solution: Generic Observer Aspect aspect ObserverPattern { public: struct ISubject {; struct IObserver { virtual void update (ISubject *) = 0; ; pointcut virtual observers() = 0; pointcut virtual subjects() = 0; abstract pointcuts that define subjects/observers (need to be overridden by a derived aspect) 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 ) { ; V/8

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 ); virtual pointcut defining all state-changing methods. (Defaults to the execution of any non-const method in subjects) void updateobservers( ISubject* subject ) { void addobserver( ISubject* subject, IObserver* observer ) { void remobserver( ISubject* subject, IObserver* observer ) { ; V/9

Solution: Generic Observer Aspect aspect ObserverPattern { public: struct ISubject {; struct IObserver { virtual void update (ISubject *) = 0; ; pointcut virtual observers() = 0; pointcut virtual subjects() = 0; Introduction of the role interface as additional baseclass into subjects / observers 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 ) { ; V/10

Solution: Generic Observer Aspect aspect ObserverPattern { public: struct ISubject {; struct IObserver { virtual void update (ISubject *) = 0; ; After advice to update observers after execution of a statechanging method 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 ) { ; V/11

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 { public: void update( ObserverPattern::ISubject* s ) { Draw(); ; ; V/12

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 on Tutorial CD V/13

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; A typical Win32 application 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; V/14

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; These Win32 API functions may fail! 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; V/15

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 return type magic value BOOL FALSE ATOM (ATOM) 0 HANDLE HWND INVALID_HANDLE_VALUE or NULL NULL V/16

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); V/17

Describing the failure: 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() ); V/18

Reporting the Error 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; V/19

Errorhandling in Legacy Code: 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 on tutorial CD V/20