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_OBJECTPTR_H 00021 #define QEXTEND_OBJECTPTR_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 ObjectPtr 00069 { 00070 Q_DISABLE_COPY(ObjectPtr); 00071 00072 00073 struct myPtr 00074 { 00075 virtual void setPtr(T * p)=0; 00076 virtual T* get()=0; 00077 virtual const T* get() const =0; 00078 virtual void Release() = 0; 00079 }; 00080 00081 //internal struct to choice between normal ptr and QWeakPointer when the class inherite from QObject 00082 template <bool P1,typename P2> 00083 struct interptr 00084 { 00085 }; 00086 //Specialization. If T is not a QObject, use a normal pointer 00087 template<typename TPTR> 00088 struct interptr<false,TPTR> : public myPtr 00089 { 00090 TPTR * ptr; 00091 interptr():ptr(0){} 00092 void setPtr(TPTR * p) {ptr = p;} 00093 TPTR* get() {return ptr;} 00094 const TPTR* get()const {return ptr;} 00095 void Release() {delete ptr;} 00096 00097 }; 00098 //Specialization. If T is a QObject, use a QWeakPointer 00099 template<typename TPTR> 00100 struct interptr<true,TPTR> : public myPtr 00101 { 00102 QWeakPointer< TPTR> ptr; 00103 void setPtr(TPTR * p) {ptr = p;} 00104 TPTR* get() {return ptr.data();} 00105 const TPTR* get()const {return ptr.data();} 00106 void Release() {delete ptr.data();} 00107 }; 00108 00109 myPtr * m_data; 00110 /*interptr 00111 < 00112 IsDerivedFrom<T,QObject>::val 00113 , T 00114 > m_data; //internal pointer depend if T is a QObejct or not*/ 00115 00116 public: 00117 00118 00119 00122 00155 template < typename P1 , 00156 typename P2 , 00157 typename P3 , 00158 typename P4 , 00159 typename P5 > 00160 ObjectPtr 00161 ( 00162 const P1 & p1 , 00163 const P2 & p2 , 00164 const P3 & p3 , 00165 const P4 & p4 , 00166 const P5 & p5 00167 ) 00168 :m_data ( new interptr < 00169 IsDerivedFrom<T,QObject>::val 00170 , T 00171 > ) 00172 { 00173 m_data->setPtr( new T( p1 , p2 , p3 , p4 , p5 ) ); 00174 } 00175 00178 00211 template < typename P1 , 00212 typename P2 , 00213 typename P3 , 00214 typename P4 > 00215 ObjectPtr 00216 ( 00217 const P1 & p1 , 00218 const P2 & p2 , 00219 const P3 & p3 , 00220 const P4 & p4 00221 ) 00222 :m_data ( new interptr < 00223 IsDerivedFrom<T,QObject>::val 00224 , T 00225 > ) 00226 { 00227 m_data->setPtr( new T( p1 , p2 , p3 , p4 ) ); 00228 } 00229 00232 00261 template < typename P1 , 00262 typename P2 , 00263 typename P3 > 00264 ObjectPtr 00265 ( 00266 const P1 & p1 , 00267 const P2 & p2 , 00268 const P3 & p3 00269 ) 00270 :m_data ( new interptr < 00271 IsDerivedFrom<T,QObject>::val 00272 , T 00273 > ) 00274 { 00275 m_data->setPtr( new T ( p1 , p2 , p3 ) ); 00276 } 00279 00300 template < typename P1 , 00301 typename P2 > 00302 ObjectPtr 00303 ( 00304 const P1 & p1 , 00305 const P2 & p2 00306 ) 00307 :m_data ( new interptr < 00308 IsDerivedFrom<T,QObject>::val 00309 , T 00310 > ) 00311 { 00312 m_data ->setPtr( new T( p1 , p2 ) ); 00313 } 00314 00317 00333 template < typename P > 00334 ObjectPtr ( const P & p ) 00335 :m_data ( new interptr < 00336 IsDerivedFrom<T,QObject>::val 00337 , T 00338 > ) 00339 { 00340 m_data->setPtr( new T( p ) ); 00341 } 00342 00345 00353 ObjectPtr () 00354 :m_data ( new interptr < 00355 IsDerivedFrom<T,QObject>::val 00356 , T 00357 > ) 00358 { 00359 m_data->setPtr( new T () ); 00360 } 00361 00362 00365 00373 ~ObjectPtr() 00374 { 00375 DeletePolicy deletePolicy; 00376 if( deletePolicy(m_data->get() ) ) 00377 { 00378 m_data->Release() ; 00379 } 00380 delete m_data; 00381 } 00382 00385 00393 bool isNull() const 00394 { 00395 return m_data->get() == 0; 00396 } 00397 00400 operator bool () const 00401 { 00402 return ! isNull (); 00403 } 00404 00407 operator T * () 00408 { 00409 return m_data->get(); 00410 } 00411 00414 operator const T * () const 00415 { 00416 return m_data->get(); 00417 } 00418 00419 00420 00423 00431 T* operator->() 00432 { 00433 return m_data->get(); 00434 } 00435 00438 00446 const T* operator->() const 00447 { 00448 return m_data->get(); 00449 } 00450 00451 00454 00462 T & operator*() 00463 { 00464 return *m_data->get(); 00465 } 00466 00469 00477 const T & operator*() const 00478 { 00479 return *m_data->get(); 00480 } 00481 }; 00482 00483 } 00484 00485 #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.