HTupleElement.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*****************************************************************************
  2. * HTupleElement.h
  3. *****************************************************************************
  4. *
  5. * Project: HALCON/C++
  6. * Description: Element of tuple used for control parameters
  7. *
  8. * (c) 2010-2020 by MVTec Software GmbH
  9. * www.mvtec.com
  10. *
  11. *****************************************************************************
  12. *
  13. *
  14. *****************************************************************************/
  15. #ifndef HCPP_TUPLE_ELEMENT_H
  16. #define HCPP_TUPLE_ELEMENT_H
  17. #include "halconcpp/HString.h"
  18. #include "halconcpp/HSmartPtr.h"
  19. #include "halconcpp/HHandleBase.h"
  20. #include "halconcpp/HHandle.h"
  21. #ifndef HCPP_NO_OVERLOAD_TUPLE_OPERATORS
  22. // provide additional overloads for many operators involving HTuple or
  23. // HTupleElement
  24. # define HCPP_OVERLOAD_TUPLE_OPERATORS
  25. #endif
  26. #if defined(HCPP_OVERLOAD_TUPLE_OPERATORS)
  27. # if defined(HCPP_INT_OVERLOADS)
  28. # define H_COMPOUND_OP_INT_OVERLOAD_DECLARATION(RET, OP_COMPOUND) \
  29. RET& operator OP_COMPOUND(int val);
  30. # else
  31. # define H_COMPOUND_OP_INT_OVERLOAD_DECLARATION(RET, OP_COMPOUND)
  32. # endif
  33. # if defined(_WIN32)
  34. # define H_COMPOUND_OP_WCHAR_OVERLOAD_DECLARATION(RET, OP_COMPOUND) \
  35. RET& operator OP_COMPOUND(const wchar_t* val);
  36. # else
  37. # define H_COMPOUND_OP_WCHAR_OVERLOAD_DECLARATION(RET, OP_COMPOUND)
  38. # endif
  39. // shared usage in classes HTuple and HTupleElement
  40. # define H_COMPOUND_OP_OVERLOAD_DECLARATION(RET, OP_COMPOUND) \
  41. H_COMPOUND_OP_INT_OVERLOAD_DECLARATION(RET, OP_COMPOUND) \
  42. H_COMPOUND_OP_WCHAR_OVERLOAD_DECLARATION(RET, OP_COMPOUND) \
  43. RET& operator OP_COMPOUND(Hlong val); \
  44. RET& operator OP_COMPOUND(float val); \
  45. RET& operator OP_COMPOUND(double val); \
  46. RET& operator OP_COMPOUND(const HString& val); \
  47. RET& operator OP_COMPOUND(const char* val); \
  48. RET& operator OP_COMPOUND(const HTupleElement& val)
  49. #else
  50. # define H_COMPOUND_OP_OVERLOAD_DECLARATION(RET, OP_COMPOUND)
  51. #endif
  52. namespace HalconCpp
  53. {
  54. enum HTupleElementType
  55. {
  56. // The element is an integer value
  57. eElementTypeLong = eTupleTypeLong,
  58. // The element is a floating point value
  59. eElementTypeDouble = eTupleTypeDouble,
  60. // The element is a string
  61. eElementTypeString = eTupleTypeString,
  62. // The element is a handle
  63. eElementTypeHandle = eTupleTypeHandle,
  64. // The element is mixed
  65. eElementTypeMixed = eTupleTypeMixed,
  66. // The element type is undefined
  67. eElementTypeUndef = eTupleTypeEmpty
  68. };
  69. // Forward declarations for internal representation
  70. class HTupleData;
  71. /*****************************************************************************/
  72. /* HTupleElementData */
  73. /*****************************************************************************/
  74. class HTupleElementData : public HSmartPtrRef
  75. {
  76. public:
  77. HTupleElementData(HTupleData* source, Hlong *index, Hlong length);
  78. virtual ~HTupleElementData();
  79. virtual HTupleElementType Type() const;
  80. virtual HTupleElementType Type(Hlong idx) const;
  81. Hlong L() const {return getL(0);}
  82. Hlong L(Hlong idx) const {return getL(idx);}
  83. double D() const {return getD(0);}
  84. double D(Hlong idx) const {return getD(idx);}
  85. HString S() const {return getS(0);}
  86. HString S(Hlong idx) const {return getS(idx);}
  87. const char* C() const {return getC(0);}
  88. const char* C(Hlong idx) const {return getC(idx);}
  89. Hphandle H() const {return getH(0);}
  90. Hphandle H(Hlong idx) const {return getH(idx);}
  91. Hcpar P() const;
  92. Hcpar P(Hlong idx) const;
  93. virtual HTupleElementData& operator=(const HTupleElementData& element);
  94. HTupleData* GetSource() const {return mSource;}
  95. Hlong* GetIndex() const {return mIndex;}
  96. Hlong GetLength() const {return mLength;}
  97. void UpdateSource(HTupleData* source) {mSource = source;}
  98. virtual bool SupportsOptimizedTupleConversion() const {return false;}
  99. virtual HTuple ToTuple() const;
  100. protected:
  101. virtual double getD(Hlong idx) const;
  102. virtual Hlong getL(Hlong idx) const;
  103. virtual HString getS(Hlong idx) const;
  104. virtual const char* getC(Hlong idx) const;
  105. virtual Hphandle getH(Hlong idx) const;
  106. protected:
  107. // Refer back to source implementation
  108. HTupleData* mSource;
  109. Hlong* mIndex;
  110. Hlong mLength;
  111. };
  112. template<class T> class HSmartPtr;
  113. typedef HSmartPtr<HTupleElementData> HTupleElementDataPtr;
  114. #if defined(_WIN32)
  115. // In C++ an explicit template instantiation should appear at most once in
  116. // a program, so it shouldn't really be in a header file. However, without
  117. // this explicit instantiation, Visual C++ 2005 complains with a warning
  118. // C4251. According to Microsoft
  119. // (see http://support.microsoft.com/kb/168958/en-us), the explicit
  120. // instantiation is required. For many other systems, putting the instantiation
  121. // here is not required, but doesn't hurt either. However, there are some
  122. // systems (ARM, and MacOS when using llvm-gcc 4.2) where putting the explicit
  123. // instantiation here will lead to the linker complaining about multiple
  124. // defined symbols because the compiler does not create weak symbols for the
  125. // template functions, so we only put this in for Windows.
  126. template class LIntExport HSmartPtr<HTupleElementData>;
  127. #endif
  128. class LIntExport HTupleElement
  129. {
  130. public:
  131. /***************************************************************************/
  132. /* Constructors */
  133. /***************************************************************************/
  134. // Element for read access
  135. HTupleElement(HTuple* parent, Hlong index);
  136. HTupleElement(HTuple* parent, Hlong index[], Hlong length);
  137. // Element for write access
  138. HTupleElement(const HTuple* parent, Hlong index);
  139. HTupleElement(const HTuple* parent, Hlong index[], Hlong length);
  140. // copy constructor
  141. HTupleElement(const HTuple& tuple);
  142. // base type to tuple element constructors
  143. HTupleElement(Hlong l);
  144. #if defined(HCPP_INT_OVERLOADS)
  145. HTupleElement(int i);
  146. #endif
  147. HTupleElement(double d);
  148. #if defined(HCPP_LEGACY_HANDLE_API)
  149. explicit HTupleElement(const HHandle& h);
  150. #else
  151. HTupleElement(const HHandle& h);
  152. #endif
  153. HTupleElement(const HString& s);
  154. HTupleElement(const char* s);
  155. #ifdef _WIN32
  156. HTupleElement(const wchar_t* s);
  157. #endif
  158. virtual ~HTupleElement();
  159. // Return the data type stored in this tuple element
  160. HTupleElementType Type() const;
  161. HTupleElementType Type(Hlong idx) const;
  162. // Return the length of the referenced tuple
  163. Hlong ParentLength() const;
  164. bool IndexInBounds() const;
  165. // Return the index in the referenced tuple
  166. Hlong* Index() const;
  167. Hlong Length() const;
  168. // Return the integer value stored in this tuple element
  169. int I() const;
  170. // Return the integer value stored in this tuple element
  171. Hlong L(Hlong idx) const;
  172. Hlong L() const;
  173. // Return the double value stored in this tuple element
  174. double D(Hlong idx) const;
  175. double D() const;
  176. // Return the string value stored in this tuple element
  177. HString S(Hlong idx) const;
  178. HString S() const;
  179. // Return the handle value stored in this tuple element
  180. HHandle H(Hlong idx) const;
  181. HHandle H() const;
  182. // Return the char* value stored in this tuple element
  183. const char* C(Hlong idx) const;
  184. const char* C() const;
  185. /***************************************************************************/
  186. /* Type casts */
  187. /***************************************************************************/
  188. operator Hlong(void) const {return L();}
  189. #if defined(HCPP_INT_OVERLOADS)
  190. operator int(void) const {return (int) L();}
  191. #endif
  192. operator float(void) const {return (float)D();}
  193. operator double(void) const {return D();}
  194. #if (!defined(HCPP_LEGACY_HANDLE_API) || defined(_LIntDLL))
  195. operator HHandle(void) const {return H();}
  196. #endif
  197. operator HString(void) const {return S();}
  198. // the C type cast to const char* returns an internal raw pointer
  199. // representation of the string with string encoding which was set for the
  200. // HALCON/C++ interface
  201. // Note for Windows programmers: there is no cast to const wchar_t* because
  202. // there is no appropriate internal representation. Use HString if a
  203. // wchar_t* is needed.
  204. operator const char*(void) const {return C();}
  205. HTupleElement& operator=(const HTupleElement& element);
  206. H_COMPOUND_OP_OVERLOAD_DECLARATION(HTupleElement,+=);
  207. H_COMPOUND_OP_OVERLOAD_DECLARATION(HTupleElement,-=);
  208. H_COMPOUND_OP_OVERLOAD_DECLARATION(HTupleElement,*=);
  209. HTuple ToTuple() const;
  210. protected:
  211. void CreateElement(Hlong index[], Hlong length);
  212. protected:
  213. // Reference back to originating tuple
  214. HTuple* mParent;
  215. // Owning temporary parent for conversion purposes
  216. bool mAttached;
  217. // Typed element reference implementation
  218. HTupleElementDataPtr mElement;
  219. };
  220. }
  221. #endif