[aspectc-user] set and get methods

Daniel Lohmann daniel.lohmann at informatik.uni-erlangen.de
Tue Oct 12 17:51:39 CEST 2004


Hans VB wrote:

> Hello,
>
> I think the possibility to use set() with primitive types (or even 
> only with 'int' or 'char') would allready help a number of people 
> (including me ;-) especially since overloading the = operator doesnt 
> work for primitive types. You wouldn't even have to consider the 
> aliasing problem to make it a useful addition to AC in my opinion.

That's good to know! We are still in the process of discussing if  "get" 
and "set" pointcut functions do make sense in AspectC++ at all - because 
of the aliasing problem. AspectC++ will probably never be able to 
support the AspectJ semantics of "get" and "set", as the aliasing 
problem is just not solvable. However, if something less powerful  is 
already considered being useful, this should be not too difficult to 
implement.

I can imagine, for instance, pointcut functions "lvalue()" and 
"rvalue()" that match all joinpoints, where a  variable *identifier* is 
used as L-value or R-value.


Regarding overloading operator = for primitive types:
This is not possible, of course. However, it is possible to build a 
class type that behaves (syntactically) like a primitive type. A simple 
(not complete) example:

 >>>>>>
struct Int {
    int _val;
    Int( int i = 0 ) : _val( i ){}
    Int& operator =( int i ) {
        _val = i;
        return *this;
    }
    Int& operator +=( int i ) {
        _val += i;
        return *this;
    }
    // same for *=, -* &= and so on
 
    operator const int&() const {
        return _val;
    }
};
<<<<<<

If you now change your e.g. global "int" variables to type "Int" the 
code should still compile, because "Int" offers the same interface as 
the primitive "int" type. Of course, the implementation of "Int" is not 
complete, however, the idea should be clear. It is now possible to match 
read/write operations:

 >>>>>>
#include <stdio.h>
#include "Int.h"

aspect getset {

    pointcut getter() = execution("Int::operator const int&() const");
    pointcut setter() = execution("% Int::operator =(%)" || "% 
Int::operator +=(%)" );

    advice getter() && that(v) : before( Int v) {
        printf("before reading int var: value= %d\n", v._val );
    }

    advice setter() && that(v) : around( Int& v) {
        printf("before modifying int var: old value= %d\n", v._val );
        tjp->proceed();
        printf("after  modifying int var: new value= %d\n", v._val );
    }
};
<<<<<<


Daniel






More information about the aspectc-user mailing list