[aspectc-user] capturing variables

Fabian Scheler fabian.scheler at gmail.com
Tue Jul 26 09:01:47 CEST 2005


Hi Jamal,

> I have a method in a class. in this method I'm declaring a variable (int
> x), and I perform certain manipulations on it. I need to put an after
> advice and somehow capture the value of x in order to perform furthur
> manipulations in the advice. Is that possible? if so how would the sytax
> be?
> 
> Here is an example:
> in base code:
> int A::a( const char *name, float b) {
> 
>    int x=7;
> //perform certain manipulations at the end the value of x is 15
> }
> 
> 
> in advice:
> advice execution("% A::a(...)")  :after(){
> .
> .
> // how can I capture the value of x which is 15 in this advice considering
> // that x is not defined in class A there I cant use tjp-that()
> cout <<"at the end "<<x<<endl;
> }

AspectC++ only supports pointcut expressions at the level of functions
up to now, so you cannot access the local information within a
function like local variables. In the most cases it is possible to
alter the function "% A::a(...)" in such a way that it calls another
function (implemented as an inline-function) with x as a parameter,
this way you can access x.

> secondly would ac++ support something like:
> advice execution("% A::a(...)")  :after(){
> tjp->that()->B::b();
> }
> assuming that B is the super class of A? if not would I need my aspect to
> inheret from A or B? or is there a way to perform this without furthur
> inheritance?

The that()-function of the Joinpoint-API returns a pointer to an
object of type A in this case, so you can call any method, that can be
called through a pointer to an object of type A. If your class B
provides a method named "b()" and A does not redefine this method and
"b()" is public, you don't even have to name this method explicitly.
You can just use

tjp->that()->b();

Hope, I could help you.

Ciao, Fabian




More information about the aspectc-user mailing list