From dae60aeebb7c3fedf9c8ce8775bbd7a11bdc508e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Slav=C3=ADk?= Date: Tue, 19 Jan 2010 13:01:40 +0000 Subject: [PATCH] Add wxThreadEvent::SetPayload(). This makes it possible to easily pass custom data between threads, in type-safe way (thanks to internal use of wxAny). This adds sizeof(wxAny)==16+sizeof(void*) overhead to wxThreadEvent, but I think it's cheaper than doing malloc/free on copying. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@63187 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/event.h | 22 ++++++++++++++++++++++ interface/wx/event.h | 31 +++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/include/wx/event.h b/include/wx/event.h index 225da47b42..36f7a8b73e 100644 --- a/include/wx/event.h +++ b/include/wx/event.h @@ -27,6 +27,7 @@ #include "wx/thread.h" #include "wx/tracker.h" #include "wx/typeinfo.h" +#include "wx/any.h" #ifdef wxHAS_EVENT_BIND #include "wx/meta/convertible.h" @@ -1203,6 +1204,10 @@ public: // make sure our string member (which uses COW, aka refcounting) is not // shared by other wxString instances: SetString(GetString().c_str()); + +#if wxUSE_ANY && (!defined(__VISUALC__) || wxCHECK_VISUALC_VERSION(7)) + m_payload = event.m_payload; +#endif } virtual wxEvent *Clone() const @@ -1215,6 +1220,23 @@ public: virtual wxEventCategory GetEventCategory() const { return wxEVT_CATEGORY_THREAD; } +#if wxUSE_ANY && (!defined(__VISUALC__) || wxCHECK_VISUALC_VERSION(7)) + template + void SetPayload(const T& payload) + { + m_payload = payload; + } + + template + T GetPayload() const + { + return m_payload.As(); + } + +protected: + wxAny m_payload; +#endif // wxUSE_ANY && (!defined(__VISUALC__) || wxCHECK_VISUALC_VERSION(7)) + private: DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxThreadEvent) }; diff --git a/interface/wx/event.h b/interface/wx/event.h index baa35fc9c3..dc010cc28d 100644 --- a/interface/wx/event.h +++ b/interface/wx/event.h @@ -2853,6 +2853,37 @@ public: when calling wxEventLoopBase::YieldFor(). */ virtual wxEventCategory GetEventCategory() const; + + /** + Sets custom data payload. + + The @a payload argument may be of any type that wxAny can handle + (i.e. pretty much anything). Note that T's copy constructor must be + thread-safe, i.e. create a copy that doesn't share anything with + the original (see Clone()). + + @note This method is not available with Visual C++ 6. + + @since 2.9.1 + + @see GetPayload(), wxAny + */ + template + void SetPayload(const T& payload); + + /** + Get custom data payload. + + Correct type is checked in debug builds. + + @note This method is not available with Visual C++ 6. + + @since 2.9.1 + + @see SetPayload(), wxAny + */ + template + T GetPayload() const; };