[go: nahoru, domu]

Jump to content

AspectC++: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
No edit summary
No edit summary
Line 6: Line 6:
existing functionality.
existing functionality.


For example, if we want to Trace all calls to an API call, rather than inserting 'cerr' or print statements in many places,
For example, if we want to trace all calls to an API call, rather than inserting 'cerr' or print statements in many places,
we can create a single aspect that will accomplish this:
we can create a single aspect that will accomplish this:
<blockquote>
<blockquote>
<code>
aspect Tracer { <br />
<pre>
advice call("% %Iter::Reset(...)") : before() { <br />
aspect Tracer
cerr << "about to call Iter::Reset for " << JoinPoint::signature() << endl; <br />
{
}<br />
advice call("% %Iter::Reset(...)") : before()
};<br />
{
cerr << "about to call Iter::Reset for " << JoinPoint::signature() << endl; <br />
}
};</pre>
</code>
</blockquote>
</blockquote>


Line 19: Line 24:
means that it will match all classes that end in Iter.
means that it will match all classes that end in Iter.


Each 'matched' location in the source code is call a joinpoint -- the advice is joined to (or advises) that code.
Each 'matched' location in the source code is called a joinpoint -- the advice is joined to (or advises) that code.
AspectC++ provides a joinpoint API to enable accessing this information. For example, the function:
AspectC++ provides a joinpoint API to provide and access to information about the joinpoint. For example, the function:
<blockquote>
<blockquote>
<code>
JoinPoint::signature()
<pre>
JoinPoint::signature()</pre>
</code>
</blockquote>
</blockquote>
returns the name of the method (that matched %Iter::Reset) that is about to be called.
returns the name of the method (that matched %Iter::Reset) that is about to be called.


The joinpoint API also provides compile-time type information that can be used within an
The joinpoint API also provides compile-time type information that can be used within an
aspect to access the type of value or arguments and the return type and return value of a
aspect to access the type or the value of the arguments and the return type and return value of a
method or function.
method or function.



Revision as of 04:55, 19 September 2007

AspectC++ is an aspect-oriented extension of C and C++ languages. It is based on source-to-source translation, translating AspectC++ source code to C++. It is available under the GNU GPL, though some extensions specific to Microsoft Windows are only available through pure-systems GmbH.

Aspect-oriented programming allows modularizing cross-cutting concerns in a single module, an aspect. Aspects can modify existing classes, but most commonly they provide 'advice' that runs before, after, or around existing functionality.

For example, if we want to trace all calls to an API call, rather than inserting 'cerr' or print statements in many places, we can create a single aspect that will accomplish this:

aspect Tracer
{ 
   advice call("% %Iter::Reset(...)") : before()
   {
      cerr << "about to call Iter::Reset for " << JoinPoint::signature() << endl; <br />
   }
};

The Tracer aspect above will print out a message before any call to %Iter::Reset. The '%Iter' syntax means that it will match all classes that end in Iter.

Each 'matched' location in the source code is called a joinpoint -- the advice is joined to (or advises) that code. AspectC++ provides a joinpoint API to provide and access to information about the joinpoint. For example, the function:

   JoinPoint::signature()

returns the name of the method (that matched %Iter::Reset) that is about to be called.

The joinpoint API also provides compile-time type information that can be used within an aspect to access the type or the value of the arguments and the return type and return value of a method or function.

In addition to the documentation and tutorials at the AspectC++ website (external link below), you can also find articles on Aspect-oriented programming and AspectC++ at past AOSD conferences [1].