[aspectc-user] target() and derived classes
Olaf Spinczyk
Olaf.Spinczyk at informatik.uni-erlangen.de
Thu Dec 4 10:06:47 CET 2003
Hello Michelangelo,
riccobene-zzzz0001 at mailblocks.com wrote:
> Hello,
> I am using ac++ version 0.7.2 on Win32 with bcc32 compiler.
>
> I am trying to write an aspect to add thread safety to a class named
> MessageSink. In my source code i need to instantiate thread safe version
> of MessageSink together with non-thread safe (original) version.
>
> I wrote a simple thread safety aspect that adds a Mutex object to the
> class to protect, and calls a Mutex::acquire() before the invocation of
> any method of that class.
>
> If I apply this aspect to MessageSink than all instances will be thread
> safe. But i want thread safe and non-thread safe instances.
> So I created an empty class, ThreadSafeSink, derived from MessageSink
> and I applied the aspect to this class.
>
> In my code if I want a thread safe instance of MessageSink I make a
> ThreadSafeSink object.
>
> BUT the code doesn't work: if i call a MessageSink's method on a
> ThreadSafeSink instance the Mutex object will non be found!!
> In the generated code I see that the type of "Target" member in joint
> point struct is MessageSink rather than ThreadSafeSink. BUT the target
> object is a ThreadSafeSink instance!
>
> Can we help me? (see the code below)
>
> thanks,
> michelangelo
Two weaknesses are responsible for this problem:
1. Weak implementation of the that/target feature in ac++ 0.7.2:
We have to admit that there are only very few cases where that or target
work as expected in ac++ 0.7.2 :-(. Therefore, this point was one of the
most important goals for our 0.8 version, which is nearly finished now.
I tried your code with the current CVS version of ac++ and the code is
woven as expected. However, there is an AspectC++ language weakness that
still prohibits your example to work (see 2.).
2. JoinPoint::target() is a pointer to the referenced class (not the
real dynamic object type) at call join points.
This problem means that you would either have to cast tjp->target () to
its real type or access the object as a context variable like this:
advice target(t) : around (ThreadSafeSink *t) { t->mutex_... }
However in both cases you need the type name "ThreadSafeSink" in the
aspect implementation. This conflicts with the idea that the
synchronization aspect should be reusable (an abstract aspect).
Summary:
If you want to use conditional synchronization by runtime checks of the
object type and not, for instance, based on lexical scopes and execution
join points I would wait for ac++ 0.8pre1. I'm very optimistic that this
version will be published within the next two weeks.
The language problem is something that needs some discussion and more
time. It's not easy to add the real instance type to the list of
typedefs in the generated JoinPoint class, because the real instance
type might be unknown at this point and we also would have to generate
JoinPoint classes for all possible cases. Until we have a solution you
can only try workarounds.
Yours,
Olaf
More information about the aspectc-user
mailing list