[aspectc-user] (no subject)
Olaf Spinczyk
olaf at ivs.cs.uni-magdeburg.de
Thu May 30 16:09:41 CEST 2002
Hi,
On Thursday, 30. May 2002 14:54, you wrote:
> Yes, it helps, but it does not solve everything. I
> would like that the around advice finally returns a
> result.
>
> A code like the following
>
> SomeObject a=FctWhichWillBeArounded();
>
> will work correctly if the around advice returns void
> ?
>
> So, if I understand you correctly, an around advice
> cannot have a return value? It seems to me incorrect,
> even if my advice contains several pointcuts with
> different return values. It's the user bussines to
> assure that the type declared in the advice will
> finally be returned. An example :
>
> pointcut Fct1()= call("Type1 fct1()")
> pointcut Fct2()= call("Type2 fct2()")
>
> pointcut theTwo()=Fct1() || Fct2()
>
> advice theTwo() : Type3* theTwo() {
> proceed();
> if resultType==Type1 return new Type3(1)
> if resultType==Type2 return new Type3(2)
> }
>
> I hope that I am not completely wrong.
>
> Thanks,
> Florin Cremenescu
You are not complete wrong. I remember that we have changed the behavior of
AspectC++ in that particular point at least once.
Remember that C++ is not Java. You have no garbage collector and you cannot
simply return a generic "Object". If a caller of fct1() expects an integer as
a result the around advice is not allowed to give out a float. If the caller
of fct2() expect some other type then the advice must give out exactly that
type. Therefore we can't simply give around advice one return type like you
have suggested.
Nevertheless, AspectC++ offers an interface to access the return value and
check the type at run-time. Here is an example (it is tested!) that shows how
you can do this:
---------------------------------------------------------------------
#include <stdio.h>
pointcut Fct1() = call("int fct1()");
pointcut Fct2() = call("float fct2()");
pointcut theTwo() = Fct1() || Fct2();
aspect Test
{
advice theTwo() : around ()
{
thisJoinPoint->action ().trigger ();
printf ("result_type: %s\n", thisJoinPoint->resulttype ());
void *result = thisJoinPoint->result ();
switch (thisJoinPoint->resulttype ()[0])
{
case 'i': *(int*)result += 1; break;
case 'f': *(float*)result += 0.001; break;
default: printf ("unexpected type\n");
}
}
};
int fct1 ()
{
return 4711;
}
float fct2 ()
{
return 0.815;
}
int main ()
{
printf ("fct1: %d\nfct2: %f\n", fct1(), fct2());
return 0;
}
---------------------------------------------------------------------
Best regards,
Olaf
More information about the aspectc-user
mailing list