EnumParameterT.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //------------------------------------------------------------------------------
  2. // Basler pylon SDK
  3. // Copyright (c) 2018-2021 Basler AG
  4. // http://www.baslerweb.com
  5. //------------------------------------------------------------------------------
  6. /*!
  7. \file
  8. \brief Contains a template class that is used to create classes derived from CEnumParameter.
  9. The derived classes use C++ enums instead of strings as enumeration values. They are used for native parameter access.
  10. */
  11. #ifndef INCLUDED_BASLER_PYLON_CENUMPARAMETERT_H
  12. #define INCLUDED_BASLER_PYLON_CENUMPARAMETERT_H
  13. #pragma once
  14. #include <pylon/EnumParameter.h>
  15. #ifdef _MSC_VER
  16. # pragma pack(push, PYLON_PACKING)
  17. #endif /* _MSC_VER */
  18. #ifdef _MSC_VER
  19. #pragma warning( push )
  20. #pragma warning( disable : 4275 ) // Class needs to have a dll interface to be used by clients of the class.
  21. #pragma warning( disable : 4250 ) // warning C4250: 'Pylon::CXYZParameter': inherits 'Pylon::CParameter::Pylon::CParameter::ZYX' via dominance
  22. #endif
  23. namespace Pylon
  24. {
  25. /*!
  26. \brief A template class that is used to create classes derived from CEnumParameter.
  27. The derived classes use C++ enums instead of strings as enumeration values. They are used for native parameter access.
  28. */
  29. template<typename EnumT>
  30. interface IEnumParameterT : virtual public IEnumerationEx
  31. {
  32. public:
  33. using IEnumerationEx::SetValue;
  34. using IEnumerationEx::TrySetValue;
  35. using IEnumerationEx::GetValueOrDefault;
  36. using IEnumerationEx::CanSetValue;
  37. using IEnumeration::operator=;
  38. /*!
  39. \brief Sets the value passed.
  40. \param[in] value The value to set.
  41. \param[in] verify Enables AccessMode and Range verification (default = true).
  42. \error
  43. Can throw exceptions if the parameter is not writable or if writing the value fails.
  44. */
  45. virtual void SetValue( EnumT value, bool verify = true ) = 0;
  46. /*!
  47. \brief Sets the value passed.
  48. \param[in] value The value to set.
  49. \error
  50. Can throw exceptions if the parameter is not readable or if reading the value fails.
  51. */
  52. virtual IEnumParameterT<EnumT>& operator=( EnumT value ) = 0;
  53. /*!
  54. \brief Gets the current parameter value.
  55. \param verify Enables Range verification (default = false). The AccessMode is always checked.
  56. \param ignoreCache If true, the value is read ignoring any caches (default = false).
  57. \return Returns the current parameter value.
  58. \error
  59. Can throw exceptions if the parameter is not readable or if reading the value fails.
  60. */
  61. virtual EnumT GetValue( bool verify = false, bool ignoreCache = false ) = 0;
  62. /*!
  63. \brief Gets the current parameter value.
  64. \return Returns the current parameter value.
  65. \error
  66. Can throw exceptions if the parameter is not readable or if reading the value fails.
  67. */
  68. virtual EnumT operator()() = 0;
  69. //! Returns the EnumEntry object belonging to the value
  70. virtual GenApi::IEnumEntry* GetEntry( const EnumT value ) = 0;
  71. /*!
  72. \brief Gets the parameter value if the parameter is readable. Otherwise returns the default value.
  73. \return Returns the parameter value if the parameter is readable. Otherwise returns the default value.
  74. \param[in] defaultValue The default value returned if the parameter is not readable.
  75. \threading
  76. The method accesses the parameter multiple times. These accesses are not synchronized by a lock.
  77. \error
  78. Can throw exceptions if reading the value fails.
  79. */
  80. virtual EnumT GetValueOrDefault( EnumT defaultValue ) = 0;
  81. /*!
  82. \brief Sets the value passed if the parameter is writable and the value is contained in the set of settable enumeration values.
  83. \return Returns false if the parameter is not writable or the value is not contained in the set of settable enumeration values.
  84. \param[in] value The value to set.
  85. \threading
  86. The method accesses the parameter multiple times. These accesses are not synchronized by a lock.
  87. \error
  88. Can throw exceptions if the preconditions are not met or if writing the value fails.
  89. */
  90. virtual bool TrySetValue( EnumT value ) = 0;
  91. /*!
  92. \brief Indicates if the value passed can be set.
  93. \return Returns true if the value can be set, otherwise false.
  94. \param[in] value The value to be checked.
  95. \threading
  96. The method accesses the parameter multiple times. These accesses are not synchronized by a lock.
  97. \error
  98. Does not throw exceptions.
  99. */
  100. virtual bool CanSetValue( EnumT value ) = 0;
  101. };
  102. // A template class that is used to create classes derived from CEnumParameter.
  103. template<typename EnumT>
  104. class CEnumParameterT : public CEnumParameter, virtual public IEnumParameterT<EnumT>
  105. {
  106. public:
  107. using IEnumeration::operator=;
  108. CEnumParameterT()
  109. {
  110. }
  111. CEnumParameterT( GenApi::INode* pNode )
  112. : CEnumParameter( pNode )
  113. {
  114. }
  115. using CEnumParameter::SetValue;
  116. // Implements IEnumParameterT<EnumT>
  117. virtual void SetValue( EnumT value, bool verify = true )
  118. {
  119. SetValue( GetTable(), static_cast<size_t>(value), verify );
  120. }
  121. // Implements IEnumParameterT<EnumT>
  122. virtual CEnumParameterT<EnumT>& operator=( EnumT value )
  123. {
  124. SetValue( value );
  125. return *this;
  126. }
  127. // Implements IEnumParameterT<EnumT>
  128. virtual EnumT GetValue( bool verify = false, bool ignoreCache = false )
  129. {
  130. EnumT result = static_cast<EnumT>(CEnumParameter::GetValue( GetTable(), verify, ignoreCache ));
  131. return result;
  132. }
  133. // Implements IEnumParameterT<EnumT>
  134. virtual EnumT operator()()
  135. {
  136. EnumT result = GetValue();
  137. return result;
  138. }
  139. // Implements IEnumParameterT<EnumT>
  140. virtual GenApi::IEnumEntry* GetEntry( const EnumT value )
  141. {
  142. GenApi::IEnumEntry* result = CEnumParameter::GetEntry( GetTable(), static_cast<size_t>(value) );
  143. return result;
  144. }
  145. // Implements IEnumParameterT<EnumT>
  146. virtual EnumT GetValueOrDefault( EnumT defaultValue )
  147. {
  148. if (this->IsReadable())
  149. {
  150. EnumT result = GetValue();
  151. return result;
  152. }
  153. return defaultValue;
  154. }
  155. // Implements IEnumParameterT<EnumT>
  156. virtual bool TrySetValue( EnumT value )
  157. {
  158. if (this->IsWritable() && CEnumParameter::CanSetValue( GetTable(), static_cast<size_t>(value) ))
  159. {
  160. SetValue( value );
  161. return true;
  162. }
  163. return false;
  164. }
  165. // Implements IEnumParameterT<EnumT>
  166. virtual bool CanSetValue( EnumT value )
  167. {
  168. bool result = CEnumParameter::CanSetValue( GetTable(), static_cast<size_t>(value) );
  169. return result;
  170. }
  171. protected:
  172. // Must be provided by the specific enum instance class
  173. virtual const Table_t& GetTable() const = 0;
  174. };
  175. }
  176. #ifdef _MSC_VER
  177. #pragma warning( pop )
  178. #endif
  179. #ifdef _MSC_VER
  180. # pragma pack(pop)
  181. #endif /* _MSC_VER */
  182. #endif /* INCLUDED_BASLER_PYLON_CENUMPARAMETERT_H */