1 /**
2  * This component provider always returns the same instance of the component. The instance
3  * is created when first required and is of the type passed in to the constructor.
4  */
5 module ashd.fsm.componentSingletonProvider;
6 
7 import ashd.core.component        : Component;
8 import ashd.fsm.IComponentProvider: IComponentProvider;
9 
10 
11 public class ComponentSingletonProvider: IComponentProvider
12 {
13     private
14     {
15         ClassInfo mComponentType;
16         Object mInstance;
17     }
18 
19     /**
20      * Constructor
21      * 
22      * @param type The type of the single instance
23      */
24     public this( ClassInfo type_a )
25     {
26         mComponentType = type_a;
27     }
28     
29     /**
30      * Used to request a component from this provider
31      * 
32      * @return The single instance
33      */
34     protected Object createDynamicInstance( ClassInfo type_a )
35     { 
36         if( !mInstance )
37         {
38             mInstance = type_a.create();
39         }
40         return mInstance; 
41     }
42     
43     /**
44      * Used to compare this provider with others. Any provider that returns the same single
45      * instance will be regarded as equivalent.
46      * 
47      * @return The single instance
48      */
49     public hash_t identifier()
50     {
51         if (!mInstance)
52         {
53             mInstance = mComponentType.create();
54         }
55         return mInstance.toHash();
56     }
57 }