NodeMapRef.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. //! Parse all Swissknife equations
  113. virtual bool _ParseSwissKnifes( GENICAM_NAMESPACE::gcstring_vector *pErrorList = NULL ) const;
  114. //! Pointer to the NodeMap
  115. INodeMap *_Ptr;
  116. private:
  117. //! The name of this device
  118. GENICAM_NAMESPACE::gcstring _DeviceName;
  119. //ATTENTION: not thread safe
  120. int* _pRefCount;
  121. void Release();
  122. void Attach(INodeMap *pNodeMap, const GENICAM_NAMESPACE::gcstring &DeviceName, int* pRefCount);
  123. };
  124. template<class TCameraParams>
  125. inline CNodeMapRefT<TCameraParams>::CNodeMapRefT(const GENICAM_NAMESPACE::gcstring &DeviceName)
  126. : _Ptr(NULL)
  127. , _DeviceName(DeviceName)
  128. , _pRefCount(NULL)
  129. {
  130. }
  131. template<class TCameraParams>
  132. inline CNodeMapRefT<TCameraParams>::CNodeMapRefT(INodeMap *pNodeMap, const GENICAM_NAMESPACE::gcstring &DeviceName)
  133. : _Ptr(NULL)
  134. , _DeviceName(DeviceName)
  135. , _pRefCount(NULL)
  136. {
  137. assert(pNodeMap);
  138. Attach(pNodeMap, DeviceName, pNodeMap ? new int(0) : NULL);
  139. }
  140. template<class TCameraParams>
  141. GENAPI_NAMESPACE::CNodeMapRefT<TCameraParams>::CNodeMapRefT(const CNodeMapRefT& Them)
  142. : TCameraParams()
  143. , _Ptr(NULL)
  144. , _DeviceName()
  145. , _pRefCount(NULL)
  146. {
  147. Attach(Them._Ptr, Them._DeviceName, Them._pRefCount);
  148. }
  149. //ATTENTION: not thread safe
  150. template<class TCameraParams>
  151. void GENAPI_NAMESPACE::CNodeMapRefT<TCameraParams>::Attach(INodeMap *pNodeMap, const GENICAM_NAMESPACE::gcstring &DeviceName, int* pRefCount)
  152. {
  153. // Must be empty
  154. assert(_Ptr == NULL);
  155. assert(_pRefCount == NULL);
  156. //always copy device name
  157. if (&_DeviceName != &DeviceName) //if not assigning member itself
  158. {
  159. _DeviceName = DeviceName;
  160. }
  161. // Attach
  162. if (pNodeMap)
  163. {
  164. assert(pRefCount);
  165. if (pRefCount)
  166. {
  167. ++*pRefCount;
  168. //assign new node map data
  169. _Ptr = pNodeMap;
  170. _pRefCount = pRefCount;
  171. // Initialize the references
  172. TCameraParams::_Initialize(_Ptr);
  173. }
  174. }
  175. }
  176. //ATTENTION: not thread safe
  177. template<class TCameraParams>
  178. void GENAPI_NAMESPACE::CNodeMapRefT<TCameraParams>::Release()
  179. {
  180. if (_Ptr)
  181. {
  182. // Copy node map data for eventual later destruction
  183. INodeMap* pToDel = _Ptr;
  184. int* pRefCount = _pRefCount;
  185. // Clear
  186. _pRefCount = NULL;
  187. _Ptr = NULL;
  188. _DeviceName = "Device";
  189. assert(pRefCount);
  190. // Check if destruction is required
  191. if (pRefCount)
  192. {
  193. assert(*pRefCount > 0);
  194. --*pRefCount;
  195. if (*pRefCount == 0)
  196. {
  197. // We do not need this anymore, all references are gone
  198. delete pRefCount;
  199. pRefCount = NULL;
  200. // Destroy the node map finally
  201. GENAPI_NAMESPACE::IDestroy *pDestroy = CastToIDestroy(pToDel);
  202. assert(pDestroy);
  203. pDestroy->Destroy(); //must not throw
  204. }
  205. }
  206. }
  207. else
  208. {
  209. // Must not have a refcount when no node map is there.
  210. assert(_pRefCount == NULL);
  211. }
  212. }
  213. template<class TCameraParams>
  214. CNodeMapRefT<TCameraParams>& GENAPI_NAMESPACE::CNodeMapRefT<TCameraParams>::operator=(INodeMap* pNodeMap)
  215. {
  216. Release();
  217. assert(pNodeMap);
  218. if (pNodeMap)
  219. {
  220. Attach(pNodeMap, pNodeMap->GetDeviceName(), new int(0));
  221. }
  222. return *this;
  223. }
  224. template<class TCameraParams>
  225. CNodeMapRefT<TCameraParams>& GENAPI_NAMESPACE::CNodeMapRefT<TCameraParams>::operator=(const CNodeMapRefT& Them)
  226. {
  227. Release();
  228. Attach(Them._Ptr, Them._DeviceName, Them._pRefCount);
  229. return *this;
  230. }
  231. template<class TCameraParams>
  232. inline CNodeMapRefT<TCameraParams>::~CNodeMapRefT()
  233. {
  234. Release();
  235. }
  236. template<class TCameraParams>
  237. inline void CNodeMapRefT<TCameraParams>::_Destroy()
  238. {
  239. Release();
  240. }
  241. template<class TCameraParams>
  242. inline void CNodeMapRefT<TCameraParams>::_LoadXMLFromFile(const GENICAM_NAMESPACE::gcstring &FileName)
  243. {
  244. // FileName environment is replaced in CNodeMapFactory ctor
  245. // Load the DLL
  246. if(_Ptr)
  247. throw RUNTIME_EXCEPTION("Node map already created");
  248. // Load the XML file
  249. CNodeMapFactory nodeMapData(ContentType_Xml, FileName);
  250. Attach(nodeMapData.CreateNodeMap(_DeviceName), _DeviceName, new int(0));
  251. }
  252. template<class TCameraParams>
  253. inline void CNodeMapRefT<TCameraParams>::_LoadXMLFromZIPFile(const GENICAM_NAMESPACE::gcstring &ZipFileName)
  254. {
  255. // FileName environment is replaced in CNodeMapFactory ctor
  256. // Load the DLL
  257. if(_Ptr)
  258. throw RUNTIME_EXCEPTION("Node map already created");
  259. // Load the XML file
  260. CNodeMapFactory nodeMapData(ContentType_ZippedXml, ZipFileName);
  261. Attach(nodeMapData.CreateNodeMap(), _DeviceName, new int(0));
  262. }
  263. template<class TCameraParams>
  264. inline void CNodeMapRefT<TCameraParams>::_LoadXMLFromFileInject(const GENICAM_NAMESPACE::gcstring &TargetFileName, const GENICAM_NAMESPACE::gcstring &InjectFileName)
  265. {
  266. // xxxFileName environment is replaced in CNodeMapFactory ctor
  267. // Load the DLL
  268. if(_Ptr)
  269. throw RUNTIME_EXCEPTION("Node map already created");
  270. // Load the XML file
  271. CNodeMapFactory nodeMapData(ContentType_Xml, TargetFileName);
  272. CNodeMapFactory injectNodeMapData(ContentType_Xml, InjectFileName);
  273. nodeMapData.AddInjectionData(injectNodeMapData);
  274. Attach(nodeMapData.CreateNodeMap(), _DeviceName, new int(0));
  275. }
  276. template<class TCameraParams>
  277. inline void CNodeMapRefT<TCameraParams>::_LoadXMLFromString(const GENICAM_NAMESPACE::gcstring& XMLData)
  278. {
  279. // Load the DLL
  280. if(_Ptr)
  281. throw RUNTIME_EXCEPTION("Node map already created");
  282. // Load the XML file
  283. CNodeMapFactory nodeMapData(ContentType_Xml, XMLData.c_str(), XMLData.size()); //avoid string copy
  284. Attach(nodeMapData.CreateNodeMap(_DeviceName), _DeviceName, new int(0));
  285. }
  286. template<class TCameraParams>
  287. inline void CNodeMapRefT<TCameraParams>::_LoadXMLFromZIPData(const void* zipData, size_t zipSize)
  288. {
  289. // Load the DLL
  290. if(_Ptr)
  291. throw RUNTIME_EXCEPTION("Node map already created");
  292. // Load the XML file
  293. CNodeMapFactory nodeMapData(ContentType_ZippedXml, zipData, zipSize);
  294. Attach(nodeMapData.CreateNodeMap(), _DeviceName, new int(0));
  295. }
  296. template<class TCameraParams>
  297. inline void CNodeMapRefT<TCameraParams>::_LoadXMLFromStringInject(const GENICAM_NAMESPACE::gcstring& TargetXMLData, const GENICAM_NAMESPACE::gcstring& InjectXMLData)
  298. {
  299. // Load the DLL
  300. if(_Ptr)
  301. throw RUNTIME_EXCEPTION("Node map already created");
  302. // Load the XML file
  303. CNodeMapFactory nodeMapData(ContentType_Xml, TargetXMLData.c_str(), TargetXMLData.size()); //avoid string copy
  304. CNodeMapFactory injectNodeMapData(ContentType_Xml, InjectXMLData.c_str(), InjectXMLData.size()); //avoid string copy
  305. nodeMapData.AddInjectionData(injectNodeMapData);
  306. Attach(nodeMapData.CreateNodeMap(), _DeviceName, new int(0));
  307. }
  308. template<class TCameraParams>
  309. inline void CNodeMapRefT<TCameraParams>::_GetSupportedSchemaVersions(GENICAM_NAMESPACE::gcstring_vector &SchemaVersions) const
  310. {
  311. CNodeMapFactory().GetSupportedSchemaVersions(SchemaVersions);
  312. }
  313. template<class TCameraParams>
  314. inline GENICAM_NAMESPACE::gcstring CNodeMapRefT<TCameraParams>::_GetDeviceName() const
  315. {
  316. if(_Ptr)
  317. return _Ptr->GetDeviceName();
  318. else
  319. throw ACCESS_EXCEPTION("Feature not present (reference not valid)");
  320. }
  321. template<class TCameraParams>
  322. inline void CNodeMapRefT<TCameraParams>::_Poll(int64_t ElapsedTime)
  323. {
  324. if(_Ptr)
  325. return _Ptr->Poll(ElapsedTime);
  326. else
  327. throw ACCESS_EXCEPTION("Feature not present (reference not valid)");
  328. }
  329. template<class TCameraParams>
  330. inline void CNodeMapRefT<TCameraParams>::_GetNodes(NodeList_t &Nodes) const
  331. {
  332. if(_Ptr)
  333. return _Ptr->GetNodes(Nodes);
  334. else
  335. throw ACCESS_EXCEPTION("Feature not present (reference not valid)");
  336. }
  337. template<class TCameraParams>
  338. inline INode* CNodeMapRefT<TCameraParams>::_GetNode(const GENICAM_NAMESPACE::gcstring& key) const
  339. {
  340. if(_Ptr)
  341. return _Ptr->GetNode(key);
  342. else
  343. throw ACCESS_EXCEPTION("Feature not present (reference not valid)");
  344. }
  345. template<class TCameraParams>
  346. inline void CNodeMapRefT<TCameraParams>::_InvalidateNodes() const
  347. {
  348. if(_Ptr)
  349. return _Ptr->InvalidateNodes();
  350. else
  351. throw ACCESS_EXCEPTION("Feature not present (reference not valid)");
  352. }
  353. template<class TCameraParams>
  354. inline bool CNodeMapRefT<TCameraParams>::_ParseSwissKnifes( GENICAM_NAMESPACE::gcstring_vector *pErrorList ) const
  355. {
  356. bool ret = false;
  357. if (_Ptr)
  358. ret = _Ptr->ParseSwissKnifes(pErrorList);
  359. else
  360. throw ACCESS_EXCEPTION("Feature not present (reference not valid)");
  361. return ret;
  362. }
  363. template<class TCameraParams>
  364. inline bool CNodeMapRefT<TCameraParams>::_Connect(IPort* pPort, const GENICAM_NAMESPACE::gcstring& PortName) const
  365. {
  366. if(_Ptr)
  367. return _Ptr->Connect(pPort, PortName);
  368. else
  369. throw ACCESS_EXCEPTION("Feature not present (reference not valid)");
  370. }
  371. template<class TCameraParams>
  372. inline bool CNodeMapRefT<TCameraParams>::_Connect(IPort* pPort) const
  373. {
  374. if(_Ptr)
  375. return _Ptr->Connect(pPort);
  376. else
  377. throw ACCESS_EXCEPTION("Feature not present (reference not valid)");
  378. }
  379. template<class TCameraParams>
  380. inline bool CNodeMapRefT<TCameraParams>::_ClearXMLCache()
  381. {
  382. return CNodeMapFactory::ClearCache();
  383. }
  384. /**
  385. \brief Empty base class used by class CNodeMapRef as generic template argument
  386. \ingroup GenApi_PublicInterface
  387. */
  388. class CGeneric_XMLLoaderParams
  389. {
  390. protected:
  391. virtual void _Initialize(GENAPI_NAMESPACE::INodeMap*) {}
  392. };
  393. /**
  394. \brief Smartpointer for NodeMaps with create function
  395. \ingroup GenApi_PublicInterface
  396. \note This class is a simple typedef definition. The class syntax is only used,
  397. because Doxygen has to generate a useful documentation.
  398. */
  399. class CNodeMapRef : public CNodeMapRefT<GENAPI_NAMESPACE::CGeneric_XMLLoaderParams>
  400. {
  401. public:
  402. //! Constructor
  403. CNodeMapRef(const GENICAM_NAMESPACE::gcstring &DeviceName = "Device")
  404. : CNodeMapRefT<GENAPI_NAMESPACE::CGeneric_XMLLoaderParams>(DeviceName)
  405. {
  406. }
  407. //! Constructor
  408. CNodeMapRef(INodeMap* pNodeMap, const GENICAM_NAMESPACE::gcstring &DeviceName = "Device")
  409. : CNodeMapRefT<GENAPI_NAMESPACE::CGeneric_XMLLoaderParams>(pNodeMap, DeviceName)
  410. {
  411. }
  412. //! Copy constructor
  413. CNodeMapRef(const CNodeMapRef& Them)
  414. : CNodeMapRefT<GENAPI_NAMESPACE::CGeneric_XMLLoaderParams>(Them)
  415. {
  416. }
  417. //! Assignment
  418. CNodeMapRef& operator=(const CNodeMapRef& Them)
  419. {
  420. CNodeMapRefT<GENAPI_NAMESPACE::CGeneric_XMLLoaderParams>::operator=(Them);
  421. return *this;
  422. }
  423. //! Assignment of an INodeMap*
  424. CNodeMapRef& operator=(INodeMap* pNodeMap)
  425. {
  426. CNodeMapRefT<GENAPI_NAMESPACE::CGeneric_XMLLoaderParams>::operator=(pNodeMap);
  427. return *this;
  428. }
  429. };
  430. }
  431. #endif // ifndef GENAPI_NODEMAPPTR_H