#LyX 1.3 created this file. For more info see http://www.lyx.org/ \lyxformat 221 \textclass article \begin_preamble %-------------------------------------------------------------------- % PDF related %-------------------------------------------------------------------- \newif\ifpdf \ifx\pdfoutput\undefined \pdffalse % we are not running PDFLaTeX \else \pdfoutput=1 % we are running PDFLaTeX \pdftrue \fi %-------------------------------------------------------------------- % setup hyperref %-------------------------------------------------------------------- \ifpdf \usepackage{color} \pagecolor{white} \usepackage{hyperref} \hypersetup{% % %% general document information pdftitle = {AspectC++ Language Reference}, pdfsubject = {Dokumentation}, pdfkeywords = {AOP, AspectC++, pure-systems}, pdfauthor = {\textcopyright\ pure-systems GmbH and Olaf Spinczyk}, pdfcreator = {\LaTeX\ with package \flqq hyperref\frqq}, pdfproducer = {pdfTeX-0.\the\pdftexversion\pdftexrevision}, % %% document style colorlinks=true, % colored link urlcolor=blue, % blue URL links bookmarks=true, % generate bookmarks bookmarksnumbered=true, % generate section numbers pdfpagemode=None % don't open bookmarks } %% set document creation date to 10.7.2003 00:00 \pdfinfo {/CreationDate (D:20040628000000)} \fi %-------------------------------------------------------------------- % create hyperrefs depending on the use of pdflatex %-------------------------------------------------------------------- \newcommand\depref[2]{% \ifpdf \href{#1}{#2} \else {#2} \fi } \usepackage{listings} \usepackage{color} \definecolor{darkgrey}{rgb}{.4, .4, .4} \definecolor{lightgrey}{rgb}{.95, .95, .95} \definecolor{lightyellow}{rgb}{.99, .97, .90} \lstloadlanguages{C++} \lstdefinelanguage[Aspect]{C++}[ISO]{C++}{ morekeywords={aspect, advice, pointcut, tjp}, morekeywords=[2]{call, execution, within, cflow, before, around, after, baseclass } } \lstdefinestyle{acstyle}{ basicstyle=\footnotesize\ttfamily, keywordstyle=[2]\bfseries\emph, showstringspaces=false, language=[Aspect]{C++}, escapechar={@}, } \lstdefinestyle{ac}{ style=acstyle, xleftmargin=6mm, lineskip=-5.5pt } \lstdefinestyle{aclisting}{ style=acstyle, backgroundcolor=\color{lightyellow}, frame=single, %frameround=tttt, rulecolor=\color{yellow} } \lstset{style=ac} \rhead{AspectC++ Language Overview} \lfoot{\copyright 2005 Olaf Spinczyk} \end_preamble \language english \inputencoding auto \fontscheme pslatex \graphics default \paperfontsize default \spacing single \papersize a4paper \paperpackage a4 \use_geometry 0 \use_amsmath 0 \use_natbib 0 \use_numerical_citations 0 \paperorientation portrait \leftmargin 1cm \topmargin 0.7cm \rightmargin 2.5cm \bottommargin 0.7cm \secnumdepth 3 \tocdepth 3 \paragraph_separation skip \defskip smallskip \quotes_language english \quotes_times 2 \papercolumns 1 \papersides 1 \paperpagestyle fancy \layout Title \noindent \series bold \emph on AspectC++ -- A Language Overview \layout Author \begin_inset ERT status Collapsed \layout Standard \backslash copyright \end_inset 2005 Olaf Spinczyk \newline Friedrich-Alexander University Erlangen-Nuremberg \newline Computer Science 4 \layout Standard This is an overview about the AspectC++ language, an aspect-oriented extension for C/C++, prepared for the European AOSD Network of Excellence. Detailed information on AspectC++ including manuals and tutorial slides with lots of examples is available from \begin_inset ERT status Open \layout Standard \backslash depref{http://www.aspectc.org/}{ \backslash emph{the AspectC++ Project Homepage}} \backslash hspace{-4pt} \end_inset . \layout Standard The overview is based on the documentation for AspectC++ 0.9.3. The language is still \begin_inset Quotes eld \end_inset work in progress \begin_inset Quotes erd \end_inset . \layout Section* Joinpoint Model and Pointcut Language \layout Standard AspectC++ supports \emph on static \emph default joinpoints as well as \emph on dynamic \emph default joinpoints. While static joinpoints are named entities in the static program structure, dynamic joinpoints are events that happen during the program execution. \layout Subsection* Static Joinpoints \layout Standard The following kinds of C++ program entities are considered as static joinpoints: \layout Itemize classes, structs, and unions \layout Itemize namespaces \layout Itemize all kinds of functions (member, non-member, operator, conversion, etc.) \layout Standard Static joinpoints are described by \emph on match expressions \emph default . For example, \family typewriter \size small "% ...::foo(...)" \family default \size default is a match expression that describes all functions called \family typewriter \size small foo \family default \size default (in any scope, with any argument list, and any result type). More information on match expressions is given below. Note that not all of these static joinpoint types are currently supported as a target of advice (see section 'Advice for Static Joinpoints'). \layout Subsection* Dynamic Joinpoints \layout Standard The following kinds of events that might happen during the execution of a program are considered as dynamic joinpoints: \layout Itemize function call \layout Itemize function execution \layout Itemize object construction \layout Itemize object destruction \layout Standard The description of dynamic joinpoints is based upon the description mechanism for static joinpoints in conjunction with joinpoint type specific \emph on pointcut functions \emph default . For example: \layout Itemize \family typewriter \series bold \size small \emph on call \series default \emph default ("% ...::foo(...)") \layout Itemize \family typewriter \series bold \size small \emph on execution \series default \emph default ("float MathFuncs::%(float)") \layout Itemize \family typewriter \series bold \size small \emph on construction \series default \emph default ("SomeClassName") \layout Itemize \family typewriter \series bold \size small \emph on destruction \series default \emph default ("A"||"B") \layout Standard While \family typewriter \size small "% ...::foo(...)" \family default \size default represents a set of static joinpoints, i.e. all functions called \family typewriter \size small foo \family default \size default , the expression \family typewriter \size small call("% ...::foo(...)") \family default \size default describes all calls to these functions. A similar mapping from static to dynamic joinpoints is done by the \family typewriter \size small execution() \family default \size default , \family typewriter \size small construction() \family default \size default , and \family typewriter \size small destruction() \family default \size default pointcut functions. \layout Subsection* Pointcut Functions \layout Standard Further pointcut functions are used to filter or select joinpoints with specific properties. Some of them can be evaluated at compile time while others yield conditions that have to be checked at run time: \layout Itemize \family typewriter \series bold \size small \emph on cflow \series default \emph default ( \emph on pointcut \emph default ) \family default \size default -- captures all joinpoints in the dynamic execution context of the joinpoints in \emph on pointcut \emph default . \layout Itemize \family typewriter \series bold \size small \emph on base \series default \emph default ( \emph on pointcut \emph default ) \family default \size default and \family typewriter \series bold \size small \emph on derived \series default \emph default ( \emph on pointcut \emph default ) \family default \size default -- yield classes based on queries in the class hierarchy \layout Itemize \family typewriter \series bold \size small \emph on within \series default \emph default ( \emph on pointcut \emph default ) \family default \size default -- filters joinpoints depending on their lexical scope \layout Itemize \family typewriter \series bold \size small \emph on that \series default \emph default ( \emph on type\SpecialChar ~ pattern \emph default ) \family default \size default , \family typewriter \series bold \size small \emph on target \series default \emph default ( \emph on type\SpecialChar ~ pattern \emph default ) \family default \size default , \family typewriter \series bold \size small \emph on result \series default \emph default ( \emph on type\SpecialChar ~ pattern \emph default ) \family default \size default , and \family typewriter \series bold \size small \emph on args \series default \emph default ( \emph on type\SpecialChar ~ pattern \emph default ) \family default \size default -- filters joinpoints depending on the current object type, the target object type in a call, and the result and arguments types of a dynamic joinpoint. \layout Itemize \family typewriter \series bold \size small && \family default \series default \size default , \family typewriter \series bold \size small || \family default \series default \size default , \family typewriter \series bold \size small ! \family default \series default \size default -- intersection, union, and exclusion of joinpoints in pointcuts \layout Standard Instead of the \emph on type pattern \emph default it is also possible to pass the name of a \emph on context variable \emph default to which context information from the joinpoint shall be bound. In this case the type of the context variable is used for the type matching. \layout Subsection* Match Mechanism Capabilities \layout Standard The match mechanism provides \family typewriter \size small % \family default \size default and \family typewriter \size small ... \family default \size default as wildcard symbols. Thereby the following features are supported: \layout Itemize pattern based name matching, e.g. \family typewriter \size small "%X%" \family default \size default matches all names that contain an uppercase \family typewriter \size small X \layout Itemize flexible scope matching, e.g. \family typewriter \size small "Foo::...::Bar" \family default \size default matches Bar in the scope \family typewriter \size small Foo \family default \size default or any of its nested scopes \layout Itemize flexible matching of function argument type lists, e.g. \family typewriter \size small "% foo(...,int)" \family default \size default matches \family typewriter \size small foo \family default \size default with an \family typewriter \size small int \family default \size default as its last argument type \layout Itemize matching template argument lists, e.g. \family typewriter \size small "C" \family default \size default matches an instance of the class template \family typewriter \size small C \family default \size default with a first template argument type \family typewriter \size small T \layout Itemize type patterns, e.g. \family typewriter \size small "const % *" \family default \size default matches all pointer types that reference objects of a constant type \layout Subsection* Named Pointcuts \layout Standard Pointcut expressions can be given a name. The definition of a named pointcut can be placed in any aspect, class, or namespace. The mechanism can be used for dynamic as well as static joinpoints. For example: \layout Standard \begin_inset ERT status Inlined \layout Standard \backslash begin{lstlisting}[style=ac] \layout Standard class OStream { \layout Standard // ... \layout Standard pointcut manipulator_types() = "hex"||"oct"||"bin"||"endl"; \layout Standard }; \layout Standard \backslash end{lstlisting} \end_inset \layout Standard In the context of an aspect, named pointcuts can also be defined as virtual or pure virtual, which allows refinement/definition in a derived aspect (see example at the end of this overview). \layout Section* Advice Model and Language \layout Subsection* Advice for Static Joinpoints \layout Standard The only kind of advice for static joinpoints that is currently supported by AspectC++ is the \emph on introduction \emph default . By using this kind of advice the aspect code is able to add new elements to classes, structures, or unions. Everything that is syntactically permitted in the body of a C++ class can be introduced by advice: \layout Itemize attribute introduction, e.g. \family typewriter \size small advice "AClass" : int _introduced_attribute; \layout Itemize type introduction, e.g. \family typewriter \size small advice "AClass" : typedef int INT; \layout Itemize member function introduction, e.g. \family typewriter \size small advice "AClass" : void f(); \layout Itemize nested type introduction, e.g. \family typewriter \size small advice "AClass" : class Inner { ... }; \layout Itemize constructor introduction, e.g. \family typewriter \size small advice "AClass" : AspectName(int, double); \layout Itemize destructor introduction, e.g. \family typewriter \size small advice "AClass" : ~AspectName() { ... } \layout Itemize base class introduction, e.g. \family typewriter \size small advice "AClass" : baseclass(ANewBaseclass); \layout Standard The syntax \family typewriter \size small advice < \emph on target \emph default - \emph on pointcut \emph default > : < \emph on introduction \emph default > \family default \size default supports the introduction of an element into an arbitrary set of target classes with a single advice. \layout Subsection* Advice for Dynamic Joinpoints \layout Standard Advice for dynamic joinpoints is used to affect the flow of control, when the joinpoint is reached. The following kinds of advice are supported: \layout Itemize before advice \layout Itemize after advice \layout Itemize around advice \layout Standard These advice types can orthogonally be combined with all dynamic joinpoint types. Advice for dynamic joinpoints is defined with the following syntax: \layout LyX-Code \series bold \size small advice \series default < \emph on target-pointcut \emph default > : ( \series bold before \series default | \series bold after \series default | \series bold around \series default ) (< \emph on arguments \emph default >) { \layout LyX-Code \size small < \emph on advice-body \emph default > \layout LyX-Code \size small } \layout Standard While the before and after advice bodies are executed before or after the event described by \family typewriter \size small \emph on \family default \size default \emph default , an around advice body is executed instead of the event. \layout Subsubsection* Advice Language and Joinpoint API \layout Standard The advice body is implemented in standard C++. Additionally, the \emph on joinpoint API \emph default can be used to access (read and write) \emph on context information \emph default (e.g. function argument and result values) as well as \emph on static type information \emph default about the current joinpoint. To access the joinpoint API the object \family typewriter \size small JoinPoint *tjp \family default \size default can be used, which is implicitly available in advice code. Advice that uses the static type information provided by the joinpoint API is called \emph on generic advice \emph default . This concept is the key for generic, type-safe, \emph on and \emph default efficient advice in AspectC++. The static type information from the joinpoint API can even be used to instantiate template metaprograms, which is a technique for joinpoint specific code generation at compile time. \layout Standard The joinpoint API is also used to proceed the execution from within around advice ( \family typewriter \size small tjp->proceed() \family default \size default ). Alternatively, \family typewriter \size small tjp->action() \family default \size default may be called to retrieve and store the \emph on proceed context \emph default as an \family typewriter \size small AC::Action \family default \size default object. Later, \family typewriter \size small action.trigger() \family default \size default may be called to proceed the intercepted flow of control. \layout Standard Catching and changing exceptions can be done by standard C++ features in around advice (try, catch, throw). \layout Subsubsection* Example \layout Standard The following advice is \emph on generic advice \emph default , because its implementation can deal with multiple overloaded \family typewriter \size small C::foo(...) \family default \size default implementations that have different result types: \layout Standard \begin_inset ERT status Open \layout Standard \backslash begin{lstlisting}[style=ac] \layout Standard advice execution("% C::foo(...)") : around() { \layout Standard cout << "executing " << JoinPoint::signature() \layout Standard << " on " << *tjp->that() << endl; \layout Standard tjp->proceed (); \layout Standard cout << "the result was " << *tjp->result() << endl; \layout Standard } \layout Standard \backslash end{lstlisting} \end_inset \layout Section* Aspect Module Model \layout Standard The following example code shows an aspect \family typewriter \size small Logging \family default \size default defined in AspectC++: \layout Standard \begin_inset ERT status Inlined \layout Standard \backslash begin{lstlisting}[style=ac] \layout Standard aspect Logging { \layout Standard ostream *_out; // ordinary attributes \layout Standard public: \layout Standard void bind_stream(ostream *o) { _out = o; } // member function \layout Standard pointcut virtual logged_classes() = 0; // pure virtual pointcut \layout Standard // some advice \layout Standard advice execution("% ...::%(...)") && within(logged_classes()) : \layout Standard before () { \layout Standard *_out << "executing " << JoinPoint::signature () << endl; \layout Standard } \layout Standard }; \layout Standard \backslash end{lstlisting} \end_inset \layout Standard Aspects are the language element that is used to group all the definitions that are needed to implement a crosscutting concern. An aspect definition is allowed to contain member functions, attributes, nested classes, named pointcuts, etc. as ordinary classes. Additionally, aspects normally contain advice definitions. \layout Standard Aspects that contain pure virtual member functions or pure virtual pointcuts are called \emph on abstract aspects \emph default . These aspects only affect the system if a (concrete) aspect is derived, which defines an implementation for the pure virtual functions and the pure virtual pointcuts. Abstract aspects are the AspectC++ mechanism to implement reusable aspect code. \layout Standard Aspect inheritance is slighly restricted. Aspects can inherit from ordinary classes and abstract aspects but not from concrete aspects. Derived aspects can redefine virtual pointcuts and virtual functions defined by base aspects. \layout Section* Aspect Instantiation Model \layout Standard By default, aspects are singletons, i.e. there is one global instance automatically created for each non-abstract aspect in the program. However, by defining the \family typewriter \size small aspectof() \family default \size default static member function of an aspect the user can implement arbitrary instantiat ion schemes, such as \emph on per-target \emph default , \emph on per-thread \emph default , or \emph on per-joinpoint \emph default . For each dynamic joinpoint that is affected by the aspect the \family typewriter \size small aspectof() \family default \size default function has to return the right aspect instance on which the advice bodies are invoked. The instances themselfs are usually created with the introduction mechanism. The \family typewriter \size small aspectof() \family default \size default function has access to the joinpoint API in order to find the right aspect instance for the current joinpoint. Here is an example: \layout Standard \begin_inset ERT status Inlined \layout Standard \backslash begin{lstlisting}[style=ac] \layout Standard aspect InstancePerTargetAspect { \layout Standard pointcut target_class() = "TargetName"; \layout Standard // an attribute of the aspect (stored per target) \layout Standard int _calls; \layout Standard // aspect instance created by introduction: \layout Standard advice target_class() : InstancePerTargetAspect _instance; \layout Standard // definition of the instantiation scheme in aspectof(): \layout Standard static InstancePerTargetAspect *aspectof() { \layout Standard return tjp->target()->_instance; \layout Standard } \layout Standard // the advice \layout Standard advice call("% ...::%(...)") && target(target_class()) : before () { \layout Standard _calls++; \layout Standard } \layout Standard // attribute initialization in the aspect constructor \layout Standard InstancePerTargetAspect () : _calls (0) {} \layout Standard }; \layout Standard \backslash end{lstlisting} \end_inset \layout Section* Aspect Composition Model \layout Standard In AspectC++ any number of aspects can be used in the same application. Aspect composition is currently restricted to inheritance from abstract aspects. Concrete aspects cannot be used for the implementation of new aspects. \layout Standard Aspect interactions can be handled by the developer on a per joinpoint basis with \emph on order advice \emph default , e.g. \family typewriter \size small advice execution("void f%(...)") : order("Me", !"Me") \family default \size default gives the aspect \family typewriter \size small Me \family default \size default the highest precedence at all joinpoint described by the pointcut expression. Order advice can be given by any aspect for any aspect, thus can be separated from the affected aspects. Aspect names in order advice declarations are match expressions and may contain wildcards, e.g. \family typewriter \size small order("kernel::%", !"kernel::%") \family default \size default gives every aspect declared in the namespace \family typewriter \size small kernel \family default \size default precedence over all other aspects. Besides this partial order in the precedence of aspects, the precedence of advice within one aspect is determined according to its position in the aspect definition. As long as it does not conflict with order advice AspectC++ aims to give aspects the same precedence at all joinpoints. \layout Section* Aspect Weaving Model \layout Standard The only implementation of AspectC++ is based on static source to source transformation. Nevertheless, the language could also be used for dynamic weavers if some really hard technical challenges like runtime introductions were solved. \layout Section* Example \layout Standard A reusable observer pattern implementation in AspectC++: \layout Standard \begin_inset ERT status Inlined \layout Standard \backslash begin{lstlisting}[style=ac] \layout Standard aspect ObserverPattern { \layout Standard // Data structures to manage subjects and observers \layout Standard ... \layout Standard public: \layout Standard // Interfaces for each role \layout Standard struct ISubject {}; \layout Standard struct IObserver { \layout Standard virtual void update(ISubject *) = 0; \layout Standard }; \layout Standard \layout Standard // To be defined by the concrete derived aspect \layout Standard pointcut virtual observers() = 0; \layout Standard pointcut virtual subjects() = 0; \layout Standard // subjectChange() matches all non-const methods by default \layout Standard pointcut virtual subjectChange() = \layout Standard execution("% ...::%(...)" && !"% ...::%(...) const") \layout Standard && within(subjects()); \layout Standard \layout Standard // Add new baseclass to each subject/observer class \layout Standard // and insert code to inform observers \layout Standard advice observers() : baseclass(IObserver); \layout Standard advice subjects() : baseclass(ISubject); \layout Standard advice subjectChange() : after() { \layout Standard ISubject* subject = tjp->that(); \layout Standard updateObservers(subject); \layout Standard } \layout Standard \layout Standard // Operations to add, remove and notify observers \layout Standard void updateObservers(ISubject* sub) { ... } \layout Standard void addObserver(ISubject* sub, IObserver* ob) { ... } \layout Standard void remObserver(ISubject* sub, IObserver* ob) { ... } \layout Standard }; \layout Standard \backslash end{lstlisting} \end_inset \layout Standard Concrete aspect, which applies the pattern: \layout Standard \begin_inset ERT status Open \layout Standard \backslash begin{lstlisting}[style=ac] \layout Standard #include "ObserverPattern.ah" \layout Standard #include "ClockTimer.h" \layout Standard \layout Standard aspect ClockObserver : public ObserverPattern { \layout Standard // define the pointcuts \layout Standard pointcut subjects() = "ClockTimer"; \layout Standard pointcut observers() = "DigitalClock"||"AnalogClock"; \layout Standard public: \layout Standard advice observers() : \layout Standard void update( ObserverPattern::ISubject* sub ) { \layout Standard Draw(); \layout Standard } \layout Standard }; \layout Standard \backslash end{lstlisting} \end_inset \the_end