DeviceSpecificGrabResultPtr.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. //-----------------------------------------------------------------------------
  2. // Basler pylon SDK
  3. // Copyright (c) 2010-2021 Basler AG
  4. // http://www.baslerweb.com
  5. // Author: Andreas Gau
  6. //-----------------------------------------------------------------------------
  7. /*!
  8. \file
  9. \brief Contains a smart pointer that holds a reference to device specific grab result data.
  10. */
  11. #ifndef INCLUDED_DEVICESPECIFICGRABRESULTPTR_H_9329951
  12. #define INCLUDED_DEVICESPECIFICGRABRESULTPTR_H_9329951
  13. #include <pylon/GrabResultPtr.h>
  14. #define PYLON_DEFINE_DEVICE_SPECIFIC_GRAB_RESULT_PTR(ClassName, ResultDataName) \
  15. class ClassName : public CDeviceSpecificGrabResultPtr<ResultDataName> \
  16. { \
  17. public: \
  18. /*!
  19. \copybrief Pylon::CGrabResultPtr::CGrabResultPtr()
  20. \copydetails Pylon::CGrabResultPtr::CGrabResultPtr()
  21. */ \
  22. ClassName() \
  23. { \
  24. } \
  25. /*!
  26. \copybrief Pylon::CGrabResultPtr::CGrabResultPtr( const CGrabResultPtr& rhs)
  27. \copydetails Pylon::CGrabResultPtr::CGrabResultPtr( const CGrabResultPtr& rhs)
  28. */ \
  29. ClassName( const CGrabResultPtr& rhs ) \
  30. : CDeviceSpecificGrabResultPtr<ResultDataName>( rhs ) \
  31. { \
  32. } \
  33. /*!
  34. \copybrief Pylon::CGrabResultPtr::CGrabResultPtr( const CGrabResultPtr& rhs)
  35. \copydetails Pylon::CGrabResultPtr::CGrabResultPtr( const CGrabResultPtr& rhs)
  36. */ \
  37. ClassName( const ClassName& rhs ) \
  38. : CDeviceSpecificGrabResultPtr<ResultDataName>( rhs ) \
  39. { \
  40. } \
  41. /*!
  42. \copybrief Pylon::CGrabResultPtr::~CGrabResultPtr
  43. \copydetails Pylon::CGrabResultPtr::~CGrabResultPtr
  44. */ \
  45. virtual ~ClassName() \
  46. { \
  47. } \
  48. };
  49. namespace Pylon
  50. {
  51. namespace PylonPrivate
  52. {
  53. // Internal use only. This is subject to change without notice.
  54. class CGrabResultDataConverter
  55. {
  56. public:
  57. // Internal use only. This is subject to change without notice.
  58. template <typename GrabResultDataT>
  59. static void Convert( GrabResultDataT*& ptr, CGrabResultPtr& target, const CGrabResultPtr& source )
  60. {
  61. ptr = dynamic_cast<GrabResultDataT*>(source.GetPtr());
  62. if (ptr)
  63. {
  64. target = source;
  65. }
  66. }
  67. };
  68. }
  69. /*!
  70. \brief Implementation Detail: Header only implementation class for creating device specific grab result ptrs.
  71. \copydetails Pylon::CGrabResultPtr
  72. */
  73. template <typename GrabResultDataT>
  74. class CDeviceSpecificGrabResultPtr
  75. {
  76. public:
  77. /*!
  78. \copybrief Pylon::CGrabResultPtr::CGrabResultPtr
  79. \copydetails Pylon::CGrabResultPtr::CGrabResultPtr
  80. */
  81. CDeviceSpecificGrabResultPtr();
  82. /*!
  83. \brief Creates a copy of a smart pointer.
  84. \param[in] rhs Another smart pointer, source of the result data to reference.
  85. The data itself is not copied.
  86. \post
  87. <ul>
  88. <li>Another reference to the grab result of the source is held if it references a grab result.
  89. <li>No grab result is referenced if the source does not reference a grab result or if the result data is not convertible.
  90. </ul>
  91. \error
  92. Still valid after error.
  93. */
  94. CDeviceSpecificGrabResultPtr( const CGrabResultPtr& rhs );
  95. /*!
  96. \brief Assignment of a smart pointer.
  97. \param[in] rhs Another smart pointer, source of the result data to reference.
  98. The data itself is not copied.
  99. \post
  100. <ul>
  101. <li>The currently referenced data is released.
  102. <li>Another reference to the grab result of the source is held if it references a grab result.
  103. <li>No grab result is referenced if the source does not reference a grab result or if the result data is not convertible.
  104. </ul>
  105. \error
  106. Still valid after error.
  107. */
  108. CDeviceSpecificGrabResultPtr<GrabResultDataT>& operator = ( const CGrabResultPtr& rhs );
  109. /*!
  110. \copybrief Pylon::CGrabResultPtr::CGrabResultPtr( const CGrabResultPtr& rhs)
  111. \copydetails Pylon::CGrabResultPtr::CGrabResultPtr( const CGrabResultPtr& rhs)
  112. */
  113. CDeviceSpecificGrabResultPtr( const CDeviceSpecificGrabResultPtr<GrabResultDataT>& rhs );
  114. /*!
  115. \copybrief Pylon::CGrabResultPtr::operator=( const CGrabResultPtr& rhs)
  116. \copydetails Pylon::CGrabResultPtr::operator=( const CGrabResultPtr& rhs)
  117. */
  118. CDeviceSpecificGrabResultPtr<GrabResultDataT>& operator = ( const CDeviceSpecificGrabResultPtr<GrabResultDataT>& rhs );
  119. /*!
  120. \copybrief Pylon::CGrabResultPtr::~CGrabResultPtr
  121. \copydetails Pylon::CGrabResultPtr::~CGrabResultPtr
  122. */
  123. virtual ~CDeviceSpecificGrabResultPtr();
  124. /*!
  125. \copybrief Pylon::CGrabResultPtr::operator->()
  126. \copydetails Pylon::CGrabResultPtr::operator->()
  127. */
  128. GrabResultDataT* operator->() const;
  129. /*!
  130. \copybrief Pylon::CGrabResultPtr::IsValid
  131. \copydetails Pylon::CGrabResultPtr::IsValid
  132. */
  133. bool IsValid() const;
  134. /*!
  135. \brief Check whether data is referenced.
  136. \return IsValid().
  137. The overloaded bool operator can be used to check whether data is referenced.
  138. Example:
  139. \code
  140. if ( grabResult)
  141. {
  142. grabResult->GetStatus();
  143. }
  144. \endcode
  145. \error
  146. Does not throw C++ exceptions.
  147. */
  148. operator bool() const;
  149. /*!
  150. */
  151. operator CGrabResultPtr() const;
  152. /*!
  153. \brief Provides an IImage interface to the grab result.
  154. This cast operator allows passing the grab result to saving functions or image format converter.
  155. The returned image is invalid if the grab was not successful. See CGrabResultData::GrabSucceeded().
  156. \attention The returned reference is only valid as long the grab result ptr is not destroyed.
  157. \error
  158. Still valid after error.
  159. */
  160. operator IImage& () const;
  161. /*!
  162. \brief Indicates that the held grab result data and buffer are only referenced by this grab result.
  163. \return Returns true if the held grab result data and buffer are only referenced by this grab result. Returns false if the grab result is invalid.
  164. \error
  165. Does not throw C++ exceptions.
  166. */
  167. bool IsUnique() const;
  168. /*!
  169. \copybrief Pylon::CGrabResultPtr::Release
  170. \copydetails Pylon::CGrabResultPtr::Release
  171. */
  172. void Release();
  173. private:
  174. GrabResultDataT* m_pGrabResultData;
  175. CGrabResultPtr m_gptr;
  176. };
  177. // Implementation ----------------------------------------------------------
  178. template <typename GrabResultDataT>
  179. CDeviceSpecificGrabResultPtr<GrabResultDataT>::CDeviceSpecificGrabResultPtr()
  180. : m_pGrabResultData( 0 )
  181. {
  182. }
  183. template <typename GrabResultDataT>
  184. CDeviceSpecificGrabResultPtr<GrabResultDataT>::CDeviceSpecificGrabResultPtr( const CGrabResultPtr& rhs )
  185. {
  186. PylonPrivate::CGrabResultDataConverter::Convert<GrabResultDataT>( m_pGrabResultData, m_gptr, rhs );
  187. }
  188. template <typename GrabResultDataT>
  189. CDeviceSpecificGrabResultPtr<GrabResultDataT>& CDeviceSpecificGrabResultPtr<GrabResultDataT>::operator = ( const CGrabResultPtr& rhs )
  190. {
  191. PylonPrivate::CGrabResultDataConverter::Convert<GrabResultDataT>( m_pGrabResultData, m_gptr, rhs );
  192. return *this;
  193. }
  194. template <typename GrabResultDataT>
  195. CDeviceSpecificGrabResultPtr<GrabResultDataT>::CDeviceSpecificGrabResultPtr( const CDeviceSpecificGrabResultPtr<GrabResultDataT>& rhs )
  196. : m_pGrabResultData( rhs.m_pGrabResultData )
  197. , m_gptr( rhs.m_gptr )
  198. {
  199. }
  200. template <typename GrabResultDataT>
  201. CDeviceSpecificGrabResultPtr<GrabResultDataT>& CDeviceSpecificGrabResultPtr<GrabResultDataT>::operator = ( const CDeviceSpecificGrabResultPtr<GrabResultDataT>& rhs )
  202. {
  203. if (this != &rhs) // self-assignment check
  204. {
  205. m_pGrabResultData = rhs.m_pGrabResultData;
  206. m_gptr = rhs.m_gptr;
  207. }
  208. return *this;
  209. }
  210. template <typename GrabResultDataT>
  211. CDeviceSpecificGrabResultPtr<GrabResultDataT>::~CDeviceSpecificGrabResultPtr()
  212. {
  213. }
  214. template <typename GrabResultDataT>
  215. GrabResultDataT* CDeviceSpecificGrabResultPtr<GrabResultDataT>::operator->() const
  216. {
  217. if (!IsValid())
  218. {
  219. throw RUNTIME_EXCEPTION( "No grab result data is referenced. Cannot access NULL pointer." );
  220. }
  221. return m_pGrabResultData;
  222. }
  223. template <typename GrabResultDataT>
  224. bool CDeviceSpecificGrabResultPtr<GrabResultDataT>::IsValid() const
  225. {
  226. return m_pGrabResultData != NULL;
  227. }
  228. template <typename GrabResultDataT>
  229. CDeviceSpecificGrabResultPtr<GrabResultDataT>::operator bool() const
  230. {
  231. return IsValid();
  232. }
  233. template <typename GrabResultDataT>
  234. CDeviceSpecificGrabResultPtr<GrabResultDataT>::operator CGrabResultPtr() const
  235. {
  236. return m_gptr;
  237. }
  238. template <typename GrabResultDataT>
  239. CDeviceSpecificGrabResultPtr<GrabResultDataT>::operator IImage& () const
  240. {
  241. return m_gptr;
  242. }
  243. template <typename GrabResultDataT>
  244. bool CDeviceSpecificGrabResultPtr<GrabResultDataT>::IsUnique() const
  245. {
  246. return m_gptr.IsUnique();
  247. }
  248. template <typename GrabResultDataT>
  249. void CDeviceSpecificGrabResultPtr<GrabResultDataT>::Release()
  250. {
  251. m_pGrabResultData = NULL;
  252. m_gptr.Release();
  253. }
  254. }
  255. #endif /* INCLUDED_DEVICESPECIFICGRABRESULTPTR_H_9329951 */