1. renamed wxFontMapper::IsWxFontMapper() to IsDummy() (with reverse semantics)

2. added wxFontMapper::Reset() to only do the cast needed when deleting
   the font mapper object once
3. reset the dummy font mapper created during the app initialization in
   wxFontMapperModule and not in init.cpp


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@39624 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2006-06-07 17:49:27 +00:00
parent 4e3e485bc8
commit d5bfbd9ab8
4 changed files with 49 additions and 29 deletions

View File

@@ -49,8 +49,6 @@ class WXDLLIMPEXP_CORE wxFontMapper;
class WXDLLIMPEXP_BASE wxFontMapperBase class WXDLLIMPEXP_BASE wxFontMapperBase
{ {
// For IsWxFontMapper()
friend class WXDLLIMPEXP_CORE wxFontMapper;
public: public:
// constructtor and such // constructtor and such
// --------------------- // ---------------------
@@ -70,6 +68,10 @@ public:
// set the singleton to 'mapper' instance and return previous one // set the singleton to 'mapper' instance and return previous one
static wxFontMapper *Set(wxFontMapper *mapper); static wxFontMapper *Set(wxFontMapper *mapper);
// delete the existing font mapper if any
static void Reset();
// translates charset strings to encoding // translates charset strings to encoding
// -------------------------------------- // --------------------------------------
@@ -124,6 +126,10 @@ public:
#endif // wxUSE_CONFIG #endif // wxUSE_CONFIG
// returns true for the base class and false for a "real" font mapper object
// (implementation-only)
virtual bool IsDummy() { return true; }
protected: protected:
#if wxUSE_CONFIG && wxUSE_FILECONFIG #if wxUSE_CONFIG && wxUSE_FILECONFIG
// get the config object we're using -- either the global config object // get the config object we're using -- either the global config object
@@ -159,9 +165,6 @@ protected:
int NonInteractiveCharsetToEncoding(const wxString& charset); int NonInteractiveCharsetToEncoding(const wxString& charset);
private: private:
// pseudo-RTTI since we aren't a wxObject.
virtual bool IsWxFontMapper();
// the global fontmapper object or NULL // the global fontmapper object or NULL
static wxFontMapper *sm_instance; static wxFontMapper *sm_instance;
@@ -239,6 +242,9 @@ public:
// are additional methods in the subclass. // are additional methods in the subclass.
static wxFontMapper *Get(); static wxFontMapper *Get();
// pseudo-RTTI since we aren't a wxObject.
virtual bool IsDummy() { return false; }
protected: protected:
// GetAltForEncoding() helper: tests for the existence of the given // GetAltForEncoding() helper: tests for the existence of the given
// encoding and saves the result in config if ok - this results in the // encoding and saves the result in config if ok - this results in the
@@ -258,9 +264,6 @@ protected:
wxWindow *m_windowParent; wxWindow *m_windowParent;
private: private:
// pseudo-RTTI since we aren't a wxObject.
virtual bool IsWxFontMapper();
DECLARE_NO_COPY_CLASS(wxFontMapper) DECLARE_NO_COPY_CLASS(wxFontMapper)
}; };

View File

@@ -233,8 +233,25 @@ class wxFontMapperModule : public wxModule
{ {
public: public:
wxFontMapperModule() : wxModule() { } wxFontMapperModule() : wxModule() { }
virtual bool OnInit() { return true; }
virtual void OnExit() { delete (wxFontMapperBase*)wxFontMapperBase::Set(NULL); } virtual bool OnInit()
{
// a dummy wxFontMapperBase object could have been created during the
// program startup before wxApp was created, we have to delete it to
// allow creating the real font mapper next time it is needed now that
// we can create it (when the modules are initialized, wxApp object
// already exists)
wxFontMapperBase *fm = wxFontMapperBase::Get();
if ( fm && fm->IsDummy() )
wxFontMapperBase::Reset();
return true;
}
virtual void OnExit()
{
wxFontMapperBase::Reset();
}
DECLARE_DYNAMIC_CLASS(wxFontMapperModule) DECLARE_DYNAMIC_CLASS(wxFontMapperModule)
}; };
@@ -267,9 +284,6 @@ wxFontMapperBase::~wxFontMapperBase()
#endif // wxUSE_CONFIG #endif // wxUSE_CONFIG
} }
bool wxFontMapperBase::IsWxFontMapper()
{ return false; }
/* static */ /* static */
wxFontMapperBase *wxFontMapperBase::Get() wxFontMapperBase *wxFontMapperBase::Get()
{ {
@@ -303,6 +317,19 @@ wxFontMapper *wxFontMapperBase::Set(wxFontMapper *mapper)
return old; return old;
} }
/* static */
void wxFontMapperBase::Reset()
{
if ( sm_instance )
{
// we need a cast as wxFontMapper is not fully declared here and so the
// compiler can't know that it derives from wxFontMapperBase (but
// run-time behaviour will be correct because the dtor is virtual)
delete sm_instance;
sm_instance = NULL;
}
}
#if wxUSE_CONFIG && wxUSE_FILECONFIG #if wxUSE_CONFIG && wxUSE_FILECONFIG
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@@ -160,17 +160,17 @@ wxFontMapper::~wxFontMapper()
{ {
} }
bool wxFontMapper::IsWxFontMapper()
{ return true; }
/* static */ /* static */
wxFontMapper *wxFontMapper::Get() wxFontMapper *wxFontMapper::Get()
{ {
wxFontMapperBase *fontmapper = wxFontMapperBase::Get(); wxFontMapperBase *fontmapper = wxFontMapperBase::Get();
wxASSERT_MSG(fontmapper->IsWxFontMapper(), wxT("GUI code requested a wxFontMapper but we only have a wxFontMapperBase.")); wxASSERT_MSG( !fontmapper->IsDummy(),
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 // Now return it anyway because there's a chance the GUI code might just
// only want to call wxFontMapperBase functions. // only want to call wxFontMapperBase functions and it's better than
return (wxFontMapper*)fontmapper; // crashing by returning NULL
return (wxFontMapper *)fontmapper;
} }
wxFontEncoding wxFontEncoding

View File

@@ -36,9 +36,6 @@
#include "wx/ptr_scpd.h" #include "wx/ptr_scpd.h"
#include "wx/module.h" #include "wx/module.h"
#include "wx/except.h" #include "wx/except.h"
#if wxUSE_FONTMAP
#include "wx/fontmap.h"
#endif
#if defined(__WXMSW__) && defined(__WXDEBUG__) #if defined(__WXMSW__) && defined(__WXDEBUG__)
#include "wx/msw/msvcrt.h" #include "wx/msw/msvcrt.h"
@@ -452,13 +449,6 @@ int wxEntry(int& argc, char **argv)
{ {
ConvertArgsToUnicode(argc, argv); ConvertArgsToUnicode(argc, argv);
#if wxUSE_FONTMAP
// If we created a font mapper during the above call,
// it will only be the base class, so delete it to allow
// app traits to create mapper.
delete (wxFontMapperBase*) wxFontMapperBase::Set(NULL);
#endif
return wxEntry(argc, gs_initData.argv); return wxEntry(argc, gs_initData.argv);
} }