SharedPointer.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*=============================================================================
  2. Copyright (C) 2012 Allied Vision Technologies. All Rights Reserved.
  3. Redistribution of this file, in original or modified form, without
  4. prior written consent of Allied Vision Technologies is prohibited.
  5. -------------------------------------------------------------------------------
  6. File: SharedPointer.h
  7. Description: Definition of an example shared pointer class that can be
  8. used with the Vimba CPP API.
  9. (This include file contains example code only.)
  10. -------------------------------------------------------------------------------
  11. THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
  12. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF TITLE,
  13. NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  14. DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  15. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  16. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  17. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  18. AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
  19. TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  20. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21. =============================================================================*/
  22. #ifndef AVT_VMBAPI_SHAREDPOINTER_H
  23. #define AVT_VMBAPI_SHAREDPOINTER_H
  24. #include <VimbaCPP/Include/Mutex.h>
  25. namespace AVT {
  26. namespace VmbAPI {
  27. //Coding style of this file is different to mimic the shared_ptr classes of std and boost.
  28. struct dynamic_cast_tag
  29. {
  30. };
  31. class ref_count_base
  32. {
  33. public:
  34. virtual ~ref_count_base() {;}
  35. virtual void inc() = 0;
  36. virtual void dec() = 0;
  37. virtual long use_count() const = 0;
  38. };
  39. template <class T>
  40. class ref_count : public virtual AVT::VmbAPI::ref_count_base
  41. {
  42. private:
  43. T *m_pObject;
  44. long m_nCount;
  45. Mutex m_Mutex;
  46. ref_count(const ref_count &rRefCount);
  47. ref_count& operator = (const ref_count &rRefCount);
  48. public:
  49. explicit ref_count(T *pObject);
  50. virtual ~ref_count();
  51. virtual void inc();
  52. virtual void dec();
  53. virtual long use_count() const;
  54. };
  55. template <class T>
  56. class shared_ptr
  57. {
  58. private:
  59. typedef shared_ptr<T> this_type;
  60. template<class T2>
  61. friend class shared_ptr;
  62. AVT::VmbAPI::ref_count_base *m_pRefCount;
  63. T *m_pObject;
  64. template <class T2>
  65. static void swap(T2 &rValue1, T2 &rValue2);
  66. public:
  67. shared_ptr();
  68. template <class T2>
  69. explicit shared_ptr(T2 *pObject);
  70. shared_ptr(const shared_ptr &rSharedPointer);
  71. template <class T2>
  72. shared_ptr(const shared_ptr<T2> &rSharedPointer);
  73. template <class T2>
  74. shared_ptr(const shared_ptr<T2> &rSharedPointer, dynamic_cast_tag);
  75. virtual ~shared_ptr();
  76. shared_ptr& operator = (const shared_ptr &rSharedPointer);
  77. template <class T2>
  78. shared_ptr& operator = (const shared_ptr<T2> &rSharedPointer);
  79. void reset();
  80. template <class T2>
  81. void reset(T2 *pObject);
  82. T* get() const;
  83. T& operator * () const;
  84. T* operator -> () const;
  85. long use_count() const;
  86. bool unique() const;
  87. typedef T* this_type::*unspecified_bool_type;
  88. operator unspecified_bool_type () const
  89. {
  90. if(m_pObject == 0)
  91. {
  92. return 0;
  93. }
  94. return &this_type::m_pObject;
  95. }
  96. void swap(shared_ptr &rSharedPointer);
  97. };
  98. template<class T, class T2>
  99. shared_ptr<T> dynamic_pointer_cast(const shared_ptr<T2> &rSharedPointer);
  100. template<class T1, class T2>
  101. bool operator==(const shared_ptr<T1>& sp1, const shared_ptr<T2>& sp2);
  102. template<class T1, class T2>
  103. bool operator!=(const shared_ptr<T1>& sp1, const shared_ptr<T2>& sp2);
  104. }} //namespace AVT::VmbAPI
  105. #include <VimbaCPP/Include/SharedPointer_impl.h>
  106. #endif //AVT_VMBAPI_SHAREDPOINTER_H