Skip to content

Commit

Permalink
Add support for initializer_list on arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
Lotendan committed Mar 2, 2023
1 parent 5f486a0 commit 1312594
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
8 changes: 8 additions & 0 deletions include/wx/arrstr.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
#include "wx/dynarray.h"

#include <vector>
#include "wx/beforestd.h"
#include <initializer_list>
#include "wx/afterstd.h"

// these functions are only used in STL build now but we define them in any
// case for compatibility with the existing code outside of the library which
Expand Down Expand Up @@ -81,6 +84,8 @@ class WXDLLIMPEXP_BASE wxArrayString : public wxArrayStringBase
wxArrayString(size_t sz, const char** a);
wxArrayString(size_t sz, const wchar_t** a);
wxArrayString(size_t sz, const wxString* a);
template<typename _IList>
wxArrayString(std::initializer_list<_IList> list) : wxArrayStringBase(list) { }

int Index(const wxString& str, bool bCase = true, bool bFromEnd = false) const;

Expand Down Expand Up @@ -182,6 +187,9 @@ class WXDLLIMPEXP_BASE wxArrayString
wxArrayString(size_t sz, const wxString* a);
// copy ctor
wxArrayString(const wxArrayString& array);
// list constructor
template<typename _IList>
wxArrayString(std::initializer_list<_IList> list) { Init(false); assign(list.begin(), list.end()); }
// assignment operator
wxArrayString& operator=(const wxArrayString& src);
// not virtual, this class should not be derived from
Expand Down
9 changes: 9 additions & 0 deletions include/wx/dynarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

#include "wx/vector.h"

#include "wx/beforestd.h"
#include <initializer_list>
#include "wx/afterstd.h"

/*
This header defines legacy dynamic arrays and object arrays (i.e. arrays
which own their elements) classes.
Expand Down Expand Up @@ -105,6 +109,9 @@ class wxBaseArray : public wxVector<T>
: base_vec(first, last)
{ }

template<typename _IList>
wxBaseArray(std::initializer_list<_IList>) : base_vec(list) {}

void Empty() { this->clear(); }
void Clear() { this->clear(); }
void Alloc(size_t uiSize) { this->reserve(uiSize); }
Expand Down Expand Up @@ -515,6 +522,8 @@ class wxBaseObjectArray : private wxBaseArray<T*>
name(size_t n, Base::const_reference v) : Base(n, v) { } \
template <class InputIterator> \
name(InputIterator first, InputIterator last) : Base(first, last) { } \
template<typename _IList> \
name(std::initializer_list<_IList> list) : Base(list) { } \
}


Expand Down
8 changes: 8 additions & 0 deletions interface/wx/arrstr.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ class wxArrayString : public wxArray
*/
wxArrayString(size_t sz, const wxString* arr);

/**
Constructs the container with the contents of the initializer_list @list.
@since 3.2.2
*/
template<typename T>
wxArrayString(std::initializer_list<T> list);

/**
Destructor frees memory occupied by the array strings. For performance
reasons it is not virtual, so this class should not be derived from.
Expand Down
8 changes: 8 additions & 0 deletions interface/wx/dynarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,14 @@ class wxArray<T>
*/
wxObjArray(const wxObjArray& array);

/**
Constructs the container with the contents of the initializer_list @list.
@since 3.2.2
*/
template<typename T>
wxArray(std::initializer_list<T> list);

/**
Performs a shallow array copy (i.e.\ doesn't copy the objects pointed to
even if the source array contains the items of pointer type).
Expand Down
12 changes: 12 additions & 0 deletions tests/arrays/arrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,15 @@ TEST_CASE("wxArrayString", "[dynarray]")
CHECK( a7.size() == 1 );

wxCLANG_WARNING_RESTORE(self-assign-overloaded)

wxArrayString a8( { wxT("dog"), wxT("human"), wxT("condor"), wxT("thermit"), wxT("alligator") } );
CHECK( a8.size() == 5 );
CHECK( a8[1] == "human" );
CHECK( a8[4] == "alligator" );

a8 = { wxT("Foo") };
CHECK( a8.size() == 1 );
CHECK( a8[0] == "Foo" );
}

TEST_CASE("wxSortedArrayString", "[dynarray]")
Expand Down Expand Up @@ -623,6 +632,9 @@ TEST_CASE("wxDynArray::" #name, "[dynarray]") \
CHECK( b.Index( 5 ) == 2 ); \
CHECK( b.Index( 6 ) == wxNOT_FOUND ); \
CHECK( b.Index( 17 ) == 3 ); \
\
wxArray##name c( { 1, 2, 3, 4, 5 } ); \
CHECK( c.size() == 5 ); \
}

TestArrayOf(UShort)
Expand Down

0 comments on commit 1312594

Please sign in to comment.