00001 /** @file nub/handle.h strong reference-counted handle for nub::object */ 00002 00003 /////////////////////////////////////////////////////////////////////// 00004 // 00005 // Copyright (c) 2000-2004 California Institute of Technology 00006 // Copyright (c) 2004-2007 University of Southern California 00007 // Rob Peters <rjpeters at usc dot edu> 00008 // 00009 // created: Wed Nov 16 08:50:12 2005 00010 // commit: $Id: handle.h 8249 2007-04-12 06:03:40Z rjpeters $ 00011 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/nub/handle.h $ 00012 // 00013 // -------------------------------------------------------------------- 00014 // 00015 // This file is part of GroovX. 00016 // [http://www.klab.caltech.edu/rjpeters/groovx/] 00017 // 00018 // GroovX is free software; you can redistribute it and/or modify it 00019 // under the terms of the GNU General Public License as published by 00020 // the Free Software Foundation; either version 2 of the License, or 00021 // (at your option) any later version. 00022 // 00023 // GroovX is distributed in the hope that it will be useful, but 00024 // WITHOUT ANY WARRANTY; without even the implied warranty of 00025 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00026 // General Public License for more details. 00027 // 00028 // You should have received a copy of the GNU General Public License 00029 // along with GroovX; if not, write to the Free Software Foundation, 00030 // Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 00031 // 00032 /////////////////////////////////////////////////////////////////////// 00033 00034 #ifndef GROOVX_NUB_HANDLE_H_UTC20051116165012_DEFINED 00035 #define GROOVX_NUB_HANDLE_H_UTC20051116165012_DEFINED 00036 00037 #include "nub/types.h" 00038 00039 #include "rutz/algo.h" // for rutz::swap2() 00040 #include "rutz/fileposition.h" // for SRC_POS macro 00041 00042 #include <typeinfo> 00043 00044 namespace nub 00045 { 00046 namespace detail 00047 { 00048 template <class T, class unref_policy> class handle; 00049 00050 void throw_ref_null(const std::type_info& info, const rutz::file_pos& pos); 00051 void throw_ref_unshareable(const std::type_info& msg, const rutz::file_pos& pos); 00052 } 00053 } 00054 00055 /// A shared implementation class for nub::floating_ref and nub::ref. 00056 /** Note that the only operation that can throw is the 00057 constructor, which throws in case it is passed a null or 00058 unshareable object pointer .*/ 00059 template <class T, class unref_policy> 00060 class nub::detail::handle 00061 { 00062 public: 00063 explicit handle(T* master) : m_master(master) 00064 { 00065 if (master == 0) 00066 throw_ref_null(typeid(T), SRC_POS); 00067 00068 if (master->is_not_shareable()) 00069 throw_ref_unshareable(typeid(T), SRC_POS); 00070 00071 m_master->incr_ref_count(); 00072 } 00073 00074 ~handle() throw() 00075 { unref_policy::unref(m_master); } 00076 00077 handle(const handle& other) throw() 00078 : 00079 m_master(other.m_master) 00080 { 00081 m_master->incr_ref_count(); 00082 } 00083 00084 handle& operator=(const handle& other) throw() 00085 { 00086 handle other_copy(other); 00087 this->swap(other_copy); 00088 return *this; 00089 } 00090 00091 T* get() const throw() 00092 { return m_master; } 00093 00094 bool operator==(const handle& other) const throw() 00095 { return m_master == other.m_master; } 00096 00097 private: 00098 void swap(handle& other) throw() 00099 { 00100 rutz::swap2(m_master, other.m_master); 00101 } 00102 00103 T* m_master; 00104 00105 }; 00106 00107 static const char __attribute__((used)) vcid_groovx_nub_handle_h_utc20051116165012[] = "$Id: handle.h 8249 2007-04-12 06:03:40Z rjpeters $ $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/nub/handle.h $"; 00108 #endif // !GROOVX_NUB_HANDLE_H_UTC20051116165012DEFINED