EnumParameter.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. //------------------------------------------------------------------------------
  2. // Basler pylon SDK
  3. // Copyright (c) 2018-2021 Basler AG
  4. // http://www.baslerweb.com
  5. //------------------------------------------------------------------------------
  6. /*!
  7. \file
  8. \brief Contains the class CEnumParameter used to simplify access to %GenApi parameters.
  9. */
  10. #ifndef INCLUDED_BASLER_PYLON_CENUMPARAMETER_H
  11. #define INCLUDED_BASLER_PYLON_CENUMPARAMETER_H
  12. #pragma once
  13. #include <pylon/PylonBase.h>
  14. #include <GenApi/IEnumeration.h>
  15. #include <pylon/Parameter.h>
  16. #ifdef _MSC_VER
  17. # pragma pack(push, PYLON_PACKING)
  18. #endif /* _MSC_VER */
  19. #ifdef _MSC_VER
  20. # pragma warning( push )
  21. # pragma warning( disable : 4275 ) // Class needs to have a dll interface to be used by clients of the class.
  22. # pragma warning( disable : 4250 ) // warning C4250: 'Pylon::CXYZParameter': inherits 'Pylon::CParameter::Pylon::CParameter::ZYX' via dominance
  23. #endif
  24. namespace Pylon
  25. {
  26. /*!
  27. \brief Extends the GenApi::IEnumeration interface with convenience methods.
  28. */
  29. interface IEnumerationEx : virtual public GenApi::IEnumeration, virtual public IValueEx
  30. {
  31. using GenApi::IEnumeration::operator=;
  32. /*!
  33. \brief Gets the value of the parameter if the parameter is readable. Otherwise returns the default value.
  34. \return Returns the parameter value if the parameter is readable. Otherwise returns the default value.
  35. \param[in] defaultValue The default value returned if the parameter is not readable.
  36. \threading
  37. The method accesses the parameter multiple times. These accesses are not synchronized by a lock.
  38. \error
  39. Can throw exceptions if reading the value fails.
  40. */
  41. virtual String_t GetValueOrDefault( const String_t& defaultValue ) = 0;
  42. /*!
  43. \brief Sets the value of the parameter if the parameter is writable and the value is contained in the set of settable enumeration values.
  44. \return Returns false if the parameter is not writable or the value is not contained in the set of settable enumeration values.
  45. \param[in] value The value to set.
  46. \threading
  47. The method accesses the parameter multiple times. These accesses are not synchronized by a lock.
  48. \error-
  49. Can throw exceptions if the preconditions are not met or if writing the value fails.
  50. */
  51. virtual bool TrySetValue( const String_t& value ) = 0;
  52. /*!
  53. \brief Sets the value of the parameter to the first valid value in a list of values.
  54. Example:
  55. \code
  56. CEnumParameter pixelFormat(nodemap, "PixelFormat");
  57. const char* list[] = { "BayerGR8", "BayerRG8", "BayerGB8", "BayerBG8", "Mono8", NULL };
  58. pixelFormat.SetValue(list);
  59. \endcode
  60. \param[in] nullTerminatedList The list of possible values to set. The list is terminated by a NULL value.
  61. \pre
  62. <ul>
  63. <li>The parameter must be writable.
  64. <li>At least one value within the list passed must be contained in the set of settable enumeration values.
  65. </ul>
  66. \threading
  67. The method accesses the parameter multiple times. These accesses are not synchronized by a lock.
  68. \error
  69. Can throw exceptions if the parameter is not writable, no value \c nullTerminatedList is settable, or writing the value fails.
  70. */
  71. virtual void SetValue( const char** nullTerminatedList ) = 0;
  72. /*!
  73. \brief If the parameter is writable, sets the value of the parameter to the first valid value in a list of values.
  74. Example:
  75. \code
  76. CEnumParameter pixelFormat(nodemap, "PixelFormat");
  77. const char* list[] = { "BayerGR8", "BayerRG8", "BayerGB8", "BayerBG8", "Mono8", NULL };
  78. pixelFormat.TrySetValue(list);
  79. \endcode
  80. \param[in] nullTerminatedList The list of possible values to set. The list is terminated by a NULL value.
  81. \return Returns false if the parameter is not writable.
  82. \pre At least one value within the passed list must be contained in the set of settable enumeration values.
  83. \threading
  84. The method accesses the parameter multiple times. These accesses are not synchronized by a lock.
  85. \error
  86. Can throw exceptions if the parameter is not writable, no value \c nullTerminatedList is settable, or writing the value fails.
  87. */
  88. virtual bool TrySetValue( const char** nullTerminatedList ) = 0;
  89. /*!
  90. \brief Indicates whether the given value can be set.
  91. \return Returns true if the value can be set, otherwise false.
  92. \param[in] value The value to be checked.
  93. \threading
  94. The method accesses the parameter multiple times. These accesses are not synchronized by a lock.
  95. \error
  96. Does not throw exceptions.
  97. */
  98. virtual bool CanSetValue( const String_t& value ) = 0;
  99. /*!
  100. \brief Sets the value of the parameter.
  101. Calls FromString().
  102. \param[in] value The value to set.
  103. \pre The value must be contained in the set of settable enumeration values.
  104. \threading
  105. The method accesses the parameter multiple times. These accesses are not synchronized by a lock.
  106. \error
  107. Can throw exceptions if the parameter is not writable, no value is valid, or writing the value fails.
  108. */
  109. virtual void SetValue( const String_t& value ) = 0;
  110. /*!
  111. \brief Gets the value of the parameter.
  112. \return Returns the current parameter value.
  113. \pre The parameter must be readable.
  114. \threading
  115. The method accesses the parameter multiple times. These accesses are not synchronized by a lock.
  116. \error
  117. Can throw exceptions if the parameter is not readable or if reading the value fails.
  118. */
  119. virtual String_t GetValue() = 0;
  120. /*!
  121. \brief Gets a list of all values of the enumeration that are currently settable.
  122. \param[out] values Returns a list of all values of the enumeration that are currently settable.
  123. \pre The parameter must be readable.
  124. \threading
  125. The method accesses the parameter multiple times. These accesses are not synchronized by a lock.
  126. \error
  127. Can throw exceptions.
  128. */
  129. virtual void GetSettableValues( GenApi::StringList_t& values ) = 0;
  130. /*!
  131. \brief Gets a list of all values of the enumeration including the values that are currently not settable.
  132. \param[out] values Returns a list of all values of the enumeration including the values that are currently not settable.
  133. \pre The parameter must be readable.
  134. \threading
  135. The method accesses the parameter multiple times. These accesses are not synchronized by a lock.
  136. \error
  137. Can throw exceptions.
  138. */
  139. virtual void GetAllValues( GenApi::StringList_t& values ) = 0;
  140. /*!
  141. \brief Gets an enumeration entry by its symbolic name. The entry is returned as a CParameter.
  142. This method can be used to access information about the enumeration value represented by the entry using CParameter::GetInfo().
  143. \param[in] value The symbolic name of the enumeration entry, e.g., "Testimage1".
  144. \pre The parameter must be readable.
  145. \threading
  146. The method accesses the parameter multiple times. These accesses are not synchronized by a lock.
  147. \error
  148. Can throw exceptions.
  149. */
  150. virtual CParameter GetEntryByNameAsParameter( const GenICam::gcstring& value ) = 0;
  151. /*!
  152. \brief Gets the currently selected entry of an enumeration. The entry is returned as a CParameter.
  153. This method can be used to access information about the enumeration value represented by the entry using CParameter::GetInfo().
  154. \pre The parameter must be readable.
  155. \threading
  156. The method accesses the parameter multiple times. These accesses are not synchronized by a lock.
  157. \error
  158. Can throw exceptions.
  159. */
  160. virtual CParameter GetCurrentEntryAsParameter() = 0;
  161. };
  162. /*!
  163. \brief CEnumParameter class used to simplify access to %GenApi parameters.
  164. */
  165. class PYLONBASE_API CEnumParameter : virtual public IEnumerationEx, public CParameter
  166. {
  167. public:
  168. /*!
  169. \brief Creates an empty CEnumParameter object.
  170. \error
  171. Does not throw C++ exceptions.
  172. */
  173. CEnumParameter();
  174. /*!
  175. \brief Creates a CEnumParameter object and attaches it to a node, typically retrieved for a nodemap calling GetNode().
  176. \param[in] pNode The node to attach.
  177. \post
  178. <ul>
  179. <li>If the passed node does not match the parameter type, the parameter will be empty, see IsValid().
  180. <li>If the passed node does match the parameter type, it is attached and the parameter object can be used to access the node's functionality.
  181. <li>The parameter object must not be used to access the node's functionality if the source of the attached \c pNode has been destroyed. In this case, call Release() or attach a new node.
  182. </ul>
  183. \error
  184. Does not throw C++ exceptions.
  185. */
  186. explicit CEnumParameter( GenApi::INode* pNode );
  187. /*!
  188. \brief Creates a CEnumParameter object and attaches it to a node of a matching type.
  189. \param[in] pEnumeration The node to attach.
  190. \post
  191. The parameter object must not be used to access the node's functionality if the source of the attached \c pEnumeration has been destroyed. In this case, call Release() or attach a new node.
  192. \error
  193. Does not throw C++ exceptions.
  194. */
  195. explicit CEnumParameter( GenApi::IEnumeration* pEnumeration );
  196. /*!
  197. \brief Creates a CEnumParameter object and attaches it to a node retrieved from the provided node map.
  198. \param[in] pNodeMap The node map. The source of the parameter.
  199. \param[in] pName The name of the parameter to attach.
  200. \post
  201. <ul>
  202. <li>If \c pNodeMap or \c name is NULL, the parameter will be empty, see IsValid().
  203. <li>If the node does not match the parameter type, the parameter will be empty, see IsValid().
  204. <li>If the node does match the parameter type, it is attached and the parameter object can be used to access the node's functionality.
  205. <li>The parameter object must not be used to access the node's functionality if the provided node map has been destroyed. In this case, call Release() or attach a new node.
  206. </ul>
  207. \error
  208. The call to GenApi::INodeMap::GetNode can throw C++ exceptions.
  209. */
  210. CEnumParameter( GenApi::INodeMap* pNodeMap, const char* pName );
  211. /*!
  212. \brief Creates a CEnumParameter object and attaches it to a node retrieved from the provided node map.
  213. \param[in] nodeMap The node map. The source of the parameter.
  214. \param[in] pName The name of the parameter to attach.
  215. \post
  216. <ul>
  217. <li>If \c name is NULL, the parameter will be empty, see IsValid().
  218. <li>If the node does not match the parameter type, the parameter will be empty, see IsValid().
  219. <li>If the node does match the parameter type, it is attached and the parameter object can be used to access the node's functionality.
  220. <li>The parameter object must not be used to access the node's functionality if the provided node map has been destroyed. In this case, call Release() or attach a new node.
  221. </ul>
  222. \error
  223. The call to GenApi::INodeMap::GetNode can throw C++ exceptions.
  224. */
  225. CEnumParameter( GenApi::INodeMap& nodeMap, const char* pName );
  226. /*!
  227. \brief Copies a CEnumParameter object.
  228. \param[in] rhs The object to copy.
  229. \error
  230. Does not throw C++ exceptions.
  231. */
  232. CEnumParameter( const CEnumParameter& rhs );
  233. /*!
  234. \brief Destroys the CEnumParameter object.
  235. Does not access the attached node.
  236. \error
  237. Does not throw C++ exceptions.
  238. */
  239. virtual ~CEnumParameter();
  240. /*!
  241. \brief Attaches a node retrieved from the provided node map.
  242. \param[in] pNodeMap The node map. The source of the parameter.
  243. \param[in] pName The name of the parameter to attach.
  244. \return Returns true if the node has been attached.
  245. \post
  246. <ul>
  247. <li>If \c pNodeMap or \c name is NULL, the parameter will be empty, see IsValid().
  248. <li>If the node does not match the parameter type, the parameter will be empty, see IsValid().
  249. <li>If the node does match the parameter type, it is attached and the parameter object can be used to access the node's functionality.
  250. <li>The parameter object must not be used to access the node's functionality if the provided node map has been destroyed. In this case, call Release() or attach a new node.
  251. </ul>
  252. \error
  253. The call to GenApi::INodeMap::GetNode can throw C++ exceptions.
  254. */
  255. virtual bool Attach( GenApi::INodeMap* pNodeMap, const char* pName );
  256. /*!
  257. \brief Attaches a node retrieved from the provided node map.
  258. \param[in] nodeMap The node map. The source of the parameter.
  259. \param[in] pName The name of the parameter to attach.
  260. \return Returns true if the node has been attached.
  261. \post
  262. <ul>
  263. <li>If \c name is NULL the parameter will be empty, see IsValid().
  264. <li>If the node does not match the parameter type, the parameter will be empty, see IsValid().
  265. <li>If the node does match the parameter type, it is attached and the parameter object can be used to access the node's functionality.
  266. <li>The parameter object must not be used to access the node's functionality if the provided node map has been destroyed. In this case, call Release() or attach a new node.
  267. </ul>
  268. \error
  269. The call to GenApi::INodeMap::GetNode can throw C++ exceptions.
  270. */
  271. virtual bool Attach( GenApi::INodeMap& nodeMap, const char* pName );
  272. /*!
  273. \brief Attaches a node, typically retrieved for a nodemap calling GetNode().
  274. \param[in] pNode The node to assign.
  275. \return Returns true if the node has been attached.
  276. \post
  277. <ul>
  278. <li>If the node does not match the parameter type, the parameter will be empty, see IsValid().
  279. <li>If the node does match the parameter type, it is attached and the parameter object can be used to access the node's functionality.
  280. <li>The parameter object must not be used to access the node's functionality if the source of the attached \c pNode has been destroyed. In this case, call Release() or attach a new node.
  281. </ul>
  282. \error
  283. Does not throw C++ exceptions.
  284. */
  285. virtual bool Attach( GenApi::INode* pNode );
  286. /*!
  287. \brief Assigns a node of the same type to the parameter object.
  288. \param[in] pEnumeration The node to assign.
  289. \return Returns true if the node has been attached.
  290. \error
  291. Does not throw C++ exceptions.
  292. */
  293. virtual bool Attach( GenApi::IEnumeration* pEnumeration );
  294. /*!
  295. \brief Assigns a CEnumParameter object.
  296. \param[in] rhs The object to assign.
  297. \error
  298. Does not throw C++ exceptions.
  299. */
  300. CEnumParameter& operator=( const CEnumParameter& rhs );
  301. /*!
  302. \brief Returns true if the same nodes are attached or both parameters are empty.
  303. \param[in] rhs The object to compare to.
  304. \return Returns true if the same nodes are attached or both parameters are empty.
  305. \error
  306. Does not throw C++ exceptions.
  307. */
  308. virtual bool Equals( const CEnumParameter& rhs ) const;
  309. /*!
  310. \brief Returns true if the attached node pointer is equal.
  311. \param[in] pNode The node to compare to.
  312. \return Returns true if the attached node pointer is equal.
  313. \error
  314. Does not throw C++ exceptions.
  315. */
  316. virtual bool Equals( const GenApi::INode* pNode ) const;
  317. /*!
  318. \brief Returns true if the attached node pointer is equal.
  319. \param[in] pEnumeration The node to compare to.
  320. \return Returns true if the attached node pointer is equal.
  321. \error
  322. Does not throw C++ exceptions.
  323. */
  324. virtual bool Equals( const GenApi::IEnumeration* pEnumeration ) const;
  325. /*!
  326. \brief Releases the attached node.
  327. \error
  328. Does not throw C++ exceptions.
  329. */
  330. virtual void Release();
  331. // Implements IValueEx
  332. virtual bool IsValid() const;
  333. // Implements GenApi::IEnumeration
  334. virtual void GetSymbolics( GenApi::StringList_t& symbolics );
  335. // Implements GenApi::IEnumeration
  336. virtual void GetEntries( GenApi::NodeList_t& entries );
  337. // Implements GenApi::IEnumeration
  338. virtual GenApi::IEnumeration& operator=( const GenICam::gcstring& valueStr );
  339. // Implements GenApi::IEnumeration
  340. virtual void SetIntValue( int64_t value, bool verify = true );
  341. // Implements GenApi::IEnumeration
  342. virtual GenICam::gcstring operator*();
  343. // Implements GenApi::IEnumeration
  344. virtual int64_t GetIntValue( bool verify = false, bool ignoreCache = false );
  345. // Implements GenApi::IEnumeration
  346. virtual GenApi::IEnumEntry* GetEntryByName( const GenICam::gcstring& symbolic );
  347. // Implements GenApi::IEnumeration
  348. virtual GenApi::IEnumEntry* GetEntry( const int64_t intValue );
  349. // Implements GenApi::IEnumeration
  350. virtual GenApi::IEnumEntry* GetCurrentEntry( bool verify = false, bool ignoreCache = false );
  351. // Implements IEnumerationEx
  352. virtual String_t GetValueOrDefault( const String_t& defaultValue );
  353. // Implements IEnumerationEx
  354. virtual bool TrySetValue( const String_t& value );
  355. // Implements IEnumerationEx
  356. virtual void SetValue( const char** nullTerminatedList );
  357. // Implements IEnumerationEx
  358. virtual bool TrySetValue( const char** nullTerminatedList );
  359. // Implements IEnumerationEx
  360. virtual bool CanSetValue( const String_t& value );
  361. // Implements IEnumerationEx
  362. virtual void SetValue( const String_t& value );
  363. // Implements IEnumerationEx
  364. virtual String_t GetValue();
  365. // Implements IEnumerationEx
  366. virtual void GetSettableValues( GenApi::StringList_t& values );
  367. // Implements IEnumerationEx
  368. virtual void GetAllValues( GenApi::StringList_t& values );
  369. // Implements IEnumerationEx
  370. virtual CParameter GetEntryByNameAsParameter( const GenICam::gcstring& value );
  371. // Implements IEnumerationEx
  372. virtual CParameter GetCurrentEntryAsParameter();
  373. public:
  374. // Start - For using C++ enums instead of strings as enumeration values (native parameter access)
  375. class TableItem_t
  376. {
  377. public:
  378. TableItem_t()
  379. : m_szName( "" )
  380. , m_sizeOfName( 0 )
  381. {
  382. }
  383. TableItem_t( const char* name, size_t size )
  384. : m_szName( name )
  385. , m_sizeOfName( size )
  386. {
  387. }
  388. inline const char* GetName() const
  389. {
  390. return m_szName;
  391. }
  392. inline size_t GetSizeOfName() const
  393. {
  394. return m_sizeOfName;
  395. }
  396. private:
  397. const char* m_szName;
  398. size_t m_sizeOfName;
  399. };
  400. class Table_t
  401. {
  402. public:
  403. Table_t()
  404. : m_items( 0 )
  405. , m_tableSize( 0 )
  406. {
  407. }
  408. Table_t( const TableItem_t* items, size_t tableSize )
  409. : m_items( items )
  410. , m_tableSize( tableSize )
  411. {
  412. }
  413. inline const TableItem_t* GetItems() const
  414. {
  415. return m_items;
  416. }
  417. inline size_t GetSizeOfTable() const
  418. {
  419. return m_tableSize;
  420. }
  421. private:
  422. const TableItem_t* m_items;
  423. size_t m_tableSize;
  424. };
  425. protected:
  426. void SetValue( const Table_t& table, size_t index, bool verify );
  427. size_t /*index*/ GetValue( const Table_t& table, bool verify, bool ignoreCache );
  428. GenApi::IEnumEntry* GetEntry( const Table_t& table, size_t index );
  429. bool CanSetValue( const Table_t& table, size_t index );
  430. // End - For using C++ enums instead of strings as enumeration values (native parameter access)
  431. protected:
  432. GenApi::IEnumeration* m_pFeature;
  433. };
  434. }
  435. #ifdef _MSC_VER
  436. # pragma warning( pop )
  437. #endif
  438. #ifdef _MSC_VER
  439. # pragma pack(pop)
  440. #endif /* _MSC_VER */
  441. #endif /* INCLUDED_BASLER_PYLON_CENUMPARAMETER_H */