BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/Handle_for_virtual.h
Go to the documentation of this file.
00001 // Copyright (c) 1999  Utrecht University (The Netherlands),
00002 // ETH Zurich (Switzerland), Freie Universitaet Berlin (Germany),
00003 // INRIA Sophia-Antipolis (France), Martin-Luther-University Halle-Wittenberg
00004 // (Germany), Max-Planck-Institute Saarbruecken (Germany), RISC Linz (Austria),
00005 // and Tel-Aviv University (Israel).  All rights reserved.
00006 //
00007 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public License as
00009 // published by the Free Software Foundation; version 2.1 of the License.
00010 // See the file LICENSE.LGPL distributed with CGAL.
00011 //
00012 // Licensees holding a valid commercial license may use this file in
00013 // accordance with the commercial license agreement provided with the software.
00014 //
00015 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00016 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00017 //
00018 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/STL_Extension/include/CGAL/Handle_for_virtual.h $
00019 // $Id: Handle_for_virtual.h 45176 2008-08-27 15:36:24Z spion $
00020 // 
00021 //
00022 // Author(s)     : Stefan Schirra
00023  
00024 
00025 #ifndef CGAL_HANDLE_FOR_VIRTUAL_H
00026 #define CGAL_HANDLE_FOR_VIRTUAL_H
00027 
00028 #include <CGAL/basic.h>
00029 #include <typeinfo>
00030 
00031 CGAL_BEGIN_NAMESPACE
00032 
00033 class Ref_counted_virtual
00034 {
00035   public:
00036     Ref_counted_virtual() : count(1) {}
00037     Ref_counted_virtual(const Ref_counted_virtual&) : count(1) {}
00038 
00039     void  add_reference() { ++count; }
00040     void  remove_reference() { --count; }
00041     bool  is_referenced() const { return (count != 0); }
00042     bool  is_shared() const { return (count > 1); }
00043 
00044     virtual const std::type_info & type() const
00045     { return typeid(void); }
00046 
00047     virtual const void * object_ptr() const
00048     { return NULL; }
00049 
00050     virtual ~Ref_counted_virtual() {}
00051 
00052   protected:
00053     unsigned int count;
00054 };
00055 
00056 
00057 template <class RefCounted>
00058 // RefCounted must provide
00059 // add_reference()
00060 // remove_reference()
00061 // bool is_referenced() const
00062 // bool is_shared() const
00063 // and initialize count to 1 in default and copy constructor
00064 class Handle_for_virtual
00065 {
00066   public:
00067 
00068     Handle_for_virtual(const RefCounted& rc)
00069     {
00070       ptr = new RefCounted(rc);
00071     }
00072 
00073     Handle_for_virtual()
00074     {
00075       ptr = NULL;
00076     }
00077 
00078     Handle_for_virtual( const Handle_for_virtual& h)
00079     {
00080       ptr = h.ptr;
00081       ptr->add_reference();
00082     }
00083 
00084     ~Handle_for_virtual()
00085     {
00086       ptr->remove_reference();
00087       if ( !ptr->is_referenced() )
00088           delete ptr;
00089     }
00090 
00091     Handle_for_virtual&
00092     operator=( const Handle_for_virtual& h)
00093     {
00094       h.ptr->add_reference();
00095       ptr->remove_reference();
00096       if ( !ptr->is_referenced() )
00097           delete ptr;
00098       ptr = h.ptr;
00099       return *this;
00100     }
00101 
00102 #ifndef CGAL_CFG_NO_CPP0X_RVALUE_REFERENCE
00103     Handle_for_virtual&
00104     operator=( Handle_for_virtual && h)
00105     {
00106       swap(h);
00107       return *this;
00108     }
00109 #endif
00110 
00111 // protected:
00112     typedef RefCounted element_type;
00113 
00114     template <class T>
00115     void
00116     initialize_with( const T& rc)
00117     {
00118         ptr = new T(rc);
00119     }
00120 
00121     bool
00122     identical( const Handle_for_virtual& h) const
00123     { return ptr == h.ptr; }
00124 
00125     long int
00126     id() const
00127     { return reinterpret_cast<long int>(&*ptr); }
00128 
00129     void
00130     swap(Handle_for_virtual & h)
00131     { std::swap(h.ptr, ptr); }
00132 
00133     const RefCounted *
00134     Ptr() const
00135     { return ptr; }
00136 
00137     const void * object_ptr() const
00138     {
00139       return ptr->object_ptr();
00140     }
00141 
00142     /*
00143     T *
00144     Ptr()
00145     {
00146         copy_on_write();
00147         return ptr;
00148     }
00149     */
00150 
00151     /*
00152 // private:
00153     void
00154     copy_on_write()
00155     {
00156       if ( ptr->is_shared() )
00157       {
00158         RefCounted* tmp_ptr = allocator.allocate(1);
00159         allocator.construct( tmp_ptr, *ptr);
00160         ptr->remove_reference();
00161         ptr = tmp_ptr;
00162       }
00163     }
00164     */
00165 protected:
00166 
00167     /*
00168     RefCounted * ptr() const
00169     {
00170         return ptr;
00171     }
00172     */
00173 
00174     RefCounted * ptr;
00175 };
00176 
00177 CGAL_END_NAMESPACE
00178 
00179 #endif // CGAL_HANDLE_FOR_VIRTUAL_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines