Aspect-Oriented Programming with C++ - About...
Transcript of Aspect-Oriented Programming with C++ - About...
© 2005 Olaf Spinczyk 1AOP with C++University of Erlangen-NurembergComputer Science 4
Aspect-Oriented Programming Aspect-Oriented Programming with C++with C++
Olaf SpinczykOlaf Spinczyk
The AspectC++ Project
© 2005 Olaf Spinczyk 2AOP with C++
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
© 2005 Olaf Spinczyk 3AOP with C++
Observations: AOP Products
➢ IBM: AspectJ and AJDT
➢ BEA: AspectWerkz
➢ JBoss: JBoss AOP
© 2005 Olaf Spinczyk 4AOP with C++
Observations: AOP Products
➢ IBM: AspectJ and AJDT
➢ BEA: AspectWerkz
➢ JBoss: JBoss AOP
© 2005 Olaf Spinczyk 5AOP with C++
Observations: AOP Products
... for C++ developers:
➢ Semantic Designs: DMS
➢ P&P Software: XWeaver
➢ pure-systems: AspectC++ Add-In
© 2005 Olaf Spinczyk 6AOP with C++
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
© 2005 Olaf Spinczyk 7AOP with C++
Observations: AOP Research
2003 2004 20050
2,5
5
7,5
10Java vs. C++ at AOSD
other
Java
C/C++
C++ is almost invisible!
© 2005 Olaf Spinczyk 8AOP with C++
Java vs. C++ in the Real World
2004/09/09 (source-forge)
0
10000
20000
30000
40000
27,38%
37,71%
34,91%
open source projects
Java
C/C++
C only
© 2005 Olaf Spinczyk 9AOP with C++
Java vs. C++ in the Real World
2003 20040,00%
25,00%
50,00%
75,00%
100,00%
26 21
41 41
35 38
requested skills
Java
C++
C
© 2005 Olaf Spinczyk 10AOP with C++
Java vs. C++ in the Real World
2003 20040,00%
25,00%
50,00%
75,00%
100,00%
26 21
41 41
35 38
requested skills
Java
C++
C
C and C++ are still dominating!
© 2005 Olaf Spinczyk 11AOP with C++
Java vs. C++ in the Real World
AOP research & products don't reflect the real world.
Why?
© 2005 Olaf Spinczyk 12AOP with C++
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
© 2005 Olaf Spinczyk 13AOP with C++
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
© 2005 Olaf Spinczyk 14AOP with C++
* WANTED *DEAD OR ALIVE
AspectC++
© 2005 Olaf Spinczyk 15AOP with C++
Requirements
an AOP solution for C++ has to ...➢ support full obliviousness and quantification
– no preparation of the component code– rich pointcut 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
© 2005 Olaf Spinczyk 16AOP with C++
Technical Approaches
process
idea
sourcecode
(virtual)machine code
coding
compilation
execution
source codetransformation
binary codetransformation
© 2005 Olaf Spinczyk 17AOP with C++
Technical Approaches
process
idea
sourcecode
(virtual)machine code
coding
compilation
execution
source codetransformation
binary codetransformation
AOP with pure C++• patterns• templates, macros
© 2005 Olaf Spinczyk 18AOP with C++
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 Q>class Counting_Aspect : public Q { int counter; // introductionpublic: void enqueue(Item* item) { // after advice Q::enqueue(item); counter++; }};
© 2005 Olaf Spinczyk 19AOP with C++
AOP with pure C++ (2)
aspect weaving by template instantiation
// component codeclass Queue { ... }
// wrappers (aspects)template <class Q>class Counting_Aspect : public Q { ... }template <class Q>class Tracing_Aspect : public Q { ... }
// template instantiation (weaving)typedef Counting_Aspect<Queue> CountingQueue;typedef Trace_Aspect<Counting_Queue> TraceCountingQueue;
© 2005 Olaf Spinczyk 20AOP with C++
AOP with pure C++ (3)
obliviousness for the client code
namespace components { class Queue { ... };}namespace aspects { template <class Q> class Counting_Aspect : public Q { ... };}namespace configuration { // select counting queue typedef aspects::Counting_Aspect<components::Queue> Queue;}
using namespace configuration;void client_code () { Queue queue; // Queue with all configured aspects queue.enqueue (new MyItem);}
© 2005 Olaf Spinczyk 21AOP with C++
AOP with pure C++ (4)
C. Diggins: macros hide the template “magic”
// the CountingAspect as beforestruct CountingAspect { // Inc and Dec is advice struct Inc { template<...> virtual void OnAfter (...) { ... } };}; // the DEF_POINTCUT macro describes sets of member functionsDEF_POINTCUT(EnqueuePointcut) SET_PROCJOINPOINT1(enqueue, Item*, item)END_POINTCUT
// the CROSSCUT macro combines a class, a pointcut, and an aspecttypedef CROSSCUT(Queue,EnqueuePointcut, CountingAspect::Inc) CountingQueue;
© 2005 Olaf Spinczyk 22AOP with C++
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
© 2005 Olaf Spinczyk 23AOP with C++
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
no tool needed, but too many restrictions
© 2005 Olaf Spinczyk 24AOP with C++
Technical Approaches
process
idea
(virtual)machine code
coding
compilation
execution
binary codetransformation
Code Transformers • AspectC++• XWeaver• DMS
sourcecode
source codetransformation
© 2005 Olaf Spinczyk 25AOP with C++
XWeaver (1)
.xml.xml.xml
.xml.xml.xml
XWeaver
.xml
XML Modelof Base Code
XML Modelof ModifiedCode
AspectXProgram
© 2005 Olaf Spinczyk 26AOP with C++
.xml.xml.xml
.xml.xml.xml
XWeaver
.xml
XML Modelof Base Code
XML Modelof ModifiedCode
AspectXProgram
* a piece of code in XML *<?xml version="1.0" encoding="ISO-8859-1"?>...<function> <type><name>int</name></type> <name>main</name> <parameter_list>()</parameter_list> <block>{}</block></function>... a concrete syntax tree
XWeaver (1)
© 2005 Olaf Spinczyk 27AOP with C++
➢ the language-dependent part
.xml.xml.xml
XWeaver
.xml
XML Modelof Base Code
XML Modelof ModifiedCode
AspectXProgram
.xml.xml.xml
.cpp
.h.cpp.h
.cpp
.h
.cpp
.h.cpp.h
.cpp
.h
C++Base Code
ModifiedCode
srcML
XWeaver (2)
© 2005 Olaf Spinczyk 28AOP with C++
current limitations:
➢ only supports “embedded C++”
➢ strongly limited join point model– function/constructor/destructor execution only
reason: srcML parser problems
➢ no semantic analysis, no function call resolutionno call advice!
➢ E.g. “int (*f())(long) {}” yields nonsense
XWeaver (3)
© 2005 Olaf Spinczyk 29AOP with C++
current limitations:
➢ only supports “embedded C++”
➢ strongly limited join point model– function/constructor/destructor execution only
reason: srcML parser problems
➢ no semantic analysis, no function call resolutionno call advice!
➢ E.g. “int (*f())(long) {}” yields nonsense
XWeaver (3)
The transformation approach is only viablewith a fully-fledged C++ parser/analyzer.
© 2005 Olaf Spinczyk 30AOP with C++
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
© 2005 Olaf Spinczyk 31AOP with C++
Technical Approaches
process
idea
sourcecode
(virtual)machine code
coding
compilation
execution
source codetransformation
binary codetransformation
Compiler Integration
© 2005 Olaf Spinczyk 32AOP with C++
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<B<C>>”
– Detlef Vollmann summarizes “Aspects of Reflection in C++”
– Daveed Vandevoorde presents a “Metaprogramming Extension” at the ACCU conference
– besides that: no revolutions
© 2005 Olaf Spinczyk 33AOP with C++
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<B<C>>”
– Detlef Vollmann summarizes “Aspects of Reflection in C++”
– Daveed Vandevoorde presents a “Metaprogramming Extension” at ACCU
– besides that: no revolutions
The transition from C to C++ took many years.
We should not expect this process to befaster in the case of AOP.
© 2005 Olaf Spinczyk 34AOP with C++
Technical Approaches
process
idea
sourcecode
(virtual)machine code
coding
compilation
execution
source codetransformation
binary codetransformation
Language-independentAOP
Runtime Weaving• Arachne
© 2005 Olaf Spinczyk 35AOP with C++
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++
Weaving in Byte/Machine Code
© 2005 Olaf Spinczyk 36AOP with C++
Runtime Aspect Weaving
➢ same restrictions as static binary code weaving
➢ advantage:
– dynamicity – needed in some application scenarios
➢ disadvantage
– additional runtime system required
© 2005 Olaf Spinczyk 37AOP with C++
Runtime Aspect Weaving
➢ same restrictions as static binary code weaving
➢ advantage:
– dynamicity – needed in some application scenarios
➢ disadvantage
– additional runtime system required
Many C++ projects have problemswith crosscutting concerns.
Dynamic weaving is nice to have, but we should comeup with a viable static AOP solution first.
© 2005 Olaf Spinczyk 38AOP with C++
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
© 2005 Olaf Spinczyk 39AOP with C++
Summary
pure C++ source level binary levelobliviousness - + +quantification - + ostrong - + -usable o + -
© 2005 Olaf Spinczyk 40AOP with C++
pure C++ source level binary levelobliviousness - + +quantification - + ostrong - + -usable o + -
Summary
Sustained success of AOP in the C++ world requires:– a source level weaving approach– a convincing freely available implementation
© 2005 Olaf Spinczyk 41AOP with C++
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
© 2005 Olaf Spinczyk 42AOP with C++
AspectC++ vs. AspectJ
aspect SimpleTracing { pointcut tracedCall() : call(void FigureElement.draw(GraphicsContext)); before() : tracedCall() { System.out.println("Entering: " + thisJoinPoint);} }
aspect SimpleTracing { pointcut tracedCall() = call(“void FigureElement::draw(GraphicsContext&)”); advice tracedCall() : before () { cout << "Entering: " << JoinPoint::signature ());} };
AspectJ
AspectC++
© 2005 Olaf Spinczyk 43AOP with C++
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
© 2005 Olaf Spinczyk 44AOP with C++
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
© 2005 Olaf Spinczyk 45AOP with C++
Compile-Time Joinpoint APIJoinPoint::That Type of affected class (call/execution)JoinPoint::Target Type of the target class (call)
JoinPoint::Result Type of the function resultJoinPoint::Arg< i >::Type Type of the i
th function argument JoinPoint::Arg< i >::ReferredType (with 0 ≤ i < ARGS)JoinPoint::ARGS Number of arguments
JoinPoint::JPID Unique identifier for this joinpointJoinPoint::JPTYPE Type of the joinpoint (call/execution)
Runtime Joinpoint APIThat* that() current object instanceTarget* target() target object instance (call)
Result* result() result valueArg< i >::ReferredType* arg< i >() value of i
th argument
...
AspectC++ – Joinpoint API
© 2005 Olaf Spinczyk 46AOP with C++
Compile-Time Joinpoint APIJoinPoint::That Type of affected class (call/execution)JoinPoint::Target Type of the target class (call)
JoinPoint::Result Type of the function resultJoinPoint::Arg< i >::Type Type of the i
th function argument JoinPoint::Arg< i >::ReferredType (with 0 ≤ i < ARGS)JoinPoint::ARGS Number of arguments
JoinPoint::JPID Unique identifier for this joinpointJoinPoint::JPTYPE Type of the joinpoint (call/execution)
Runtime Joinpoint APIThat* that() current object instanceTarget* target() target object instance (call)
Result* result() result valueArg< i >::ReferredType* arg< i >() value of i
th argument
...
AspectC++ – Joinpoint API
Complete signature of the affected function is available
© 2005 Olaf Spinczyk 47AOP with C++
Compile-Time Joinpoint APIJoinPoint::That Type of affected class (call/execution)JoinPoint::Target Type of the target class (call)
JoinPoint::Result Type of the function resultJoinPoint::Arg< i >::Type Type of the i
th function argument JoinPoint::Arg< i >::ReferredType (with 0 ≤ i < ARGS)JoinPoint::ARGS Number of arguments
JoinPoint::JPID Unique identifier for this joinpointJoinPoint::JPTYPE Type of the joinpoint (call/execution)
Runtime Joinpoint APIThat* that() current object instanceTarget* target() target object instance (call)
Result* result() result valueArg< i >::ReferredType* arg< i >() value of i
th argument
...
AspectC++ – Joinpoint API
Type-safe access to actual values at runtime
© 2005 Olaf Spinczyk 48AOP with C++
Generic Advice
A compile-time switch with overloaded functions
aspect TraceService { advice call(...) : after() { ... cout << *tjp->result(); }};
... operator <<(..., int)
... operator <<(..., bool)
... operator <<(..., long)
... operator <<(..., Foo)
© 2005 Olaf Spinczyk 49AOP with C++
Generic Advice
A compile-time switch with overloaded functions
aspect TraceService { advice call(...) : after() { ... cout << *tjp->result(); }};
... operator <<(..., int)
... operator <<(..., bool)
... operator <<(..., long)
... operator <<(..., Foo)
▪ no runtime type checks are needed▪ unhandled types are detected at compile-time▪ functions can be inlined
© 2005 Olaf Spinczyk 50AOP with C++
Generic AdviceInstantiation of template metaprograms
template<class TJP, int i> struct ArgPrinter { static void work( TJP* tjp ) { ArgPrinter<TJP, i-1>::work(tjp); cout << ", " << *tjp->arg<i>(); }};template<class TJP, 0> struct ArgPrinter {...};
aspect TraceService { advice call(...) : after() { ... ArgPrinter<JoinPoint, JoinPoint::ARGS>::work(tjp); }};
© 2005 Olaf Spinczyk 51AOP with C++
Generic AdviceInstantiation of template metaprograms
template<class TJP, int i> struct ArgPrinter { static void work( TJP* tjp ) { ArgPrinter<TJP, i-1>::work(tjp); cout << ", " << *tjp->arg<i>(); }};template<class TJP, 0> struct ArgPrinter {...};
aspect TraceService { advice call(...) : after() { ... ArgPrinter<JoinPoint, JoinPoint::ARGS>::work(tjp); }};
full power of template metaprogramming is available for aspects
© 2005 Olaf Spinczyk 52AOP with C++
AspectC++ Implementation
2 kg of syntax and semantics
© 2005 Olaf Spinczyk 53AOP with C++
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
© 2005 Olaf Spinczyk 54AOP with C++
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
© 2005 Olaf Spinczyk 55AOP with C++
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!)
© 2005 Olaf Spinczyk 56AOP with C++
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)
© 2005 Olaf Spinczyk 57AOP with C++
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?
© 2005 Olaf Spinczyk 58AOP with C++
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
References
© 2005 Olaf Spinczyk 59AOP with C++
Thank you for your attention!