GCString.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /****************************************************************************
  2. (c) 2005 by STEMMER IMAGING
  3. // License: This file is published under the license of the EMVA GenICam Standard Group.
  4. // A text file describing the legal terms is included in your installation as 'GenICam_license.pdf'.
  5. // If for some reason you are missing this file please contact the EMVA or visit the website
  6. // (http://www.genicam.org) for a full copy.
  7. //
  8. // THIS SOFTWARE IS PROVIDED BY THE EMVA GENICAM STANDARD GROUP "AS IS"
  9. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  10. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  11. // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE EMVA GENICAM STANDARD GROUP
  12. // OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  13. // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  14. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  15. // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  16. // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  17. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  18. // POSSIBILITY OF SUCH DAMAGE.
  19. ****************************************************************************/
  20. /// \file
  21. /// \brief Portable string implementation
  22. /// \ingroup Base_PublicUtilities
  23. #ifndef GENICAM_GCSTRING_H
  24. #define GENICAM_GCSTRING_H
  25. #include <new>
  26. #include <string>
  27. #include <iostream>
  28. #include <Base/GCTypes.h>
  29. #pragma pack(push, 8)
  30. /**
  31. \brief Indicates either 'not found' or 'all remaining characters'.
  32. \ingroup GenApi_Implementation
  33. */
  34. #define GCSTRING_NPOS size_t(-1)
  35. namespace GENICAM_NAMESPACE
  36. {
  37. /**
  38. \brief A string class which is a clone of std::string
  39. \ingroup Base_PublicUtilities
  40. */
  41. class GCBASE_API gcstring
  42. {
  43. # if defined(_MSC_VER) && !defined(PHARLAP_WIN32)
  44. public:
  45. //! Helper class for storing shared-ownership wchar_t *
  46. class GCBASE_API gcwchar
  47. {
  48. public:
  49. //! Creates a buffer of \a n wchar_ts
  50. explicit gcwchar( size_t n );
  51. // copy constructor
  52. gcwchar( const gcwchar &rhs );
  53. // assignment operator
  54. gcwchar & operator = (const gcwchar &rhs);
  55. //! Gets the c-string.
  56. const wchar_t * c_str() const;
  57. //! Gets the length of the buffer.
  58. size_t length() const;
  59. //! destructor
  60. ~gcwchar();
  61. private:
  62. class impl;
  63. impl *m_pimpl;
  64. };
  65. # endif
  66. // Ctor / Dtor
  67. // -------------------------------------------------------------------------
  68. public:
  69. gcstring ();
  70. gcstring ( const char *pc );
  71. gcstring ( const char *pc, size_t n );
  72. gcstring ( size_t count, char ch );
  73. gcstring ( const gcstring &str );
  74. # if defined(_MSC_VER) && !defined(PHARLAP_WIN32)
  75. explicit gcstring ( const wchar_t *pBufferUTF16 );
  76. gcstring ( const wchar_t *pBufferUTF16, size_t n );
  77. # endif
  78. virtual ~gcstring ( void );
  79. // Data access
  80. // -------------------------------------------------------------------------
  81. public:
  82. virtual gcstring & append ( const gcstring &str );
  83. virtual gcstring & append ( size_t count, char ch );
  84. virtual gcstring & assign ( const gcstring &str );
  85. virtual gcstring & assign ( size_t count, char ch );
  86. virtual gcstring & assign ( const char *pc );
  87. virtual gcstring & assign ( const char *pc, size_t n );
  88. # if defined(_MSC_VER) && !defined(PHARLAP_WIN32)
  89. virtual gcstring & assign ( const wchar_t *pStringUTF16 );
  90. virtual gcstring & assign ( const wchar_t *pStringUTF16, int n );
  91. # endif
  92. virtual int compare ( const gcstring &str ) const;
  93. # if defined(_MSC_VER) && !defined(PHARLAP_WIN32)
  94. virtual gcwchar w_str ( void ) const;
  95. # endif
  96. virtual const char *c_str ( void ) const;
  97. virtual bool empty ( void ) const;
  98. virtual size_t find ( char ch, size_t offset = 0 ) const;
  99. virtual size_t find ( const gcstring &str, size_t offset = 0 ) const;
  100. virtual size_t find ( const gcstring &str, size_t offset, size_t count ) const;
  101. virtual size_t find ( const char* pc, size_t offset = 0) const;
  102. virtual size_t find ( const char* pc, size_t offset, size_t count ) const;
  103. virtual size_t length ( void ) const;
  104. virtual size_t size ( void ) const;
  105. virtual void resize ( size_t n );
  106. virtual size_t max_size ( ) const;
  107. virtual gcstring substr ( size_t offset = 0, size_t count = GCSTRING_NPOS ) const;
  108. virtual size_t find_first_of ( const gcstring &str, size_t offset = 0 ) const;
  109. virtual size_t find_first_not_of ( const gcstring &str, size_t offset = 0 ) const;
  110. static size_t _npos ( void );
  111. virtual void swap ( gcstring &Right );
  112. // Operators
  113. // -------------------------------------------------------------------------
  114. public:
  115. bool operator != ( const gcstring &str ) const;
  116. bool operator != ( const char *pc ) const;
  117. gcstring & operator += ( const gcstring &str );
  118. gcstring operator += ( const gcstring &str ) const;
  119. gcstring & operator += ( const char *pc );
  120. gcstring & operator += ( char ch );
  121. gcstring operator += ( char ch ) const;
  122. gcstring & operator = ( const gcstring &str );
  123. # if defined(_MSC_VER) && !defined(PHARLAP_WIN32)
  124. gcstring & operator = ( const wchar_t *pStringUTF16 );
  125. # endif
  126. bool operator == ( const gcstring &str ) const;
  127. bool operator == ( const char *pc ) const;
  128. bool operator < ( const gcstring &str ) const;
  129. bool operator > ( const gcstring &str ) const;
  130. operator const char * ( void ) const;
  131. void operator delete ( void *pWhere );
  132. void operator delete ( void *pWhere, void *pNewWhere );
  133. void * operator new ( size_t uiSize );
  134. void * operator new ( size_t uiSize, void *pWhere );
  135. GCBASE_API
  136. friend gcstring operator + ( const gcstring &left, const gcstring &right );
  137. GCBASE_API
  138. friend gcstring operator + ( const gcstring &left, const char *right );
  139. GCBASE_API
  140. friend gcstring operator + ( const char *left, const gcstring &right );
  141. // Member
  142. // -------------------------------------------------------------------------
  143. private:
  144. // redundant pointer to make the possible to see the contents of the string in the debugger
  145. const char* m_psz;
  146. // actual std::string object
  147. uint8_t m_opaqueData[64];
  148. // Constants
  149. // -------------------------------------------------------------------------
  150. public:
  151. // Beware : this static member prevents delay loading
  152. // use _npos() instead
  153. static const size_t npos;
  154. };
  155. }
  156. namespace GENICAM_NAMESPACE
  157. {
  158. // this is necessary due to the circular dependency between string/exception
  159. GCBASE_API void ThrowBadAlloc(const char *source, int line);
  160. //! STL getline
  161. //! \ingroup Base_PublicUtilities
  162. inline std::istream & getline(std::istream& is, GENICAM_NAMESPACE::gcstring& str)
  163. {
  164. try
  165. {
  166. std::string tmp;
  167. std::getline(is, tmp);
  168. str.assign(tmp.c_str(), tmp.size());
  169. }
  170. catch(std::bad_alloc &)
  171. {
  172. ThrowBadAlloc(__FILE__, __LINE__);
  173. }
  174. return is;
  175. }
  176. //! STL getline
  177. //! \ingroup Base_PublicUtilities
  178. inline std::istream & getline(std::istream& is, GENICAM_NAMESPACE::gcstring& str, char delim)
  179. {
  180. try
  181. {
  182. std::string tmp;
  183. std::getline(is, tmp, delim);
  184. str.assign(tmp.c_str(), tmp.size());
  185. }
  186. catch(std::bad_alloc &)
  187. {
  188. ThrowBadAlloc(__FILE__, __LINE__);
  189. }
  190. return is;
  191. }
  192. }
  193. //! STL operator out
  194. //! \ingroup Base_PublicUtilities
  195. inline std::ostream & operator <<(std::ostream &ostr, const GENICAM_NAMESPACE::gcstring &str)
  196. {
  197. try
  198. {
  199. // use formatted output operator of std::string
  200. ostr << str.c_str();
  201. }
  202. catch(std::bad_alloc &)
  203. {
  204. GENICAM_NAMESPACE::ThrowBadAlloc(__FILE__, __LINE__);
  205. }
  206. return ostr;
  207. }
  208. //! STL operator in
  209. //! \ingroup Base_PublicUtilities
  210. inline std::istream & operator >>(std::istream &istr, GENICAM_NAMESPACE::gcstring &str)
  211. {
  212. try
  213. {
  214. std::string tmp;
  215. istr >> tmp;
  216. str.assign(tmp.c_str(), tmp.size());
  217. }
  218. catch(std::bad_alloc &)
  219. {
  220. GENICAM_NAMESPACE::ThrowBadAlloc(__FILE__, __LINE__);
  221. }
  222. return istr;
  223. }
  224. #pragma pack(pop)
  225. #endif // GENICAM_GCSTRING_H