Pointer.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. //-----------------------------------------------------------------------------
  2. // (c) 2006 by Basler Vision Technologies
  3. // Section: Vision Components
  4. // Project: GenApi
  5. // Author: Fritz Dierks
  6. // $Header$
  7. //
  8. // License: This file is published under the license of the EMVA GenICam Standard Group.
  9. // A text file describing the legal terms is included in your installation as 'GenICam_license.pdf'.
  10. // If for some reason you are missing this file please contact the EMVA or visit the website
  11. // (http://www.genicam.org) for a full copy.
  12. //
  13. // THIS SOFTWARE IS PROVIDED BY THE EMVA GENICAM STANDARD GROUP "AS IS"
  14. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE EMVA GENICAM STANDARD GROUP
  17. // OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  18. // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  19. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  20. // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  21. // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  23. // POSSIBILITY OF SUCH DAMAGE.
  24. //-----------------------------------------------------------------------------
  25. /*!
  26. \file
  27. \brief Definition of template CPointer
  28. \ingroup GenApi_PublicUtilities
  29. */
  30. #ifndef GENAPI_POINTER_H
  31. #define GENAPI_POINTER_H
  32. #include <assert.h>
  33. #include <GenICamFwd.h>
  34. #include <GenApi/IEnumeration.h>
  35. #include <GenApi/IFloat.h>
  36. #include <GenApi/IInteger.h>
  37. namespace GENAPI_NAMESPACE
  38. {
  39. //*************************************************************
  40. // CPointer class
  41. //*************************************************************
  42. /**
  43. \brief Encapsulates a GenApi pointer dealing with the dynamic_cast automatically
  44. \ingroup GenApi_PublicUtilities
  45. */
  46. template <class T, class B = IBase>
  47. class CPointer
  48. {
  49. public:
  50. //! Default constructor.
  51. CPointer(void) throw()
  52. : m_pT( NULL )
  53. {
  54. }
  55. //! Constructor from INode pointer type.
  56. CPointer( B *pB )
  57. : m_pT( dynamic_cast<T*>(pB) )
  58. {
  59. }
  60. virtual ~CPointer(void)
  61. {
  62. }
  63. //! Assign INode Pointer
  64. void operator=( B *pB )
  65. {
  66. m_pT = dynamic_cast<T*>(pB);
  67. }
  68. //! Dereferencing
  69. operator T*(void) const
  70. {
  71. if (NULL == m_pT)
  72. throw LOGICAL_ERROR_EXCEPTION( "NULL pointer dereferenced" );
  73. return m_pT;
  74. }
  75. //! Dereferencing
  76. T& operator*(void) const
  77. {
  78. if (NULL == m_pT)
  79. throw LOGICAL_ERROR_EXCEPTION( "NULL pointer dereferenced" );
  80. return *m_pT;
  81. }
  82. //! Dereferencing
  83. T& operator()(void) const
  84. {
  85. if (NULL == m_pT)
  86. throw LOGICAL_ERROR_EXCEPTION( "NULL pointer dereferenced" );
  87. return *m_pT;
  88. }
  89. //! Dereferencing
  90. T* operator->(void) const
  91. {
  92. if (NULL == m_pT)
  93. throw LOGICAL_ERROR_EXCEPTION( "NULL pointer dereferenced" );
  94. return m_pT;
  95. }
  96. //! true if the pointer is valid
  97. bool IsValid() const throw()
  98. {
  99. return m_pT != NULL;
  100. }
  101. //! true if the pointer is valid
  102. operator bool(void) const throw()
  103. {
  104. return m_pT != NULL;
  105. }
  106. //! pointer equal
  107. bool operator==(T* pT) const
  108. {
  109. return m_pT == pT;
  110. }
  111. //! pointer equal
  112. bool operator==(const CPointer<T,B> &rT) const
  113. {
  114. return m_pT == rT.m_pT;
  115. }
  116. //! pointer equal
  117. bool operator==(int nMustBeNull) const
  118. {
  119. if (0 != nMustBeNull)
  120. throw LOGICAL_ERROR_EXCEPTION( "argument must be NULL" );
  121. return NULL == m_pT;
  122. }
  123. protected:
  124. //! Underlying raw pointer.
  125. T* m_pT;
  126. };
  127. //*************************************************************
  128. // Smartpointer for all interface
  129. //*************************************************************
  130. //! \addtogroup GenApi_PublicUtilities
  131. //! \{
  132. //! SmartPointer for IBase interface pointer
  133. typedef CPointer<IBase> CBasePtr;
  134. //! SmartPointer for INode interface pointer
  135. typedef CPointer<INode> CNodePtr;
  136. //! SmartPointer for IValue interface pointer
  137. typedef CPointer<IValue> CValuePtr;
  138. //! SmartPointer for ICategory interface pointer
  139. typedef CPointer<ICategory> CCategoryPtr;
  140. //! SmartPointer for IBoolean interface pointer
  141. typedef CPointer<IBoolean> CBooleanPtr;
  142. //! SmartPointer for IInteger interface pointer
  143. typedef CPointer<IInteger> CIntegerPtr;
  144. //! SmartPointer for IString interface pointer
  145. typedef CPointer<IString> CStringPtr;
  146. //! SmartPointer for IRegister interface pointer
  147. typedef CPointer<IRegister> CRegisterPtr;
  148. //! SmartPointer for IEnumeration interface pointer
  149. typedef CPointer<IEnumeration> CEnumerationPtr;
  150. //! SmartPointer for IEnumEntry interface pointer
  151. typedef CPointer<IEnumEntry> CEnumEntryPtr;
  152. //! SmartPointer for IPort interface pointer
  153. typedef CPointer<IPort> CPortPtr;
  154. //! SmartPointer for IPortReplay interface pointer
  155. typedef CPointer<IPortReplay> CPortReplayPtr;
  156. //! SmartPointer for IPortRecorder interface pointer
  157. typedef CPointer<IPortRecorder> CPortRecorderPtr;
  158. //! SmartPointer for IPortWriteList interface pointer
  159. typedef CPointer<IPortWriteList, IPortWriteList> CPortWriteListPtr;
  160. //! SmartPointer for IChunkPort interface pointer
  161. typedef CPointer<IChunkPort> CChunkPortPtr;
  162. //! SmartPointer for INodeMap interface pointer
  163. typedef CPointer<INodeMap, INodeMap> CNodeMapPtr;
  164. //! SmartPointer for IDeviceInfo interface pointer
  165. typedef CPointer<IDeviceInfo, INodeMap> CDeviceInfoPtr;
  166. //! SmartPointer for IUserData interface pointer
  167. typedef CPointer<IUserData, INodeMap> CNodeMapUserDataPtr;
  168. //! SmartPointer for IUserData interface pointer
  169. typedef CPointer<IUserData> CNodeUserDataPtr;
  170. //! SmartPointer for ISelector interface pointer
  171. typedef CPointer<ISelector> CSelectorPtr;
  172. //! SmartPointer for ICommand interface pointer
  173. typedef CPointer<ICommand> CCommandPtr;
  174. //! SmartPointer for IFloat interface pointer
  175. class CFloatPtr : public CPointer<IFloat, IBase>
  176. {
  177. public:
  178. //! Default constructor.
  179. CFloatPtr() throw()
  180. : CPointer<IFloat, IBase>( )
  181. {
  182. }
  183. //! Constructor from IBase pointer type.
  184. CFloatPtr( IBase *pB )
  185. : CPointer<IFloat, IBase>( pB )
  186. {
  187. }
  188. //! Assign IBase Pointer
  189. void operator=( IBase *pB )
  190. {
  191. CPointer<IFloat, IBase>::operator =(pB);
  192. }
  193. //! gets the interface of an integer alias node.
  194. IInteger *GetIntAlias()
  195. {
  196. return dynamic_cast<IInteger*>(m_pT->GetNode()->GetCastAlias());
  197. }
  198. //! gets the interface of an enum alias node.
  199. IEnumeration *GetEnumAlias()
  200. {
  201. return dynamic_cast<IEnumeration*>(m_pT->GetNode()->GetCastAlias());
  202. }
  203. };
  204. //! \}
  205. //! \addtogroup GenApi_PublicImpl
  206. //! \{
  207. //! SmartPointer for IPortConstruct interface pointer
  208. typedef CPointer<IPortConstruct> CPortConstructPtr;
  209. //! Returns the name of the main interface as string
  210. //! DEPRECATED, use IBase::GetPrincipalInterfaceType() instead
  211. inline GENICAM_NAMESPACE::gcstring GetInterfaceName(IBase *pBase)
  212. {
  213. # ifdef _MSC_VER
  214. # pragma warning (push) // icc -W4 complains: controlling expression is constant
  215. # pragma warning (disable : 279)
  216. # endif
  217. assert(pBase && "don't call this with a NULL pointer");
  218. # ifdef _MSC_VER
  219. # pragma warning (pop)
  220. # endif
  221. CNodePtr ptrNode(pBase);
  222. switch(ptrNode->GetPrincipalInterfaceType())
  223. {
  224. case intfIValue:
  225. return GENICAM_NAMESPACE::gcstring("IValue");
  226. case intfIInteger:
  227. return GENICAM_NAMESPACE::gcstring("IInteger");
  228. case intfIBoolean:
  229. return GENICAM_NAMESPACE::gcstring("IBoolean");
  230. case intfICommand:
  231. return GENICAM_NAMESPACE::gcstring("ICommand");
  232. case intfIFloat:
  233. return GENICAM_NAMESPACE::gcstring("IFloat");
  234. case intfIString:
  235. return GENICAM_NAMESPACE::gcstring("IString");
  236. case intfIRegister:
  237. return GENICAM_NAMESPACE::gcstring("IRegister");
  238. case intfICategory:
  239. return GENICAM_NAMESPACE::gcstring("ICategory");
  240. case intfIEnumeration:
  241. return GENICAM_NAMESPACE::gcstring("IEnumeration");
  242. case intfIEnumEntry:
  243. return GENICAM_NAMESPACE::gcstring("IEnumEntry");
  244. case intfIPort:
  245. return GENICAM_NAMESPACE::gcstring("IPort");
  246. // Do not use this pragma in public header files (warnings in depend projects): #pragma BullseyeCoverage off
  247. case intfIBase:
  248. default:
  249. return GENICAM_NAMESPACE::gcstring("IBase");
  250. // Do not use this pragma in public header files (warnings in depend projects): #pragma BullseyeCoverage on
  251. }
  252. }
  253. //! Checks if a node is readable
  254. template <class T, class B>
  255. inline bool IsReadable( const CPointer<T, B>& ptr)
  256. {
  257. return ptr.IsValid() && IsReadable( ptr->GetAccessMode() );
  258. }
  259. //! Checks if a node is Writable
  260. template <class T, class B>
  261. inline bool IsWritable( const CPointer<T, B>& ptr)
  262. {
  263. return ptr.IsValid() && IsWritable( ptr->GetAccessMode() );
  264. }
  265. //! Checks if a node is Implemented
  266. template <class T, class B>
  267. inline bool IsImplemented( const CPointer<T, B>& ptr)
  268. {
  269. return ptr.IsValid() && IsImplemented( ptr->GetAccessMode() );
  270. }
  271. //! Checks if a node is Available
  272. template <class T, class B>
  273. inline bool IsAvailable( const CPointer<T, B>& ptr)
  274. {
  275. return ptr.IsValid() && IsAvailable( ptr->GetAccessMode() );
  276. }
  277. //! \}
  278. }
  279. #endif // ifndef GENAPI_POINTER_H