00001 /* 00002 Copyright 2008 Larry Gritz and the other authors and contributors. 00003 All Rights Reserved. 00004 00005 Redistribution and use in source and binary forms, with or without 00006 modification, are permitted provided that the following conditions are 00007 met: 00008 * Redistributions of source code must retain the above copyright 00009 notice, this list of conditions and the following disclaimer. 00010 * Redistributions in binary form must reproduce the above copyright 00011 notice, this list of conditions and the following disclaimer in the 00012 documentation and/or other materials provided with the distribution. 00013 * Neither the name of the software's owners nor the names of its 00014 contributors may be used to endorse or promote products derived from 00015 this software without specific prior written permission. 00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00017 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00018 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00019 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00020 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00021 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00022 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00023 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00024 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00025 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00026 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 00028 (This is the Modified BSD License) 00029 */ 00030 00031 00037 00038 00039 #ifndef OPENIMAGEIO_REFCNT_H 00040 #define OPENIMAGEIO_REFCNT_H 00041 00042 #include "thread.h" 00043 00044 // Use Boost for shared pointers 00045 #include <boost/tr1/memory.hpp> 00046 using std::tr1::shared_ptr; 00047 #include <boost/intrusive_ptr.hpp> 00048 using boost::intrusive_ptr; 00049 00050 00051 #ifdef OPENIMAGEIO_NAMESPACE 00052 namespace OPENIMAGEIO_NAMESPACE { 00053 #endif 00054 00055 00056 00059 class RefCnt { 00060 public: 00061 RefCnt () { m_refcnt = 0; } 00062 00065 RefCnt (RefCnt &r) { m_refcnt = 0; } 00066 00069 void _incref () const { ++m_refcnt; } 00070 00073 bool _decref () const { return (--m_refcnt) == 0; } 00074 00077 const RefCnt & operator= (const RefCnt& r) const { return *this; } 00078 00079 private: 00080 mutable atomic_int m_refcnt; 00081 }; 00082 00083 00084 00087 template <class T> 00088 inline void intrusive_ptr_add_ref (T *x) 00089 { 00090 x->_incref (); 00091 } 00092 00095 template <class T> 00096 inline void intrusive_ptr_release (T *x) 00097 { 00098 if (x->_decref ()) 00099 delete x; 00100 } 00101 00102 // Note that intrusive_ptr_add_ref and intrusive_ptr_release MUST be a 00103 // templated on the full type, so that they pass the right address to 00104 // 'delete' and destroy the right type. If you try to just 00105 // 'inline void intrusive_ptr_release (RefCnt *x)', that might seem 00106 // clever, but it will end up getting the address of (and destroying) 00107 // just the inherited RefCnt sub-object, not the full subclass you 00108 // meant to delete and destroy. 00109 00110 00111 #ifdef OPENIMAGEIO_NAMESPACE 00112 }; // end namespace OPENIMAGEIO_NAMESPACE 00113 using namespace OPENIMAGEIO_NAMESPACE; 00114 #endif 00115 00116 #endif // OPENIMAGEIO_REFCNT_H