|
1 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 |
|
17 |
|
18 // INCLUDE FILES |
|
19 #include "epos_cpossingletonmanager.h" |
|
20 #include "epos_defaultproxycommon.h" |
|
21 |
|
22 // ================= MEMBER FUNCTIONS ======================= |
|
23 |
|
24 // C++ default constructor can NOT contain any code, that |
|
25 // might leave. |
|
26 // |
|
27 CPosSingletonManager::CPosSingletonManager() |
|
28 { |
|
29 } |
|
30 |
|
31 // --------------------------------------------------------- |
|
32 // CPosSingletonManager::GetInstanceL |
|
33 // --------------------------------------------------------- |
|
34 // |
|
35 CPosSingletonManager* CPosSingletonManager::GetInstanceL() |
|
36 { |
|
37 CPosSingletonManager* self = reinterpret_cast< CPosSingletonManager* > ( |
|
38 Dll::Tls() ); |
|
39 |
|
40 if ( !self ) |
|
41 { |
|
42 self = new ( ELeave ) CPosSingletonManager; |
|
43 Dll::SetTls( self ); |
|
44 } |
|
45 |
|
46 return self; |
|
47 } |
|
48 |
|
49 // --------------------------------------------------------- |
|
50 // CPosSingletonManager::GetInstanceL |
|
51 // --------------------------------------------------------- |
|
52 // |
|
53 CPosSingletonManager* CPosSingletonManager::GetInstance() |
|
54 { |
|
55 CPosSingletonManager* self = reinterpret_cast< CPosSingletonManager* > ( |
|
56 Dll::Tls() ); |
|
57 |
|
58 return self; |
|
59 } |
|
60 |
|
61 // Destructor |
|
62 CPosSingletonManager::~CPosSingletonManager() |
|
63 { |
|
64 TRACESTRING( "CPosSingletonManager::destructor" ) |
|
65 iObjectsArray.Close(); |
|
66 Dll::SetTls( NULL ); |
|
67 } |
|
68 |
|
69 // --------------------------------------------------------- |
|
70 // CPosSingletonManager::GetObject |
|
71 // --------------------------------------------------------- |
|
72 // |
|
73 CBase* CPosSingletonManager::GetObject( TInt aObjectId ) |
|
74 { |
|
75 CPosSingletonManager* self = GetInstance(); |
|
76 if ( self ) |
|
77 { |
|
78 return self->GetAObject( aObjectId ); |
|
79 } |
|
80 return NULL; |
|
81 } |
|
82 |
|
83 // --------------------------------------------------------- |
|
84 // CPosSingletonManager::GetAObject |
|
85 // --------------------------------------------------------- |
|
86 // |
|
87 CBase* CPosSingletonManager::GetAObject( TInt aObjectId ) |
|
88 { |
|
89 TInt count = iObjectsArray.Count(); |
|
90 for( TInt i = 0; i < count; i++ ) |
|
91 { |
|
92 if( iObjectsArray[i].iObjectId == aObjectId ) |
|
93 { |
|
94 return iObjectsArray[i].iObject; |
|
95 } |
|
96 } |
|
97 return NULL; |
|
98 } |
|
99 |
|
100 |
|
101 // --------------------------------------------------------- |
|
102 // CPosSingletonManager::SetObjectL |
|
103 // --------------------------------------------------------- |
|
104 // |
|
105 void CPosSingletonManager::SetObjectL( |
|
106 CBase* aObject, |
|
107 TInt aObjectId ) |
|
108 { |
|
109 if( aObject ) |
|
110 { |
|
111 CPosSingletonManager* self = GetInstanceL(); |
|
112 self->SetAObjectL( aObject, aObjectId ); |
|
113 } |
|
114 else |
|
115 { |
|
116 CPosSingletonManager* self = GetInstance(); |
|
117 if ( self ) |
|
118 { |
|
119 self->SetAObjectL( aObject, aObjectId ); |
|
120 } |
|
121 } |
|
122 } |
|
123 |
|
124 // --------------------------------------------------------- |
|
125 // CPosSingletonManager::SetAObjectL |
|
126 // --------------------------------------------------------- |
|
127 // |
|
128 void CPosSingletonManager::ReleaseObject( |
|
129 TInt aObjectId ) |
|
130 { |
|
131 CPosSingletonManager* self = GetInstance(); |
|
132 if ( self ) |
|
133 { |
|
134 self->ReleaseAObject( aObjectId ); |
|
135 } |
|
136 } |
|
137 |
|
138 // --------------------------------------------------------- |
|
139 // CPosSingletonManager::SetAObjectL |
|
140 // --------------------------------------------------------- |
|
141 // |
|
142 void CPosSingletonManager::SetAObjectL( |
|
143 CBase* aObject, |
|
144 TInt aObjectId ) |
|
145 { |
|
146 //Check if the object is already in the array. If |
|
147 //yes, then delete the old object and store the new one. |
|
148 TInt count = iObjectsArray.Count(); |
|
149 for( TInt i = 0; i < count; i++ ) |
|
150 { |
|
151 if( iObjectsArray[i].iObjectId == aObjectId ) |
|
152 { |
|
153 delete iObjectsArray[i].iObject; |
|
154 iObjectsArray[i].iObject = aObject; |
|
155 ClearIfNeeded(); |
|
156 return; |
|
157 } |
|
158 } |
|
159 |
|
160 //If the object has not be stored, then add this object |
|
161 //to the array. |
|
162 if( aObject ) |
|
163 { |
|
164 TSingletonOb ob; |
|
165 ob.iObject = aObject; |
|
166 ob.iObjectId = aObjectId; |
|
167 |
|
168 User::LeaveIfError( iObjectsArray.Append( ob ) ); |
|
169 } |
|
170 } |
|
171 |
|
172 // --------------------------------------------------------- |
|
173 // CPosSingletonManager::ReleaseAObject |
|
174 // --------------------------------------------------------- |
|
175 // |
|
176 void CPosSingletonManager::ReleaseAObject( TInt aObjectId ) |
|
177 { |
|
178 TInt count = iObjectsArray.Count(); |
|
179 for( TInt i = 0; i < count; i++ ) |
|
180 { |
|
181 if( iObjectsArray[i].iObjectId == aObjectId ) |
|
182 { |
|
183 delete iObjectsArray[i].iObject; |
|
184 iObjectsArray[i].iObject = NULL; |
|
185 ClearIfNeeded(); |
|
186 return; |
|
187 } |
|
188 } |
|
189 |
|
190 } |
|
191 |
|
192 // --------------------------------------------------------- |
|
193 // CPosSingletonManager::ClearIfNeeded |
|
194 // --------------------------------------------------------- |
|
195 // |
|
196 void CPosSingletonManager::ClearIfNeeded() |
|
197 { |
|
198 //Check if there is still singleton objects stored. |
|
199 TInt count = iObjectsArray.Count(); |
|
200 for( TInt i=0; i < count; i++ ) |
|
201 { |
|
202 TSingletonOb& ob = iObjectsArray[i]; |
|
203 if( ob.iObject ) |
|
204 { |
|
205 //There is still singleton objects. We shall not |
|
206 //clear this object |
|
207 return; |
|
208 } |
|
209 } |
|
210 |
|
211 //No singleton objects is stored, then delete this object |
|
212 delete this; |
|
213 } |
|
214 |
|
215 |
|
216 // End of File |