SharedPointer_impl.h
Go to the documentation of this file.
1 /*=============================================================================
2  Copyright (C) 2012 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 
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 
66  template <class T>
68  {
69  m_Mutex.Lock();
70 
71  m_nCount++;
72 
73  m_Mutex.Unlock();
74  }
75 
76  template <class T>
78  {
79  m_Mutex.Lock();
80 
81  if(m_nCount > 1)
82  {
83  m_nCount--;
84 
85  m_Mutex.Unlock();
86  }
87  else
88  {
89  delete this;
90  }
91  }
92 
93  template <class T>
95  {
96  return m_nCount;
97  }
98 
99  template <class T>
100  template <class T2>
101  void shared_ptr<T>::swap(T2 &rValue1, T2 &rValue2)
102  {
103  T2 buffer = rValue1;
104  rValue1 = rValue2;
105  rValue2 = buffer;
106  }
107 
108  template <class T>
110  : m_pRefCount(NULL)
111  , m_pObject(NULL)
112  {
113  }
114 
115  template <class T>
116  template <class T2>
118  : m_pRefCount(NULL)
119  , m_pObject(NULL)
120  {
121  m_pRefCount = new ref_count<T2>(pObject);
122  if(NULL == m_pRefCount)
123  {
124  delete pObject;
125 
126  throw std::bad_alloc();
127  }
128 
129  m_pObject = pObject;
130  }
131 
132  template <class T>
133  template <class T2>
135  : m_pRefCount(NULL)
136  , m_pObject(NULL)
137  {
138  if(NULL != rSharedPointer.m_pRefCount)
139  {
140  rSharedPointer.m_pRefCount->inc();
141 
142  m_pRefCount = rSharedPointer.m_pRefCount;
143  m_pObject = rSharedPointer.m_pObject;
144  }
145  }
146 
147  template <class T>
148  template <class T2>
150  : m_pRefCount(NULL)
151  , m_pObject(NULL)
152  {
153  if(NULL != rSharedPointer.m_pRefCount)
154  {
155  T *pObject = dynamic_cast<T*>(rSharedPointer.m_pObject);
156  if(NULL != pObject)
157  {
158  rSharedPointer.m_pRefCount->inc();
159 
160  m_pRefCount = rSharedPointer.m_pRefCount;
161  m_pObject = pObject;
162  }
163  }
164  }
165 
166  template <class T>
167  shared_ptr<T>::shared_ptr(const shared_ptr &rSharedPointer)
168  : m_pRefCount(NULL)
169  , m_pObject(NULL)
170  {
171  if(NULL != rSharedPointer.m_pRefCount)
172  {
173  rSharedPointer.m_pRefCount->inc();
174 
175  m_pRefCount = rSharedPointer.m_pRefCount;
176  m_pObject = rSharedPointer.m_pObject;
177  }
178  }
179 
180  template <class T>
182  {
183  if(NULL != m_pRefCount)
184  {
185  m_pRefCount->dec();
186  m_pRefCount = NULL;
187  m_pObject = NULL;
188  }
189  }
190 
191  template <class T>
192  template <class T2>
194  {
195  shared_ptr(rSharedPointer).swap(*this);
196 
197  return *this;
198  }
199 
200  template <class T>
202  {
203  shared_ptr(rSharedPointer).swap(*this);
204 
205  return *this;
206  }
207 
208  template <class T>
210  {
211  shared_ptr().swap(*this);
212  }
213 
214  template <class T>
215  template <class T2>
216  void shared_ptr<T>::reset(T2 *pObject)
217  {
218  shared_ptr(pObject).swap(*this);
219  }
220 
221  template <class T>
223  {
224  return m_pObject;
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  if(NULL == m_pRefCount)
243  {
244  return 0;
245  }
246 
247  return m_pRefCount->use_count();
248  }
249 
250  template <class T>
252  {
253  return (use_count() == 1);
254  }
255 
256  template <class T>
257  void shared_ptr<T>::swap(shared_ptr &rSharedPointer)
258  {
259  swap(m_pObject, rSharedPointer.m_pObject);
260  swap(m_pRefCount, rSharedPointer.m_pRefCount);
261  }
262 
263  template<class T, class T2>
265  {
266  return shared_ptr<T>(rSharedPointer, dynamic_cast_tag());
267  }
268 
269  template <class T1, class T2>
270  bool operator==(const shared_ptr<T1>& sp1, const shared_ptr<T2>& sp2)
271  {
272  return sp1.get() == sp2.get();
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 }} //namespace AVT::VmbAPI
282 
283 #endif //AVT_VMBAPI_SHAREDPOINTER_IMPL_H
virtual long use_count() const
ref_count & operator=(const ref_count &rRefCount)
IMEXPORT void Unlock()
Definition: Mutex.cpp:88
AVT::VmbAPI::ref_count_base * m_pRefCount
Definition: SharedPointer.h:83
IMEXPORT void Lock()
Definition: Mutex.cpp:79
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
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): Miquel Massot , Allied Vision Technologies
autogenerated on Mon Jun 10 2019 12:50:39