|
1 /* |
|
2 * Copyright (c) 2000-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: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "UniqueInstanceImpl.h" |
|
20 #include "AssertFileAndLine.h" |
|
21 |
|
22 using namespace UniqueInstance; |
|
23 |
|
24 #ifdef _DEBUG |
|
25 #define USE_INTEGRITY_CHECK 1 |
|
26 #else |
|
27 #define USE_INTEGRITY_CHECK 0 |
|
28 #endif |
|
29 |
|
30 // CUniqueInstanceRepositoryBase : user version in front of compiler firewall |
|
31 void CUniqueInstanceRepositoryBase::ConstructL(TCompareFn* aCompare, |
|
32 TDeleteFn* aDelete, TCopyFnL* aCopyL, |
|
33 TInt aMaxLinks, TInt aObjectSize) |
|
34 { |
|
35 CRepositoryImpl* p = new(ELeave) CRepositoryImpl(aCompare, |
|
36 aDelete, aCopyL, aObjectSize); |
|
37 CleanupStack::PushL(p); |
|
38 p->ConstructL(aMaxLinks); |
|
39 CleanupStack::Pop(p); |
|
40 delete iImpl; |
|
41 iImpl = p; |
|
42 } |
|
43 |
|
44 CUniqueInstanceRepositoryBase::~CUniqueInstanceRepositoryBase() |
|
45 { |
|
46 delete iImpl; |
|
47 } |
|
48 |
|
49 void CUniqueInstanceRepositoryBase::Test() const |
|
50 { |
|
51 iImpl->Test(); |
|
52 } |
|
53 |
|
54 // RInstanceImpl : manager for a unique instance |
|
55 RInstanceImpl::RInstanceImpl(CUniqueInstanceRepositoryBase& aRepository) |
|
56 : iRepository(aRepository.iImpl) |
|
57 { |
|
58 iPtr = iRepository->NullElement(); |
|
59 } |
|
60 |
|
61 RInstanceImpl::~RInstanceImpl() |
|
62 { |
|
63 ASSERT(iRepository->IsNull(iPtr)); |
|
64 } |
|
65 |
|
66 void RInstanceImpl::TakeL(void* a) |
|
67 { |
|
68 ASSERT(a); |
|
69 #if USE_INTEGRITY_CHECK |
|
70 iRepository->Test(); |
|
71 #endif |
|
72 UniqueInstance::SElement* r = iRepository->InsertOrIncL(a); |
|
73 iRepository->DeleteOrDec(iPtr); |
|
74 iPtr = r; |
|
75 #if USE_INTEGRITY_CHECK |
|
76 iRepository->Test(); |
|
77 #endif |
|
78 } |
|
79 |
|
80 void RInstanceImpl::TakeCopyL(void* a) |
|
81 { |
|
82 ASSERT(a); |
|
83 #if USE_INTEGRITY_CHECK |
|
84 iRepository->Test(); |
|
85 #endif |
|
86 UniqueInstance::SElement* r = iRepository->IncOrCopyL(a); |
|
87 iRepository->DeleteOrDec(iPtr); |
|
88 iPtr = r; |
|
89 #if USE_INTEGRITY_CHECK |
|
90 iRepository->Test(); |
|
91 #endif |
|
92 } |
|
93 |
|
94 void* RInstanceImpl::Peek() const |
|
95 { |
|
96 #if USE_INTEGRITY_CHECK |
|
97 iRepository->Test(); |
|
98 #endif |
|
99 return iPtr->iObject; |
|
100 } |
|
101 |
|
102 void* RInstanceImpl::DropL() |
|
103 { |
|
104 #if USE_INTEGRITY_CHECK |
|
105 iRepository->Test(); |
|
106 #endif |
|
107 void* object = iRepository->DetatchOrCopyL(iPtr); |
|
108 iPtr = iRepository->NullElement(); |
|
109 #if USE_INTEGRITY_CHECK |
|
110 iRepository->Test(); |
|
111 #endif |
|
112 return object; |
|
113 } |
|
114 |
|
115 void RInstanceImpl::Close() |
|
116 { |
|
117 #if USE_INTEGRITY_CHECK |
|
118 iRepository->Test(); |
|
119 #endif |
|
120 iRepository->DeleteOrDec(iPtr); |
|
121 iPtr = iRepository->NullElement(); |
|
122 #if USE_INTEGRITY_CHECK |
|
123 iRepository->Test(); |
|
124 #endif |
|
125 } |
|
126 |
|
127 void RInstanceImpl::CopyTo(RInstanceImpl& aOther) const |
|
128 { |
|
129 #if USE_INTEGRITY_CHECK |
|
130 iRepository->Test(); |
|
131 CRepositoryImpl* otherRep = aOther.iRepository; |
|
132 otherRep->Test(); |
|
133 #endif |
|
134 ++(iPtr->iRefCount); |
|
135 SElement* e = iPtr; |
|
136 aOther.Close(); |
|
137 aOther.iRepository = iRepository; |
|
138 aOther.iPtr = e; |
|
139 #if USE_INTEGRITY_CHECK |
|
140 iRepository->Test(); |
|
141 otherRep->Test(); |
|
142 #endif |
|
143 } |
|
144 |
|
145 void RInstanceImpl::MoveTo(RInstanceImpl& aOther) |
|
146 { |
|
147 #if USE_INTEGRITY_CHECK |
|
148 iRepository->Test(); |
|
149 CRepositoryImpl* otherRep = aOther.iRepository; |
|
150 otherRep->Test(); |
|
151 #endif |
|
152 if (&aOther != this) |
|
153 { |
|
154 aOther.Close(); |
|
155 aOther.iRepository = iRepository; |
|
156 aOther.iPtr = iPtr; |
|
157 iPtr = iRepository->NullElement(); |
|
158 } |
|
159 #if USE_INTEGRITY_CHECK |
|
160 iRepository->Test(); |
|
161 otherRep->Test(); |
|
162 #endif |
|
163 } |
|
164 |
|
165 // standard copy and delete functions |
|
166 void* CUniqueInstanceRepositoryBase::DumbCopyL(void* aBuf, TInt aSize) |
|
167 { |
|
168 void* newBuf = User::AllocL(aSize); |
|
169 Mem::Copy(newBuf, aBuf, aSize); |
|
170 return newBuf; |
|
171 } |
|
172 |
|
173 void CUniqueInstanceRepositoryBase::DumbDelete(void* aBuf) |
|
174 { |
|
175 User::Free(aBuf); |
|
176 } |
|
177 |
|
178 // CUniqueInstanceRepository<TDes> |
|
179 void* CUniqueInstanceRepository<TDes>::DesCopyL(void* a, TInt) |
|
180 { |
|
181 TDesC* des = reinterpret_cast<TDesC*>(a); |
|
182 return des->AllocL(); |
|
183 } |
|
184 |
|
185 TInt CUniqueInstanceRepository<TDes>::DesCompare(void* aL, void* aR) |
|
186 { |
|
187 TDesC* lh = reinterpret_cast<TDesC*>(aL); |
|
188 TDesC* rh = reinterpret_cast<TDesC*>(aR); |
|
189 return lh->Compare(*rh); |
|
190 } |
|
191 |
|
192 void CUniqueInstanceRepository<TDes>::DesDelete(void* a) |
|
193 { |
|
194 operator delete(a); |
|
195 } |
|
196 |