VR-Forces 4.6 Class 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) 2014 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 namespace makVrv
17 {
18 
29  class DtUnicode
30  {
31  public:
32  typedef unsigned short CodeUnit;
33 
35  inline DtUnicode();
36 
38  inline virtual ~DtUnicode();
39 
41  explicit inline DtUnicode( size_t len, DtUnicodeChar c = DtUnicodeChar('\0'));
42 
46  inline DtUnicode(const DtUnicode& str);
47 
51  inline DtUnicode& operator=(const DtUnicode& str);
52 
54 
57  inline static DtUnicode fromWCharArray(const wchar_t utf16[], int len = -1);
58 
60  inline static DtUnicode fromWideString(const std::wstring& str);
61 
64  inline static DtUnicode fromUtf8(const unsigned char* utf8, int len = -1);
65  inline static DtUnicode fromUtf8(const std::string& utf8);
66 
69  inline static DtUnicode fromUtf16(const unsigned short* utf16, int len = -1);
70 
73  inline static DtUnicode fromUtf32(const unsigned int* utf32, int len = -1);
74 
77  inline static DtUnicode fromAscii(const std::string& ascii);
78 
81  inline static DtUnicode fromAscii(const char* ascii, int len = -1);
82 
88  inline static DtUnicode tr(const char* ascii)
89  {
90  return fromAscii(ascii);
91  }
92 
95  template <typename QStringType, typename QCoreApp>
96  QStringType translate() const
97  {
98  return QCoreApp::translate("makVrv::DtUnicode",toUtf8().c_str(), 0);
99  }
100 
102  template <typename QStringType>
103  inline static DtUnicode fromQString(QStringType qstr)
104  {
105  return fromUtf16(qstr.utf16(),qstr.length());
106  }
107 
109 
114  inline void setFromWCharArray(const wchar_t utf16[], int len = -1);
115 
118  inline void setFromUtf8(const unsigned char* utf8, int len = -1);
119  inline void setFromUtf8(const std::string& utf8);
120 
123  inline void setFromLatin1(const char* latin, int len = -1);
124 
127  inline void setFromUtf16(const unsigned short* utf16, int len = -1);
128 
131  inline void setFromUtf32(const unsigned int* utf32, int len = -1);
132 
135  inline void setFromAscii(const std::string& ascii);
136 
139  inline void setFromAscii(const char* ascii, int len = -1);
140 
142  template <typename QStringType>
143  inline void setFromQString(QStringType qstr)
144  {
145  setFromUtf16(qstr.utf16(),qstr.length());
146  }
147 
149 
152  inline void toAscii(std::string& str) const;
153  inline std::string toAscii() const { std::string temp; toAscii(temp); return temp;}
154 
157  inline void toLatin1(std::string& str) const;
158  inline std::string toLatin1() const { std::string temp; toLatin1(temp); return temp;}
159 
160  inline void toUtf8(std::string& str) const;
161  inline std::string toUtf8() const { std::string temp; toUtf8(temp); return temp;}
162  inline void toWideString(std::wstring& str) const;
163  inline std::wstring toWideString() const { std::wstring temp; toWideString(temp); return temp;}
164 
166  template <typename QStringType>
167  inline QStringType toQString() const
168  {
170  }
171 
173  template <typename QStringType>
174  inline void toQString(QStringType& qstr) const
175  {
176  qstr.setUtf16(codeUnits(),length());
177  }
178 
180  inline DtUnicode& operator+=(const DtUnicode& str);
182  inline DtUnicode& operator+=(const DtUnicodeChar& x);
184  inline DtUnicode operator+(const DtUnicode& str) const;
185 
187  inline bool operator==(const DtUnicode& other) const;
189  inline bool operator!=(const DtUnicode& other) const;
192  inline bool operator<(const DtUnicode& other) const;
193 
196  inline size_t length() const;
197 
199  inline size_t sizeInBytes() const { return length() << 1; }
200 
202  inline size_t calculateCodePoints() const;
203 
205  inline const CodeUnit* codeUnits() const { return myCodeUnits.begin; }
206 
208  inline int indexOf( const DtUnicodeChar& x, size_t pos = 0) const;
209 
211  inline int indexOf( const DtUnicode& x, size_t pos = 0) const;
212 
216  inline DtUnicode subString( size_t pos = 0, int len = -1 ) const;
217 
219  inline void push_back ( const DtUnicodeChar& x );
220 
228  inline void resize( int size );
229 
231  virtual bool containsRightToLeft() const;
232 
233  protected: // THESE are the protected functions not to be used directly.
234 
235  enum Encoding
236  {
240  };
241 
242  //Ascii CTOR
243  inline explicit DtUnicode(const char* ascii, int len = -1,Encoding encoding = Ascii);
244 
245  //UTF16 constructor
246  inline explicit DtUnicode(const unsigned short* utf16, int len = -1);
247 
248  //UTF32 constructor.
249  inline explicit DtUnicode(const unsigned int* utf32, int len = -1);
250 
251  //Std string constructor.
252  inline explicit DtUnicode(const std::string& ascii);
253 
254  //Concat constructor.
255  inline explicit DtUnicode(const DtUnicode& a, const DtUnicode& b);
256 
257  protected:
258 
261  {
262  inline CodeUnitArray();
263  inline CodeUnitArray(const CodeUnitArray&);
264  inline CodeUnitArray& operator=(const CodeUnitArray&);
265  inline ~CodeUnitArray();
266 
268  inline void resize(size_t newSize);
269 
272  inline void clear();
273 
275  inline void reserve(size_t capacity);
276 
277  //Calculate the capacity
278  inline size_t calculateCapacity(size_t necessarySize);
279 
280  //Return size in bytes
281  inline size_t size() const;
282 
283  //Return capacity in bytes
284  inline size_t capacity() const;
285 
286  //Concat another array, supports concat operations on itself.
287  inline void concat(const CodeUnitArray& other);
288 
290  inline bool unique() const;
291 
293  inline bool empty() const;
294 
296  inline void add_ref();
297 
299  inline void dec_ref();
300 
301  volatile long* count;
305  };
307  };
308 }
309 
310 //Allows DtUnicode to be used in boost hash containers.
311 namespace makVrv
312 {
313  inline std::size_t hash_value(DtUnicode const& val)
314  {
315  //Copied from TR1 implementation.
316 
317  size_t seed = 2166136261U;
318  size_t first = 0;
319  size_t last = val.length();
320  size_t stride = 1 + last / 10;
321 
322  if (stride < last)
323  {
324  last -= stride;
325  }
326  for(; first < last ; first += stride )
327  {
328  seed = 16777619U * seed ^ (size_t)val.codeUnits()[first];
329  }
330  return seed;
331  }
332 }
333 
334 #include "DtUnicode.inl"

Document ID: Generated on Thu Apr 12 03:15:37 EDT 2018 from SVN revision 187986
Copyright © 2005-2018 VT MÄK. All Rights Reserved (www.mak.com)