HTupleElement.h 9.1 KB

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