/*============================================================================= Copyright (C) 2012 - 2017 Allied Vision Technologies. All Rights Reserved. Redistribution of this file, in original or modified form, without prior written consent of Allied Vision Technologies is prohibited. ------------------------------------------------------------------------------- File: SharedPointer_impl.h Description: Implementation of an example shared pointer class for the Vimba CPP API. (This include file contains example code only.) ------------------------------------------------------------------------------- THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR 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. =============================================================================*/ #ifndef AVT_VMBAPI_SHAREDPOINTER_IMPL_H #define AVT_VMBAPI_SHAREDPOINTER_IMPL_H #include #include namespace AVT { namespace VmbAPI { template ref_count::ref_count(const ref_count &rRefCount) { } template ref_count& ref_count::operator = (const ref_count &rRefCount) { //This is a dummy method to satisfy the compiler return *this; } template ref_count::ref_count(T *pObject) : m_pObject(pObject) , m_nCount(1) { } template ref_count::~ref_count() { if(NULL != m_pObject) { delete m_pObject; } m_Mutex.Unlock(); } template void ref_count::inc() { m_Mutex.Lock(); m_nCount++; m_Mutex.Unlock(); } template void ref_count::dec() { m_Mutex.Lock(); if( m_nCount == 0 ) { throw std::logic_error("shared pointer, used incorectly"); } if(m_nCount > 1) { m_nCount--; m_Mutex.Unlock(); } else { // m_Mutex will be unlocked in d'tor delete this; } } template long ref_count::use_count() const { return m_nCount; } template template void shared_ptr::swap(T2 &rValue1, T2 &rValue2) { T2 buffer = rValue1; rValue1 = rValue2; rValue2 = buffer; } template shared_ptr::shared_ptr() : m_pRefCount(NULL) , m_pObject(NULL) { } template template shared_ptr::shared_ptr(T2 *pObject) : m_pRefCount(NULL) , m_pObject(NULL) { m_pRefCount = new ref_count(pObject); if(NULL == m_pRefCount) { delete pObject; throw std::bad_alloc(); } m_pObject = pObject; } template template shared_ptr::shared_ptr(const shared_ptr &rSharedPointer) : m_pRefCount(NULL) , m_pObject(NULL) { if(NULL != rSharedPointer.m_pRefCount) { rSharedPointer.m_pRefCount->inc(); m_pRefCount = rSharedPointer.m_pRefCount; m_pObject = rSharedPointer.m_pObject; } } template template shared_ptr::shared_ptr(const shared_ptr &rSharedPointer, dynamic_cast_tag) : m_pRefCount(NULL) , m_pObject(NULL) { if(NULL != rSharedPointer.m_pRefCount) { T *pObject = dynamic_cast(rSharedPointer.m_pObject); if(NULL != pObject) { rSharedPointer.m_pRefCount->inc(); m_pRefCount = rSharedPointer.m_pRefCount; m_pObject = pObject; } } } template shared_ptr::shared_ptr(const shared_ptr &rSharedPointer) : m_pRefCount(NULL) , m_pObject(NULL) { if(NULL != rSharedPointer.m_pRefCount) { rSharedPointer.m_pRefCount->inc(); m_pRefCount = rSharedPointer.m_pRefCount; m_pObject = rSharedPointer.m_pObject; } } template shared_ptr::~shared_ptr() { if(NULL != m_pRefCount) { m_pRefCount->dec(); m_pRefCount = NULL; m_pObject = NULL; } } template template shared_ptr& shared_ptr::operator = (const shared_ptr &rSharedPointer) { shared_ptr(rSharedPointer).swap(*this); return *this; } template shared_ptr& shared_ptr::operator = (const shared_ptr &rSharedPointer) { shared_ptr(rSharedPointer).swap(*this); return *this; } template void shared_ptr::reset() { shared_ptr().swap(*this); } template template void shared_ptr::reset(T2 *pObject) { shared_ptr(pObject).swap(*this); } template T* shared_ptr::get() const { return m_pObject; } template T& shared_ptr::operator * () const { return *m_pObject; } template T* shared_ptr::operator -> () const { return m_pObject; } template long shared_ptr::use_count() const { if(NULL == m_pRefCount) { return 0; } return m_pRefCount->use_count(); } template bool shared_ptr::unique() const { return (use_count() == 1); } template void shared_ptr::swap(shared_ptr &rSharedPointer) { swap(m_pObject, rSharedPointer.m_pObject); swap(m_pRefCount, rSharedPointer.m_pRefCount); } template shared_ptr dynamic_pointer_cast(const shared_ptr &rSharedPointer) { return shared_ptr(rSharedPointer, dynamic_cast_tag()); } template bool operator==(const shared_ptr& sp1, const shared_ptr& sp2) { return sp1.get() == sp2.get(); } template bool operator!=(const shared_ptr& sp1, const shared_ptr& sp2) { return sp1.get() != sp2.get(); } }} //namespace AVT::VmbAPI #endif //AVT_VMBAPI_SHAREDPOINTER_IMPL_H