00001 #if !defined(__MCREFCOUNTACCESSOR_H_) 00002 #define __MCREFCOUNTACCESSOR_H_ 00003 00004 #ident "@(#)$Id: MCRefCountAccessor.h,v 1.7 2004/12/02 07:05:20 mike Exp $" 00005 00006 /* 00007 * Copyright Miba Consulting Ltd. (c) 2000-2003 00008 * 00009 * This library is free software; you can redistribute it and/or 00010 * modify it under the terms of the GNU Lesser General Public 00011 * License as published by the Free Software Foundation; either 00012 * version 2.1 of the License, or (at your option) any later version. 00013 * 00014 * This library is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00017 * Lesser General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU Lesser General Public 00020 * License along with this library; if not, write to the Free Software 00021 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00022 * 00023 */ 00024 00025 #include "mcllib/MCTypes.h" 00026 #include "mcllib/MCRefCount.h" 00027 #include "mcllib/MCThreadModel.h" 00028 00029 namespace mcllib 00030 { 00036 class MCRefCountAccessorBase 00037 { 00038 protected: 00044 inline mcatomic_t getCnt(MCRefCount* pCtr) const 00045 { 00046 return(pCtr->m_cnt); 00047 } 00054 mcatomic_t safeIncCnt(MCRefCount* pCtr) const; 00061 mcatomic_t safeDecCnt(MCRefCount* pCtr) const; 00068 inline mcatomic_t unsafeIncCnt(MCRefCount* pCtr) const 00069 { 00070 return(++(pCtr->m_cnt)); 00071 } 00078 inline mcatomic_t unsafeDecCnt(MCRefCount* pCtr) const 00079 { 00080 return(--(pCtr->m_cnt)); 00081 } 00082 }; 00083 00089 template<MCThreadModel::EModel MODEL> 00090 class MCRefCountAccessorTmpl : public MCRefCountAccessorBase 00091 { 00092 public: 00097 mcatomic_t incCnt(MCRefCount* pCtr) const; 00102 mcatomic_t decCnt(MCRefCount* pCtr) const; 00103 }; 00104 00112 template<> 00113 class MCRefCountAccessorTmpl<MCThreadModel::THRMODELMULTI> 00114 : public MCRefCountAccessorBase 00115 { 00116 public: 00121 inline mcatomic_t incCnt(MCRefCount* pCtr) const 00122 { 00123 return(safeIncCnt(pCtr)); 00124 } 00129 mcatomic_t decCnt(MCRefCount* pCtr) const 00130 { 00131 return(safeDecCnt(pCtr)); 00132 } 00133 }; 00137 typedef MCRefCountAccessorTmpl<MCThreadModel::THRMODELMULTI> 00138 MCRefCountAccessor; 00139 00149 template<> 00150 class MCRefCountAccessorTmpl<MCThreadModel::THRMODELSINGLE> 00151 : public MCRefCountAccessorBase 00152 { 00153 public: 00158 inline mcatomic_t incCnt(MCRefCount* pCtr) const 00159 { 00160 return(unsafeIncCnt(pCtr)); 00161 } 00166 mcatomic_t decCnt(MCRefCount* pCtr) const 00167 { 00168 return(unsafeDecCnt(pCtr)); 00169 } 00170 }; 00171 00177 class MCRefCountGetter : public MCRefCountAccessor 00178 { 00179 public: 00181 MCRefCountGetter(MCRefCount* pRef); 00183 operator mcatomic_t(); 00184 private: 00185 mcatomic_t m_cnt; 00186 }; 00187 00188 }; 00189 00190 #endif