FastJet 3.0alpha3
SharedPtr.hh
00001 #ifndef __FASTJET_SHARED_PTR_HH__
00002 #define __FASTJET_SHARED_PTR_HH__
00003 
00004 //STARTHEADER
00005 // $Id: SharedPtr.hh 2098 2011-05-12 18:41:44Z salam $
00006 //
00007 // Copyright (c) 2005-2010, Matteo Cacciari, Gavin Salam and Gregory Soyez
00008 //
00009 //----------------------------------------------------------------------
00010 // This file is part of FastJet.
00011 //
00012 //  FastJet is free software; you can redistribute it and/or modify
00013 //  it under the terms of the GNU General Public License as published by
00014 //  the Free Software Foundation; either version 2 of the License, or
00015 //  (at your option) any later version.
00016 //
00017 //  The algorithms that underlie FastJet have required considerable
00018 //  development and are described in hep-ph/0512210. If you use
00019 //  FastJet as part of work towards a scientific publication, please
00020 //  include a citation to the FastJet paper.
00021 //
00022 //  FastJet is distributed in the hope that it will be useful,
00023 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00024 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025 //  GNU General Public License for more details.
00026 //
00027 //  You should have received a copy of the GNU General Public License
00028 //  along with FastJet; if not, write to the Free Software
00029 //  Foundation, Inc.:
00030 //      59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00031 //----------------------------------------------------------------------
00032 //ENDHEADER
00033 
00034 #include "fastjet/internal/base.hh"
00035 #include <cstdlib>  // for NULL!!!
00036 
00037 // for testing purposes, the following define makes it possible
00038 // for our SharedPtr simply to be derived from the STL TR1 one.
00039 // #define USETR1SHAREDPTR
00040 
00041 #ifdef USETR1SHAREDPTR
00042 #include <tr1/memory>
00043 #endif // USETR1SHAREDPTR
00044 
00045 FASTJET_BEGIN_NAMESPACE      // defined in fastjet/internal/base.hh
00046 
00047 #ifdef USETR1SHAREDPTR
00048 
00049 /// @ingroup advanced_usage
00050 /// \class SharedPtr
00051 /// replaces our shared pointer with the TR1 one (for testing purpose)
00052 ///
00053 /// for testing purposes, it can be useful to replace our home-made
00054 /// SharedPtr with the standard library one. Having a class derived
00055 /// from the standard one is way of arranging for this to happen.
00056 /// 
00057 /// The other way of working this is a template class with an 
00058 /// internal typedef (http://bytes.com/topic/c/answers/60312-typedef-template)
00059 /// since templated typedefs don't work in standard C++
00060 template<class T>
00061 class SharedPtr : public std::tr1::shared_ptr<T> {
00062 public:
00063   SharedPtr() : std::tr1::shared_ptr<T>() {}
00064   SharedPtr(T * t) : std::tr1::shared_ptr<T>(t) {}
00065   SharedPtr(const SharedPtr<T> & t) : std::tr1::shared_ptr<T>(t) {}
00066   // for some reason operator() doesn't get inherited
00067   inline operator bool() const {return (this->get()!=NULL);}
00068   /// return the pointer we're pointing to  
00069   T* operator ()() const{
00070     return this->get(); // automatically returns NULL when out-of-scope
00071   }
00072 };
00073 
00074 
00075 #else // USETR1SHAREDPTR
00076 
00077 /**
00078  * @ingroup advanced_usage
00079  * \class SharedPtr
00080  * an implementation of C++0x shared pointers (or boost's)
00081  *
00082  * this class implements a smart pointer, based on the shared+ptr
00083  * proposal. A description of shared_ptr can be found in Section 2.2.3
00084  * of the first C++ Technical Report (TR1)
00085  *   http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2005/n1745.pdf
00086  * or, alternatively, on the Boost C++ library website at
00087  *   http://www.boost.org/doc/libs/1_42_0/libs/smart_ptr/shared_ptr.htm
00088  *
00089  * Our implementation is compatible with both of these apart from a
00090  * series of members and functions that have not been implemented:
00091  *  - conversion from weak and auto pointers
00092  *  - support for deleters and allocators
00093  *  - static, constant and dynamic casts
00094  *  - constructor and assignment sharing ownership with a shared
00095  *    pointer r but storing a different pointer than r (needed for the
00096  *    previous item)
00097  * In the last 2 cases, their implementation would require storing two
00098  * pointers for every copies of the shared pointer, while our
00099  * implementation only needs one. We did not implement then since we
00100  * want to limit as much as possible memory and time consumption, and
00101  * can easily avoid (at least for our needs so far) the casts.
00102  *
00103  * We also add the possibility to force an update of the count.
00104  * 
00105  * The class has been tested against the existing boost (v1.42)
00106  * implementation (for the parts that we have implemented).
00107  */
00108 template<class T>
00109 class SharedPtr{
00110 public:
00111   /// forward declaration of the counting container
00112   class __SharedCountingPtr;
00113 
00114   /// default ctor
00115   SharedPtr() : _ptr(NULL){}
00116   
00117   /// initialise with the main data
00118   /// \param  t  : the object we want a smart pointer to
00119   template<class Y> explicit SharedPtr(Y* ptr){
00120     _ptr = new __SharedCountingPtr(ptr);
00121   }
00122   
00123   /// overload the copy ctor so that it updates count
00124   /// \param  share : the object we want to copy
00125   SharedPtr(SharedPtr const & share) : _ptr(NULL){
00126     reset(share);
00127   }
00128     
00129   /// overload the copy ctor so that it updates count
00130   /// \param  share : the object we want to copy
00131   template<class Y> SharedPtr(SharedPtr<Y> const & share) : _ptr(NULL){
00132     reset(share);
00133   }
00134 
00135   /// default dtor
00136   ~SharedPtr(){
00137     // make sure the object has been allocated
00138     if (_ptr==NULL) return;
00139 
00140     _decrease_count();
00141   }
00142 
00143   /// reset the pointer to default value (NULL)
00144   void reset(){
00145     // // if we already are pointing to sth, be sure to decrease its count
00146     // if (_ptr!=NULL) _decrease_count();
00147     // _ptr = NULL;
00148     SharedPtr().swap(*this);
00149   }
00150   
00151   /// reset from a pointer
00152   template<class Y> void reset(Y * ptr){
00153     // // if we already are pointing to sth, be sure to decrease its count
00154     // if (_ptr!=NULL) _decrease_count();
00155     // 
00156     // _ptr = new __SharedCountingPtr(ptr);
00157     SharedPtr(ptr).swap(*this);
00158   }
00159 
00160   // not part of the standard
00161   /// do a smart copy
00162   /// \param  share : the object we want to copy
00163   /// Q? Do we need a non-template<Y> version as for the ctor and the assignment?
00164   template<class Y> void reset(SharedPtr<Y> const & share){
00165     // if we already are pointing to sth, be sure to decrease its count
00166     if (_ptr!=NULL){
00167       // in the specific case where we're having the same
00168       // share,reset() has actually no effect. However if *this is the
00169       // only instance still alive (implying share==*this) bringing
00170       // the count down to 0 and deleting the object will not have the
00171       // expected effect. So we just avoid that situation explicitly
00172       if (_ptr == share._get_container()) return;
00173     
00174       _decrease_count();
00175     }
00176     
00177     // Watch out: if share is empty, construct an empty shared_ptr
00178     
00179     // copy the container
00180     _ptr = share._get_container();  // Note: automatically set it to NULL if share is empty
00181     
00182     if (_ptr!=NULL)
00183       (*_ptr)++;
00184   }
00185   
00186   /// overload the = operator so that it updates count
00187   /// \param  share : the object we want to copy
00188   SharedPtr& operator=(SharedPtr const & share){
00189     reset(share);
00190     return *this;
00191   }
00192   
00193   /// overload the = operator so that it updates count
00194   /// \param  share : the object we want to copy
00195   template<class Y> SharedPtr& operator=(SharedPtr<Y> const & share){
00196     reset(share);
00197     return *this;
00198   }
00199   
00200   /// return the pointer we're pointing to  
00201   T* operator ()() const{
00202     if (_ptr==NULL) return NULL;
00203     return _ptr->get(); // automatically returns NULL when out-of-scope
00204   }
00205   
00206   /// indirection, get a reference to the stored pointer
00207   ///
00208   /// !!! WATCH OUT
00209   /// It fails the requirement that the stored pointer must no be NULL!!
00210   /// So you need explicitly to check the validity in your code
00211   inline T& operator*() const{
00212     return *(_ptr->get());
00213   }
00214 
00215   /// indirection, get the stored pointer
00216   ///
00217   /// !!! WATCH OUT
00218   /// It fails the requirement that the stored pointer must no be NULL!!
00219   /// So you need explicitly to check the validity in your code
00220   inline T* operator->() const{
00221     if (_ptr==NULL) return NULL;
00222     return _ptr->get();
00223   }  
00224 
00225   /// get the stored pointer
00226   inline T* get() const{
00227     if (_ptr==NULL) return NULL;
00228     return _ptr->get();
00229   }
00230 
00231   /// check if the instance is unique
00232   inline bool unique() const{
00233     return (use_count()==1);
00234   }
00235 
00236   /// return the number of counts
00237   inline long use_count() const{
00238     if (_ptr==NULL) return 0;
00239     return _ptr->use_count(); // automatically returns NULL when out-of-scope
00240   }
00241 
00242   /// conversion to bool
00243   /// This will allow you to use the indirection nicely
00244   inline operator bool() const{
00245     return (get()!=NULL);
00246   }
00247 
00248   /// exchange the content of the two pointers
00249   inline void swap(SharedPtr & share){
00250     __SharedCountingPtr* share_container = share._ptr;
00251     share._ptr = _ptr;
00252     _ptr = share_container;
00253   }
00254 
00255   /// force the count to be set to a specified value
00256   ///   \param count   the value that we ned to reset to
00257   void set_count(const long & count){
00258     if (_ptr==NULL) return;
00259     _ptr->set_count(count);
00260   }
00261 
00262   /**
00263    * \if internal_doc
00264    * \class __SharedCountingPtr
00265    * A reference-counting pointer
00266    *
00267    * This is implemented as a container for that pointer together with
00268    * reference counting.
00269    * The pointer is deleted when the number of counts goes to 0;
00270    * \endif
00271    */
00272   class __SharedCountingPtr{
00273   public:
00274     /// default ctor
00275     __SharedCountingPtr() : _ptr(NULL), _count(0){}
00276     
00277     /// ctor with initialisation
00278     template<class Y> explicit __SharedCountingPtr(Y* ptr) : _ptr(ptr), _count(1){}
00279     
00280     /// default dtor
00281     ~__SharedCountingPtr(){ 
00282       // force the deletion of the object we keep track of
00283       if (_ptr!=NULL){ delete _ptr;}
00284     }
00285 
00286     /// return a pointer to the object
00287     inline T* get() const {return _ptr;}
00288 
00289     /// return the count
00290     inline long use_count() const {return _count;}
00291 
00292     /// postfix incrementation
00293     inline long operator++(int unused){return _count++;}
00294 
00295     /// postfix decrementation
00296     inline long operator--(int unused){return _count--;}
00297 
00298     /// prefix incrementation
00299     inline long operator++(){return ++_count;}
00300 
00301     /// prefix decrementation
00302     inline long operator--(){return --_count;}
00303 
00304     /// force the count to be set to a specified value
00305     ///   \param count   the value that we ned to reset to
00306     void set_count(const long & count){
00307       _count = count;
00308     }
00309 
00310   private:
00311     T *_ptr;              ///< the pointer we're counting the references to
00312     long _count;  ///< the number of references
00313   };
00314 
00315 private:
00316   /// return the common container
00317   inline __SharedCountingPtr* _get_container() const{
00318     return _ptr;
00319   }
00320 
00321   /// decrease the pointer count and support deletion
00322   /// Warning: we don't test that the pointer is allocated
00323   ///          This can be dangerous if we have explicitly reset the
00324   ///          count.  Generally speaking, if the count goes negative
00325   ///          after _ptr has been effectively deleted, this is going
00326   ///          to lead to a segmentation fault. But, if in the course
00327   ///          of the deletion of _ptr, the deletion of its pointer
00328   ///          (_ptr::_ptr, i.e. the real data we're storing) makes
00329   ///          the counts to become negative, this is going to pass
00330   ///          smoothly.
00331   void _decrease_count(){
00332     // decrease the count
00333     (*_ptr)--;
00334     
00335     // if no one else is using it, free the allocated memory
00336     if (_ptr->use_count()==0)
00337       delete _ptr; // that automatically deletes the object itself
00338   }
00339 
00340   // the real info
00341   __SharedCountingPtr *_ptr;
00342 };
00343 
00344 
00345 /// comparison: equality
00346 template<class T,class U>
00347 inline bool operator==(SharedPtr<T> const & t, SharedPtr<U> const & u){
00348   return t.get() == u.get();
00349 }
00350 
00351 /// comparison: difference
00352 template<class T,class U>
00353 inline bool operator!=(SharedPtr<T> const & t, SharedPtr<U> const & u){
00354   return t.get() != u.get();
00355 }
00356 
00357 /// comparison: orgering
00358 template<class T,class U>
00359 inline bool operator<(SharedPtr<T> const & t, SharedPtr<U> const & u){
00360   return t.get() < u.get();
00361 }
00362 
00363 /// swapping
00364 template<class T>
00365 inline void swap(SharedPtr<T> & a, SharedPtr<T> & b){
00366   return a.swap(b);
00367 }
00368 
00369 /// getting the pointer
00370 template<class T>
00371 inline T* get_pointer(SharedPtr<T> const & t){
00372   return t.get();
00373 }
00374 
00375 #endif // USETR1SHAREDPTR
00376 
00377 FASTJET_END_NAMESPACE      // defined in fastjet/internal/base.hh
00378 
00379 #endif   // __FASTJET_SHARED_PTR_HH__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends