[aspectc-user] Singleton pattern

Daniel Lohmann daniel.lohmann at informatik.uni-erlangen.de
Wed Jan 10 09:42:24 CET 2007


 > Dear All,
 >
 > my name is Luigi and I'm new in AspectC programming.

Hi Luigi,

Welcome in the AspectC++ community :-) (Please note the "++" as there are 
several projects called "AspectC", which frequently leads to some confusion.)

Here is an example that should answer your questions:

[code]
aspect AbstractSingleton {
	pointcut virtual singleton() = 0;

         /* Question 1: Elements to introduced are given
            as a named "slice". Slices are merged into
            the target class the introduction is given for,
            the name of the slice can be used as a synonym
            for the affected class (your "That") */
	advice singleton() : slice struct Inst {
		static Inst inst_;	   // the instance
		static Inst& getInst() {   // the instance getter
			return inst_;
		}
	};
};

/* Question 2: Non-inline definition of introduced elements.
    They have to be defined as being elements of the
    slice "Inst", which is in our case an inner slice of the aspect
    "Abstract Singleton". Except the slice keyword this is exactly
    the same syntax as for symbol definitions in ordinary C++ */

slice AbstractSingleton::Inst AbstractSingleton::Inst::inst_;

aspect ConcreteSingleton : public AbstractSingleton {
	pointcut singleton() = "SingletonTester";
};


/* Test code */
class SingletonTester {
   public:
   void foo(){};
};

int main() {
	SingletonTester& st = SingletonTester::getInst();
	st.foo();
}
[/code]

Hope that helps

Daniel

Luigi 'Comio' Mantellini schrieb:
> 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
> 
> 



More information about the aspectc-user mailing list