Skip to content

Commit 425d945

Browse files
Lotendanvadz
authored andcommitted
Add support for initializer_list to wx dynamic arrays
While these array classes are deprecated in the user code, they're still used, for compatibility, in many places in wxWidgets API and allowing to create them from initializer_list makes using it more ergonomic as it's now possible to just pass an initializer list of items to fill the control with, for example, instead of appending them one by one. This is the equivalent of 4d62df4 (Add support for initializer_list to wx dynamic arrays, 2023-03-02) from master. See wxWidgets#23309. Closes wxWidgets#23966.
1 parent a87c857 commit 425d945

File tree

7 files changed

+83
-0
lines changed

7 files changed

+83
-0
lines changed

docs/changes.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ Changes in behaviour which may result in build errors
235235
3.2.4: (released 2024-??-??)
236236
----------------------------
237237

238+
All:
239+
240+
- Allow creating wxArrays from std::initialized_list (Lotendan, #23966).
241+
238242
All (GUI):
239243

240244
- Fix refreshing multiple selection items in generic wxListCtrl.

include/wx/arrstr.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
#if wxUSE_STD_CONTAINERS_COMPATIBLY
1919
#include <vector>
2020
#endif
21+
#ifdef wxHAVE_INITIALIZER_LIST
22+
#include <initializer_list>
23+
#endif
2124

2225
// these functions are only used in STL build now but we define them in any
2326
// case for compatibility with the existing code outside of the library which
@@ -83,6 +86,10 @@ class WXDLLIMPEXP_BASE wxArrayString : public wxArrayStringBase
8386
wxArrayString(size_t sz, const char** a);
8487
wxArrayString(size_t sz, const wchar_t** a);
8588
wxArrayString(size_t sz, const wxString* a);
89+
#ifdef wxHAVE_INITIALIZER_LIST
90+
template<typename U>
91+
wxArrayString(std::initializer_list<U> list) : wxArrayStringBase(list) { }
92+
#endif
8693

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

@@ -184,6 +191,11 @@ class WXDLLIMPEXP_BASE wxArrayString
184191
wxArrayString(size_t sz, const wxString* a);
185192
// copy ctor
186193
wxArrayString(const wxArrayString& array);
194+
#ifdef wxHAVE_INITIALIZER_LIST
195+
// list constructor
196+
template<typename U>
197+
wxArrayString(std::initializer_list<U> list) { Init(false); assign(list.begin(), list.end()); }
198+
#endif
187199
// assignment operator
188200
wxArrayString& operator=(const wxArrayString& src);
189201
// not virtual, this class should not be derived from

include/wx/defs.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,14 @@ typedef short int WXTYPE;
505505
#endif
506506
#endif /* HAVE_WOSTREAM */
507507

508+
#ifndef wxHAVE_INITIALIZER_LIST
509+
#if __cplusplus >= 201103L
510+
#define wxHAVE_INITIALIZER_LIST
511+
#elif wxCHECK_VISUALC_VERSION(11)
512+
#define wxHAVE_INITIALIZER_LIST
513+
#endif
514+
#endif /* wxHAVE_INITIALIZER_LIST */
515+
508516
/* ---------------------------------------------------------------------------- */
509517
/* portable calling conventions macros */
510518
/* ---------------------------------------------------------------------------- */

include/wx/dynarray.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515

1616
#include "wx/vector.h"
1717

18+
#ifdef wxHAVE_INITIALIZER_LIST
19+
#include <initializer_list>
20+
#endif
21+
1822
/*
1923
This header defines legacy dynamic arrays and object arrays (i.e. arrays
2024
which own their elements) classes.
@@ -105,6 +109,11 @@ class wxBaseArray : public wxVector<T>
105109
: base_vec(first, last)
106110
{ }
107111

112+
#ifdef wxHAVE_INITIALIZER_LIST
113+
template<typename U>
114+
wxBaseArray(std::initializer_list<U> list) : base_vec(list.begin(), list.end()) {}
115+
#endif
116+
108117
void Empty() { this->clear(); }
109118
void Clear() { this->clear(); }
110119
void Alloc(size_t uiSize) { this->reserve(uiSize); }
@@ -495,6 +504,14 @@ class wxBaseObjectArray : private wxBaseArray<T*>
495504
#define WX_DEFINE_USER_EXPORTED_TYPEARRAY_PTR(T, name, base, expdecl) \
496505
WX_DEFINE_TYPEARRAY_WITH_DECL_PTR(T, name, base, class expdecl)
497506

507+
#ifdef wxHAVE_INITIALIZER_LIST
508+
#define WX_DEFINE_CTOR_FROM_INIT_LIST(T, name, base, classdecl) \
509+
template<typename U> \
510+
name(std::initializer_list<U> list) : Base(list.begin(), list.end()) { }
511+
#else
512+
#define WX_DEFINE_CTOR_FROM_INIT_LIST(T, name, base, classdecl) // No support for initializer_list
513+
#endif
514+
498515
// This is the only non-trivial macro, which actually defines the array class
499516
// with the given name containing the elements of the specified type.
500517
//
@@ -519,6 +536,7 @@ class wxBaseObjectArray : private wxBaseArray<T*>
519536
name(size_t n, Base::const_reference v) : Base(n, v) { } \
520537
template <class InputIterator> \
521538
name(InputIterator first, InputIterator last) : Base(first, last) { } \
539+
WX_DEFINE_CTOR_FROM_INIT_LIST(T, name, base, classdecl) \
522540
}
523541

524542

interface/wx/arrstr.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ class wxArrayString : public wxArray
8989
*/
9090
wxArrayString(size_t sz, const wxString* arr);
9191

92+
/**
93+
Constructs the container with the contents of the initializer_list @a list.
94+
95+
@since 3.2.4
96+
*/
97+
template<typename T>
98+
wxArrayString(std::initializer_list<T> list);
99+
92100
/**
93101
Destructor frees memory occupied by the array strings. For performance
94102
reasons it is not virtual, so this class should not be derived from.

interface/wx/dynarray.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,14 @@ class wxArray<T>
271271
*/
272272
wxObjArray(const wxObjArray& array);
273273

274+
/**
275+
Constructs the container with the contents of the initializer_list @a list.
276+
277+
@since 3.2.4
278+
*/
279+
template<typename T>
280+
wxArray(std::initializer_list<T> list);
281+
274282
/**
275283
Performs a shallow array copy (i.e.\ doesn't copy the objects pointed to
276284
even if the source array contains the items of pointer type).

tests/arrays/arrays.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,21 @@ TEST_CASE("wxArrayString", "[dynarray]")
340340
CHECK( a7.size() == 1 );
341341

342342
wxCLANG_WARNING_RESTORE(self-assign-overloaded)
343+
344+
#ifdef wxHAVE_INITIALIZER_LIST
345+
wxArrayString a8( { wxT("dog"), wxT("human"), wxT("condor"), wxT("thermit"), wxT("alligator") } );
346+
CHECK( a8.size() == 5 );
347+
CHECK( a8[1] == "human" );
348+
CHECK( a8[4] == "alligator" );
349+
350+
a8 = { wxT("Foo") }; // Test operator=
351+
CHECK( a8.size() == 1 );
352+
CHECK( a8[0] == "Foo" );
353+
354+
// Same test with std::initializer_list<std::string>
355+
wxArrayString a9( { std::string("dog"), std::string("human"), std::string("condor"), std::string("thermit"), std::string("alligator") } );
356+
CHECK( a9.size() == 5 );
357+
#endif // wxHAVE_INITIALIZER_LIST
343358
}
344359

345360
TEST_CASE("wxSortedArrayString", "[dynarray]")
@@ -584,6 +599,14 @@ TEST_CASE("wxObjArrayPtr", "[dynarray]")
584599
CHECK( barptrs.size() == 0 );
585600
}
586601

602+
#ifdef wxHAVE_INITIALIZER_LIST
603+
#define TestArrayWithInitializerListOf(name) \
604+
wxArray##name c({1,2,3}); \
605+
CHECK(c.size() == 3);
606+
#else
607+
#define TestArrayWithInitializerListOf(name) // No support for initializer_list
608+
#endif
609+
587610
#define TestArrayOf(name) \
588611
\
589612
TEST_CASE("wxDynArray::" #name, "[dynarray]") \
@@ -623,6 +646,8 @@ TEST_CASE("wxDynArray::" #name, "[dynarray]") \
623646
CHECK( b.Index( 5 ) == 2 ); \
624647
CHECK( b.Index( 6 ) == wxNOT_FOUND ); \
625648
CHECK( b.Index( 17 ) == 3 ); \
649+
\
650+
TestArrayWithInitializerListOf(name) \
626651
}
627652

628653
TestArrayOf(UShort)

0 commit comments

Comments
 (0)