[aspectc-user] Around advice and return values
Olaf Spinczyk
Olaf.Spinczyk at informatik.uni-erlangen.de
Thu May 12 11:00:57 CEST 2005
Hello,
Loesch Felix (CR/AEF4) wrote:
> Hello,
>
> I tried to use around advice with return values with AspectC++, but I
> was unable to use return values inside
> advice code.
>
> I tried the following example code but always got the following error of
> the AspectC++ compiler:
>
> --- error begin ---
>
> ../MinValue.ah: In member function `void MinValue::__a0_around(int, int,
> int)':
> ../MinValue.ah:9: error: return-statement with a value, in function
> declared
>
> --- error end ----
>
>
> ---- begin main.cpp ----
> #include <stdio.h>
> using namespace std;
>
> // compute max value of i, j, k
> int REF(int i, int j, int k) {
> int max = 0;
> max = i;
> if (j > max) { max = j; }
> if (k > max) { max = k; }
> return max;
> }
>
> int main() {
> printf("Reference value: %d", REF(3,5,4));
> }
> --- end main.cpp---
>
> MinValue.ah
>
> --- begin MinValue.ah ----
> #include <stdio.h>
>
> #include <stdio.h>
>
> aspect MinValue {
> advice call("int REF(...)") && args(i,j,k) : int around(int
> i,int j,int k) {
> int min = 0;
> min = i;
> if (j < min) { min = j; }
> if (k < min) { min = k; }
> return min;
> }
> };
>
> --- end MinValue.ah ---
>
> - Why does AspectC++ declare the inlined function as "void"?
> - Why does AspectC++ not support return types of functions that are
> replaced by "around" advice?
> - Is there a workaround to support return statements in advice code?
>
>
> --
> Felix Loesch
AspectC++ supports the manipulation of the result value like this:
aspect MinValue {
advice call("int REF(...)") && args(i,j,k) :
around(int i,int j,int k) { // don't write *int* around here!
int min = 0;
min = i;
if (j < min) { min = j; }
if (k < min) { min = k; }
*tjp->result () = min;
// no return statement!
}
};
Two additional remarks:
* an aspect that transforms a max function into a min function is
critical, because it dramatically changes the semantics of the
component code. It is not an example for best practices ;-).
* the advice code is normally inlined. Therefore, larger advice bodies
should better be moved into a separate function, which is not inlined
and called be the advice:
aspect MinValue {
int min (int i, int j, int k); // defined somewhere else
advice call("int REF(...)") && args(i,j,k) :
around(int i,int j,int k) { // don't write *int* around here!
*tjp->result () = min (i, j, k);
// no return statement!
}
};
Best regards,
Olaf
More information about the aspectc-user
mailing list