ReferenceCounting.cppGo to the documentation of this file.00001 /* 00002 * Copyright (c) 2000-2003 Barak Weichselbaum <barak@komodia.com> 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 00007 * are met: 00008 * 1. Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * 2. 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 * 00014 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 00015 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00016 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00017 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 00018 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00019 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00020 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00021 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00022 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00023 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00024 * SUCH DAMAGE. 00025 * 00026 * Contact info: 00027 * ------------- 00028 * 00029 * Site: http://www.komodia.com 00030 * Main contact: barak@komodia.com 00031 * For custom projects, 00032 * consulting, or other 00033 * paid services: sales@komodia.com 00034 */ 00035 00036 #include "stdafx.h" 00037 #include "ReferenceCounting.h" 00038 00039 #ifdef _MEMORY_DEBUG 00040 #define new DEBUG_NEW 00041 #define malloc DEBUG_MALLOC 00042 static char THIS_FILE[] = __FILE__; 00043 #endif 00044 00045 KOMODIA_NAMESPACE_START 00046 00047 //----------------------- CReferenceSmartPtr start ----------------------- 00048 00049 CReferenceSmartPtr::CReferenceSmartPtr(const CReferenceCounting* pCounter, 00050 bool bAutoAdd) : m_pCounter(pCounter) 00051 { 00052 //Do we need to add 00053 if (bAutoAdd) 00054 //Add the ref 00055 AddRef(); 00056 } 00057 00058 CReferenceSmartPtr::~CReferenceSmartPtr() 00059 { 00060 //Release the ref 00061 ReleaseRef(); 00062 } 00063 00064 const CReferenceCounting* CReferenceSmartPtr::Detach() 00065 { 00066 //Save to tmp 00067 const CReferenceCounting* pTmp; 00068 pTmp=m_pCounter; 00069 00070 //Clear out var 00071 m_pCounter=NULL; 00072 00073 //Done 00074 return pTmp; 00075 } 00076 00077 long CReferenceSmartPtr::AddRef() 00078 { 00079 if (m_pCounter) 00080 return m_pCounter->AddRef(); 00081 else 00082 return 0; 00083 } 00084 00085 long CReferenceSmartPtr::ReleaseRef() 00086 { 00087 if (m_pCounter) 00088 return m_pCounter->ReleaseRef(); 00089 else 00090 return 0; 00091 } 00092 00093 //----------------------- CReferenceSmartPtr end ----------------------- 00094 00095 //----------------------- CReferenceCounting start ----------------------- 00096 00097 CReferenceCounting::CReferenceCounting() : m_lCount(1) 00098 { 00099 } 00100 00101 CReferenceCounting::CReferenceCounting(const CReferenceCounting& rReference) : m_lCount(1) 00102 { 00103 } 00104 00105 CReferenceCounting::~CReferenceCounting() 00106 { 00107 } 00108 00109 00110 long CReferenceCounting::AddRef()const 00111 { 00112 //Add a reference 00113 return ++m_lCount; 00114 } 00115 00116 long CReferenceCounting::ReleaseRef()const 00117 { 00118 //Was it zero 00119 if (!m_lCount) 00120 return 0; 00121 00122 //Decrese the count 00123 --m_lCount; 00124 00125 //Check if we need to destruct 00126 if (m_lCount<=0) 00127 { 00128 //Save the count 00129 long lCount; 00130 lCount=m_lCount; 00131 00132 //Delete this class 00133 Destruct(); 00134 00135 //Done 00136 return lCount; 00137 } 00138 else 00139 return m_lCount; 00140 } 00141 00142 void CReferenceCounting::Destruct()const 00143 { 00144 //Delete the object 00145 delete this; 00146 } 00147 00148 CReferenceCounting& CReferenceCounting::operator=(const CReferenceCounting& rReference) 00149 { 00150 //Set zero reference count 00151 m_lCount=1; 00152 00153 //Done 00154 return *this; 00155 } 00156 00157 //----------------------- CReferenceCounting end ----------------------- 00158 00159 KOMODIA_NAMESPACE_END |