[aspectc-user] target() and derived classes

riccobene-zzzz0001 at mailblocks.com riccobene-zzzz0001 at mailblocks.com
Wed Dec 3 14:40:31 CET 2003


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


SOURCE CODE: 
------------------------------------------------------------------------------------------------------------------------------------

// ANSI C++ include
#include <stdio.h>


// CLASSes 
*******************************************************************

/*********
    Mutex
*/
class Mutex {
public:
    void acquire() {
        printf("*** Mutex::acquire\n");
    }
    void release() {
        printf("*** Mutex::release\n");
    }
private:
    // ...
};


/*********
    Message
*/
class Message {
public:
    void process() {};
};


/*********
    MessageSink
*/
class MessageSink {
public:
    MessageSink() {};

    virtual void push(Message*) {
        printf("MessageSink::push...\n");
    };

    virtual void process(Message*) {
        printf("MessageSink::process...\n");
    };
};


/*********
    ThreadSafeSink
*/
class ThreadSafeSink: public MessageSink {
public:
    ThreadSafeSink() {};

};


// ASPECTs 
*******************************************************************

/*********
    ThreadSafety
*/
aspect ThreadSafety {
    pointcut virtual class_to_protect() = 0;

    advice class_to_protect(): Mutex mutex_;

    advice target(class_to_protect()): around() {
        tjp->target()->mutex_.acquire();
        thisJoinPoint->proceed();
        tjp->target()->mutex_.release();
    }
};

/*********
    ThreadSafety for MessageSink
*/
aspect MessageSink_ThreadSafety: public ThreadSafety {
    pointcut class_to_protect() = "ThreadSafeSink";
};

/* NOTE: if we apply ThreadSafety aspect to MessageSink class than all 
instances of MessageSink will be thread safe.
    Instead we apply ThreadSafety to ThreadSafeSink, an empty class 
derived from MessageSink so we can choose to
   make a MessageSink or a ThreadSafeSink.

*/

// APPLICATION 
***************************************************************

int main()
{
	ThreadSafeSink ms;               // this make explicitly a thread safe 
MessageSink.

	ms.push(new Message());          // we expect that mutex_.acquire() 
will be called on ThreadSafeSink

}








More information about the aspectc-user mailing list