From: David Elliott Date: Sat, 26 Mar 2005 21:47:54 +0000 (+0000) Subject: Improve type safety by doing a runtime check to make sure the X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/2a12cc65c8a0d576654b2c24c60503564ae07712?ds=inline Improve type safety by doing a runtime check to make sure the wxFontMapper really is a wxFontMapper when GUI code asks for it. Remove some incorrect comments and add some new ones. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@33080 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/fontmap.h b/include/wx/fontmap.h index d8149fab21..d7555d3b57 100644 --- a/include/wx/fontmap.h +++ b/include/wx/fontmap.h @@ -49,6 +49,8 @@ class WXDLLIMPEXP_CORE wxFontMapper; class WXDLLIMPEXP_BASE wxFontMapperBase { + // For IsWxFontMapper() + friend class WXDLLIMPEXP_CORE wxFontMapper; public: // constructtor and such // --------------------- @@ -61,6 +63,8 @@ public: // return instance of the wxFontMapper singleton // wxBase code only cares that it's a wxFontMapperBase + // In wxBase, wxFontMapper is only forward declared + // so one cannot implicitly cast from it to wxFontMapperBase. static wxFontMapperBase *Get(); // set the singleton to 'mapper' instance and return previous one @@ -156,6 +160,9 @@ protected: int NonInteractiveCharsetToEncoding(const wxString& charset); private: + // pseudo-RTTI since we aren't a wxObject. + virtual bool IsWxFontMapper(); + // the global fontmapper object or NULL static wxFontMapper *sm_instance; @@ -252,6 +259,9 @@ protected: wxWindow *m_windowParent; private: + // pseudo-RTTI since we aren't a wxObject. + virtual bool IsWxFontMapper(); + DECLARE_NO_COPY_CLASS(wxFontMapper) }; diff --git a/src/common/fmapbase.cpp b/src/common/fmapbase.cpp index 759c7541eb..e473054fe8 100644 --- a/src/common/fmapbase.cpp +++ b/src/common/fmapbase.cpp @@ -199,7 +199,7 @@ class wxFontMapperModule : public wxModule public: wxFontMapperModule() : wxModule() { } virtual bool OnInit() { return true; } - virtual void OnExit() { delete wxFontMapperBase::Set(NULL); } + virtual void OnExit() { delete (wxFontMapperBase*)wxFontMapperBase::Set(NULL); } DECLARE_DYNAMIC_CLASS(wxFontMapperModule) }; @@ -233,11 +233,10 @@ wxFontMapperBase::~wxFontMapperBase() #endif // wxUSE_CONFIG } +bool wxFontMapperBase::IsWxFontMapper() +{ return false; } + /* static */ -// Declared as returning wxFontMapper when wxUSE_GUI=1. Unfortunately, it's -// only implemented in wxBase library. Note that if the last resort -// is taken and GUI code tries to treat it as a real wxFontMapper -// then you'd be in trouble. wxFontMapperBase *wxFontMapperBase::Get() { if ( !sm_instance ) diff --git a/src/common/fontmap.cpp b/src/common/fontmap.cpp index 87b28c7bd2..be05e44b85 100644 --- a/src/common/fontmap.cpp +++ b/src/common/fontmap.cpp @@ -163,14 +163,17 @@ wxFontMapper::~wxFontMapper() { } +bool wxFontMapper::IsWxFontMapper() +{ return true; } + /* static */ -// Declared as returning wxFontMapper when wxUSE_GUI=1. Unfortunately, it's -// only implemented in wxBase library. Note that if the last resort -// is taken and GUI code tries to treat it as a real wxFontMapper -// then you'd be in trouble. wxFontMapper *wxFontMapper::Get() { - return (wxFontMapper*)wxFontMapperBase::Get(); + wxFontMapperBase *fontmapper = wxFontMapperBase::Get(); + wxASSERT_MSG(fontmapper->IsWxFontMapper(), wxT("GUI code requested a wxFontMapper but we only have a wxFontMapperBase.")); + // Now return it anyway because there's a chance the GUI code might just + // only want to call wxFontMapperBase functions. + return (wxFontMapper*)fontmapper; } wxFontEncoding