SharedPointer_impl.h
Go to the documentation of this file.
1 /*=============================================================================
2  Copyright (C) 2012 - 2017 Allied Vision Technologies. All Rights Reserved.
3 
4  Redistribution of this file, in original or modified form, without
5  prior written consent of Allied Vision Technologies is prohibited.
6 
7 -------------------------------------------------------------------------------
8 
9  File: SharedPointer_impl.h
10 
11  Description: Implementation of an example shared pointer class for the Vimba
12  CPP API.
13  (This include file contains example code only.)
14 
15 -------------------------------------------------------------------------------
16 
17  THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
18  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF TITLE,
19  NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
21  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24  AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
25  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 =============================================================================*/
29 
30 #ifndef AVT_VMBAPI_SHAREDPOINTER_IMPL_H
31 #define AVT_VMBAPI_SHAREDPOINTER_IMPL_H
32 
34 #include <stdexcept>
35 namespace AVT {
36 namespace VmbAPI {
37 
38  template <class T>
40  {
41  }
42 
43  template <class T>
45  {
46  //This is a dummy method to satisfy the compiler
47  return *this;
48  }
49 
50  template <class T>
52  : m_pObject(pObject)
53  , m_nCount(1)
54  {
55  }
56 
57  template <class T>
59  {
60  if(NULL != m_pObject)
61  {
62  delete m_pObject;
63  }
64 
65  m_Mutex.Unlock();
66  }
67 
68  template <class T>
70  {
71  m_Mutex.Lock();
72 
73  m_nCount++;
74 
75  m_Mutex.Unlock();
76  }
77 
78  template <class T>
80  {
81  m_Mutex.Lock();
82  if( m_nCount == 0 )
83  {
84  throw std::logic_error("shared pointer, used incorectly");
85  }
86  if(m_nCount > 1)
87  {
88  m_nCount--;
89 
90  m_Mutex.Unlock();
91  }
92  else
93  {
94  // m_Mutex will be unlocked in d'tor
95  delete this;
96  }
97  }
98 
99  template <class T>
101  {
102  return m_nCount;
103  }
104 
105  template <class T>
106  template <class T2>
107  void shared_ptr<T>::swap(T2 &rValue1, T2 &rValue2)
108  {
109  T2 buffer = rValue1;
110  rValue1 = rValue2;
111  rValue2 = buffer;
112  }
113 
114  template <class T>
116  : m_pRefCount(NULL)
117  , m_pObject(NULL)
118  {
119  }
120 
121  template <class T>
122  template <class T2>
124  : m_pRefCount(NULL)
125  , m_pObject(NULL)
126  {
127  m_pRefCount = new ref_count<T2>(pObject);
128  if(NULL == m_pRefCount)
129  {
130  delete pObject;
131 
132  throw std::bad_alloc();
133  }
134 
135  m_pObject = pObject;
136  }
137 
138  template <class T>
139  template <class T2>
141  : m_pRefCount(NULL)
142  , m_pObject(NULL)
143  {
144  if(NULL != rSharedPointer.m_pRefCount)
145  {
146  rSharedPointer.m_pRefCount->inc();
147 
148  m_pRefCount = rSharedPointer.m_pRefCount;
149  m_pObject = rSharedPointer.m_pObject;
150  }
151  }
152 
153  template <class T>
154  template <class T2>
156  : m_pRefCount(NULL)
157  , m_pObject(NULL)
158  {
159  if(NULL != rSharedPointer.m_pRefCount)
160  {
161  T *pObject = dynamic_cast<T*>(rSharedPointer.m_pObject);
162  if(NULL != pObject)
163  {
164  rSharedPointer.m_pRefCount->inc();
165 
166  m_pRefCount = rSharedPointer.m_pRefCount;
167  m_pObject = pObject;
168  }
169  }
170  }
171 
172  template <class T>
173  shared_ptr<T>::shared_ptr(const shared_ptr &rSharedPointer)
174  : m_pRefCount(NULL)
175  , m_pObject(NULL)
176  {
177  if(NULL != rSharedPointer.m_pRefCount)
178  {
179  rSharedPointer.m_pRefCount->inc();
180 
181  m_pRefCount = rSharedPointer.m_pRefCount;
182  m_pObject = rSharedPointer.m_pObject;
183  }
184  }
185 
186  template <class T>
188  {
189  if(NULL != m_pRefCount)
190  {
191  m_pRefCount->dec();
192  m_pRefCount = NULL;
193  m_pObject = NULL;
194  }
195  }
196 
197  template <class T>
198  template <class T2>
200  {
201  shared_ptr(rSharedPointer).swap(*this);
202 
203  return *this;
204  }
205 
206  template <class T>
208  {
209  shared_ptr(rSharedPointer).swap(*this);
210 
211  return *this;
212  }
213 
214  template <class T>
216  {
217  shared_ptr().swap(*this);
218  }
219 
220  template <class T>
221  template <class T2>
222  void shared_ptr<T>::reset(T2 *pObject)
223  {
224  shared_ptr(pObject).swap(*this);
225  }
226 
227  template <class T>
229  {
230  return m_pObject;
231  }
232 
233  template <class T>
235  {
236  return *m_pObject;
237  }
238 
239  template <class T>
241  {
242  return m_pObject;
243  }
244 
245  template <class T>
247  {
248  if(NULL == m_pRefCount)
249  {
250  return 0;
251  }
252 
253  return m_pRefCount->use_count();
254  }
255 
256  template <class T>
258  {
259  return (use_count() == 1);
260  }
261 
262  template <class T>
263  void shared_ptr<T>::swap(shared_ptr &rSharedPointer)
264  {
265  swap(m_pObject, rSharedPointer.m_pObject);
266  swap(m_pRefCount, rSharedPointer.m_pRefCount);
267  }
268 
269  template<class T, class T2>
271  {
272  return shared_ptr<T>(rSharedPointer, dynamic_cast_tag());
273  }
274 
275  template <class T1, class T2>
276  bool operator==(const shared_ptr<T1>& sp1, const shared_ptr<T2>& sp2)
277  {
278  return sp1.get() == sp2.get();
279  }
280 
281  template <class T1, class T2>
282  bool operator!=(const shared_ptr<T1>& sp1, const shared_ptr<T2>& sp2)
283  {
284  return sp1.get() != sp2.get();
285  }
286 
287 }} //namespace AVT::VmbAPI
288 
289 #endif //AVT_VMBAPI_SHAREDPOINTER_IMPL_H
ref_count & operator=(const ref_count &rRefCount)
IMEXPORT void Unlock()
Definition: Mutex.cpp:87
AVT::VmbAPI::ref_count_base * m_pRefCount
Definition: SharedPointer.h:83
IMEXPORT void Lock()
Definition: Mutex.cpp:78
bool operator!=(const shared_ptr< T1 > &sp1, const shared_ptr< T2 > &sp2)
shared_ptr & operator=(const shared_ptr &rSharedPointer)
virtual long use_count() const =0
virtual long use_count() const
static void swap(T2 &rValue1, T2 &rValue2)
ref_count(const ref_count &rRefCount)
bool operator==(const shared_ptr< T1 > &sp1, const shared_ptr< T2 > &sp2)
shared_ptr< T > dynamic_pointer_cast(const shared_ptr< T2 > &rSharedPointer)


avt_vimba_camera
Author(s): Allied Vision Technologies, Miquel Massot
autogenerated on Fri Jun 2 2023 02:21:10