123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- /*=============================================================================
- Copyright (C) 2012 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.h
- Description: Definition of an example shared pointer class that can be
- used with 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_H
- #define AVT_VMBAPI_SHAREDPOINTER_H
- #include <VimbaCPP/Include/Mutex.h>
- namespace AVT {
- namespace VmbAPI {
- //Coding style of this file is different to mimic the shared_ptr classes of std and boost.
- struct dynamic_cast_tag
- {
- };
- class ref_count_base
- {
- public:
- virtual ~ref_count_base() {;}
- virtual void inc() = 0;
- virtual void dec() = 0;
- virtual long use_count() const = 0;
- };
- template <class T>
- class ref_count : public virtual AVT::VmbAPI::ref_count_base
- {
- private:
- T *m_pObject;
- long m_nCount;
- Mutex m_Mutex;
- ref_count(const ref_count &rRefCount);
- ref_count& operator = (const ref_count &rRefCount);
- public:
- explicit ref_count(T *pObject);
- virtual ~ref_count();
- virtual void inc();
- virtual void dec();
- virtual long use_count() const;
- };
- template <class T>
- class shared_ptr
- {
- private:
- typedef shared_ptr<T> this_type;
-
- template<class T2>
- friend class shared_ptr;
- AVT::VmbAPI::ref_count_base *m_pRefCount;
- T *m_pObject;
- template <class T2>
- static void swap(T2 &rValue1, T2 &rValue2);
- public:
- shared_ptr();
- template <class T2>
- explicit shared_ptr(T2 *pObject);
- shared_ptr(const shared_ptr &rSharedPointer);
- template <class T2>
- shared_ptr(const shared_ptr<T2> &rSharedPointer);
- template <class T2>
- shared_ptr(const shared_ptr<T2> &rSharedPointer, dynamic_cast_tag);
- virtual ~shared_ptr();
- shared_ptr& operator = (const shared_ptr &rSharedPointer);
- template <class T2>
- shared_ptr& operator = (const shared_ptr<T2> &rSharedPointer);
-
- void reset();
- template <class T2>
- void reset(T2 *pObject);
- T* get() const;
- T& operator * () const;
- T* operator -> () const;
- long use_count() const;
- bool unique() const;
-
- typedef T* this_type::*unspecified_bool_type;
- operator unspecified_bool_type () const
- {
- if(m_pObject == 0)
- {
- return 0;
- }
-
- return &this_type::m_pObject;
- }
- void swap(shared_ptr &rSharedPointer);
- };
- template<class T, class T2>
- shared_ptr<T> dynamic_pointer_cast(const shared_ptr<T2> &rSharedPointer);
- template<class T1, class T2>
- bool operator==(const shared_ptr<T1>& sp1, const shared_ptr<T2>& sp2);
- template<class T1, class T2>
- bool operator!=(const shared_ptr<T1>& sp1, const shared_ptr<T2>& sp2);
- }} //namespace AVT::VmbAPI
- #include <VimbaCPP/Include/SharedPointer_impl.h>
- #endif //AVT_VMBAPI_SHAREDPOINTER_H
|