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 00034 00036 00038 00069 template <typename T , typename DeletePolicy = SCOPE > 00070 class ObjectPtr 00071 { 00072 Q_DISABLE_COPY(ObjectPtr); 00073 00074 00075 struct myPtr 00076 { 00077 virtual void setPtr(T * p)=0; 00078 virtual T* get()=0; 00079 virtual const T* get() const =0; 00080 virtual void Release() = 0; 00081 }; 00082 00083 //internal struct to choice between normal ptr and QWeakPointer when the class inherite from QObject 00084 template <bool P1,typename P2> 00085 struct interptr 00086 { 00087 }; 00088 //Specialization. If T is not a QObject, use a normal pointer 00089 template<typename TPTR> 00090 struct interptr<false,TPTR> : public myPtr 00091 { 00092 TPTR * ptr; 00093 interptr():ptr(0){} 00094 void setPtr(TPTR * p) {ptr = p;} 00095 TPTR* get() {return ptr;} 00096 const TPTR* get()const {return ptr;} 00097 void Release() {delete ptr;} 00098 00099 }; 00100 //Specialization. If T is a QObject, use a QWeakPointer 00101 template<typename TPTR> 00102 struct interptr<true,TPTR> : public myPtr 00103 { 00104 QWeakPointer< TPTR> ptr; 00105 void setPtr(TPTR * p) {ptr = p;} 00106 TPTR* get() {return ptr.data();} 00107 const TPTR* get()const {return ptr.data();} 00108 void Release() {delete ptr.data();} 00109 }; 00110 00111 myPtr * m_data; 00112 /*interptr 00113 < 00114 IsDerivedFrom<T,QObject>::val 00115 , T 00116 > m_data; //internal pointer depend if T is a QObejct or not*/ 00117 00118 public: 00119 00120 00121 00122 00123 00125 00126 00128 00161 template < typename P1 , 00162 typename P2 , 00163 typename P3 , 00164 typename P4 , 00165 typename P5 > 00166 ObjectPtr 00167 ( 00168 const P1 & p1 , 00169 const P2 & p2 , 00170 const P3 & p3 , 00171 const P4 & p4 , 00172 const P5 & p5 00173 ) 00174 :m_data ( new interptr < 00175 IsDerivedFrom<T,QObject>::val 00176 , T 00177 > ) 00178 { 00179 m_data->setPtr( new T( p1 , p2 , p3 , p4 , p5 ) ); 00180 } 00181 00182 00183 00185 00186 00188 00221 template < typename P1 , 00222 typename P2 , 00223 typename P3 , 00224 typename P4 > 00225 ObjectPtr 00226 ( 00227 const P1 & p1 , 00228 const P2 & p2 , 00229 const P3 & p3 , 00230 const P4 & p4 00231 ) 00232 :m_data ( new interptr < 00233 IsDerivedFrom<T,QObject>::val 00234 , T 00235 > ) 00236 { 00237 m_data->setPtr( new T( p1 , p2 , p3 , p4 ) ); 00238 } 00239 00240 00241 00243 00244 00246 00275 template < typename P1 , 00276 typename P2 , 00277 typename P3 > 00278 ObjectPtr 00279 ( 00280 const P1 & p1 , 00281 const P2 & p2 , 00282 const P3 & p3 00283 ) 00284 :m_data ( new interptr < 00285 IsDerivedFrom<T,QObject>::val 00286 , T 00287 > ) 00288 { 00289 m_data->setPtr( new T ( p1 , p2 , p3 ) ); 00290 } 00291 00292 00294 00295 00297 00318 template < typename P1 , 00319 typename P2 > 00320 ObjectPtr 00321 ( 00322 const P1 & p1 , 00323 const P2 & p2 00324 ) 00325 :m_data ( new interptr < 00326 IsDerivedFrom<T,QObject>::val 00327 , T 00328 > ) 00329 { 00330 m_data ->setPtr( new T( p1 , p2 ) ); 00331 } 00332 00333 00334 00336 00337 00339 00355 template < typename P > 00356 ObjectPtr ( const P & p ) 00357 :m_data ( new interptr < 00358 IsDerivedFrom<T,QObject>::val 00359 , T 00360 > ) 00361 { 00362 m_data->setPtr( new T( p ) ); 00363 } 00364 00365 00366 00368 00369 00371 00379 ObjectPtr () 00380 :m_data ( new interptr < 00381 IsDerivedFrom<T,QObject>::val 00382 , T 00383 > ) 00384 { 00385 m_data->setPtr( new T () ); 00386 } 00387 00388 00389 00390 00392 00393 00395 00403 ~ObjectPtr() 00404 { 00405 DeletePolicy deletePolicy; 00406 if( deletePolicy(m_data->get() ) ) 00407 { 00408 m_data->Release() ; 00409 } 00410 delete m_data; 00411 } 00412 00413 00414 00416 00417 00419 00427 bool isNull() const 00428 { 00429 return m_data->get() == 0; 00430 } 00431 00432 00433 00435 00436 00438 operator bool () const 00439 { 00440 return ! isNull (); 00441 } 00442 00443 00444 00446 00447 00449 operator T * () 00450 { 00451 return m_data->get(); 00452 } 00453 00454 00455 00457 00458 00460 operator const T * () const 00461 { 00462 return m_data->get(); 00463 } 00464 00465 00466 00467 00468 00470 00471 00473 00481 T* operator->() 00482 { 00483 return m_data->get(); 00484 } 00485 00486 00487 00489 00490 00492 00500 const T* operator->() const 00501 { 00502 return m_data->get(); 00503 } 00504 00505 00506 00507 00509 00510 00512 00520 T & operator*() 00521 { 00522 return *m_data->get(); 00523 } 00524 00525 00526 00528 00529 00531 00539 const T & operator*() const 00540 { 00541 return *m_data->get(); 00542 } 00543 }; 00544 00545 } 00546 00547 #endif // QEXTEND_ObjectPtr
© 2000-2024 - www.developpez.com