VR-Forces 4.3 Class Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 ~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(),
99  0, QCoreApp::UnicodeUTF8);
100  }
101 
103  template <typename QStringType>
104  inline static DtUnicode fromQString(QStringType qstr)
105  {
106  return fromUtf16(qstr.utf16(),qstr.length());
107  }
108 
110 
115  inline void setFromWCharArray(const wchar_t utf16[], int len = -1);
116 
119  inline void setFromUtf8(const unsigned char* utf8, int len = -1);
120  inline void setFromUtf8(const std::string& utf8);
121 
124  inline void setFromLatin1(const char* latin, int len = -1);
125 
128  inline void setFromUtf16(const unsigned short* utf16, int len = -1);
129 
132  inline void setFromUtf32(const unsigned int* utf32, int len = -1);
133 
136  inline void setFromAscii(const std::string& ascii);
137 
140  inline void setFromAscii(const char* ascii, int len = -1);
141 
143  template <typename QStringType>
144  inline void setFromQString(QStringType qstr)
145  {
146  setFromUtf16(qstr.utf16(),qstr.length());
147  }
148 
150 
153  inline void toAscii(std::string& str) const;
154  inline std::string toAscii() const { std::string temp; toAscii(temp); return temp;}
155 
158  inline void toLatin1(std::string& str) const;
159  inline std::string toLatin1() const { std::string temp; toLatin1(temp); return temp;}
160 
161  inline void toUtf8(std::string& str) const;
162  inline std::string toUtf8() const { std::string temp; toUtf8(temp); return temp;}
163  inline void toWideString(std::wstring& str) const;
164  inline std::wstring toWideString() const { std::wstring temp; toWideString(temp); return temp;}
165 
167  template <typename QStringType>
168  inline QStringType toQString() const
169  {
171  }
172 
174  template <typename QStringType>
175  inline void toQString(QStringType& qstr) const
176  {
177  qstr.setUtf16(codeUnits(),length());
178  }
179 
181  inline DtUnicode& operator+=(const DtUnicode& str);
183  inline DtUnicode& operator+=(const DtUnicodeChar& x);
185  inline DtUnicode operator+(const DtUnicode& str) const;
186 
188  inline bool operator==(const DtUnicode& other) const;
190  inline bool operator!=(const DtUnicode& other) const;
193  inline bool operator<(const DtUnicode& other) const;
194 
197  inline size_t length() const;
198 
200  inline size_t sizeInBytes() const { return length() << 1; }
201 
203  inline size_t calculateCodePoints() const;
204 
206  inline const CodeUnit* codeUnits() const { return myCodeUnits.begin; }
207 
209  inline int indexOf( const DtUnicodeChar& x, size_t pos = 0) const;
210 
212  inline int indexOf( const DtUnicode& x, size_t pos = 0) const;
213 
217  inline DtUnicode subString( size_t pos = 0, int len = -1 ) const;
218 
220  inline void push_back ( const DtUnicodeChar& x );
221 
229  inline void resize( int size );
230 
232  virtual bool containsRightToLeft() const;
233 
234  protected: // THESE are the protected functions not to be used directly.
235 
236  enum Encoding
237  {
241  };
242 
243  //Ascii CTOR
244  inline explicit DtUnicode(const char* ascii, int len = -1,Encoding encoding = Ascii);
245 
246  //UTF16 constructor
247  inline explicit DtUnicode(const unsigned short* utf16, int len = -1);
248 
249  //UTF32 constructor.
250  inline explicit DtUnicode(const unsigned int* utf32, int len = -1);
251 
252  //Std string constructor.
253  inline explicit DtUnicode(const std::string& ascii);
254 
255  //Concat constructor.
256  inline explicit DtUnicode(const DtUnicode& a, const DtUnicode& b);
257 
258  protected:
259 
262  {
263  inline CodeUnitArray();
264  inline CodeUnitArray(const CodeUnitArray&);
265  inline CodeUnitArray& operator=(const CodeUnitArray&);
266  inline ~CodeUnitArray();
267 
269  inline void resize(size_t newSize);
270 
273  inline void clear();
274 
276  inline void reserve(size_t capacity);
277 
278  //Calculate the capacity
279  inline size_t calculateCapacity(size_t necessarySize);
280 
281  //Return size in bytes
282  inline size_t size() const;
283 
284  //Return capacity in bytes
285  inline size_t capacity() const;
286 
287  //Concat another array, supports concat operations on itself.
288  inline void concat(const CodeUnitArray& other);
289 
291  inline bool unique() const;
292 
294  inline bool empty() const;
295 
297  inline void add_ref();
298 
300  inline void dec_ref();
301 
302  volatile long* count;
306  };
308  };
309 }
310 
311 //Allows DtUnicode to be used in boost hash containers.
312 namespace makVrv
313 {
314  inline std::size_t hash_value(DtUnicode const& val)
315  {
316  //Copied from TR1 implementation.
317 
318  size_t seed = 2166136261U;
319  size_t first = 0;
320  size_t last = val.length();
321  size_t stride = 1 + last / 10;
322 
323  if (stride < last)
324  {
325  last -= stride;
326  }
327  for(; first < last ; first += stride )
328  {
329  seed = 16777619U * seed ^ (size_t)val.codeUnits()[first];
330  }
331  return seed;
332  }
333 }
334 
335 #include "DtUnicode.inl"

Document ID: Generated on Wed Mar 11 21:20:57 EDT 2015 from SVN revision 150940
Copyright © 2005-2014 VT MÄK. All Rights Reserved (www.mak.com)