Aspect-Oriented Programming with C++

Olaf Spinczyk

University of Erlangen-Nuremberg The AspectC++ Project ACOomPpute rw Sciietnche 4 C++ © 2005 Olaf Spinczyk 1 This talk is about ...

➢ the importance of C++ for the success of AOP

➢ different AOP approaches for C++

– language independent vs. pure C++ vs. extension

➢ the AspectC++ project

– history, language, implementation

➢ the future

AOP with C++ © 2005 Olaf Spinczyk 2 Observations: AOP Products

➢ IBM: AspectJ and AJDT

➢ BEA: AspectWerkz

➢ JBoss: JBoss AOP

AOP with C++ © 2005 Olaf Spinczyk 3 Observations: AOP Products

➢ IBM: AspectJ and AJDT

➢ BEA: AspectWerkz

➢ JBoss: JBoss AOP

AOP with C++ © 2005 Olaf Spinczyk 4 Observations: AOP Products

... for C++ developers:

➢ Semantic Designs: DMS

➢ P&P Software: XWeaver

➢ pure-systems: AspectC++ Add-In

AOP with C++ © 2005 Olaf Spinczyk 5 Observations: AOP Products

... for C++ developers:

➢ Semantic Designs: DMS

➢ P&P Software: XWeaver

➢ pure-systems: AspectC++ Add-In

these teams play in a different league

AOP with C++ © 2005 Olaf Spinczyk 6 Observations: AOP Research

Java vs. C++ at AOSD 10

7,5 other 5 Java C/C++ 2,5

0 2003 2004 2005

C++ is almost invisible!

AOP with C++ © 2005 Olaf Spinczyk 7 Java vs. C++ in the Real World

open source projects 40000

30000 34,91% Java 20000 C/C++ 37,71% 10000 C only 27,38% 0 2004/09/09 (source- forge)

AOP with C++ © 2005 Olaf Spinczyk 8 Java vs. C++ in the Real World

requested skills 100,00% 35 38 75,00% Java C++ 50,00% 41 41 C 25,00% 26 21 0,00% 2003 2004

AOP with C++ © 2005 Olaf Spinczyk 9 Java vs. C++ in the Real World

requested skills 100,00% 35 38 75,00% Java C++ 50,00% 41 41 C 25,00% 26 21 0,00% 2003 2004 C and C++ are still dominating!

AOP with C++ © 2005 Olaf Spinczyk 10 Java vs. C++ in the Real World AOP research & products don't reflect the real world.

Why?

AOP with C++ © 2005 Olaf Spinczyk 11 Reasons

C++ is one of the most complex languages today

 developing tools and extensions is painfully hard

 C++ is not common in academic research

 no transfer from academia to industry

AOP with C++ © 2005 Olaf Spinczyk 12 The Problem

AOP research & products don't reflect the real world.

➢ the unwanted message is:

AOP is Java.

➢ the unwanted consequences are:

– no large scale adoption by the IT industry

– billions lines of C/C++ code don't benefit from AOP

AOP with C++ © 2005 Olaf Spinczyk 13 * WANTED * DEAD OR ALIVE

AspectC++

AOP with C++ © 2005 Olaf Spinczyk 14 Requirements

an AOP solution for C++ has to ... ➢ support full obliviousness and quantification – no preparation of the component code – rich language ➢ be strong were C++ is strong – no runtime system – support for procedural, object-oriented, and generic code – exploit and support the powerful static type system – efficient code ➢ be usable – simple – easy integration

AOP with C++ © 2005 Olaf Spinczyk 15 Technical Approaches

idea coding source source code code transformation compilation (virtual) binary code machine code transformation execution process

AOP with C++ © 2005 Olaf Spinczyk 16 Technical Approaches

idea AOP with pure C++ coding • patterns source sourc• ete cmodpelates, macros code transformation compilation (virtual) binary code machine code transformation execution process

AOP with C++ © 2005 Olaf Spinczyk 17 AOP with pure C++ (1)

C. Czarnecki, U. Eisenecker, L. Dominick:

// generic wrapper (aspect) that adds counting to // any queue class Q, as long as it implements the // proper interface

template class Counting_Aspect : public Q { int counter; // introduction public: void enqueue(Item* item) { // after advice Q::enqueue(item); counter++; } };

AOP with C++ © 2005 Olaf Spinczyk 18 AOP with pure C++ (2)

aspect weaving by template instantiation

// component code class Queue { ... }

// wrappers (aspects) template class Counting_Aspect : public Q { ... } template class Tracing_Aspect : public Q { ... }

// template instantiation (weaving) typedef Counting_Aspect CountingQueue; typedef Trace_Aspect TraceCountingQueue;

AOP with C++ © 2005 Olaf Spinczyk 19 AOP with pure C++ (3)

obliviousness for the client code

namespace components { class Queue { ... }; } namespace aspects { template class Counting_Aspect : public Q { ... }; } namespace configuration { // select counting queue typedef aspects::Counting_Aspect Queue; } using namespace configuration; void client_code () { Queue queue; // Queue with all configured aspects queue.enqueue (new MyItem); }

AOP with C++ © 2005 Olaf Spinczyk 20 AOP with pure C++ (4)

C. Diggins: macros hide the template “magic”

// the CountingAspect as before struct CountingAspect { // Inc and Dec is advice struct Inc { template<...> virtual void OnAfter (...) { ... } }; };

// the DEF_POINTCUT macro describes sets of member functions DEF_POINTCUT(EnqueuePointcut) SET_PROCJOINPOINT1(enqueue, Item*, item) END_POINTCUT

// the CROSSCUT macro combines a class, a pointcut, and an aspect typedef CROSSCUT(Queue,EnqueuePointcut, CountingAspect::Inc) CountingQueue;

AOP with C++ © 2005 Olaf Spinczyk 21 AOP with pure C++ - Review

obliviousness: not given – component code has to be “prepared” quantification: not given – aspects have to be applied manually strong: not really – basically supports weaving in public (virtual) class member functions usable: not often + no special tool support required – code is hard to develop, understand, and maintain

AOP with C++ © 2005 Olaf Spinczyk 22 AOP with pure C++ - Review

obliviousness: not given – component code has to be “prepared” quantification: not given – aspects have to be applied manually sntroo ntogo: nl onte reedalelyd, but too many restrictions – basically supports weaving in public (virtual) class member functions usable: not often + no special tool support required – code is hard to develop, understand, and maintain

AOP with C++ © 2005 Olaf Spinczyk 23 Technical Approaches

idea coding source source code code transformation compilation Code Transformers (virtual) binary code machine code trans• fAorsmpaeticotCn ++ execution • XWeaver • DMS process

AOP with C++ © 2005 Olaf Spinczyk 24 XWeaver (1)

XML Model .xml XML Model .xml .xml .xml of Modified .xml of Base Code .xml Code

XWeaver

.xml AspectX Program

AOP with C++ © 2005 Olaf Spinczyk 25 XWeaver (1)

* a piece of code in XML * ... XML Model .xml XML Model .xml .xml .xml of Modified .xml of B a sinte Code .xml Code main () {} XWeaver ... a concrete syntax tree

.xml AspectX Program

AOP with C++ © 2005 Olaf Spinczyk 26 XWeaver (2)

➢ the language-dependent part

.cpp .cpp C++ .cp.cppp Modified .cpp.h .cpp .h Base Code .h.h Code .h .h

srcML

XML Model .xml XML Model .xml .xml .xml of Modified of Base Code .xml .xml Code

XWeaver

.xml AspectX Program

AOP with C++ © 2005 Olaf Spinczyk 27 XWeaver (3)

current limitations:

➢ only supports “embedded C++”

➢ strongly limited model – function/constructor/destructor execution only reason: srcML parser problems

➢ no semantic analysis, no function call resolution no call advice!

➢ E.g. “int (*f())(long) {}” yields nonsense

AOP with C++ © 2005 Olaf Spinczyk 28 XWeaver (3)

current limitations:

➢ only supports “embedded C++”

➢ strongly limited join point model – function/constructor/destructor execution only rTeahseo ntr: asrncsMfoL rpmaarsetior pnro abplepmrsoach is only viable

➢wnitho s aem fuanllytic-f alenadglysise,d n oC fu+n+c tpaion rcsaell rr/aesnaolutlyionzer. no call advice!

➢ E.g. “int (*f())(long) {}” yields nonsense

AOP with C++ © 2005 Olaf Spinczyk 29 Code Transformation - Review obliviousness: possible + weaver has full control quantification: possible + only a matter of aspect language features strong: definitely + generated code can be as efficient as tangled code usable: yes + AspectJ-like programming model possible + easy integration into existing tool chains + platform-independent

AOP with C++ © 2005 Olaf Spinczyk 30 Technical Approaches

idea coding source source code code transformation compilation Compiler Integration (virtual) binary code machine code transformation execution process

AOP with C++ © 2005 Olaf Spinczyk 31 Compiler Integration

Could an AOP extension for C++ make it into...

– commercial compilers?

– the C++ standardization? ISO/IEC JTC1/SC22/WG21

– very busy with problems like “A>”

– Detlef Vollmann summarizes “Aspects of Reflection in C++”

– Daveed Vandevoorde presents a “Metaprogramming Extension” at the ACCU conference

– besides that: no revolutions

AOP with C++ © 2005 Olaf Spinczyk 32 Compiler Integration

Could an AOP extension for C++ make it into...

– commercial compilers?

– Tthhee C t+ra+n sstaitniodnar dfrizoamtio Cn? to C++ took many years. ISO/IEC JTC1/SC22/WG21 We should not expect this process to be – very busy with problems like “A>” faster in the case of AOP. – Detlef Vollmann summarizes “Aspects of Reflection in C++”

– Daveed Vandevoorde presents a “Metaprogramming Extension” at ACCU

– besides that: no revolutions

AOP with C++ © 2005 Olaf Spinczyk 33 Technical Approaches

idea coding source sLaoungrceu caogde-independent code tAraOnsPformation compilation (virtual) binary code machine code transformation execution Runtime Weaving process • Arachne

AOP with C++ © 2005 Olaf Spinczyk 34 Weaving in Byte/Machine Code

completely decouples the weaver from the parser, but ...

➢ aspect weaving in machine code ...

– strictly limits available AOP features – has to be implemented for numerous platforms

➢ aspect weaving in virtual machine code ...

– is not feasible in most C++ dominated domains

– compromizes the strengths of C++

AOP with C++ © 2005 Olaf Spinczyk 35 Runtime Aspect Weaving

➢ same restrictions as static binary code weaving

➢ advantage:

– dynamicity – needed in some application scenarios

➢ disadvantage

– additional runtime system required

AOP with C++ © 2005 Olaf Spinczyk 36 Runtime Aspect Weaving

➢ same restrictions as static binary code weaving

➢ advantage:

– dynamicity – needed in some application scenarios Many C++ projects have problems ➢ disadvantawgeith crosscutting concerns.

– additional runtime system required Dynamic weaving is nice to have, but we should come up with a viable static AOP solution first.

AOP with C++ © 2005 Olaf Spinczyk 37 Binary Code Weaving - Review

obliviousness: given quantification: limited – restricted set of join point types and possible transformations – introductions are a huge problem strong: not really – binary code weaving conflicts with code optimization – potential loss of static type information usable: specific cases – highly platform dependent – aspect program expressivenes depends on machine model

AOP with C++ © 2005 Olaf Spinczyk 38 Summary

pure C++ source level binary level obliviousness - + + quantification - + o strong - + - usable o + -

AOP with C++ © 2005 Olaf Spinczyk 39 Summary

pure C++ source level binary level obliviousness - + + quantification - + o strong - + - usable o + -

Sustained success of AOP in the C++ world requires: – a source level weaving approach – a convincing freely available implementation

AOP with C++ © 2005 Olaf Spinczyk 40 The AspectC++ Project

“We are definitely not targeting C++ for our work.” (Gregor Kiczales, July 2001)

➢ Language Level Goals:

– AspectJ-like syntax and semantic – AspectC++ should fit well into the C++ philosophy

➢ Implementation Level Goals – support for various hardware platforms and C++ dialects – IDE integrations

AOP with C++ © 2005 Olaf Spinczyk 41 AspectC++ vs. AspectJ

aspect SimpleTracing { AspectJ pointcut tracedCall() : call(void FigureElement.draw(GraphicsContext)); before() : tracedCall() { System.out.println("Entering: " + thisJoinPoint); } }

aspect SimpleTracing { AspectC++ pointcut tracedCall() = call(“void FigureElement::draw(GraphicsContext&)”); advice tracedCall() : before () { cout << "Entering: " << JoinPoint::signature ()); } };

AOP with C++ © 2005 Olaf Spinczyk 42 The C++ Philosophy

➢ C compatibility – re-use of billions lines of code – but: untyped pointers, preprocessor, ... ➢ strong focus on static typing – generic programming – function and operator overloading ➢ multi-paradigm development – object-oriented, procedural, generic ➢ generative programming ➢ efficiency in time and space

AOP with C++ © 2005 Olaf Spinczyk 43 Consequences for AspectC++

AspectC++ has to cope with the C++ philosophy ➢ weaving in C-style code ➢ statically typed aspect implementations – “generic advice” ➢ multi-paradigm AOP – advice for C-style functions – advice for classes and objects – advice for generic code and template instances – advice for operator functions and conversion functions – ... ➢ generation of efficient code

AOP with C++ © 2005 Olaf Spinczyk 44 AspectC++ – Joinpoint API

Compile-Time Joinpoint API JoinPoint::That Type of affected class (call/execution) JoinPoint::Target Type of the target class (call) JoinPoint::Result Type of the function result th JoinPoint::Arg< i >::Type Type of the i function argument JoinPoint::Arg< i >::ReferredType (with 0 ≤ i < ARGS) JoinPoint::ARGS Number of arguments JoinPoint::JPID Unique identifier for this joinpoint JoinPoint::JPTYPE Type of the joinpoint (call/execution)

Runtime Joinpoint API That* that() current object instance Target* target() target object instance (call) Result* result() result value th Arg< i >::ReferredType* arg< i >() value of i argument ...

AOP with C++ © 2005 Olaf Spinczyk 45 AspectC++ – Joinpoint API

Compile-Time Joinpoint API JoinPoint::That Type of affected class (call/execution) JoinPoint::Target Type of the target class (call) JoinPoint::Result Type of the function result th JoinPoint::Arg< i >::Type Type of the i function argument JoinPoint::Arg< i >::ReferredType (with 0 ≤ i < ARGS) JoinPoint::ARGS Number of arguments JoinPoint::JPID Unique identifier for this joinpoint JoinPoint::JPTYPE Type of the joinpoint (call/execution)

Runtime Joinpoint API Complete signature of That* that() currtehnet o abjfefcet cintsetadn cfeunction Target* target() target object instance (call) is available Result* result() result value th Arg< i >::ReferredType* arg< i >() value of i argument ...

AOP with C++ © 2005 Olaf Spinczyk 46 AspectC++ – Joinpoint API Compile-Time Joinpoint API JoinPoint::That Type of affected class (call/execution) JoinPoint::Target Type of the target class (call) JoinPoint::Result Type of the function result th JoinPoint::Arg< i >::Type Type of the i function argument JoinPoint::Arg< i >::ReferredType (with 0 ≤ i < ARGS) JoinPoint::ARGS Number of arguments JoinPoint::JPID Unique identifier for this joinpoint JoinPoint::JPTYPE Type of the joinpoint (call/execution) Type-safe access to actual Runtime Joinpoint API values at runtime That* that() current object instance Target* target() target object instance (call) Result* result() result value th Arg< i >::ReferredType* arg< i >() value of i argument ...

AOP with C++ © 2005 Olaf Spinczyk 47 Generic Advice

A compile-time switch with overloaded functions

... operator <<(..., int)

aspect TraceService { advice call(...) : after() { ... operator <<(..., long) ... cout << *tjp->result(); } ... operator <<(..., bool) };

... operator <<(..., Foo)

AOP with C++ © 2005 Olaf Spinczyk 48 Generic Advice

A compile-time switch with overloaded functions

... operator <<(..., int)

aspect TraceService { advice call(...) : after() { ... operator <<(..., long) ... cout << *tjp->result(); } ... operator <<(..., bool) }; ▪ no runtime type checks are needed ▪ unhandled types are detected at compile-time ▪ functions can be inlined ... operator <<(..., Foo)

AOP with C++ © 2005 Olaf Spinczyk 49 Generic Advice Instantiation of template metaprograms

template struct ArgPrinter { static void work( TJP* tjp ) { ArgPrinter::work(tjp); cout << ", " << *tjp->arg(); } }; template struct ArgPrinter {...};

aspect TraceService { advice call(...) : after() { ... ArgPrinter::work(tjp); } };

AOP with C++ © 2005 Olaf Spinczyk 50 Generic Advice Instantiation of template metaprograms

template struct ArgPrinter { static void work( TJP* tjp ) { ArgPrinter::work(tjp); cout << ", " << *tjp->arg(); } }; templaftuell< cplowasse Tr JofP, te0>m sptlarutcet m AergtaPrpirnogteram {.m..i}n;g is available for aspects aspect TraceService { advice call(...) : after() { ... ArgPrinter::work(tjp); } };

AOP with C++ © 2005 Olaf Spinczyk 51 AspectC++ Implementation

2 kg of syn tax an d sem antics

AOP with C++ © 2005 Olaf Spinczyk 52 Dealing with Real-World C++ Code

➢ a standard compliant C++ parser is HUGE – currently 70.000 lines of AspecC++ code ➢ even commercial compilers are not fully compliant – EDG announced to have the first fully standard compliant parser a few years ago! ➢ compiler-specific language extensions ➢ the standard is interpreted differently

AOP with C++ © 2005 Olaf Spinczyk 53 State of the Implementation

➢ works – parser handles real-world code – rich aspect language ➢ does not work yet – weaving in template instances ➢ should be improved – performance – dependency management – weaving in C code

AOP with C++ © 2005 Olaf Spinczyk 54 AspectC++ IDEs

➢ AspectC++ Add-In for Visual Studio .NET – commercial Visual Studio extension by pure-systems GmbH ➢ AspectC++ Development Tools for Eclipse (ACDT) – open source Eclipse plugin (demo!)

AOP with C++ © 2005 Olaf Spinczyk 55 User Community

➢ 150 subscribed AspectC++ users

– most of them from ...com

➢ 500 downloads of AspectC++ 0.9.1 (binary version) since published at february, 10th

➢ application areas

– mobile phones and PDAs: Nokia, Siemens

– telecommunications: Samsung

– real-time databases: Linkøping University

– operating systems: Unversity of Erlangen (CiAO, ECOS, L4)

AOP with C++ © 2005 Olaf Spinczyk 56 AspectC++ in the Future

➢ documentation – language – resource consumption – application class coverage ➢ integration into Linux distributions – Debian is on the way ➢ C support – extension of the join point model ➢ more partners – who wants to support the AspectC++ project?

AOP with C++ © 2005 Olaf Spinczyk 57 References

K. Czarnecki, U.W. Eisenecker et. al.: "Aspektorientierte Programmierung in C++", iX – Magazin für professionelle Informationstechnik, 08/09/10, 2001  A comprehensive analysis of doing AOP with pure C++: what's possible and what not  http://www.heise.de/ix/artikel/2001/08/143 C. Diggins: “Aspect-oriented Programming in C++”, Dr. Dobb's Journal, August, 2004. Semantic Designs, Inc.: "Aspect-oriented Programming with DMS"  http://www.semdesigns.com/Products/DMS/AspectOrientedProgramming.html pure-systems GmbH: AspectC++ Add-In for Visual Studio .NET  http://www.pure-systems.com P&P Software GmbH: “The XWeaver Project”  http:://www.pnp-software.com/XWeaver

AOP with C++ © 2005 Olaf Spinczyk 58 Thank you for your attention!

AOP with C++ © 2005 Olaf Spinczyk 59