|
1 /* |
|
2 * Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: TLS store for singleton objects |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 #ifndef __C_GLXSINGLETONSTORE_H__ |
|
22 #define __C_GLXSINGLETONSTORE_H__ |
|
23 |
|
24 #include <e32base.h> |
|
25 |
|
26 /** |
|
27 * CGlxSingletonStore |
|
28 * |
|
29 * Store for singleton objects |
|
30 * |
|
31 * Usage: |
|
32 * |
|
33 * Singleton class must be derived from CBase (either |
|
34 * directly or via base class). Otherwise, singleton's destructor |
|
35 * is not called correctly. |
|
36 * |
|
37 * class CMyClass : public CBase ... |
|
38 * { |
|
39 * public: |
|
40 * static CMyClass* InstanceL() |
|
41 * { |
|
42 * // Pass in the factory function |
|
43 * return CGlxSingletonStore::InstanceL(&NewL); |
|
44 * } |
|
45 * |
|
46 * void Close() |
|
47 * { |
|
48 * CGlxSingletonStore::Close(this); |
|
49 * } |
|
50 * |
|
51 * private: |
|
52 * static CMyClass* NewL() |
|
53 * { |
|
54 * ... |
|
55 * } |
|
56 * }; |
|
57 * |
|
58 */ |
|
59 NONSHARABLE_CLASS(CGlxSingletonStore) : public CBase |
|
60 { |
|
61 public: |
|
62 /** |
|
63 * Return a singleton instance and add to reference count |
|
64 */ |
|
65 template <class T> |
|
66 static T* InstanceL(T* (*aFactoryFuncL)()); |
|
67 |
|
68 /** |
|
69 * Decrement reference count of singleton instance |
|
70 * and delete if last reference removed |
|
71 */ |
|
72 IMPORT_C static void Close(CBase* aInstance); |
|
73 |
|
74 /** Destructor */ |
|
75 IMPORT_C ~CGlxSingletonStore(); |
|
76 |
|
77 private: |
|
78 /** Constructor */ |
|
79 CGlxSingletonStore(); |
|
80 |
|
81 /** |
|
82 * Two-phase constructor |
|
83 * Creates an instance if one does not already exist |
|
84 * Exported for accessing from inline function |
|
85 */ |
|
86 IMPORT_C static CGlxSingletonStore* InstanceLC(); |
|
87 |
|
88 /** Cleanup function for Cleanup Stack */ |
|
89 static void Cleanup(TAny* aSingletonStore); |
|
90 |
|
91 private: |
|
92 /** Struct to store a singleton an its ref count */ |
|
93 class TSingleton |
|
94 { |
|
95 public: |
|
96 CBase* iObject; |
|
97 TInt iReferenceCount; |
|
98 }; |
|
99 |
|
100 /** Array of singletons */ |
|
101 RArray<TSingleton> iSingletons; |
|
102 }; |
|
103 |
|
104 #include "glxsingletonstore.inl" |
|
105 |
|
106 #endif // __C_GLXSINGLETONSTORE_H__ |