Add wxClearList() function replacing WX_CLEAR_LIST() macro

Keep the macro for compatibility but there is really no reason to use a
macro here.

Also document this macro, even if it's deprecated, as it's used outside
of wxWidgets code too and this allows us to redirect people using it to
wxClearList().
This commit is contained in:
Vadim Zeitlin
2024-04-13 19:36:52 +02:00
parent cfc5a0eff5
commit 756f010f35
16 changed files with 50 additions and 35 deletions
+11 -12
View File
@@ -1292,18 +1292,17 @@ public:
#endif // wxLIST_COMPATIBILITY
// delete all list elements
//
// NB: the class declaration of the list elements must be visible from the
// place where you use this macro, otherwise the proper destructor may not
// be called (a decent compiler should give a warning about it, but don't
// count on it)!
#define WX_CLEAR_LIST(type, list) \
{ \
type::iterator it, en; \
for( it = (list).begin(), en = (list).end(); it != en; ++it ) \
delete *it; \
(list).clear(); \
}
template <class T>
inline void wxClearList(T& list)
{
for ( auto& elem: list )
delete elem;
list.clear();
}
// Deprecated macro, use wxClearList() instead.
#define WX_CLEAR_LIST(type, list) wxClearList(list)
// append all element of one list to another one
//
+2 -2
View File
@@ -86,7 +86,7 @@ public:
wxVariant does.
Note that objects constructed from list-valued variants
require the list to be explicitly cleared using `WX_CLEAR_LIST`
require the list to be explicitly cleared using wxClearList()
to avoid leaking memory. This unfortunate behaviour will not
be changed to prevent breaking the existing code relying on it.
@@ -101,7 +101,7 @@ public:
// Clear the list to avoid the memory leak.
wxAnyList anyList = any.As<wxAnyList>();
WX_CLEAR_LIST(wxAnyList, anyList);
wxClearList(anyList);
@endcode
*/
wxAny(const wxVariant& variant);
+16
View File
@@ -475,3 +475,19 @@ public:
defining it.
*/
#define WX_DEFINE_LIST(T, name)
/**
Deprecated macro equivalent to wxClearList().
@deprecated This macro is deprecated, use wxClearList() instead.
*/
#define WX_CLEAR_LIST(type, list) wxClearList(list)
/**
Function deletes all elements of the given list, which must be pointers.
This simple helper function just calls delete on each element of the list.
@since 3.3.0
*/
template <typename T> inline void wxClearList(T& list);
+1 -1
View File
@@ -640,7 +640,7 @@ bool wxFileSystem::HasHandlerForPath(const wxString &location)
void wxFileSystem::CleanUpHandlers()
{
WX_CLEAR_LIST(wxList, m_Handlers);
wxClearList(m_Handlers);
}
// Returns the native path for a file URL
+3 -3
View File
@@ -143,7 +143,7 @@ wxCONSTRUCTOR_DUMMY( wxMenuBar )
const wxMenuInfoHelperList& wxMenuBarBase::GetMenuInfos() const
{
wxMenuInfoHelperList* list = const_cast< wxMenuInfoHelperList* > (& m_menuInfos);
WX_CLEAR_LIST( wxMenuInfoHelperList, *list);
wxClearList(*list);
for (size_t i = 0 ; i < GetMenuCount(); ++i)
{
wxMenuInfoHelper* info = new wxMenuInfoHelper();
@@ -382,7 +382,7 @@ void wxMenuBase::Init(long style)
wxMenuBase::~wxMenuBase()
{
WX_CLEAR_LIST(wxMenuItemList, m_items);
wxClearList(m_items);
}
// ----------------------------------------------------------------------------
@@ -901,7 +901,7 @@ wxMenuBarBase::wxMenuBarBase()
wxMenuBarBase::~wxMenuBarBase()
{
WX_CLEAR_LIST(wxMenuList, m_menus);
wxClearList(m_menus);
}
// ----------------------------------------------------------------------------
+1 -1
View File
@@ -120,7 +120,7 @@ void wxNotebookBase::AddPageInfo( wxNotebookPageInfo* info )
const wxNotebookPageInfoList& wxNotebookBase::GetPageInfos() const
{
wxNotebookPageInfoList* list = const_cast< wxNotebookPageInfoList* >( &m_pageInfos );
WX_CLEAR_LIST( wxNotebookPageInfoList, *list );
wxClearList(*list);
for( size_t i = 0; i < GetPageCount(); ++i )
{
wxNotebookPageInfo *info = new wxNotebookPageInfo();
+2 -2
View File
@@ -802,7 +802,7 @@ bool wxSizerItem::IsShown() const
wxSizer::~wxSizer()
{
WX_CLEAR_LIST(wxSizerItemList, m_children);
wxClearList(m_children);
}
wxSizerItem* wxSizer::DoInsert( size_t index, wxSizerItem *item )
@@ -1080,7 +1080,7 @@ void wxSizer::Clear( bool delete_windows )
DeleteWindows();
// Now empty the list
WX_CLEAR_LIST(wxSizerItemList, m_children);
wxClearList(m_children);
}
void wxSizer::DeleteWindows()
+1 -1
View File
@@ -529,7 +529,7 @@ bool wxToolBarBase::Realize()
wxToolBarBase::~wxToolBarBase()
{
WX_CLEAR_LIST(wxToolBarToolsList, m_tools);
wxClearList(m_tools);
// notify the frame that it doesn't have a tool bar any longer to avoid
// dangling pointers
+1 -1
View File
@@ -293,7 +293,7 @@ wxCursor *wxHtmlWindow::ms_cursorDefault = nullptr;
void wxHtmlWindow::CleanUpStatics()
{
wxDELETE(m_DefaultFilter);
WX_CLEAR_LIST(wxList, m_Filters);
wxClearList(m_Filters);
wxDELETE(m_GlobalProcessors);
wxDELETE(ms_cursorLink);
wxDELETE(ms_cursorText);
+2 -2
View File
@@ -2869,8 +2869,8 @@ void wxMSWDCImpl::AddToDCCache(wxDCCacheEntry* entry)
void wxMSWDCImpl::ClearCache()
{
WX_CLEAR_LIST(wxList, sm_dcCache);
WX_CLEAR_LIST(wxList, sm_bitmapCache);
wxClearList(sm_dcCache);
wxClearList(sm_bitmapCache);
}
// Clean up cache at app exit
+1 -1
View File
@@ -11759,7 +11759,7 @@ bool wxRichTextCommand::Undo()
void wxRichTextCommand::ClearActions()
{
WX_CLEAR_LIST(wxList, m_actions);
wxClearList(m_actions);
}
/*!
+4 -4
View File
@@ -366,10 +366,10 @@ wxRichTextStyleDefinition* wxRichTextStyleSheet::FindStyle(const wxList& list, c
/// Delete all styles
void wxRichTextStyleSheet::DeleteStyles()
{
WX_CLEAR_LIST(wxList, m_characterStyleDefinitions);
WX_CLEAR_LIST(wxList, m_paragraphStyleDefinitions);
WX_CLEAR_LIST(wxList, m_listStyleDefinitions);
WX_CLEAR_LIST(wxList, m_boxStyleDefinitions);
wxClearList(m_characterStyleDefinitions);
wxClearList(m_paragraphStyleDefinitions);
wxClearList(m_listStyleDefinitions);
wxClearList(m_boxStyleDefinitions);
}
/// Insert into list of style sheets
+2 -2
View File
@@ -201,7 +201,7 @@ wxTextCtrl::wxTextCtrl( wxWindow *parent,
wxTextCtrl::~wxTextCtrl()
{
WX_CLEAR_LIST(wxList, m_undos);
wxClearList(m_undos);
}
bool wxTextCtrl::Create( wxWindow *parent,
@@ -403,7 +403,7 @@ void wxTextCtrl::Clear()
SetScrollbars( m_charWidth, m_lineHeight, 0, 0, 0, 0 );
Refresh();
WX_CLEAR_LIST(wxList, m_undos);
wxClearList(m_undos);
}
void wxTextCtrl::Replace(long from, long to, const wxString& value)
+1 -1
View File
@@ -657,7 +657,7 @@ void wxAnyTestCase::wxVariantConversions()
CPPUNIT_ASSERT(variant[0].GetLong() == 15);
CPPUNIT_ASSERT(variant[1].GetString() == "abc");
// Avoid the memory leak.
WX_CLEAR_LIST(wxAnyList, anyList);
wxClearList(anyList);
any = wxAny(vCustomType);
CPPUNIT_ASSERT(wxANY_CHECK_TYPE(any, wxVariantData*));
+1 -1
View File
@@ -89,7 +89,7 @@ wx28HtmlParser::~wx28HtmlParser()
}
delete m_HandlersStack;
m_HandlersHash.Clear();
WX_CLEAR_LIST(wxList, m_HandlersList);
wxClearList(m_HandlersList);
delete m_entitiesParser;
}
+1 -1
View File
@@ -203,7 +203,7 @@ void ListsTestCase::wxListCtorTest()
#if !wxUSE_STD_CONTAINERS
list1.DeleteContents(true);
#else
WX_CLEAR_LIST(wxListBazs, list1);
wxClearList(list1);
#endif
}