|
1 /* |
|
2 * Copyright (c) 2006-2007 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: ?Description |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 #ifndef __HUIOWNEDPOINTER_H__ |
|
21 #define __HUIOWNEDPOINTER_H__ |
|
22 |
|
23 |
|
24 /* Types. */ |
|
25 |
|
26 /** Ownership options. */ |
|
27 enum THuiOwnership |
|
28 { |
|
29 EHuiDoesNotHaveOwnership, |
|
30 EHuiHasOwnership |
|
31 }; |
|
32 |
|
33 |
|
34 /** |
|
35 * Pointer that optionally has ownership. |
|
36 */ |
|
37 template< class T > |
|
38 class RHuiOwnedPointer |
|
39 { |
|
40 public: |
|
41 |
|
42 /* Constructors and destructor. */ |
|
43 |
|
44 /** |
|
45 * Default constructor. |
|
46 */ |
|
47 RHuiOwnedPointer() |
|
48 : iOwnership(EHuiDoesNotHaveOwnership), iPointer(0) |
|
49 { |
|
50 } |
|
51 |
|
52 RHuiOwnedPointer(T* aPointer, THuiOwnership aOwnership) |
|
53 : iOwnership(aOwnership), iPointer(aPointer) |
|
54 { |
|
55 } |
|
56 |
|
57 /** |
|
58 * Destructor. The object is deleted if the pointer has ownership. |
|
59 */ |
|
60 virtual ~RHuiOwnedPointer() |
|
61 { |
|
62 Close(); |
|
63 } |
|
64 |
|
65 |
|
66 /* Methods. */ |
|
67 |
|
68 /** |
|
69 * Reset the pointer. |
|
70 */ |
|
71 inline void Close() |
|
72 { |
|
73 if(iOwnership == EHuiHasOwnership) |
|
74 { |
|
75 delete iPointer; |
|
76 } |
|
77 iPointer = 0; |
|
78 iOwnership = EHuiDoesNotHaveOwnership; |
|
79 } |
|
80 |
|
81 inline operator T* () |
|
82 { |
|
83 return iPointer; |
|
84 } |
|
85 |
|
86 inline operator const T* () const |
|
87 { |
|
88 return iPointer; |
|
89 } |
|
90 |
|
91 inline T* operator -> () |
|
92 { |
|
93 return iPointer; |
|
94 } |
|
95 |
|
96 inline const T* operator -> () const |
|
97 { |
|
98 return iPointer; |
|
99 } |
|
100 |
|
101 inline T* Ptr() |
|
102 { |
|
103 return iPointer; |
|
104 } |
|
105 |
|
106 inline const T* Ptr() const |
|
107 { |
|
108 return iPointer; |
|
109 } |
|
110 |
|
111 inline T& Ref() |
|
112 { |
|
113 return *iPointer; |
|
114 } |
|
115 |
|
116 inline const T& Ref() const |
|
117 { |
|
118 return *iPointer; |
|
119 } |
|
120 |
|
121 inline T& NonConstRef() const |
|
122 { |
|
123 return *iPointer; |
|
124 } |
|
125 |
|
126 inline void Set(T* aPointer, THuiOwnership aOwnership) |
|
127 { |
|
128 // Delete the previous object. |
|
129 if(iOwnership == EHuiHasOwnership) |
|
130 { |
|
131 delete iPointer; |
|
132 iPointer = 0; |
|
133 } |
|
134 |
|
135 iOwnership = aOwnership; |
|
136 iPointer = aPointer; |
|
137 } |
|
138 |
|
139 inline THuiOwnership Ownership() const |
|
140 { |
|
141 return iOwnership; |
|
142 } |
|
143 |
|
144 |
|
145 private: |
|
146 |
|
147 /** |
|
148 * Assignment prohibited. This isn't defined anywhere. |
|
149 */ |
|
150 RHuiOwnedPointer& operator = (const RHuiOwnedPointer& aOwnedPointer); |
|
151 |
|
152 |
|
153 private: |
|
154 |
|
155 /** Have ownership of the object being pointed to. If the owned pointer |
|
156 is deleted, the object will be deleted as well. */ |
|
157 THuiOwnership iOwnership; |
|
158 |
|
159 T* iPointer; |
|
160 |
|
161 }; |
|
162 |
|
163 |
|
164 #endif // __HUIOWNERSHIPPOINTER_H__ |