BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/Kinetic/Ref_counted.h
Go to the documentation of this file.
00001 // Copyright (c) 2005  Stanford University (USA).
00002 // All rights reserved.
00003 //
00004 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or
00005 // modify it under the terms of the GNU Lesser General Public License as
00006 // published by the Free Software Foundation; version 2.1 of the License.
00007 // See the file LICENSE.LGPL distributed with CGAL.
00008 //
00009 // Licensees holding a valid commercial license may use this file in
00010 // accordance with the commercial license agreement provided with the software.
00011 //
00012 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00013 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00014 //
00015 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Kinetic_data_structures/include/CGAL/Kinetic/Ref_counted.h $
00016 // $Id: Ref_counted.h 39365 2007-07-10 22:09:27Z drussel $
00017 // 
00018 //
00019 // Author(s)     : Daniel Russel <drussel@alumni.princeton.edu>
00020 
00021 #ifndef CGAL_REF_COUNTED_H
00022 #define CGAL_REF_COUNTED_H
00023 #include <CGAL/Kinetic/basic.h>
00024 #include <boost/intrusive_ptr.hpp>
00025 #include <boost/utility.hpp>
00026 
00027 //#define NEW_REF_COUNTED
00028 
00029 CGAL_KINETIC_BEGIN_INTERNAL_NAMESPACE
00030 
00031 class Ref_counted_base;
00032 
00033 void intrusive_ptr_add_ref(const Ref_counted_base *t);
00034 
00035 void intrusive_ptr_release(const Ref_counted_base *t);
00036 
00037 
00038 class Ref_counted_base: boost::noncopyable
00039 {
00040   typedef Ref_counted_base This;
00041 
00042 public:
00043   Ref_counted_base() : reference_count_(0) {}
00044 
00045   void write(std::ostream &out) const
00046   {
00047     out << "(" << reference_count_ << ")";
00048   }
00049   bool is_referenced() const
00050   {
00051     return reference_count_ != 0;
00052   }
00053 
00054   virtual ~Ref_counted_base() {
00055     CGAL_assertion(reference_count_==0);
00056   }
00057 
00058   friend void intrusive_ptr_release(const This *t);
00059   friend void intrusive_ptr_add_ref(const This *t);
00060 
00061   unsigned int reference_count() const {return reference_count_;}
00062 
00063   void  new_ref() const { ++reference_count_; }
00064   void  delete_ref() const
00065   {
00066     CGAL_precondition(reference_count_!=0);
00067     --reference_count_;
00068   }
00069 
00070   mutable unsigned int reference_count_;
00071 };
00072 
00073 inline void intrusive_ptr_add_ref(const Ref_counted_base *t)
00074 {
00075   t->new_ref();
00076 }
00077 
00078 
00079 inline void intrusive_ptr_release(const Ref_counted_base *t)
00080 {
00081   t->delete_ref();
00082   if (t->reference_count() == 0) {
00083     delete t;
00084   }
00085 }
00086 
00087 
00088 
00089 struct Non_ref_counted_base{};
00090 
00091 inline void intrusive_ptr_add_ref(const Non_ref_counted_base *)
00092 {
00093 }
00094 
00095 
00096 inline void intrusive_ptr_release(const Non_ref_counted_base *)
00097 {
00098 }
00099 
00100 
00101 
00102 CGAL_KINETIC_END_INTERNAL_NAMESPACE
00103 
00104 CGAL_KINETIC_BEGIN_NAMESPACE
00105 
00106 template <class T>
00107 class Ref_counted: public internal::Ref_counted_base
00108 
00109 {
00110   typedef internal::Ref_counted_base P;
00111  public:
00112   typedef T This;
00113   typedef typename boost::intrusive_ptr<T> Handle;
00114 
00115   typedef typename boost::intrusive_ptr<const T> Const_handle;
00116 };
00117 
00118 template <class T>
00119 struct Non_ref_counted: public internal::Non_ref_counted_base
00120 {
00121   typedef T This;
00122   typedef typename boost::intrusive_ptr<T> Handle;
00123   typedef typename boost::intrusive_ptr<const T> Const_handle;
00124 };
00125 
00126 
00127 
00128 CGAL_KINETIC_END_NAMESPACE
00129 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines