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 00036 00067 template <typename T , typename DeletePolicy = SCOPE > 00068 class ExtendPtr 00069 { 00070 00071 00072 //internal struct to choice between normal ptr and QWeakPointer when the class inherite from QObject 00073 template <bool P1,typename P2> 00074 struct interptr 00075 { 00076 }; 00077 //Specialization. If T is not a QObject, use a normal pointer 00078 template<typename TPTR> 00079 struct interptr<false,TPTR> 00080 { 00081 TPTR * ptr; 00082 interptr():ptr(0){} 00083 void setPtr(TPTR * p) {ptr = p;} 00084 TPTR* get() {return ptr;} 00085 const TPTR* get()const {return ptr;} 00086 }; 00087 //Specialization. If T is a QObject, use a QWeakPointer 00088 template<typename TPTR> 00089 struct interptr<true,TPTR> 00090 { 00091 QWeakPointer< TPTR> ptr; 00092 void setPtr(TPTR * p) {ptr = p;} 00093 TPTR* get() {return ptr.data();} 00094 const TPTR* get()const {return ptr.data();} 00095 }; 00096 00097 interptr 00098 < 00099 IsDerivedFrom<T,QObject>::val 00100 , T 00101 > m_data; //internal pointer depend if T is a QObejct or not 00102 00103 public: 00104 00105 00106 00109 00121 ExtendPtr ( T * p = 0 ) 00122 { 00123 m_data.setPtr( p ); 00124 } 00125 00128 00140 ExtendPtr<T,DeletePolicy> &operator=( T * p ) 00141 { 00142 DeletePolicy deletePolicy; 00143 if( deletePolicy(m_data.get() ) ) 00144 { 00145 delete m_data.get(); 00146 } 00147 m_data.setPtr( p ); 00148 return *this; 00149 } 00150 00153 00161 ~ExtendPtr() 00162 { 00163 DeletePolicy deletePolicy; 00164 if( deletePolicy(m_data.get() ) ) 00165 { 00166 delete m_data.get(); 00167 } 00168 } 00169 00172 00180 bool isNull() const 00181 { 00182 return m_data.get() == 0; 00183 } 00184 00187 operator bool () const 00188 { 00189 return ! isNull (); 00190 } 00191 00194 operator T * () 00195 { 00196 return m_data.get(); 00197 } 00198 00201 operator const T * () const 00202 { 00203 return m_data.get(); 00204 } 00205 00206 00207 00210 00218 T* operator->() 00219 { 00220 return m_data.get(); 00221 } 00222 00225 00233 const T* operator->() const 00234 { 00235 return m_data.get(); 00236 } 00237 00238 00241 00249 T & operator*() 00250 { 00251 return *m_data.get(); 00252 } 00253 00254 00257 00265 const T & operator*() const 00266 { 00267 return *m_data.get(); 00268 } 00269 }; 00270 00271 } 00272 00273 #endif // QEXTEND_ObjectPtr
Les sources présentées sur cette page sont libres de droits et vous pouvez les utiliser à votre convenance. Par contre, la page de présentation constitue une œuvre intellectuelle protégée par les droits d'auteur. Copyright © 2010 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site ni de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon vous encourez selon la loi jusqu'à trois ans de prison et jusqu'à 300 000 € de dommages et intérêts.