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