VR-Forces 4.1.1 Class Documentation
DtUnicode.h
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright (c) 2010 MAK Technologies, Inc.
3 ** All rights reserved.
4 ******************************************************************************/
5 
8 
9 #ifndef DtUnicode_H
10 #define DtUnicode_H
11 
12 #include <wchar.h>
13 #include <vrvUtil/DtUnicodeChar.h>
14 #include <boost/functional/hash.hpp>
15 #include <boost/serialization/split_free.hpp>
16 
17 namespace makVrv
18 {
19 
30  class DtUnicode
31  {
32  public:
33  typedef unsigned short CodeUnit;
34 
36  inline DtUnicode();
37 
39  inline ~DtUnicode();
40 
42  explicit inline DtUnicode( size_t len, DtUnicodeChar c = DtUnicodeChar('\0'));
43 
47  inline DtUnicode(const DtUnicode& str);
48 
52  inline DtUnicode& operator=(const DtUnicode& str);
53 
55 
58  inline static DtUnicode fromWCharArray(const wchar_t utf16[], int len = -1);
59 
61  inline static DtUnicode fromWideString(const std::wstring& str);
62 
65  inline static DtUnicode fromUtf8(const unsigned char* utf8, int len = -1);
66  inline static DtUnicode fromUtf8(const std::string& utf8);
67 
70  inline static DtUnicode fromUtf16(const unsigned short* utf16, int len = -1);
71 
74  inline static DtUnicode fromUtf32(const unsigned int* utf32, int len = -1);
75 
78  inline static DtUnicode fromAscii(const std::string& ascii);
79 
82  inline static DtUnicode fromAscii(const char* ascii, int len = -1);
83 
89  inline static DtUnicode tr(const char* ascii)
90  {
91  return fromAscii(ascii);
92  }
93 
96  template <typename QStringType, typename QCoreApp>
97  QStringType translate() const
98  {
99  return QCoreApp::translate("makVrv::DtUnicode",toUtf8().c_str(),
100  0, QCoreApp::UnicodeUTF8);
101  }
102 
104  template <typename QStringType>
105  inline static DtUnicode fromQString(QStringType qstr)
106  {
107  return fromUtf16(qstr.utf16(),qstr.length());
108  }
109 
111 
116  inline void setFromWCharArray(const wchar_t utf16[], int len = -1);
117 
120  inline void setFromUtf8(const unsigned char* utf8, int len = -1);
121  inline void setFromUtf8(const std::string& utf8);
122 
125  inline void setFromLatin1(const char* latin, int len = -1);
126 
129  inline void setFromUtf16(const unsigned short* utf16, int len = -1);
130 
133  inline void setFromUtf32(const unsigned int* utf32, int len = -1);
134 
137  inline void setFromAscii(const std::string& ascii);
138 
141  inline void setFromAscii(const char* ascii, int len = -1);
142 
144  template <typename QStringType>
145  inline void setFromQString(QStringType qstr)
146  {
147  setFromUtf16(qstr.utf16(),qstr.length());
148  }
149 
151 
154  inline void toAscii(std::string& str) const;
155  inline std::string toAscii() const { std::string temp; toAscii(temp); return temp;}
156 
159  inline void toLatin1(std::string& str) const;
160  inline std::string toLatin1() const { std::string temp; toLatin1(temp); return temp;}
161 
162  inline void toUtf8(std::string& str) const;
163  inline std::string toUtf8() const { std::string temp; toUtf8(temp); return temp;}
164  inline void toWideString(std::wstring& str) const;
165  inline std::wstring toWideString() const { std::wstring temp; toWideString(temp); return temp;}
166 
168  template <typename QStringType>
169  inline QStringType toQString() const
170  {
172  }
173 
175  template <typename QStringType>
176  inline void toQString(QStringType& qstr) const
177  {
178  qstr.setUtf16(codeUnits(),length());
179  }
180 
182  inline DtUnicode& operator+=(const DtUnicode& str);
184  inline DtUnicode& operator+=(const DtUnicodeChar& x);
186  inline DtUnicode operator+(const DtUnicode& str) const;
187 
189  inline bool operator==(const DtUnicode& other) const;
191  inline bool operator!=(const DtUnicode& other) const;
194  inline bool operator<(const DtUnicode& other) const;
195 
198  inline size_t length() const;
199 
201  inline size_t sizeInBytes() const { return length() << 1; }
202 
204  inline size_t calculateCodePoints() const;
205 
207  inline const CodeUnit* codeUnits() const { return myCodeUnits.begin; }
208 
210  inline int indexOf( const DtUnicodeChar& x, size_t pos = 0) const;
211 
213  inline int indexOf( const DtUnicode& x, size_t pos = 0) const;
214 
218  inline DtUnicode subString( size_t pos = 0, int len = -1 ) const;
219 
221  inline void push_back ( const DtUnicodeChar& x );
222 
230  inline void resize( int size );
231 
232  protected: // THESE are the protected functions not to be used directly.
233 
234  enum Encoding
235  {
239  };
240 
241  //Ascii CTOR
242  inline explicit DtUnicode(const char* ascii, int len = -1,Encoding encoding = Ascii);
243 
244  //UTF16 constructor
245  inline explicit DtUnicode(const unsigned short* utf16, int len = -1);
246 
247  //UTF32 constructor.
248  inline explicit DtUnicode(const unsigned int* utf32, int len = -1);
249 
250  //Std string constructor.
251  inline explicit DtUnicode(const std::string& ascii);
252 
253  //Concat constructor.
254  inline explicit DtUnicode(const DtUnicode& a, const DtUnicode& b);
255 
256  protected:
257 
260  {
261  inline CodeUnitArray();
262  inline CodeUnitArray(const CodeUnitArray&);
263  inline CodeUnitArray& operator=(const CodeUnitArray&);
264  inline ~CodeUnitArray();
265 
267  inline void resize(size_t newSize);
268 
271  inline void clear();
272 
274  inline void reserve(size_t capacity);
275 
276  //Calculate the capacity
277  inline size_t calculateCapacity(size_t necessarySize);
278 
279  //Return size in bytes
280  inline size_t size() const;
281 
282  //Return capacity in bytes
283  inline size_t capacity() const;
284 
285  //Concat another array, supports concat operations on itself.
286  inline void concat(const CodeUnitArray& other);
287 
289  inline bool unique() const;
290 
292  inline bool empty() const;
293 
295  inline void add_ref();
296 
298  inline void dec_ref();
299 
300  volatile long* count;
304  };
306  };
307 }
308 
309 //Allows DtUnicode to be used in boost hash containers.
310 namespace makVrv
311 {
312  inline std::size_t hash_value(DtUnicode const& val)
313  {
314  //Copied from TR1 implementation.
315 
316  size_t seed = 2166136261U;
317  size_t first = 0;
318  size_t last = val.length();
319  size_t stride = 1 + last / 10;
320 
321  if (stride < last)
322  {
323  last -= stride;
324  }
325  for(; first < last ; first += stride )
326  {
327  seed = 16777619U * seed ^ (size_t)val.codeUnits()[first];
328  }
329  return seed;
330  }
331 }
332 
333 #include "DtUnicode.inl"
334 
335 #endif

Document ID: Generated on Mon Apr 8 19:24:01 EDT 2013 from SVN revision 125877
Copyright © 2005-2013 VT MÄK. All Rights Reserved (www.mak.com)