BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/Handle.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.h $
00019 // $Id: Handle.h 42873 2008-04-12 10:38:07Z spion $
00020 // 
00021 //
00022 // Author(s)     : Sylvain Pion
00023 
00024 #ifndef CGAL_HANDLE_H
00025 #define CGAL_HANDLE_H
00026 
00027 #include <CGAL/Handle_for.h>
00028 #include <CGAL/assertions.h>
00029 
00030 CGAL_BEGIN_NAMESPACE
00031 
00032 class Rep
00033 {
00034     friend class Handle;
00035   protected:
00036              Rep() { count = 1; }
00037     virtual ~Rep() {}
00038 
00039     int      count;
00040 };
00041 
00042 class Handle
00043 {
00044   public:
00045     Handle()
00046         : PTR(static_cast<Rep*>(0)) {}
00047 
00048     Handle(const Handle& x)
00049     {
00050       CGAL_precondition( x.PTR != static_cast<Rep*>(0) );
00051       PTR = x.PTR;
00052       PTR->count++;
00053     }
00054 
00055     ~Handle()
00056     {
00057         if ( PTR && (--PTR->count == 0))
00058             delete PTR;
00059     }
00060 
00061     Handle&
00062     operator=(const Handle& x)
00063     {
00064       CGAL_precondition( x.PTR != static_cast<Rep*>(0) );
00065       x.PTR->count++;
00066       if ( PTR && (--PTR->count == 0))
00067           delete PTR;
00068       PTR = x.PTR;
00069       return *this;
00070     }
00071 
00072     int
00073     refs()  const { return PTR->count; }
00074 
00075     friend unsigned long id(const Handle& x);
00076     friend bool identical(const Handle& h1, const Handle& h2);
00077 
00078   protected:
00079     Rep* PTR;
00080 };
00081 
00082 inline
00083 unsigned long
00084 id(const Handle& x)
00085 { return reinterpret_cast<unsigned long>(x.PTR); }
00086 
00087 inline
00088 bool
00089 identical(const Handle &h1, const Handle &h2)
00090 { return reinterpret_cast<unsigned long>(h1.PTR) ==
00091          reinterpret_cast<unsigned long>(h2.PTR); }
00092 
00093 CGAL_END_NAMESPACE
00094 
00095 #endif // CGAL_HANDLE_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines