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