From bd09b4132d708d6c2267d080ed36ca6dd2eab53e Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 12 Jul 2019 16:10:26 +0200 Subject: [PATCH] Remove wxTimerEvent default constructor This ctor just created unusable wxTimerEvent objects (all of the methods specific to this class would just crash if called on them) and doesn't seem to be useful at all. It was added in e47859daebd15efcecb969e612295c868e944d79 apparently only in order to allow using wxIMPLEMENT_DYNAMIC_CLASS() instead of the previously used wxIMPLEMENT_ABSTRACT_CLASS() for wxTimerEvent, but there doesn't seem to be any reason to prefer macro over another, and there are good reasons to not allow creating objects in an invalid state. The only place where we relied on having default ctor for this event was in wxEvent::Clone() unit test, so update it to handle wxTimerEvent specially now that this ctor doesn't exist any longer. --- include/wx/timer.h | 11 ++++------- src/common/timercmn.cpp | 4 +++- tests/events/clone.cpp | 30 ++++++++++++++++++++++-------- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/include/wx/timer.h b/include/wx/timer.h index 956cc2a87e..fb15b3551b 100644 --- a/include/wx/timer.h +++ b/include/wx/timer.h @@ -159,26 +159,23 @@ private: class WXDLLIMPEXP_BASE wxTimerEvent : public wxEvent { public: - wxTimerEvent() - : wxEvent(wxID_ANY, wxEVT_TIMER) { m_timer=NULL; } - wxTimerEvent(wxTimer& timer) : wxEvent(timer.GetId(), wxEVT_TIMER), - m_timer(&timer) + m_timer(timer) { SetEventObject(timer.GetOwner()); } // accessors - int GetInterval() const { return m_timer->GetInterval(); } - wxTimer& GetTimer() const { return *m_timer; } + int GetInterval() const { return m_timer.GetInterval(); } + wxTimer& GetTimer() const { return m_timer; } // implement the base class pure virtual virtual wxEvent *Clone() const wxOVERRIDE { return new wxTimerEvent(*this); } virtual wxEventCategory GetEventCategory() const wxOVERRIDE { return wxEVT_CATEGORY_TIMER; } private: - wxTimer* m_timer; + wxTimer& m_timer; wxDECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxTimerEvent); }; diff --git a/src/common/timercmn.cpp b/src/common/timercmn.cpp index 79104c1acb..a70668eff6 100644 --- a/src/common/timercmn.cpp +++ b/src/common/timercmn.cpp @@ -38,7 +38,9 @@ // wxWin macros // ---------------------------------------------------------------------------- -wxIMPLEMENT_DYNAMIC_CLASS(wxTimerEvent, wxEvent); +// This class is not really abstract, but this macro has to be used because it +// doesn't have a default ctor. +wxIMPLEMENT_ABSTRACT_CLASS(wxTimerEvent, wxEvent); wxDEFINE_EVENT(wxEVT_TIMER, wxTimerEvent); diff --git a/tests/events/clone.cpp b/tests/events/clone.cpp index 2f06827fda..6ee15e84b3 100644 --- a/tests/events/clone.cpp +++ b/tests/events/clone.cpp @@ -18,6 +18,7 @@ #ifndef WX_PRECOMP #include "wx/event.h" + #include "wx/timer.h" #endif // WX_PRECOMP // -------------------------------------------------------------------------- @@ -47,6 +48,9 @@ CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( EventCloneTestCase, "EventCloneTestCase" void EventCloneTestCase::CheckAll() { + // Dummy timer needed just to create a wxTimerEvent. + wxTimer dummyTimer; + // check if event classes implement Clone() correctly // NOTE: the check is done against _all_ event classes which are linked to // the executable currently running, which are not necessarily all @@ -61,20 +65,30 @@ void EventCloneTestCase::CheckAll() cn == "wxEvent" ) continue; - const std::string - msg = std::string("Event class \"") + - std::string(cn.c_str()) + "\""; + INFO("Event class \"" << cn << "\""); - CPPUNIT_ASSERT_MESSAGE( msg, ci->IsDynamic() ); + wxEvent* test; + if ( ci->IsDynamic() ) + { + test = wxDynamicCast(ci->CreateObject(),wxEvent); + } + else if ( cn == "wxTimerEvent" ) + { + test = new wxTimerEvent(dummyTimer); + } + else + { + FAIL("Can't create objects of type " + cn); + continue; + } - wxEvent * const test = wxDynamicCast(ci->CreateObject(),wxEvent); - CPPUNIT_ASSERT_MESSAGE( msg, test ); + REQUIRE( test ); wxEvent * const cloned = test->Clone(); delete test; - CPPUNIT_ASSERT_MESSAGE( msg, cloned ); - CPPUNIT_ASSERT_MESSAGE( msg, cloned->GetClassInfo() == ci ); + REQUIRE( cloned ); + CHECK( cloned->GetClassInfo() == ci ); delete cloned; }