VR-Vantage 3.0 API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
DtUnicode.h
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright (c) 2020 MAK Technologies, Inc.
3 ** All rights reserved.
4 ******************************************************************************/
5 
8 
9 #pragma once
10 
11 #include <wchar.h>
12 #include <vrvUtil/DtUnicodeChar.h>
13 #include <boost/functional/hash.hpp>
14 #include <boost/serialization/split_free.hpp>
15 
16 #ifndef __rhentws7__
17 #include <atomic>
18 #endif
19 
20 namespace makVrv
21 {
22 
33  class DtUnicode
34  {
35  public:
36  typedef unsigned short CodeUnit;
37 
39  inline DtUnicode();
40 
42  inline virtual ~DtUnicode();
43 
45  explicit inline DtUnicode( size_t len, DtUnicodeChar c = DtUnicodeChar('\0'));
46 
50  inline DtUnicode(const DtUnicode& str);
51 
55  inline DtUnicode& operator=(const DtUnicode& str);
56 
58 
61  inline static DtUnicode fromWCharArray(const wchar_t utf16[], int len = -1);
62 
64  inline static DtUnicode fromWideString(const std::wstring& str);
65 
68  inline static DtUnicode fromUtf8(const unsigned char* utf8, int len = -1);
69  inline static DtUnicode fromUtf8(const std::string& utf8);
70 
73  inline static DtUnicode fromUtf16(const unsigned short* utf16, int len = -1);
74 
77  inline static DtUnicode fromUtf32(const unsigned int* utf32, int len = -1);
78 
81  inline static DtUnicode fromAscii(const std::string& ascii);
82 
85  inline static DtUnicode fromAscii(const char* ascii, int len = -1);
86 
92  inline static DtUnicode tr(const char* ascii)
93  {
94  return fromAscii(ascii);
95  }
96 
99  template <typename QStringType, typename QCoreApp>
100  QStringType translate() const
101  {
102  return QCoreApp::translate("makVrv::DtUnicode",toUtf8().c_str(), 0);
103  }
104 
106  template <typename QStringType>
107  inline static DtUnicode fromQString(QStringType qstr)
108  {
109  return fromUtf16(qstr.utf16(),qstr.length());
110  }
111 
113 
118  inline void setFromWCharArray(const wchar_t utf16[], int len = -1);
119 
122  inline void setFromUtf8(const unsigned char* utf8, int len = -1);
123  inline void setFromUtf8(const std::string& utf8);
124 
127  inline void setFromLatin1(const char* latin, int len = -1);
128 
131  inline void setFromUtf16(const unsigned short* utf16, int len = -1);
132 
135  inline void setFromUtf32(const unsigned int* utf32, int len = -1);
136 
139  inline void setFromAscii(const std::string& ascii);
140 
143  inline void setFromAscii(const char* ascii, int len = -1);
144 
146  template <typename QStringType>
147  inline void setFromQString(QStringType qstr)
148  {
149  setFromUtf16(qstr.utf16(),qstr.length());
150  }
151 
153 
156  inline void toAscii(std::string& str) const;
157  inline std::string toAscii() const { std::string temp; toAscii(temp); return temp;}
158 
161  inline void toLatin1(std::string& str) const;
162  inline std::string toLatin1() const { std::string temp; toLatin1(temp); return temp;}
163 
164  inline void toUtf8(std::string& str) const;
165  inline std::string toUtf8() const { std::string temp; toUtf8(temp); return temp;}
166  inline void toWideString(std::wstring& str) const;
167  inline std::wstring toWideString() const { std::wstring temp; toWideString(temp); return temp;}
168 
170  template <typename QStringType>
171  inline QStringType toQString() const
172  {
174  }
175 
177  template <typename QStringType>
178  inline void toQString(QStringType& qstr) const
179  {
180  qstr.setUtf16(codeUnits(),length());
181  }
182 
184  inline DtUnicode& operator+=(const DtUnicode& str);
186  inline DtUnicode& operator+=(const DtUnicodeChar& x);
188  inline DtUnicode operator+(const DtUnicode& str) const;
190  inline DtUnicode operator+(const DtUnicodeChar& character) const;
191 
193  inline bool operator==(const DtUnicode& other) const;
195  inline bool operator!=(const DtUnicode& other) const;
198  inline bool operator<(const DtUnicode& other) const;
199 
202  inline size_t length() const;
203 
205  inline size_t sizeInBytes() const { return length() << 1; }
206 
208  inline size_t calculateCodePoints() const;
209 
211  inline const CodeUnit* codeUnits() const { return myCodeUnits.begin; }
212 
214  inline int indexOf( const DtUnicodeChar& x, size_t pos = 0) const;
215 
217  inline int indexOf( const DtUnicode& x, size_t pos = 0) const;
218 
222  inline DtUnicode subString( size_t pos = 0, int len = -1 ) const;
223 
225  inline void push_back ( const DtUnicodeChar& x );
226 
234  inline void resize( int size );
235 
237  virtual bool containsRightToLeft() const;
238 
239  protected: // THESE are the protected functions not to be used directly.
240 
241  enum Encoding
242  {
246  };
247 
248  //Ascii CTOR
249  inline explicit DtUnicode(const char* ascii, int len = -1,Encoding encoding = Ascii);
250 
251  //UTF16 constructor
252  inline explicit DtUnicode(const unsigned short* utf16, int len = -1);
253 
254  //UTF32 constructor.
255  inline explicit DtUnicode(const unsigned int* utf32, int len = -1);
256 
257  //Std string constructor.
258  inline explicit DtUnicode(const std::string& ascii);
259 
260  //Concat constructor.
261  inline explicit DtUnicode(const DtUnicode& a, const DtUnicode& b);
262 
263  protected:
264 
267  {
268  inline CodeUnitArray();
269  inline CodeUnitArray(const CodeUnitArray&);
270  inline CodeUnitArray& operator=(const CodeUnitArray&);
271  inline ~CodeUnitArray();
272 
274  inline void resize(size_t newSize);
275 
278  inline void clear();
279 
281  inline void reserve(size_t capacity);
282 
283  //Calculate the capacity
284  inline size_t calculateCapacity(size_t necessarySize);
285 
286  //Return size in bytes
287  inline size_t size() const;
288 
289  //Return capacity in bytes
290  inline size_t capacity() const;
291 
292  //Concat another array, supports concat operations on itself.
293  inline void concat(const CodeUnitArray& other);
294 
296  inline bool unique() const;
297 
299  inline bool empty() const;
300 
302  inline void add_ref();
303 
305  inline void dec_ref();
306 
307 #ifdef __rhentws7__
308  volatile long* count;
309 #else
310  volatile std::atomic_long* count;
311 #endif
315  };
317  };
318 }
319 
320 //Allows DtUnicode to be used in boost hash containers.
321 namespace makVrv
322 {
323  inline std::size_t hash_value(DtUnicode const& val)
324  {
325  //Copied from TR1 implementation.
326 
327  size_t seed = 2166136261U;
328  size_t first = 0;
329  size_t last = val.length();
330  size_t stride = 1 + last / 10;
331 
332  if (stride < last)
333  {
334  last -= stride;
335  }
336  for(; first < last ; first += stride )
337  {
338  seed = 16777619U * seed ^ (size_t)val.codeUnits()[first];
339  }
340  return seed;
341  }
342 }
343 
344 #include "DtUnicode.inl"


Copyright © 2005-2021 MAK Technologies. All Rights Reserved (www.mak.com)