/* * $Id: refptr.h,v 1.1 2005/12/02 07:08:01 ozawa Exp $ * * Copyright 2005- ONGS Inc. All rights reserved. * * author: Masanori OZAWA (ozawa@ongs.co.jp) * version: $Revision: 1.1 $ * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY ONGS INC ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL ONGS INC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * The views and conclusions contained in the software and documentation are * those of the authors and should not be interpreted as representing official * policies, either expressed or implied, of the ONGS Inc. * */ #ifndef ___REFPTR_H #define ___REFPTR_H #include template class RefPtr { public: inline RefPtr(); explicit inline RefPtr(T *obj); inline RefPtr(const RefPtr& src); inline virtual ~RefPtr(); public: inline RefPtr& operator=(const RefPtr& src); inline bool operator==(const RefPtr& src) const; inline bool operator!=(const RefPtr& src) const; inline T* operator->() const; inline bool operator()() const; protected: inline virtual void reference(); inline virtual void unreference(); inline virtual void destroy(); protected: T *m_obj; int32_t *m_ref_count; }; //_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ template inline RefPtr::RefPtr() { m_obj = NULL; m_ref_count = NULL; } template inline RefPtr::RefPtr(T *obj) { if (!obj) throw "RefPtr: Bad argument. Out of memory?"; m_obj = obj; m_ref_count = (int32_t*)malloc(sizeof(*m_ref_count)); if (!m_ref_count) throw "Out of memory!"; *m_ref_count = 1; } template inline RefPtr::RefPtr(const RefPtr& src) { m_obj = src.m_obj; m_ref_count = src.m_ref_count; reference(); } template inline RefPtr::~RefPtr() { unreference(); } template inline RefPtr& RefPtr::operator=(const RefPtr& src) { unreference(); m_obj = src.m_obj; m_ref_count = src.m_ref_count; reference(); return (*this); } template inline bool RefPtr::operator==(const RefPtr& src) const { return (m_obj == src.m_obj); } template inline bool RefPtr::operator!=(const RefPtr& src) const { return (m_obj != src.m_obj); } template inline T* RefPtr::operator->() const { return m_obj; } template inline bool RefPtr::operator()() const { return (NULL != m_obj); } template inline void RefPtr::reference() { if (m_ref_count) { if (0x7fffffff <= (*m_ref_count)) { throw "RefPtr: Out of range."; } *m_ref_count += 1; } } template inline void RefPtr::unreference() { if (m_ref_count) { *m_ref_count -= 1; if (0 >= (*m_ref_count)) { destroy(); free(m_ref_count); m_ref_count = NULL; } } } template inline void RefPtr::destroy() { if (m_obj) { delete m_obj; m_obj = NULL; } } #endif