NodeMapRef.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  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 CNodeMapRef
  28. \ingroup GenApi_PublicInterface
  29. */
  30. #ifndef GENAPI_NODEMAPREF_H
  31. #define GENAPI_NODEMAPREF_H
  32. #include <GenICamVersion.h>
  33. #include <Base/GCString.h>
  34. #include <Base/GCException.h>
  35. #include <GenApi/Pointer.h>
  36. #include <GenApi/INodeMap.h>
  37. #include <GenApi/IDestroy.h>
  38. #include <GenApi/NodeMapFactory.h>
  39. #include <cstdlib>
  40. namespace GENAPI_NAMESPACE
  41. {
  42. # ifdef _WIN32
  43. // see below in the Linux branch
  44. inline IDestroy *CastToIDestroy(INodeMap *pNodeMap)
  45. {
  46. return dynamic_cast<IDestroy *>(pNodeMap);
  47. }
  48. # else
  49. //! makes sure the dynamic_cast operator is implemented in the DLL (due to a Linux bug)
  50. GENAPI_DECL IDestroy *CastToIDestroy(INodeMap *pNodeMap);
  51. # endif
  52. /**
  53. \brief Smartpointer template for NodeMaps with create function
  54. \ingroup GenApi_PublicInterface
  55. \tparam TCameraParams The camera specific parameter class (auto generated from camera xml file)
  56. */
  57. template<class TCameraParams>
  58. class CNodeMapRefT : public TCameraParams
  59. {
  60. public:
  61. //! Constructor
  62. CNodeMapRefT(const GENICAM_NAMESPACE::gcstring &DeviceName = "Device");
  63. //! Constructor
  64. CNodeMapRefT(INodeMap* pNodeMap, const GENICAM_NAMESPACE::gcstring &DeviceName = "Device");
  65. //! Copy constructor
  66. CNodeMapRefT(const CNodeMapRefT& Them);
  67. //! Assignment
  68. CNodeMapRefT& operator=(const CNodeMapRefT& Them);
  69. //! Assignment of an INodeMap*
  70. CNodeMapRefT& operator=(INodeMap* pNodeMap);
  71. //! Destructor
  72. virtual ~CNodeMapRefT();
  73. //! Destroys the node map
  74. void _Destroy();
  75. //! Creates the object from a XML file with given file name
  76. void _LoadXMLFromFile(const GENICAM_NAMESPACE::gcstring &FileName);
  77. //! Creates the object from a ZIP'd XML file with given file name
  78. void _LoadXMLFromZIPFile(const GENICAM_NAMESPACE::gcstring &ZipFileName);
  79. //! Creates the object from a ZIP'd XML file given in a string
  80. void _LoadXMLFromZIPData(const void* zipData, size_t zipSize);
  81. //! Creates the object from a XML target and an inject file with given file name
  82. void _LoadXMLFromFileInject(const GENICAM_NAMESPACE::gcstring &TargetFileName, const GENICAM_NAMESPACE::gcstring &InjectFileName);
  83. //! Creates the object from XML data given in a string
  84. void _LoadXMLFromString(const GENICAM_NAMESPACE::gcstring& XMLData);
  85. //! Creates the object from XML data given in a string with injection
  86. void _LoadXMLFromStringInject(const GENICAM_NAMESPACE::gcstring& TargetXMLDataconst, const GENICAM_NAMESPACE::gcstring& InjectXMLData);
  87. //! Gets a list of supported schema versions
  88. /*! Each list entry is a string with the format "{Major}.{Minor}" were {Major} and {Minor} are integers
  89. Example: {"1.1", "1.2"} indicates that the schema v1.1 and v1.2 are supported.
  90. The SubMinor version number is not given since it is for fully compatible bug fixes only
  91. */
  92. virtual void _GetSupportedSchemaVersions(GENICAM_NAMESPACE::gcstring_vector &SchemaVersions) const;
  93. //! Get device name
  94. virtual GENICAM_NAMESPACE::gcstring _GetDeviceName() const;
  95. //! Fires nodes which have a polling time
  96. virtual void _Poll(int64_t ElapsedTime);
  97. //! Clears the cache of the camera description files
  98. static bool _ClearXMLCache();
  99. //----------------------------------------------------------------
  100. // INodeMap
  101. //----------------------------------------------------------------
  102. //! Retrieves all nodes in the node map
  103. virtual void _GetNodes(NodeList_t &Nodes) const;
  104. //! Retrieves the node from the central map by name
  105. virtual INode* _GetNode(const GENICAM_NAMESPACE::gcstring& key) const;
  106. //! Invalidates all nodes
  107. virtual void _InvalidateNodes() const;
  108. //! Connects a port to a port node with given name
  109. virtual bool _Connect(IPort* pPort, const GENICAM_NAMESPACE::gcstring& PortName) const;
  110. //! Connects a port to the standard port "Device"
  111. virtual bool _Connect(IPort* pPort) const;
  112. //! Pointer to the NodeMap
  113. INodeMap *_Ptr;
  114. private:
  115. //! The name of this device
  116. GENICAM_NAMESPACE::gcstring _DeviceName;
  117. //ATTENTION: not thread safe
  118. int* _pRefCount;
  119. void Release();
  120. void Attach(INodeMap *pNodeMap, const GENICAM_NAMESPACE::gcstring &DeviceName, int* pRefCount);
  121. };
  122. template<class TCameraParams>
  123. inline CNodeMapRefT<TCameraParams>::CNodeMapRefT(const GENICAM_NAMESPACE::gcstring &DeviceName)
  124. : _Ptr(NULL)
  125. , _DeviceName(DeviceName)
  126. , _pRefCount(NULL)
  127. {
  128. }
  129. template<class TCameraParams>
  130. inline CNodeMapRefT<TCameraParams>::CNodeMapRefT(INodeMap *pNodeMap, const GENICAM_NAMESPACE::gcstring &DeviceName)
  131. : _Ptr(NULL)
  132. , _DeviceName(DeviceName)
  133. , _pRefCount(NULL)
  134. {
  135. assert(pNodeMap);
  136. Attach(pNodeMap, DeviceName, pNodeMap ? new int(0) : NULL);
  137. }
  138. template<class TCameraParams>
  139. GENAPI_NAMESPACE::CNodeMapRefT<TCameraParams>::CNodeMapRefT(const CNodeMapRefT& Them)
  140. : TCameraParams()
  141. , _Ptr(NULL)
  142. , _DeviceName()
  143. , _pRefCount(NULL)
  144. {
  145. Attach(Them._Ptr, Them._DeviceName, Them._pRefCount);
  146. }
  147. //ATTENTION: not thread safe
  148. template<class TCameraParams>
  149. void GENAPI_NAMESPACE::CNodeMapRefT<TCameraParams>::Attach(INodeMap *pNodeMap, const GENICAM_NAMESPACE::gcstring &DeviceName, int* pRefCount)
  150. {
  151. // Must be empty
  152. assert(_Ptr == NULL);
  153. assert(_pRefCount == NULL);
  154. //always copy device name
  155. if (&_DeviceName != &DeviceName) //if not assigning member itself
  156. {
  157. _DeviceName = DeviceName;
  158. }
  159. // Attach
  160. if (pNodeMap)
  161. {
  162. assert(pRefCount);
  163. if (pRefCount)
  164. {
  165. ++*pRefCount;
  166. //assign new node map data
  167. _Ptr = pNodeMap;
  168. _pRefCount = pRefCount;
  169. // Initialize the references
  170. TCameraParams::_Initialize(_Ptr);
  171. }
  172. }
  173. }
  174. //ATTENTION: not thread safe
  175. template<class TCameraParams>
  176. void GENAPI_NAMESPACE::CNodeMapRefT<TCameraParams>::Release()
  177. {
  178. if (_Ptr)
  179. {
  180. // Copy node map data for eventual later destruction
  181. INodeMap* pToDel = _Ptr;
  182. int* pRefCount = _pRefCount;
  183. // Clear
  184. _pRefCount = NULL;
  185. _Ptr = NULL;
  186. _DeviceName = "Device";
  187. assert(pRefCount);
  188. // Check if destruction is required
  189. if (pRefCount)
  190. {
  191. assert(*pRefCount > 0);
  192. --*pRefCount;
  193. if (*pRefCount == 0)
  194. {
  195. // We do not need this anymore, all references are gone
  196. delete pRefCount;
  197. pRefCount = NULL;
  198. // Destroy the node map finally
  199. GENAPI_NAMESPACE::IDestroy *pDestroy = CastToIDestroy(pToDel);
  200. assert(pDestroy);
  201. pDestroy->Destroy(); //must not throw
  202. }
  203. }
  204. }
  205. else
  206. {
  207. // Must not have a refcount when no node map is there.
  208. assert(_pRefCount == NULL);
  209. }
  210. }
  211. template<class TCameraParams>
  212. CNodeMapRefT<TCameraParams>& GENAPI_NAMESPACE::CNodeMapRefT<TCameraParams>::operator=(INodeMap* pNodeMap)
  213. {
  214. Release();
  215. assert(pNodeMap);
  216. if (pNodeMap)
  217. {
  218. Attach(pNodeMap, pNodeMap->GetDeviceName(), new int(0));
  219. }
  220. return *this;
  221. }
  222. template<class TCameraParams>
  223. CNodeMapRefT<TCameraParams>& GENAPI_NAMESPACE::CNodeMapRefT<TCameraParams>::operator=(const CNodeMapRefT& Them)
  224. {
  225. Release();
  226. Attach(Them._Ptr, Them._DeviceName, Them._pRefCount);
  227. return *this;
  228. }
  229. template<class TCameraParams>
  230. inline CNodeMapRefT<TCameraParams>::~CNodeMapRefT()
  231. {
  232. Release();
  233. }
  234. template<class TCameraParams>
  235. inline void CNodeMapRefT<TCameraParams>::_Destroy()
  236. {
  237. Release();
  238. }
  239. template<class TCameraParams>
  240. inline void CNodeMapRefT<TCameraParams>::_LoadXMLFromFile(const GENICAM_NAMESPACE::gcstring &FileName)
  241. {
  242. // FileName environment is replaced in CNodeMapFactory ctor
  243. // Load the DLL
  244. if(_Ptr)
  245. throw RUNTIME_EXCEPTION("Node map already created");
  246. // Load the XML file
  247. CNodeMapFactory nodeMapData(ContentType_Xml, FileName);
  248. Attach(nodeMapData.CreateNodeMap(_DeviceName), _DeviceName, new int(0));
  249. }
  250. template<class TCameraParams>
  251. inline void CNodeMapRefT<TCameraParams>::_LoadXMLFromZIPFile(const GENICAM_NAMESPACE::gcstring &ZipFileName)
  252. {
  253. // FileName environment is replaced in CNodeMapFactory ctor
  254. // Load the DLL
  255. if(_Ptr)
  256. throw RUNTIME_EXCEPTION("Node map already created");
  257. // Load the XML file
  258. CNodeMapFactory nodeMapData(ContentType_ZippedXml, ZipFileName);
  259. Attach(nodeMapData.CreateNodeMap(), _DeviceName, new int(0));
  260. }
  261. template<class TCameraParams>
  262. inline void CNodeMapRefT<TCameraParams>::_LoadXMLFromFileInject(const GENICAM_NAMESPACE::gcstring &TargetFileName, const GENICAM_NAMESPACE::gcstring &InjectFileName)
  263. {
  264. // xxxFileName environment is replaced in CNodeMapFactory ctor
  265. // Load the DLL
  266. if(_Ptr)
  267. throw RUNTIME_EXCEPTION("Node map already created");
  268. // Load the XML file
  269. CNodeMapFactory nodeMapData(ContentType_Xml, TargetFileName);
  270. CNodeMapFactory injectNodeMapData(ContentType_Xml, InjectFileName);
  271. nodeMapData.AddInjectionData(injectNodeMapData);
  272. Attach(nodeMapData.CreateNodeMap(), _DeviceName, new int(0));
  273. }
  274. template<class TCameraParams>
  275. inline void CNodeMapRefT<TCameraParams>::_LoadXMLFromString(const GENICAM_NAMESPACE::gcstring& XMLData)
  276. {
  277. // Load the DLL
  278. if(_Ptr)
  279. throw RUNTIME_EXCEPTION("Node map already created");
  280. // Load the XML file
  281. CNodeMapFactory nodeMapData(ContentType_Xml, XMLData.c_str(), XMLData.size()); //avoid string copy
  282. Attach(nodeMapData.CreateNodeMap(_DeviceName), _DeviceName, new int(0));
  283. }
  284. template<class TCameraParams>
  285. inline void CNodeMapRefT<TCameraParams>::_LoadXMLFromZIPData(const void* zipData, size_t zipSize)
  286. {
  287. // Load the DLL
  288. if(_Ptr)
  289. throw RUNTIME_EXCEPTION("Node map already created");
  290. // Load the XML file
  291. CNodeMapFactory nodeMapData(ContentType_ZippedXml, zipData, zipSize);
  292. Attach(nodeMapData.CreateNodeMap(), _DeviceName, new int(0));
  293. }
  294. template<class TCameraParams>
  295. inline void CNodeMapRefT<TCameraParams>::_LoadXMLFromStringInject(const GENICAM_NAMESPACE::gcstring& TargetXMLData, const GENICAM_NAMESPACE::gcstring& InjectXMLData)
  296. {
  297. // Load the DLL
  298. if(_Ptr)
  299. throw RUNTIME_EXCEPTION("Node map already created");
  300. // Load the XML file
  301. CNodeMapFactory nodeMapData(ContentType_Xml, TargetXMLData.c_str(), TargetXMLData.size()); //avoid string copy
  302. CNodeMapFactory injectNodeMapData(ContentType_Xml, InjectXMLData.c_str(), InjectXMLData.size()); //avoid string copy
  303. nodeMapData.AddInjectionData(injectNodeMapData);
  304. Attach(nodeMapData.CreateNodeMap(), _DeviceName, new int(0));
  305. }
  306. template<class TCameraParams>
  307. inline void CNodeMapRefT<TCameraParams>::_GetSupportedSchemaVersions(GENICAM_NAMESPACE::gcstring_vector &SchemaVersions) const
  308. {
  309. CNodeMapFactory().GetSupportedSchemaVersions(SchemaVersions);
  310. }
  311. template<class TCameraParams>
  312. inline GENICAM_NAMESPACE::gcstring CNodeMapRefT<TCameraParams>::_GetDeviceName() const
  313. {
  314. if(_Ptr)
  315. return _Ptr->GetDeviceName();
  316. else
  317. throw ACCESS_EXCEPTION("Feature not present (reference not valid)");
  318. }
  319. template<class TCameraParams>
  320. inline void CNodeMapRefT<TCameraParams>::_Poll(int64_t ElapsedTime)
  321. {
  322. if(_Ptr)
  323. return _Ptr->Poll(ElapsedTime);
  324. else
  325. throw ACCESS_EXCEPTION("Feature not present (reference not valid)");
  326. }
  327. template<class TCameraParams>
  328. inline void CNodeMapRefT<TCameraParams>::_GetNodes(NodeList_t &Nodes) const
  329. {
  330. if(_Ptr)
  331. return _Ptr->GetNodes(Nodes);
  332. else
  333. throw ACCESS_EXCEPTION("Feature not present (reference not valid)");
  334. }
  335. template<class TCameraParams>
  336. inline INode* CNodeMapRefT<TCameraParams>::_GetNode(const GENICAM_NAMESPACE::gcstring& key) const
  337. {
  338. if(_Ptr)
  339. return _Ptr->GetNode(key);
  340. else
  341. throw ACCESS_EXCEPTION("Feature not present (reference not valid)");
  342. }
  343. template<class TCameraParams>
  344. inline void CNodeMapRefT<TCameraParams>::_InvalidateNodes() const
  345. {
  346. if(_Ptr)
  347. return _Ptr->InvalidateNodes();
  348. else
  349. throw ACCESS_EXCEPTION("Feature not present (reference not valid)");
  350. }
  351. template<class TCameraParams>
  352. inline bool CNodeMapRefT<TCameraParams>::_Connect(IPort* pPort, const GENICAM_NAMESPACE::gcstring& PortName) const
  353. {
  354. if(_Ptr)
  355. return _Ptr->Connect(pPort, PortName);
  356. else
  357. throw ACCESS_EXCEPTION("Feature not present (reference not valid)");
  358. }
  359. template<class TCameraParams>
  360. inline bool CNodeMapRefT<TCameraParams>::_Connect(IPort* pPort) const
  361. {
  362. if(_Ptr)
  363. return _Ptr->Connect(pPort);
  364. else
  365. throw ACCESS_EXCEPTION("Feature not present (reference not valid)");
  366. }
  367. template<class TCameraParams>
  368. inline bool CNodeMapRefT<TCameraParams>::_ClearXMLCache()
  369. {
  370. return CNodeMapFactory::ClearCache();
  371. }
  372. /**
  373. \brief Empty base class used by class CNodeMapRef as generic template argument
  374. \ingroup GenApi_PublicInterface
  375. */
  376. class CGeneric_XMLLoaderParams
  377. {
  378. protected:
  379. virtual void _Initialize(GENAPI_NAMESPACE::INodeMap*) {}
  380. };
  381. /**
  382. \brief Smartpointer for NodeMaps with create function
  383. \ingroup GenApi_PublicInterface
  384. \note This class is a simple typedef definition. The class syntax is only used,
  385. because Doxygen has to generate a useful documentation.
  386. */
  387. class CNodeMapRef : public CNodeMapRefT<GENAPI_NAMESPACE::CGeneric_XMLLoaderParams>
  388. {
  389. public:
  390. //! Constructor
  391. CNodeMapRef(const GENICAM_NAMESPACE::gcstring &DeviceName = "Device")
  392. : CNodeMapRefT<GENAPI_NAMESPACE::CGeneric_XMLLoaderParams>(DeviceName)
  393. {
  394. }
  395. //! Constructor
  396. CNodeMapRef(INodeMap* pNodeMap, const GENICAM_NAMESPACE::gcstring &DeviceName = "Device")
  397. : CNodeMapRefT<GENAPI_NAMESPACE::CGeneric_XMLLoaderParams>(pNodeMap, DeviceName)
  398. {
  399. }
  400. //! Copy constructor
  401. CNodeMapRef(const CNodeMapRef& Them)
  402. : CNodeMapRefT<GENAPI_NAMESPACE::CGeneric_XMLLoaderParams>(Them)
  403. {
  404. }
  405. //! Assignment
  406. CNodeMapRef& operator=(const CNodeMapRef& Them)
  407. {
  408. CNodeMapRefT<GENAPI_NAMESPACE::CGeneric_XMLLoaderParams>::operator=(Them);
  409. return *this;
  410. }
  411. //! Assignment of an INodeMap*
  412. CNodeMapRef& operator=(INodeMap* pNodeMap)
  413. {
  414. CNodeMapRefT<GENAPI_NAMESPACE::CGeneric_XMLLoaderParams>::operator=(pNodeMap);
  415. return *this;
  416. }
  417. };
  418. }
  419. #endif // ifndef GENAPI_NODEMAPPTR_H