Skip to content

Commit

Permalink
Make wxList iterators conform to input iterator requirements
Browse files Browse the repository at this point in the history
Define "pointer", "reference", "difference_type" and "iterator_category"
typedefs to ensure that wxList iterator classes are seen as iterators by
the standard library in C++11 and later, as otherwise standard container
template ctors taking iterators couldn't be used with them because
they're only available if input iterator requirements are satisfied.

This notably fixes creation of std::list from wxList iterators; add a
test which didn't compile before to show it.
  • Loading branch information
vadz committed Feb 23, 2018
1 parent 0d5378f commit fdbe357
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
8 changes: 8 additions & 0 deletions docs/changes.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ Changes in behaviour which may result in build errors
wxGraphicsContext::CreatePen() continues to compile and work as before.


3.1.2: (released 2018-??-??)
----------------------------

All:

- Make wxList iterators conform to input iterator requirements.


3.1.1: (released 2018-02-19)
----------------------------

Expand Down
38 changes: 32 additions & 6 deletions include/wx/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,17 @@ friend class wxHashTableBase; // should be able to call untyped Find()
// macros for definition of "template" list type
// -----------------------------------------------------------------------------

// Helper macro defining common iterator typedefs
#if wxUSE_STD_CONTAINERS_COMPATIBLY
#include <iterator>

#define WX_DECLARE_LIST_ITER_DIFF_AND_CATEGORY() \
typedef std::ptrdiff_t difference_type; \
typedef std::bidirectional_iterator_tag iterator_category;
#else
#define WX_DECLARE_LIST_ITER_DIFF_AND_CATEGORY()
#endif

// and now some heavy magic...

// declare a list type named 'name' and containing elements of type 'T *'
Expand Down Expand Up @@ -762,15 +773,19 @@ friend class wxHashTableBase; // should be able to call untyped Find()
classexp iterator \
{ \
public: \
WX_DECLARE_LIST_ITER_DIFF_AND_CATEGORY() \
typedef T* value_type; \
typedef value_type* pointer; \
typedef value_type& reference; \
\
typedef nodetype Node; \
typedef iterator itor; \
typedef T* value_type; \
typedef value_type* ptr_type; \
typedef value_type& reference; \
\
Node* m_node; \
Node* m_init; \
public: \
/* Compatibility typedefs, don't use */ \
typedef reference reference_type; \
typedef ptr_type pointer_type; \
\
Expand Down Expand Up @@ -811,8 +826,12 @@ friend class wxHashTableBase; // should be able to call untyped Find()
classexp const_iterator \
{ \
public: \
typedef nodetype Node; \
WX_DECLARE_LIST_ITER_DIFF_AND_CATEGORY() \
typedef T* value_type; \
typedef const value_type* pointer; \
typedef const value_type& reference; \
\
typedef nodetype Node; \
typedef const value_type& const_reference; \
typedef const_iterator itor; \
typedef value_type* ptr_type; \
Expand Down Expand Up @@ -863,11 +882,14 @@ friend class wxHashTableBase; // should be able to call untyped Find()
classexp reverse_iterator \
{ \
public: \
typedef nodetype Node; \
WX_DECLARE_LIST_ITER_DIFF_AND_CATEGORY() \
typedef T* value_type; \
typedef value_type* pointer; \
typedef value_type& reference; \
\
typedef nodetype Node; \
typedef reverse_iterator itor; \
typedef value_type* ptr_type; \
typedef value_type& reference; \
\
Node* m_node; \
Node* m_init; \
Expand Down Expand Up @@ -901,8 +923,12 @@ friend class wxHashTableBase; // should be able to call untyped Find()
classexp const_reverse_iterator \
{ \
public: \
typedef nodetype Node; \
WX_DECLARE_LIST_ITER_DIFF_AND_CATEGORY() \
typedef T* value_type; \
typedef const value_type* pointer; \
typedef const value_type& reference; \
\
typedef nodetype Node; \
typedef const_reverse_iterator itor; \
typedef value_type* ptr_type; \
typedef const value_type& const_reference; \
Expand Down
24 changes: 24 additions & 0 deletions tests/lists/lists.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,3 +210,27 @@ void ListsTestCase::wxListCtorTest()
CPPUNIT_ASSERT( Baz::GetNumber() == 0 );
}

#if wxUSE_STD_CONTAINERS_COMPATIBLY

#include <list>

// Check that we convert wxList to std::list using the latter's ctor taking 2
// iterators: this used to be broken in C++11 because wxList iterators didn't
// fully implement input iterator requirements.
TEST_CASE("wxList::iterator", "[list][std][iterator]")
{
Baz baz1("one"),
baz2("two");

wxListBazs li;
li.push_back(&baz1);
li.push_back(&baz2);

std::list<Baz*> stdli(li.begin(), li.end());
CHECK( stdli.size() == 2 );

const wxListBazs cli;
CHECK( std::list<Baz*>(cli.begin(), cli.end()).empty() );
}

#endif // wxUSE_STD_CONTAINERS_COMPATIBLY

0 comments on commit fdbe357

Please sign in to comment.