|
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: Smart pointer |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 #ifndef T_GLXPOINTER_H |
|
22 #define T_GLXPOINTER_H |
|
23 |
|
24 #include "glxpanic.h" |
|
25 |
|
26 /** |
|
27 * TGlxPointer |
|
28 * |
|
29 * Smart pointer class. Template that can house any object |
|
30 * |
|
31 * @lib None |
|
32 */ |
|
33 template <class T> |
|
34 class TGlxPointer { |
|
35 private: |
|
36 T* pointer; |
|
37 const void* iOwns; // Defined as void* instead of TBool so that can use <pointer> directly as boolean value |
|
38 |
|
39 public: |
|
40 /** |
|
41 * Default constructor |
|
42 */ |
|
43 TGlxPointer() { |
|
44 pointer = NULL; |
|
45 iOwns = 0; |
|
46 } |
|
47 |
|
48 /** |
|
49 * Constructor T* |
|
50 */ |
|
51 explicit TGlxPointer(T* pointer) { |
|
52 this->pointer = pointer; |
|
53 iOwns = pointer; |
|
54 } |
|
55 |
|
56 /** |
|
57 * Constructor |
|
58 */ |
|
59 TGlxPointer(const TGlxPointer<T>& ptrObj) { // Should not be const, but without this, ARM compiler refuses |
|
60 iOwns = ptrObj.iOwns; |
|
61 pointer = const_cast<TGlxPointer<T>&>(ptrObj).Release(); |
|
62 } |
|
63 |
|
64 /** |
|
65 * Destructor |
|
66 */ |
|
67 ~TGlxPointer() { |
|
68 if (iOwns) { |
|
69 __ASSERT_DEBUG(pointer != NULL, Panic(EGlxPanicIllegalState)); |
|
70 delete pointer; |
|
71 } |
|
72 } |
|
73 |
|
74 /** |
|
75 * Operator = |
|
76 */ |
|
77 TGlxPointer<T>& operator=(TGlxPointer<T>& ptrObj) { |
|
78 if (this != &ptrObj) { |
|
79 if (iOwns && pointer != ptrObj.pointer) { |
|
80 delete this->pointer; |
|
81 } |
|
82 iOwns = ptrObj.iOwns; |
|
83 pointer = ptrObj.Release(); |
|
84 } |
|
85 return *this; |
|
86 } |
|
87 |
|
88 /** |
|
89 * Operator = |
|
90 */ |
|
91 TGlxPointer<T>& operator=(T* pointer) { |
|
92 if (iOwns && pointer != this->pointer) { |
|
93 delete this->pointer; |
|
94 } |
|
95 iOwns = pointer; |
|
96 this->pointer = pointer; |
|
97 return *this; |
|
98 } |
|
99 |
|
100 /** |
|
101 * operator* |
|
102 */ |
|
103 inline T& operator*() const { |
|
104 __ASSERT_DEBUG(pointer != NULL, Panic(EGlxPanicNullPointer)); |
|
105 return *pointer; |
|
106 } |
|
107 |
|
108 /** |
|
109 * operator -> |
|
110 */ |
|
111 inline T* operator->() const { |
|
112 __ASSERT_DEBUG(pointer != NULL, Panic(EGlxPanicNullPointer)); |
|
113 return pointer; |
|
114 } |
|
115 |
|
116 /** |
|
117 * Ptr |
|
118 */ |
|
119 inline T* Ptr() const { |
|
120 return pointer; |
|
121 } |
|
122 |
|
123 /** |
|
124 * Release |
|
125 * Return the pointer and release ownership |
|
126 */ |
|
127 T* Release() { |
|
128 iOwns = 0; |
|
129 return pointer; |
|
130 } |
|
131 |
|
132 /** |
|
133 * Owns |
|
134 * Returns ETrue if the object owns the pointer |
|
135 */ |
|
136 inline TBool Owns() const { |
|
137 return (TBool)iOwns; |
|
138 } |
|
139 |
|
140 /** |
|
141 * PtrRef |
|
142 * Return a reference to the pointer |
|
143 */ |
|
144 inline T*& PtrRef() { |
|
145 __ASSERT_DEBUG(pointer != NULL, Panic(EGlxPanicNullPointer)); |
|
146 return pointer; |
|
147 } |
|
148 }; |
|
149 |
|
150 #endif // T_GLXPOINTER_H |