HHandleBase.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*****************************************************************************
  2. * HHandleBase.h
  3. *****************************************************************************
  4. *
  5. * Project: HALCON/C++
  6. * Description: Base class for wrapping all handle types
  7. *
  8. * (c) 2017-2020 by MVTec Software GmbH
  9. * www.mvtec.com
  10. *
  11. *****************************************************************************
  12. *
  13. *
  14. *****************************************************************************/
  15. #ifndef HCPP_HANDLE_BASE_H
  16. #define HCPP_HANDLE_BASE_H
  17. #include "Halcon.h"
  18. #include "halconcpp/HString.h"
  19. namespace HalconCpp
  20. {
  21. class LIntExport HHandleBase
  22. {
  23. friend class HTuple;
  24. friend class HTupleElement;
  25. friend class HalconAPI;
  26. friend class HalconAPIInternal;
  27. public:
  28. // Create an uninitialized handle
  29. HHandleBase();
  30. // Creation from numeric handle, for interacting with legacy code
  31. // or for transferring handle across language interface boundaries
  32. explicit HHandleBase(Hlong handle);
  33. // Copy constructor
  34. HHandleBase(const HHandleBase &handle);
  35. // Destructor
  36. virtual ~HHandleBase();
  37. // Set from numeric handle, for interacting with legacy code or
  38. // for transferring handle across language interface boundaries
  39. void SetHandle(Hlong handle);
  40. // Return the managed handle
  41. Hlong GetHandle() const;
  42. // Return true if this handle instance manages a valid handle. Returns
  43. // false if the managed handle has already been destroyed or when this
  44. // instance manages no handle (uninitialized or cleared). Equivalent to
  45. // new HALCON operator HandleIsValid but kept for convenience of having
  46. // boolean signature and backwards compatible name.
  47. bool IsInitialized() const;
  48. // Return a textual representation of the handle. This is mainly for
  49. // logging and debugging purposes, result should not be parsed and text
  50. // format may change in the future.
  51. HString ToString() const;
  52. // Release the managed handle (decreases reference count of handle)
  53. void Clear();
  54. /******************************************************************/
  55. /* Operator overloads */
  56. /******************************************************************/
  57. // Assignment
  58. HHandleBase& operator = (const HHandleBase& obj);
  59. // Comparison
  60. bool operator==(const HHandleBase& obj) const;
  61. bool operator!=(const HHandleBase& obj) const;
  62. #if defined(HCPP_LEGACY_HANDLE_API)
  63. // Extraction to long is now rare use case and should be done explicitely
  64. operator Hlong() const;
  65. // For all practical purposes now handled by HHandle constructor of HTuple
  66. operator HTuple() const;
  67. #endif
  68. /******************************************************************/
  69. /* Internal operations, exposed for use by extension package only */
  70. /******************************************************************/
  71. // Load output handle from HALCON operator call
  72. void Load(Hproc_handle proc, HINT par_index, Herror err) const;
  73. // Store as input handle for HALCON operator call
  74. Herror Store(Hproc_handle proc, HINT par_index) const;
  75. /******************************************************************/
  76. /* Legacy handle management */
  77. /******************************************************************/
  78. #if defined(HCPP_LEGACY_HANDLE_API)
  79. // Not useful with new reference counting mechanism
  80. void Detach();
  81. // Not useful with new reference counting mechanism
  82. void InvalidateHandle();
  83. // Deprecated, replaced by IsInitialized
  84. bool IsToolValid() const { return IsInitialized(); };
  85. #endif
  86. protected:
  87. // Over management of input handle
  88. HHandleBase(Hphandle handle, bool copy);
  89. // Return the managed handle
  90. Hphandle GetHandleInternal() const;
  91. // For copy=false takes over management of input handle
  92. void SetHandleInternal(Hphandle handle, bool copy);
  93. // Verify type of external handle, raise exception on mismatch
  94. virtual void AssertType(Hphandle handle) const;
  95. // Verify type of wrapped handle, raise exception on mismatch
  96. void AssertType() const;
  97. protected:
  98. static const Hphandle HANDLE_INVALID;
  99. Hphandle mHandle;
  100. };
  101. // Base class for actual tool array classes
  102. class LIntExport HHandleBaseArray
  103. {
  104. public:
  105. // Destructor
  106. virtual ~HHandleBaseArray() {};
  107. // Clears array and all tool instances
  108. virtual void Clear() = 0;
  109. // Get number of tools
  110. virtual Hlong Length() const = 0;
  111. // Create tool array from tuple of handles
  112. virtual void SetFromTuple(const HTuple& handles) = 0;
  113. // Get tuple of handles for tool array
  114. virtual HTuple ConvertToTuple() const = 0;
  115. };
  116. // Forward declaration
  117. class HHandle;
  118. // Constant for supporting export of HNULL literal from HDevelop
  119. extern const LIntExport HHandle HNULL;
  120. }
  121. #endif