1. added default constructors for wxString iterators

2. fixed assignment of iterator to iterator that points to a different string

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@46996 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Václav Slavík
2007-06-28 12:49:58 +00:00
parent e6310bbc5a
commit 39c20230ba
3 changed files with 61 additions and 33 deletions

View File

@@ -380,17 +380,32 @@ protected:
// see the comment near wxString::iterator for why we need this // see the comment near wxString::iterator for why we need this
struct WXDLLIMPEXP_BASE wxStringIteratorNode struct WXDLLIMPEXP_BASE wxStringIteratorNode
{ {
inline wxStringIteratorNode(const wxString *str, wxStringIteratorNode()
wxStringImpl::const_iterator *citer); : m_str(NULL), m_citer(NULL), m_iter(NULL), m_prev(NULL), m_next(NULL) {}
inline wxStringIteratorNode(const wxString *str, wxStringIteratorNode(const wxString *str,
wxStringImpl::iterator *iter); wxStringImpl::const_iterator *citer)
inline ~wxStringIteratorNode(); { 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; const wxString *m_str;
wxStringImpl::const_iterator *m_citer; wxStringImpl::const_iterator *m_citer;
wxStringImpl::iterator *m_iter; wxStringImpl::iterator *m_iter;
wxStringIteratorNode *m_prev, *m_next; 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 // the node belongs to a particular iterator instance, it's not copied
// when a copy of the iterator is made // when a copy of the iterator is made
DECLARE_NO_COPY_CLASS(wxStringIteratorNode) DECLARE_NO_COPY_CLASS(wxStringIteratorNode)
@@ -665,10 +680,11 @@ public:
WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef); WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef);
public: public:
iterator() {}
iterator(const iterator& i) iterator(const iterator& i)
: m_cur(i.m_cur), m_node(i.str(), &m_cur) {} : m_cur(i.m_cur), m_node(i.str(), &m_cur) {}
iterator& operator=(const iterator& i) iterator& operator=(const iterator& i)
{ m_cur = i.m_cur; return *this; } { m_cur = i.m_cur; m_node.set(i.str(), &m_cur); return *this; }
reference operator*() reference operator*()
{ return wxUniCharRef::CreateForString(m_node, m_cur); } { return wxUniCharRef::CreateForString(m_node, m_cur); }
@@ -700,15 +716,16 @@ public:
WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar); WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar);
public: public:
const_iterator() {}
const_iterator(const const_iterator& i) const_iterator(const const_iterator& i)
: m_cur(i.m_cur), m_node(i.str(), &m_cur) {} : m_cur(i.m_cur), m_node(i.str(), &m_cur) {}
const_iterator(const iterator& i) const_iterator(const iterator& i)
: m_cur(i.m_cur), m_node(i.str(), &m_cur) {} : m_cur(i.m_cur), m_node(i.str(), &m_cur) {}
const_iterator& operator=(const const_iterator& i) const_iterator& operator=(const const_iterator& i)
{ m_cur = i.m_cur; return *this; } { m_cur = i.m_cur; m_node.set(i.str(), &m_cur); return *this; }
const_iterator& operator=(const iterator& i) const_iterator& operator=(const iterator& i)
{ m_cur = i.m_cur; return *this; } { m_cur = i.m_cur; m_node.set(i.str(), &m_cur); return *this; }
reference operator*() const reference operator*() const
{ return wxStringOperations::DecodeChar(m_cur); } { return wxStringOperations::DecodeChar(m_cur); }
@@ -742,6 +759,7 @@ public:
WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef); WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef);
public: public:
iterator() {}
iterator(const iterator& i) : m_cur(i.m_cur) {} iterator(const iterator& i) : m_cur(i.m_cur) {}
reference operator*() reference operator*()
@@ -771,6 +789,7 @@ public:
WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar); WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar);
public: public:
const_iterator() {}
const_iterator(const const_iterator& i) : m_cur(i.m_cur) {} const_iterator(const const_iterator& i) : m_cur(i.m_cur) {}
const_iterator(const iterator& i) : m_cur(i.m_cur) {} const_iterator(const iterator& i) : m_cur(i.m_cur) {}
@@ -812,6 +831,7 @@ public:
typedef typename T::reference reference; typedef typename T::reference reference;
typedef typename T::pointer *pointer; typedef typename T::pointer *pointer;
reverse_iterator_impl() {}
reverse_iterator_impl(iterator_type i) : m_cur(i) {} reverse_iterator_impl(iterator_type i) : m_cur(i) {}
reverse_iterator_impl(const reverse_iterator_impl& ri) reverse_iterator_impl(const reverse_iterator_impl& ri)
: m_cur(ri.m_cur) {} : m_cur(ri.m_cur) {}
@@ -3102,40 +3122,36 @@ inline wxWCharBuffer::wxWCharBuffer(const wxCStrData& cstr)
// implementation of wxStringIteratorNode inline methods // implementation of wxStringIteratorNode inline methods
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
wxStringIteratorNode::wxStringIteratorNode(const wxString *str, void wxStringIteratorNode::DoSet(const wxString *str,
wxStringImpl::const_iterator *citer) wxStringImpl::const_iterator *citer,
: m_str(str), wxStringImpl::iterator *iter)
m_citer(citer),
m_iter(NULL),
m_prev(NULL),
m_next(str->m_iterators.ptr)
{ {
m_next = 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; wx_const_cast(wxString*, m_str)->m_iterators.ptr = this;
if ( m_next ) if ( m_next )
m_next->m_prev = this; m_next->m_prev = this;
}
} }
wxStringIteratorNode::wxStringIteratorNode(const wxString *str, void wxStringIteratorNode::clear()
wxStringImpl::iterator *iter)
: m_str(str),
m_citer(NULL),
m_iter(iter),
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()
{ {
if ( m_next ) if ( m_next )
m_next->m_prev = m_prev; m_next->m_prev = m_prev;
if ( m_prev ) if ( m_prev )
m_prev->m_next = m_next; 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; 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 #endif // wxUSE_UNICODE_UTF8

View File

@@ -198,6 +198,7 @@ public:
typedef ptr_type pointer; \ typedef ptr_type pointer; \
typedef int difference_type; \ typedef int difference_type; \
\ \
iterator_name() : m_ptr(NULL) { } \
iterator_name(pointer ptr) : m_ptr(ptr) { } \ iterator_name(pointer ptr) : m_ptr(ptr) { } \
\ \
reference operator*() const { return *m_ptr; } \ reference operator*() const { return *m_ptr; } \

View File

@@ -33,6 +33,7 @@ public:
private: private:
CPPUNIT_TEST_SUITE( StdStringTestCase ); CPPUNIT_TEST_SUITE( StdStringTestCase );
CPPUNIT_TEST( StdConstructors ); CPPUNIT_TEST( StdConstructors );
CPPUNIT_TEST( StdIterators );
CPPUNIT_TEST( StdAppend ); CPPUNIT_TEST( StdAppend );
CPPUNIT_TEST( StdAssign ); CPPUNIT_TEST( StdAssign );
CPPUNIT_TEST( StdCompare ); CPPUNIT_TEST( StdCompare );
@@ -52,6 +53,7 @@ private:
CPPUNIT_TEST_SUITE_END(); CPPUNIT_TEST_SUITE_END();
void StdConstructors(); void StdConstructors();
void StdIterators();
void StdAppend(); void StdAppend();
void StdAssign(); void StdAssign();
void StdCompare(); void StdCompare();
@@ -108,6 +110,15 @@ void StdStringTestCase::StdConstructors()
WX_ASSERT_STR_EQUAL( "a", wxString(pw, pw + 1) ); WX_ASSERT_STR_EQUAL( "a", wxString(pw, pw + 1) );
} }
void StdStringTestCase::StdIterators()
{
// test compilation of default iterators ctors:
wxString::iterator i1;
wxString::const_iterator i2;
wxString::reverse_iterator i3;
wxString::const_reverse_iterator i4;
}
void StdStringTestCase::StdAppend() void StdStringTestCase::StdAppend()
{ {
wxString s1, s2, s3, s4, s5, s6, s7, s8; wxString s1, s2, s3, s4, s5, s6, s7, s8;