HString.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*****************************************************************************
  2. * HString.h
  3. *****************************************************************************
  4. *
  5. * Project: HALCON/C++
  6. * Description: String class for automatic memory management
  7. *
  8. * (c) 2010-2022 by MVTec Software GmbH
  9. * www.mvtec.com
  10. *
  11. *****************************************************************************
  12. *
  13. *
  14. *****************************************************************************/
  15. #ifndef HCPP_STRING_H
  16. #define HCPP_STRING_H
  17. namespace HalconCpp
  18. {
  19. class HStringData;
  20. class HTupleElement;
  21. class LIntExport HString
  22. {
  23. public:
  24. // Create an empty string
  25. HString();
  26. // Create from C string
  27. HString(const char* text);
  28. #if _WIN32
  29. // Create from wide character string
  30. HString(const wchar_t* text);
  31. #endif
  32. // Copy constructor
  33. HString(const HString& string);
  34. // Destroy string
  35. virtual ~HString();
  36. // Assignment operator
  37. HString& operator=(const HString& string);
  38. // String equality
  39. bool operator==(const HString& string) const;
  40. bool operator!=(const HString& string) const;
  41. bool operator==(const char* string) const;
  42. bool operator!=(const char* string) const;
  43. #if _WIN32
  44. bool operator==(const wchar_t* string) const;
  45. bool operator!=(const wchar_t* string) const;
  46. #endif
  47. // Concatenation
  48. HString operator+(const HString& string);
  49. HTuple operator+(const HTuple& tuple);
  50. HTuple operator+(const HTupleElement& te);
  51. HString& operator+=(const HString& string);
  52. HString& operator+=(const char* c);
  53. #if _WIN32
  54. HString& operator+=(const wchar_t* c);
  55. #endif
  56. /* Methods */
  57. // Clear contents of string
  58. void Clear();
  59. // Test for empty string
  60. bool IsEmpty() const;
  61. // ANSI implementations of the Length and Text method
  62. // Length of the char* string in bytes
  63. // note that the returned length of a string with non-ASCII characters
  64. // depends on the encoding and can be greater than the number of characters
  65. size_t LengthA() const;
  66. // Pointer to string data (internal char* representation with
  67. // the selected interface encoding)
  68. // the pointer is valid as long as the HString is not modified
  69. const char* TextA() const;
  70. // implicit cast operator to char* C string
  71. operator const char*(void) const
  72. {
  73. return TextA();
  74. }
  75. #ifdef _WIN32
  76. // wide char implementations of the Length and Text method (Windows only)
  77. // Length of the wchar_t* string in wchar_t words
  78. // note that the length of a string which contains Unicode characters which
  79. // are not part of the Basic Multilingual Plane (BMP, code points > 0xffff)
  80. // is greater than the number of characters
  81. size_t LengthW() const;
  82. # ifdef _NATIVE_WCHAR_T_DEFINED
  83. // pointer to wide string data (internal wchar_t* representation with
  84. // UTF-16 encoding)
  85. // and implicit cast operator to wchar_t* C string
  86. // the pointer is valid as long as the HString is not modified
  87. const wchar_t* TextW() const;
  88. operator const wchar_t*(void) const;
  89. # else
  90. const wchar_t* TextW() const
  91. {
  92. return TextUS();
  93. }
  94. # endif
  95. // the following methods are for being compatible with applications which
  96. // are compiled with /Zc:wchar_t-
  97. const unsigned short* TextUS() const;
  98. operator const unsigned short*(void) const;
  99. #endif
  100. // For compatibility reasons the functions Text and Length are defined
  101. // with the char* semantics.
  102. // Please note the encoding of the returned strings now depends on the
  103. // interface encoding and can be set via SetHcppInterfaceStringEncodingIsUtf8
  104. // Length of (char*) string (in the current interface encoding, in bytes)
  105. size_t Length() const
  106. {
  107. return LengthA();
  108. }
  109. // Pointer to string data (internal char* representation with
  110. // the selected interface encoding)
  111. // the pointer is valid as long as the HString is not modified
  112. const char* Text() const
  113. {
  114. return TextA();
  115. }
  116. // transcoding functions
  117. //
  118. // these functions return a pointer to memory owned by this HString instance
  119. // the pointer becomes invalid, when the string is changed.
  120. const char* ToLocal8bit() const;
  121. const char* ToUtf8() const;
  122. //
  123. static HString FromLocal8bit(const char* str);
  124. static HString FromUtf8(const char* str);
  125. protected:
  126. // Internal constructor
  127. HString(const HStringData& text);
  128. // Data container
  129. HStringData& mText;
  130. };
  131. }
  132. #endif