[aspectc-user] Can you attach an aspect to the main() function?

Olaf Spinczyk Olaf.Spinczyk at informatik.uni-erlangen.de
Fri Sep 15 09:18:31 CEST 2006


Hi Bill,

Bill Sousan wrote:
> 1. Can you attach an aspect to the main() function ? 
> Or have a class constructed right after main starts? 
> In other words, I need to "weave" in some
> initialization code to start inside main() but before
> any functions or methods are called.  My other aspects
> depend on this initialization.

yes, you can define before execution advice for main, e.g.:

pointcut main_exec() = execution ("int main(...)");
aspect MySubsystemInitializer {
   advice main_exec() : before () {
     mySubsystem.init ();
   }
};

This is a very nice idiom that we frequently use to initialize 
subsystems of complex software. By using order advice you can even 
define rules for the advice execution in case that more than one aspect 
is bound to this join point.

aspect GlobalInitializationOrder {
   // MySubsystemInitializer is always the first at main_exec
   advice main_exec() :
     order ("MySubsystemInitializer", !"MySubsystemInitializer");
};

Note that there is one small problem: Your main function *has to* 
contain a return statement, although the C++ standard says that in the 
case of 'int main(...)' a return statement is not required.

Also keep in mind that the constructors of global objects will be 
executed before your advice.

> 2. Will AspectC++ allow you to attach to an array
> index?  That is can you attach an aspect to something
> like:
> 
> value = array[x];

No, not yet. there is no pointcut function that would capture this kind 
of kind join point unless array is an instance of a class with an array 
index operator. In this case you could define call or execution advice 
for the operator function.

Best regards,

Olaf




More information about the aspectc-user mailing list