![]() |
VR-Vantage 1.4.1 API Class Documentation
|
00001 /****************************************************************************** 00002 ** Copyright (c) 2010 MAK Technologies, Inc. 00003 ** All rights reserved. 00004 ******************************************************************************/ 00005 /********************************************************************* 00006 ** $RCSfile: DtUnicode.h,v $ $Revision: $ $State: $ 00007 *********************************************************************/ 00008 00011 00012 #pragma once 00013 00014 #include <wchar.h> 00015 #include <vrvUtil/DtUnicodeChar.h> 00016 #include <boost/functional/hash.hpp> 00017 #include <boost/serialization/split_free.hpp> 00018 00019 namespace makVrv 00020 { 00021 00032 class DtUnicode 00033 { 00034 public: 00035 typedef unsigned short CodeUnit; 00036 00038 inline DtUnicode(); 00039 00041 inline ~DtUnicode(); 00042 00044 explicit inline DtUnicode( size_t len, DtUnicodeChar c = DtUnicodeChar('\0')); 00045 00049 inline DtUnicode(const DtUnicode& str); 00050 00054 inline DtUnicode& operator=(const DtUnicode& str); 00055 00057 00060 inline static DtUnicode fromWCharArray(const wchar_t utf16[], int len = -1); 00061 00063 inline static DtUnicode fromWideString(const std::wstring& str); 00064 00067 inline static DtUnicode fromUtf8(const unsigned char* utf8, int len = -1); 00068 inline static DtUnicode fromUtf8(const std::string& utf8); 00069 00072 inline static DtUnicode fromUtf16(const unsigned short* utf16, int len = -1); 00073 00076 inline static DtUnicode fromUtf32(const unsigned int* utf32, int len = -1); 00077 00080 inline static DtUnicode fromAscii(const std::string& ascii); 00081 00084 inline static DtUnicode fromAscii(const char* ascii, int len = -1); 00085 00091 inline static DtUnicode tr(const char* ascii) 00092 { 00093 return fromAscii(ascii); 00094 } 00095 00098 template <typename QStringType, typename QCoreApp> 00099 QStringType translate() const 00100 { 00101 return QCoreApp::translate("makVrv::DtUnicode",toUtf8().c_str(), 00102 0, QCoreApp::UnicodeUTF8); 00103 } 00104 00106 template <typename QStringType> 00107 inline static DtUnicode fromQString(QStringType qstr) 00108 { 00109 return fromUtf16(qstr.utf16(),qstr.length()); 00110 } 00111 00113 00118 inline void setFromWCharArray(const wchar_t utf16[], int len = -1); 00119 00122 inline void setFromUtf8(const unsigned char* utf8, int len = -1); 00123 inline void setFromUtf8(const std::string& utf8); 00124 00127 inline void setFromLatin1(const char* latin, int len = -1); 00128 00131 inline void setFromUtf16(const unsigned short* utf16, int len = -1); 00132 00135 inline void setFromUtf32(const unsigned int* utf32, int len = -1); 00136 00139 inline void setFromAscii(const std::string& ascii); 00140 00143 inline void setFromAscii(const char* ascii, int len = -1); 00144 00146 template <typename QStringType> 00147 inline void setFromQString(QStringType qstr) 00148 { 00149 setFromUtf16(qstr.utf16(),qstr.length()); 00150 } 00151 00153 00156 inline void toAscii(std::string& str) const; 00157 inline std::string toAscii() const { std::string temp; toAscii(temp); return temp;} 00158 00161 inline void toLatin1(std::string& str) const; 00162 inline std::string toLatin1() const { std::string temp; toLatin1(temp); return temp;} 00163 00164 inline void toUtf8(std::string& str) const; 00165 inline std::string toUtf8() const { std::string temp; toUtf8(temp); return temp;} 00166 inline void toWideString(std::wstring& str) const; 00167 inline std::wstring toWideString() const { std::wstring temp; toWideString(temp); return temp;} 00168 00170 template <typename QStringType> 00171 inline QStringType toQString() const 00172 { 00173 return QStringType::fromUtf16(codeUnits(),length()); 00174 } 00175 00177 template <typename QStringType> 00178 inline void toQString(QStringType& qstr) const 00179 { 00180 qstr.setUtf16(codeUnits(),length()); 00181 } 00182 00184 inline DtUnicode& operator+=(const DtUnicode& str); 00186 inline DtUnicode& operator+=(const DtUnicodeChar& x); 00188 inline DtUnicode operator+(const DtUnicode& str) const; 00189 00191 inline bool operator==(const DtUnicode& other) const; 00193 inline bool operator!=(const DtUnicode& other) const; 00196 inline bool operator<(const DtUnicode& other) const; 00197 00200 inline size_t length() const; 00201 00203 inline size_t sizeInBytes() const { return length() << 1; } 00204 00206 inline size_t calculateCodePoints() const; 00207 00209 inline const CodeUnit* codeUnits() const { return myCodeUnits.begin; } 00210 00212 inline int indexOf( const DtUnicodeChar& x, size_t pos = 0) const; 00213 00215 inline int indexOf( const DtUnicode& x, size_t pos = 0) const; 00216 00220 inline DtUnicode subString( size_t pos = 0, int len = -1 ) const; 00221 00223 inline void push_back ( const DtUnicodeChar& x ); 00224 00232 inline void resize( int size ); 00233 00234 protected: // THESE are the protected functions not to be used directly. 00235 00236 enum Encoding 00237 { 00238 Ascii, 00239 Latin1, 00240 Utf8 00241 }; 00242 00243 //Ascii CTOR 00244 inline explicit DtUnicode(const char* ascii, int len = -1,Encoding encoding = Ascii); 00245 00246 //UTF16 constructor 00247 inline explicit DtUnicode(const unsigned short* utf16, int len = -1); 00248 00249 //UTF32 constructor. 00250 inline explicit DtUnicode(const unsigned int* utf32, int len = -1); 00251 00252 //Std string constructor. 00253 inline explicit DtUnicode(const std::string& ascii); 00254 00255 //Concat constructor. 00256 inline explicit DtUnicode(const DtUnicode& a, const DtUnicode& b); 00257 00258 protected: 00259 00261 struct CodeUnitArray 00262 { 00263 inline CodeUnitArray(); 00264 inline CodeUnitArray(const CodeUnitArray&); 00265 inline CodeUnitArray& operator=(const CodeUnitArray&); 00266 inline ~CodeUnitArray(); 00267 00269 inline void resize(size_t newSize); 00270 00273 inline void clear(); 00274 00276 inline void reserve(size_t capacity); 00277 00278 //Calculate the capacity 00279 inline size_t calculateCapacity(size_t necessarySize); 00280 00281 //Return size in bytes 00282 inline size_t size() const; 00283 00284 //Return capacity in bytes 00285 inline size_t capacity() const; 00286 00287 //Concat another array, supports concat operations on itself. 00288 inline void concat(const CodeUnitArray& other); 00289 00291 inline bool unique() const; 00292 00294 inline bool empty() const; 00295 00297 inline void add_ref(); 00298 00300 inline void dec_ref(); 00301 00302 volatile long* count; 00303 CodeUnit* begin; 00304 CodeUnit* end; 00305 CodeUnit* last; 00306 }; 00307 CodeUnitArray myCodeUnits; 00308 }; 00309 } 00310 00311 //Allows DtUnicode to be used in boost hash containers. 00312 namespace makVrv 00313 { 00314 inline std::size_t hash_value(DtUnicode const& val) 00315 { 00316 //Copied from TR1 implementation. 00317 00318 size_t seed = 2166136261U; 00319 size_t first = 0; 00320 size_t last = val.length(); 00321 size_t stride = 1 + last / 10; 00322 00323 if (stride < last) 00324 { 00325 last -= stride; 00326 } 00327 for(; first < last ; first += stride ) 00328 { 00329 seed = 16777619U * seed ^ (size_t)val.codeUnits()[first]; 00330 } 00331 return seed; 00332 } 00333 } 00334 00335 #include "DtUnicode.inl"