123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- /*****************************************************************************
- * HTupleElement.h
- *****************************************************************************
- *
- * Project: HALCON/C++
- * Description: Element of tuple used for control parameters
- *
- * (c) 2010-2020 by MVTec Software GmbH
- * www.mvtec.com
- *
- *****************************************************************************
- *
- *
- *****************************************************************************/
- #ifndef HCPP_TUPLE_ELEMENT_H
- #define HCPP_TUPLE_ELEMENT_H
- // clang-format off
- #include "halconcpp/HString.h"
- #include "halconcpp/HSmartPtr.h"
- #include "halconcpp/HHandleBase.h"
- #include "halconcpp/HHandle.h"
- // clang-format on
- #ifndef HCPP_NO_OVERLOAD_TUPLE_OPERATORS
- // provide additional overloads for many operators involving HTuple or
- // HTupleElement
- # define HCPP_OVERLOAD_TUPLE_OPERATORS
- #endif
- #if defined(HCPP_OVERLOAD_TUPLE_OPERATORS)
- # if defined(HCPP_INT_OVERLOADS)
- # define H_COMPOUND_OP_INT_OVERLOAD_DECLARATION(RET, OP_COMPOUND) \
- RET& operator OP_COMPOUND(int val);
- # else
- # define H_COMPOUND_OP_INT_OVERLOAD_DECLARATION(RET, OP_COMPOUND)
- # endif
- # if defined(_WIN32)
- # define H_COMPOUND_OP_WCHAR_OVERLOAD_DECLARATION(RET, OP_COMPOUND) \
- RET& operator OP_COMPOUND(const wchar_t* val);
- # else
- # define H_COMPOUND_OP_WCHAR_OVERLOAD_DECLARATION(RET, OP_COMPOUND)
- # endif
- // shared usage in classes HTuple and HTupleElement
- # define H_COMPOUND_OP_OVERLOAD_DECLARATION(RET, OP_COMPOUND) \
- H_COMPOUND_OP_INT_OVERLOAD_DECLARATION(RET, OP_COMPOUND) \
- H_COMPOUND_OP_WCHAR_OVERLOAD_DECLARATION(RET, OP_COMPOUND) \
- RET& operator OP_COMPOUND(Hlong val); \
- RET& operator OP_COMPOUND(float val); \
- RET& operator OP_COMPOUND(double val); \
- RET& operator OP_COMPOUND(const HString& val); \
- RET& operator OP_COMPOUND(const char* val); \
- RET& operator OP_COMPOUND(const HTupleElement& val)
- #else
- # define H_COMPOUND_OP_OVERLOAD_DECLARATION(RET, OP_COMPOUND)
- #endif
- namespace HalconCpp
- {
- enum HTupleElementType
- {
- // The element is an integer value
- eElementTypeLong = eTupleTypeLong,
- // The element is a floating point value
- eElementTypeDouble = eTupleTypeDouble,
- // The element is a string
- eElementTypeString = eTupleTypeString,
- // The element is a handle
- eElementTypeHandle = eTupleTypeHandle,
- // The element is mixed
- eElementTypeMixed = eTupleTypeMixed,
- // The element type is undefined
- eElementTypeUndef = eTupleTypeEmpty
- };
- // Forward declarations for internal representation
- class HTupleData;
- /*****************************************************************************/
- /* HTupleElementData */
- /*****************************************************************************/
- class HTupleElementData : public HSmartPtrRef
- {
- public:
- HTupleElementData(HTupleData* source, Hlong* index, Hlong length);
- virtual ~HTupleElementData();
- virtual HTupleElementType Type() const;
- virtual HTupleElementType Type(Hlong idx) const;
- Hlong L() const
- {
- return getL(0);
- }
- Hlong L(Hlong idx) const
- {
- return getL(idx);
- }
- double D() const
- {
- return getD(0);
- }
- double D(Hlong idx) const
- {
- return getD(idx);
- }
- HString S() const
- {
- return getS(0);
- }
- HString S(Hlong idx) const
- {
- return getS(idx);
- }
- const char* C() const
- {
- return getC(0);
- }
- const char* C(Hlong idx) const
- {
- return getC(idx);
- }
- Hphandle H() const
- {
- return getH(0);
- }
- Hphandle H(Hlong idx) const
- {
- return getH(idx);
- }
- Hcpar P() const;
- Hcpar P(Hlong idx) const;
- virtual HTupleElementData& operator=(const HTupleElementData& element);
- HTupleData* GetSource() const
- {
- return mSource;
- }
- Hlong* GetIndex() const
- {
- return mIndex;
- }
- Hlong GetLength() const
- {
- return mLength;
- }
- void UpdateSource(HTupleData* source)
- {
- mSource = source;
- }
- virtual bool SupportsOptimizedTupleConversion() const
- {
- return false;
- }
- virtual HTuple ToTuple() const;
- protected:
- virtual double getD(Hlong idx) const;
- virtual Hlong getL(Hlong idx) const;
- virtual HString getS(Hlong idx) const;
- virtual const char* getC(Hlong idx) const;
- virtual Hphandle getH(Hlong idx) const;
- protected:
- // Refer back to source implementation
- HTupleData* mSource;
- Hlong* mIndex;
- Hlong mLength;
- };
- template<class T> class HSmartPtr;
- typedef HSmartPtr<HTupleElementData> HTupleElementDataPtr;
- #if defined(_WIN32)
- // In C++ an explicit template instantiation should appear at most once in
- // a program, so it shouldn't really be in a header file. However, without
- // this explicit instantiation, Visual C++ 2005 complains with a warning
- // C4251. According to Microsoft
- // (see http://support.microsoft.com/kb/168958/en-us), the explicit
- // instantiation is required. For many other systems, putting the instantiation
- // here is not required, but doesn't hurt either. However, there are some
- // systems (ARM, and MacOS when using llvm-gcc 4.2) where putting the explicit
- // instantiation here will lead to the linker complaining about multiple
- // defined symbols because the compiler does not create weak symbols for the
- // template functions, so we only put this in for Windows.
- template class LIntExport HSmartPtr<HTupleElementData>;
- #endif
- class LIntExport HTupleElement
- {
- public:
- /***************************************************************************/
- /* Constructors */
- /***************************************************************************/
- // Element for read access
- HTupleElement(HTuple* parent, Hlong index);
- HTupleElement(HTuple* parent, Hlong index[], Hlong length);
- // Element for write access
- HTupleElement(const HTuple* parent, Hlong index);
- HTupleElement(const HTuple* parent, Hlong index[], Hlong length);
- // copy constructor
- HTupleElement(const HTuple& tuple);
- // base type to tuple element constructors
- HTupleElement(Hlong l);
- #if defined(HCPP_INT_OVERLOADS)
- HTupleElement(int i);
- #endif
- HTupleElement(double d);
- #if defined(HCPP_LEGACY_HANDLE_API)
- explicit HTupleElement(const HHandle& h);
- #else
- HTupleElement(const HHandle& h);
- #endif
- HTupleElement(const HString& s);
- HTupleElement(const char* s);
- #ifdef _WIN32
- HTupleElement(const wchar_t* s);
- #endif
- virtual ~HTupleElement();
- // Return the data type stored in this tuple element
- HTupleElementType Type() const;
- HTupleElementType Type(Hlong idx) const;
- // Return the length of the referenced tuple
- Hlong ParentLength() const;
- bool IndexInBounds() const;
- // Return the index in the referenced tuple
- Hlong* Index() const;
- Hlong Length() const;
- // Return the integer value stored in this tuple element
- int I() const;
- // Return the integer value stored in this tuple element
- Hlong L(Hlong idx) const;
- Hlong L() const;
- // Return the double value stored in this tuple element
- double D(Hlong idx) const;
- double D() const;
- // Return the string value stored in this tuple element
- HString S(Hlong idx) const;
- HString S() const;
- // Return the handle value stored in this tuple element
- HHandle H(Hlong idx) const;
- HHandle H() const;
- // Return the char* value stored in this tuple element
- const char* C(Hlong idx) const;
- const char* C() const;
- /***************************************************************************/
- /* Type casts */
- /***************************************************************************/
- operator Hlong(void) const
- {
- return L();
- }
- #if defined(HCPP_INT_OVERLOADS)
- operator int(void) const
- {
- return (int)L();
- }
- #endif
- operator float(void) const
- {
- return (float)D();
- }
- operator double(void) const
- {
- return D();
- }
- #if (!defined(HCPP_LEGACY_HANDLE_API) || defined(_LIntDLL))
- operator HHandle(void) const
- {
- return H();
- }
- #endif
- operator HString(void) const
- {
- return S();
- }
- // the C type cast to const char* returns an internal raw pointer
- // representation of the string with string encoding which was set for the
- // HALCON/C++ interface
- // Note for Windows programmers: there is no cast to const wchar_t* because
- // there is no appropriate internal representation. Use HString if a
- // wchar_t* is needed.
- operator const char*(void) const
- {
- return C();
- }
- HTupleElement& operator=(const HTupleElement& element);
- H_COMPOUND_OP_OVERLOAD_DECLARATION(HTupleElement, +=);
- H_COMPOUND_OP_OVERLOAD_DECLARATION(HTupleElement, -=);
- H_COMPOUND_OP_OVERLOAD_DECLARATION(HTupleElement, *=);
- HTuple ToTuple() const;
- protected:
- void CreateElement(Hlong index[], Hlong length);
- protected:
- // Reference back to originating tuple
- HTuple* mParent;
- // Owning temporary parent for conversion purposes
- bool mAttached;
- // Typed element reference implementation
- HTupleElementDataPtr mElement;
- };
- }
- #endif
|