[aspectc-user] syntax question

Olaf Spinczyk olaf at ivs.cs.uni-magdeburg.de
Tue Feb 12 17:16:57 CET 2002

On Tuesday, 12. February 2002 12:57, Law, Colin wrote:
> Excellent, that works well (or should I say that(aptr) works well).
> I am having a little trouble getting the hang of the syntax, in the line
> advice that(aptr) && execution ("void %::a()"): void before(A *aptr)
> presumably 'that()' is getting its parameter from 'before(A* aptr)', which
> does not seem intuitive.

You can read a pointcut as a function saying "yes" or "no" to every join 
point. The difference to a function is that the (optional) formal arguments 
are not used as parameters, but to expose context information from the join 
point. Perhaps it is more clear if you use a separate pointcut definition as 
you have done below.

> I attempted to get the same effect using a pointcut declaration as follows,
> using my class types
> pointcut ini() = that( "CWnd" ) && execution("int %::OnInitDialog()");
> advice ini() : void before( CWnd* pWnd ) {...}
> I did not have all that much hope that I had got it right, but it compiled
> in ac++ but generated uncompilable code, with
>    TestAspect::aspectOf ()->TestAspect::__advice_0 ();
> appearing in the new OnInitDialog(), but
> void __advice_0( CWnd* pWnd )
> appearing in the aspect declaration.

If you use arguments in before, after, or around advice they must be bound to 
arguments of the pointcut. In this case here "pWnd" is not bound. AspectC++ 
should give you an error message instead of wrong code. We will put this on 
the TODO list. If you omit the argument declaration ('void before ()') 
everything must work fine.

> On the requirement for the helper function access_A, I tried it without the
> helper function, directly accessing the pointer in before() and it was OK.
> Can you explain this issue in more detail?

Ok, look at the code...
class A;

aspect O
   void access_A(A* aptr);

   advice that(aptr) && execution ("void %::a()") :
      void before (A *aptr)
      { access_A(aptr);

class A
      int i;
      A() { i = 4711; }
      virtual void a () {}

// B and C as show above

void O::access_A (A *aptr)
 { cout << "before a(), i is " << aptr->i << endl;

// main as show above
Without access_A we would try to use aptr->i in the advice, before class A is 
defined completely. This wouldn't compile. Alternatively we could define A 
before the aspect but then the generated code in A::a() would try to call the 
advice, which is not defined yet. This wouldn't compile as well. It is a 
cyclic access relation. I don't know why there is no problem in your case. Do 
you access an attribute or function of a base class of "class A"?

> Thanks for all the help, I hope you are finding my input useful also.

Yes, it is!


More information about the aspectc-user mailing list