blocxx

COWIntrusiveCountableBase.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2005, Vintela, Inc. All rights reserved.
00003 * Copyright (C) 2006, Novell, Inc. 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 met:
00007 * 
00008 *     * Redistributions of source code must retain the above copyright notice,
00009 *       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 
00014 *       Vintela, Inc., 
00015 *       nor Novell, Inc., 
00016 *       nor the names of its contributors or employees may be used to 
00017 *       endorse or promote products derived from this software without 
00018 *       specific prior written permission.
00019 * 
00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00022 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00023 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00024 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00025 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00026 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00027 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00028 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00029 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00030 * POSSIBILITY OF SUCH DAMAGE.
00031 *******************************************************************************/
00032 
00033 
00038 //
00039 //  Copyright (c) 2001, 2002 Peter Dimov
00040 //
00041 //  Permission to copy, use, modify, sell and distribute this software
00042 //  is granted provided this copyright notice appears in all copies.
00043 //  This software is provided "as is" without express or implied
00044 //  warranty, and with no claim as to its suitability for any purpose.
00045 //
00046 
00047 #ifndef BLOCXX_COW_INTRUSIVE_COUNTABLE_BASE_HPP_INCLUDE_GUARD_
00048 #define BLOCXX_COW_INTRUSIVE_COUNTABLE_BASE_HPP_INCLUDE_GUARD_
00049 
00050 #include "blocxx/BLOCXX_config.h"
00051 #include "blocxx/RefCount.hpp"
00052 
00053 namespace BLOCXX_NAMESPACE
00054 {
00055 
00072 class COWIntrusiveCountableBase; 
00073 void COWIntrusiveReferenceAddRef(COWIntrusiveCountableBase * p); 
00074 void COWIntrusiveReferenceRelease(COWIntrusiveCountableBase * p); 
00075 bool COWIntrusiveReferenceUnique(COWIntrusiveCountableBase* p); 
00076 template <typename T> T* COWIntrusiveReferenceClone(T* p); 
00077 
00078 class BLOCXX_COMMON_API COWIntrusiveCountableBase
00079 {
00080 private:
00081    RefCount m_usecount;
00082 
00083 protected:
00087    COWIntrusiveCountableBase(COWIntrusiveCountableBase const &)
00088       : m_usecount(0)
00089    {}
00090 
00096    COWIntrusiveCountableBase & operator=(COWIntrusiveCountableBase const &x)
00097    {
00098       // don't assign or change the ref count, since it won't be different.
00099       return *this;
00100    }
00101 
00105    COWIntrusiveCountableBase(): m_usecount(0)
00106    {
00107    }
00108 
00112    virtual ~COWIntrusiveCountableBase();
00113 
00117    RefCount getRefCount() const
00118    {
00119       return m_usecount;
00120    }
00121 
00122 public:
00128    inline friend void COWIntrusiveReferenceAddRef(COWIntrusiveCountableBase * p)
00129    {
00130       p->m_usecount.inc();
00131    }
00132 
00140    inline friend void COWIntrusiveReferenceRelease(COWIntrusiveCountableBase * p)
00141    {
00142       if (p->m_usecount.decAndTest())
00143          delete p;
00144    }
00145    
00152    inline friend bool COWIntrusiveReferenceUnique(COWIntrusiveCountableBase* p)
00153    {
00154       return p->m_usecount.get() == 1;
00155    }
00156 
00162    template <typename T>
00163    friend T* COWIntrusiveReferenceClone(T* p); 
00164 };
00165 
00166 template <typename T>
00167 inline T* COWIntrusiveReferenceClone(T* p)
00168 {
00169    // this needs to happen first to avoid a race condition between 
00170    // another thread deleting the object and this one making a copy.
00171    T* tmp = p->clone();
00172    if (p->m_usecount.decAndTest())
00173    {
00174       // only copy--don't need to clone, also not a race condition.
00175       // undo the decAndTest.
00176       p->m_usecount.inc();
00177       delete tmp; // we won't need this anymore.
00178       return p;
00179    }
00180    else
00181    {
00182       // need to become unique
00183       if (tmp)    
00184          COWIntrusiveReferenceAddRef(tmp);
00185 
00186       return tmp;
00187    }
00188 }
00189 
00190 } // end namespace BLOCXX_NAMESPACE
00191 
00192 #endif
00193 
00194