00001 /* 00002 Copyright © 2010 yan Verdavaine 00003 00004 This file is part of QExtend. 00005 00006 QExtend is free software: you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation, either version 3 of the License, or 00009 any later version. 00010 00011 QExtend is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 GNU General Public License for more details. 00015 00016 You should have received a copy of the GNU General Public License 00017 along with QExtend. If not, see <http://www.gnu.org/licenses/>. 00018 */ 00019 00020 #ifndef QEXTEND_ExtendPtr_H 00021 #define QEXTEND_ExtendPtr_H 00022 00023 #include <QtCore/QWeakPointer> 00024 #include <QExtend/Core/qextend_global> 00025 00026 #include "objectextendptrpolicy.hpp" 00027 #include "templatetools.hpp" 00028 namespace QExtend 00029 { 00030 00031 00032 00033 00034 00036 00038 00069 template <typename T , typename DeletePolicy = SCOPE > 00070 class ExtendPtr 00071 { 00072 00073 00074 //internal struct to choice between normal ptr and QWeakPointer when the class inherite from QObject 00075 template <bool P1,typename P2> 00076 struct interptr 00077 { 00078 }; 00079 //Specialization. If T is not a QObject, use a normal pointer 00080 template<typename TPTR> 00081 struct interptr<false,TPTR> 00082 { 00083 TPTR * ptr; 00084 interptr():ptr(0){} 00085 void setPtr(TPTR * p) {ptr = p;} 00086 TPTR* get() {return ptr;} 00087 const TPTR* get()const {return ptr;} 00088 }; 00089 //Specialization. If T is a QObject, use a QWeakPointer 00090 template<typename TPTR> 00091 struct interptr<true,TPTR> 00092 { 00093 QWeakPointer< TPTR> ptr; 00094 void setPtr(TPTR * p) {ptr = p;} 00095 TPTR* get() {return ptr.data();} 00096 const TPTR* get()const {return ptr.data();} 00097 }; 00098 00099 interptr 00100 < 00101 IsDerivedFrom<T,QObject>::val 00102 , T 00103 > m_data; //internal pointer depend if T is a QObejct or not 00104 00105 public: 00106 00107 00108 00109 00110 00112 00113 00115 00127 ExtendPtr ( T * p = 0 ) 00128 { 00129 m_data.setPtr( p ); 00130 } 00131 00132 00133 00135 00136 00138 00150 ExtendPtr<T,DeletePolicy> &operator=( T * p ) 00151 { 00152 DeletePolicy deletePolicy; 00153 if( deletePolicy(m_data.get() ) ) 00154 { 00155 delete m_data.get(); 00156 } 00157 m_data.setPtr( p ); 00158 return *this; 00159 } 00160 00161 00162 00164 00165 00167 00175 ~ExtendPtr() 00176 { 00177 DeletePolicy deletePolicy; 00178 if( deletePolicy(m_data.get() ) ) 00179 { 00180 delete m_data.get(); 00181 } 00182 } 00183 00184 00185 00187 00188 00190 00198 bool isNull() const 00199 { 00200 return m_data.get() == 0; 00201 } 00202 00203 00204 00206 00207 00209 operator bool () const 00210 { 00211 return ! isNull (); 00212 } 00213 00214 00215 00217 00218 00220 operator T * () 00221 { 00222 return m_data.get(); 00223 } 00224 00225 00226 00228 00229 00231 operator const T * () const 00232 { 00233 return m_data.get(); 00234 } 00235 00236 00237 00238 00239 00241 00242 00244 00252 T* operator->() 00253 { 00254 return m_data.get(); 00255 } 00256 00257 00258 00260 00261 00263 00271 const T* operator->() const 00272 { 00273 return m_data.get(); 00274 } 00275 00276 00277 00278 00280 00281 00283 00291 T & operator*() 00292 { 00293 return *m_data.get(); 00294 } 00295 00296 00297 00298 00300 00301 00303 00311 const T & operator*() const 00312 { 00313 return *m_data.get(); 00314 } 00315 }; 00316 00317 } 00318 00319 #endif // QEXTEND_ObjectPtr
© 2000-2025 - www.developpez.com