X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/3a3dde0d44c43bc70a1e9791381ada04b4953a5d..01101e2da6f4493ce8cfcf4d9509163b9c264d58:/include/wx/string.h diff --git a/include/wx/string.h b/include/wx/string.h index 86eb7eef96..b6002fd5aa 100644 --- a/include/wx/string.h +++ b/include/wx/string.h @@ -59,7 +59,7 @@ #include "wx/stringops.h" #include "wx/unichar.h" -class WXDLLIMPEXP_BASE wxString; +class WXDLLIMPEXP_FWD_BASE wxString; // unless this symbol is predefined to disable the compatibility functions, do // use them @@ -276,7 +276,7 @@ private: size_t m_offset; bool m_owned; - friend class WXDLLIMPEXP_BASE wxString; + friend class WXDLLIMPEXP_FWD_BASE wxString; }; // ---------------------------------------------------------------------------- @@ -380,16 +380,35 @@ protected: // see the comment near wxString::iterator for why we need this struct WXDLLIMPEXP_BASE wxStringIteratorNode { - inline wxStringIteratorNode(const wxString *str, - wxStringImpl::const_iterator *citer); - inline wxStringIteratorNode(const wxString *str, - wxStringImpl::iterator *iter); - inline ~wxStringIteratorNode(); + wxStringIteratorNode() + : m_str(NULL), m_citer(NULL), m_iter(NULL), m_prev(NULL), m_next(NULL) {} + wxStringIteratorNode(const wxString *str, + wxStringImpl::const_iterator *citer) + { DoSet(str, citer, NULL); } + wxStringIteratorNode(const wxString *str, wxStringImpl::iterator *iter) + { DoSet(str, NULL, iter); } + ~wxStringIteratorNode() + { clear(); } + + inline void set(const wxString *str, wxStringImpl::const_iterator *citer) + { clear(); DoSet(str, citer, NULL); } + inline void set(const wxString *str, wxStringImpl::iterator *iter) + { clear(); DoSet(str, NULL, iter); } const wxString *m_str; wxStringImpl::const_iterator *m_citer; wxStringImpl::iterator *m_iter; wxStringIteratorNode *m_prev, *m_next; + +private: + inline void clear(); + inline void DoSet(const wxString *str, + wxStringImpl::const_iterator *citer, + wxStringImpl::iterator *iter); + + // the node belongs to a particular iterator instance, it's not copied + // when a copy of the iterator is made + DECLARE_NO_COPY_CLASS(wxStringIteratorNode) }; #endif // wxUSE_UNICODE_UTF8 @@ -633,13 +652,13 @@ public: /* for internal wxString use only: */ \ underlying_iterator impl() const { return m_cur; } \ \ - friend class WXDLLIMPEXP_BASE wxString; \ - friend class WXDLLIMPEXP_BASE wxCStrData; \ + friend class wxString; \ + friend class wxCStrData; \ \ private: \ underlying_iterator m_cur - class const_iterator; + class WXDLLIMPEXP_FWD_BASE const_iterator; #if wxUSE_UNICODE_UTF8 // NB: In UTF-8 build, (non-const) iterator needs to keep reference @@ -656,13 +675,16 @@ public: // string and traversing it in wxUniCharRef::operator=(). Head of the // list is stored in wxString. (FIXME-UTF8) - class iterator + class WXDLLIMPEXP_BASE iterator { WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef); public: + iterator() {} iterator(const iterator& i) : m_cur(i.m_cur), m_node(i.str(), &m_cur) {} + iterator& operator=(const iterator& i) + { m_cur = i.m_cur; m_node.set(i.str(), &m_cur); return *this; } reference operator*() { return wxUniCharRef::CreateForString(m_node, m_cur); } @@ -687,18 +709,24 @@ public: friend class const_iterator; }; - class const_iterator + class WXDLLIMPEXP_BASE const_iterator { // NB: reference_type is intentionally value, not reference, the character // may be encoded differently in wxString data: WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar); public: + const_iterator() {} const_iterator(const const_iterator& i) : m_cur(i.m_cur), m_node(i.str(), &m_cur) {} const_iterator(const iterator& i) : m_cur(i.m_cur), m_node(i.str(), &m_cur) {} + const_iterator& operator=(const const_iterator& i) + { m_cur = i.m_cur; m_node.set(i.str(), &m_cur); return *this; } + const_iterator& operator=(const iterator& i) + { m_cur = i.m_cur; m_node.set(i.str(), &m_cur); return *this; } + reference operator*() const { return wxStringOperations::DecodeChar(m_cur); } @@ -726,11 +754,12 @@ public: #else // !wxUSE_UNICODE_UTF8 - class iterator + class WXDLLIMPEXP_BASE iterator { WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef); public: + iterator() {} iterator(const iterator& i) : m_cur(i.m_cur) {} reference operator*() @@ -753,13 +782,14 @@ public: friend class const_iterator; }; - class const_iterator + class WXDLLIMPEXP_BASE const_iterator { // NB: reference_type is intentionally value, not reference, the character // may be encoded differently in wxString data: WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar); public: + const_iterator() {} const_iterator(const const_iterator& i) : m_cur(i.m_cur) {} const_iterator(const iterator& i) : m_cur(i.m_cur) {} @@ -801,6 +831,7 @@ public: typedef typename T::reference reference; typedef typename T::pointer *pointer; + reverse_iterator_impl() {} reverse_iterator_impl(iterator_type i) : m_cur(i) {} reverse_iterator_impl(const reverse_iterator_impl& ri) : m_cur(ri.m_cur) {} @@ -992,7 +1023,12 @@ public: wxString(const std::string& str) { assign(str.c_str(), str.length()); } #endif +#endif // wxUSE_STD_STRING + // Unlike ctor from std::string, we provide conversion to std::string only + // if wxUSE_STL and not merely wxUSE_STD_STRING (which is on by default), + // because it conflicts with operator const char/wchar_t*: +#if wxUSE_STL #if wxUSE_UNICODE_WCHAR && wxUSE_STL_BASED_WXSTRING // wxStringImpl is std::string in the encoding we want operator const wxStdWideString&() const { return m_impl; } @@ -1165,6 +1201,13 @@ public: // implicit conversion to C string operator wxCStrData() const { return c_str(); } + + // the first two operators conflict with operators for conversion to + // std::string and they must be disabled in STL build; the next one only + // makes sense if conversions to char* are also defined and not defining it + // in STL build also helps us to get more clear error messages for the code + // which relies on implicit conversion to char* in STL build +#if !wxUSE_STL operator const char*() const { return c_str(); } operator const wchar_t*() const { return c_str(); } @@ -1172,6 +1215,7 @@ public: // wxWidgets versions: this is the same as conversion to const char * so it // may fail! operator const void*() const { return c_str(); } +#endif // wxUSE_STL // identical to c_str(), for MFC compatibility const wxCStrData GetData() const { return c_str(); } @@ -1194,11 +1238,14 @@ public: // the behaviour of these functions with the strings containing anything // else than 7 bit ASCII characters is undefined, use at your own risk. #if wxUSE_UNICODE + static wxString FromAscii(const char *ascii, size_t len); // string static wxString FromAscii(const char *ascii); // string static wxString FromAscii(const char ascii); // char const wxCharBuffer ToAscii() const; #else // ANSI static wxString FromAscii(const char *ascii) { return wxString( ascii ); } + static wxString FromAscii(const char *ascii, size_t len) + { return wxString( ascii, len ); } static wxString FromAscii(const char ascii) { return wxString( ascii ); } const char *ToAscii() const { return c_str(); } #endif // Unicode/!Unicode @@ -1207,11 +1254,19 @@ public: #if wxUSE_UNICODE_UTF8 static wxString FromUTF8(const char *utf8) { + if ( !utf8 ) + return wxEmptyString; + wxASSERT( wxStringOperations::IsValidUtf8String(utf8) ); return FromImpl(wxStringImpl(utf8)); } static wxString FromUTF8(const char *utf8, size_t len) { + if ( !utf8 ) + return wxEmptyString; + if ( len == npos ) + return FromUTF8(utf8); + wxASSERT( wxStringOperations::IsValidUtf8String(utf8, len) ); return FromImpl(wxStringImpl(utf8, len)); } @@ -2520,6 +2575,12 @@ private: private: wxStringImpl m_impl; +#ifdef __VISUALC__ + // "struct 'ConvertedBuffer' needs to have dll-interface to be used by + // clients of class 'wxString'" - this is private, we don't care + #pragma warning (disable:4251) +#endif + // buffers for compatibility conversion from (char*)c_str() and // (wchar_t*)c_str(): // FIXME-UTF8: bechmark various approaches to keeping compatibility buffers @@ -2548,6 +2609,10 @@ private: ConvertedBuffer m_convertedToWChar; #endif +#ifdef __VISUALC__ + #pragma warning (default:4251) +#endif + #if wxUSE_UNICODE_UTF8 // FIXME-UTF8: (try to) move this elsewhere (TLS) or solve differently // assigning to character pointer to by wxString::interator may @@ -2557,15 +2622,19 @@ private: { wxStringIteratorNodeHead() : ptr(NULL) {} wxStringIteratorNode *ptr; + + // copying is disallowed as it would result in more than one pointer into + // the same linked list + DECLARE_NO_COPY_CLASS(wxStringIteratorNodeHead) }; wxStringIteratorNodeHead m_iterators; - friend class WXDLLIMPEXP_BASE wxStringIteratorNode; - friend class WXDLLIMPEXP_BASE wxUniCharRef; + friend class WXDLLIMPEXP_FWD_BASE wxStringIteratorNode; + friend class WXDLLIMPEXP_FWD_BASE wxUniCharRef; #endif // wxUSE_UNICODE_UTF8 - friend class WXDLLIMPEXP_BASE wxCStrData; + friend class WXDLLIMPEXP_FWD_BASE wxCStrData; friend class wxImplStringBuffer; friend class wxImplStringBufferLength; }; @@ -2944,7 +3013,7 @@ inline wxCStrData::wxCStrData(const wxCStrData& data) inline wxCStrData::~wxCStrData() { if ( m_owned ) - delete m_str; + delete wx_const_cast(wxString*, m_str); // cast to silence warnings } // simple cases for AsChar() and AsWChar(), the complicated ones are @@ -3056,40 +3125,40 @@ inline wxWCharBuffer::wxWCharBuffer(const wxCStrData& cstr) // implementation of wxStringIteratorNode inline methods // ---------------------------------------------------------------------------- -wxStringIteratorNode::wxStringIteratorNode(const wxString *str, - wxStringImpl::const_iterator *citer) - : m_str(str), - m_citer(citer), - m_iter(NULL), - m_prev(NULL), - m_next(str->m_iterators.ptr) -{ - wx_const_cast(wxString*, m_str)->m_iterators.ptr = this; - if ( m_next ) - m_next->m_prev = this; -} - -wxStringIteratorNode::wxStringIteratorNode(const wxString *str, - wxStringImpl::iterator *iter) - : m_str(str), - m_citer(NULL), - m_iter(iter), - m_prev(NULL), - m_next(str->m_iterators.ptr) +void wxStringIteratorNode::DoSet(const wxString *str, + wxStringImpl::const_iterator *citer, + wxStringImpl::iterator *iter) { - wx_const_cast(wxString*, m_str)->m_iterators.ptr = this; - if ( m_next) - m_next->m_prev = this; + m_prev = NULL; + m_iter = iter; + m_citer = citer; + m_str = str; + if ( str ) + { + m_next = str->m_iterators.ptr; + wx_const_cast(wxString*, m_str)->m_iterators.ptr = this; + if ( m_next ) + m_next->m_prev = this; + } + else + { + m_next = NULL; + } } -wxStringIteratorNode::~wxStringIteratorNode() +void wxStringIteratorNode::clear() { if ( m_next ) m_next->m_prev = m_prev; if ( m_prev ) m_prev->m_next = m_next; - else // first in the list + else if ( m_str ) // first in the list wx_const_cast(wxString*, m_str)->m_iterators.ptr = m_next; + + m_next = m_prev = NULL; + m_citer = NULL; + m_iter = NULL; + m_str = NULL; } #endif // wxUSE_UNICODE_UTF8