[aspectc-user] Singleton pattern

Luigi 'Comio' Mantellini luigi.mantellini at gmail.com
Wed Jan 10 02:04:43 CET 2007


Dear All,

my name is Luigi and I'm new in AspectC programming.
I need to create a Singleton Pattern (just an exercise to learn
more)... but I don't
understand some aspectc's mechanism.

The idea is to create a "wrapper" aspect that adds to a matched class a
static pointer to the single instance and a public method that returns
this pointer.

My first problem is how I can obtain the Type of the Class addressed by
the static jointpoint (I tried with "That" type without success).

The second problem is how assign a start value to a static value
introduced by the advice (in my example _instance and _count).

I produced this code:

[code]
aspect AAbstractSingleton {

public:
       pointcut virtual singletonize() = 0;

       advice singletonize():
               public: static That *Instance() {
                       if (!_instance) {
                             _instance = new That; /** First problem:
how obtain the
type of the class addressed by joinpoint */
                       }
                       return _instance;
               }
       advice singletonize():
               protected: static That* _instance; /** First problem:
how obtain the
type of the class addressed by joinpoint */
       advice singletonize():
               private: static int _count; /** Second problem: how
initialize an
introduced static
member */
protected:
       advice construction(singletonize()): before() {
               // Evito l'accesso diretto al costruttore
               if (tjp->that()->_count) {
                       // Costruttore bloccato
                       printf("Impossibile accedere al costruttore\n");
                       exit(1);
               }
               tjp->that()->_count++;
       }

};

aspect AConcrteteSingleton: public AAbstractSingleton {
public:
       pointcut virtual singletonize() = "SampleClass";
};

[/code]

Thanks in advance.

Best Regards,

Luigi Mantellini


-- 
Luigi 'Comio' Mantellini

The three laws of users:

1. A user may not injure a sysadmin, or through inaction,
   allow a sysadmin to come to harm.
2. A user must obey the orders given it by sysadmins except
   where such orders would conflict with the First Law.
3. A user must protect its own existence as long as such
   protection does not conflict with the First or Second Law.



More information about the aspectc-user mailing list