[aspectc-user] Joinpoint Context information when monitoring class instances

Reinhard Tartler siretart at tauware.de
Tue Sep 26 15:12:11 CEST 2006


Bill Sousan <blsousan at yahoo.com> writes:

> Hi,
>
>     I am trying to make a "canned" aspect for tracking
> the number instances for a given class.  However, I
> would like this canned aspect to work on a group of
> classes thus it needs specific context information per
> aspect instanciation.  Basically just a counter for
> tracking the number of instances.
>
> I copied some code from one of your research papers
> and it seems to work.  My problem is that I also want
> to printout which class it is presently called from.
> (i.e. the joinpoint class)  I used the following code:
>
>
>
> aspect Instances 
> {
>     pointcut virtual pClasses () = 0;
>     advice pClasses () :
>         class Counter 
>     {
>         static int counter;
>         public:
>             Counter () 
>             {
>                 counter++;
> 		// Want to print out which class this joinpoint is
> attached to		
>                 printf( "CONST %s Instance Count:
> %d\n" , JoinPoint::signature( ) , counter ) ;
>             }
>             ~Counter ()
>             {
>                 counter--;
> 		// Want to print out which class this joinpoint is
> attached to	
>                 printf( "DEST %s Instance Count: %d\n"
> , JoinPoint::signature( ) , counter ) ;
>             }
>     } __counter;
> };
>

I'm a bit confused about this advice. You introduce in all pClasses() an
inner class 'Counter'. This inner class doesn't have access to the
joinpoint api. 

I think you'll want to try something like this:
#include <cstdio>

class XXX {};

aspect intro {
  pointcut observed () = "XXX";
  // define a new slice, which introduces the counter
  slice struct counting_slice { public: static int counter; };
  // actually insert the slice
  advice observed() : slice counting_slice;
  
  advice construction( observed() ) : before() {
    int &counter = tjp->target()->counter;
    printf("Creating new class. Instance count is %d\n", ++counter);
  }

  advice destruction( observed() ) : after() {
    int &counter = tjp->target()->counter;
    printf("destroying new class. Instance count is %d\n", --counter);
  }
};

// define the introduces static member
slice int intro::counting_slice::counter = 0;

// testcase
int main(int argc, char **argv) {
  printf("%s starting ...\n", argv[0]);
  XXX a;
  XXX b;
}


This also shows the current slice syntax in combination with introduced
static members. The old syntax for introduction is deprecated now and
will be dropped in some future version of ac++. The current ac++ even
gives a warning for the old one.

I hope this helps you!

-- 
Gruesse/greetings,
Reinhard Tartler, KeyID 945348A4



More information about the aspectc-user mailing list