From 824e8eaaccab583f121e05e03678b110588a48cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Slav=C3=ADk?= Date: Thu, 17 Jan 2002 23:41:53 +0000 Subject: [PATCH] added singleton interface to wxXmlResource, replaces wxTheXmlResource git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@13618 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- contrib/include/wx/xrc/xmlres.h | 16 +++++++++++++--- contrib/samples/xrc/xrcdemo.cpp | 12 ++++++------ contrib/src/xrc/xmlres.cpp | 29 +++++++++++++++++++++-------- contrib/utils/wxrc/wxrc.cpp | 2 +- include/wx/xrc/xmlres.h | 16 +++++++++++++--- samples/xrc/xrcdemo.cpp | 12 ++++++------ src/xrc/xmlres.cpp | 29 +++++++++++++++++++++-------- utils/wxrc/wxrc.cpp | 2 +- 8 files changed, 82 insertions(+), 36 deletions(-) diff --git a/contrib/include/wx/xrc/xmlres.h b/contrib/include/wx/xrc/xmlres.h index 01f70f294b..8566b230f7 100644 --- a/contrib/include/wx/xrc/xmlres.h +++ b/contrib/include/wx/xrc/xmlres.h @@ -101,7 +101,7 @@ public: wxXmlResource(int flags = wxXRC_USE_LOCALE); wxXmlResource(const wxString& filemask, int flags = wxXRC_USE_LOCALE); ~wxXmlResource(); - + // Loads resources from XML files that match given filemask. // This method understands VFS (see filesys.h). bool Load(const wxString& filemask); @@ -172,6 +172,13 @@ public: int CompareVersion(int major, int minor, int release, int revision) const { return GetVersion() - (major*256*256*256 + minor*256*256 + release*256 + revision); } + + // Singleton accessors: + + // Gets global resources object or create one if none exists + static wxXmlResource *Get(); + // Sets global resources object and returns pointer to previous one (may be NULL). + static wxXmlResource *Set(wxXmlResource *res); protected: // Scans resources list for unloaded files and loads them. Also reloads @@ -199,11 +206,14 @@ private: #endif friend class wxXmlResourceHandler; + + // singleton instance: + static wxXmlResource *ms_instance; }; -// Global instance of resource class. For your convenience. -extern WXXMLDLLEXPORT wxXmlResource *wxTheXmlResource; +// This is here only for backward compatibility. Do NOT use!! +#define wxTheXmlResource wxXmlResource::Get() // This macro translates string identifier (as used in XML resource, // e.g. ...) to integer id that is needed by diff --git a/contrib/samples/xrc/xrcdemo.cpp b/contrib/samples/xrc/xrcdemo.cpp index be7bb1b41c..91cc6b70a8 100644 --- a/contrib/samples/xrc/xrcdemo.cpp +++ b/contrib/samples/xrc/xrcdemo.cpp @@ -111,8 +111,8 @@ IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { wxImage::AddHandler(new wxGIFHandler); - wxTheXmlResource->InitAllHandlers(); - wxTheXmlResource->Load("rc/resource.xrc"); + wxXmlResource::Get()->InitAllHandlers(); + wxXmlResource::Get()->Load("rc/resource.xrc"); MyFrame *frame = new MyFrame("XML resources demo", wxPoint(50, 50), wxSize(450, 340)); @@ -130,8 +130,8 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) { SetIcon(wxICON(appicon)); - SetMenuBar(wxTheXmlResource->LoadMenuBar("mainmenu")); - SetToolBar(wxTheXmlResource->LoadToolBar(this, "toolbar")); + SetMenuBar(wxXmlResource::Get()->LoadMenuBar("mainmenu")); + SetToolBar(wxXmlResource::Get()->LoadToolBar(this, "toolbar")); } @@ -155,7 +155,7 @@ void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event)) { wxDialog dlg; - wxTheXmlResource->LoadDialog(&dlg, this, "dlg1"); + wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg1"); dlg.ShowModal(); } @@ -163,6 +163,6 @@ void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnDlg2(wxCommandEvent& WXUNUSED(event)) { wxDialog dlg; - wxTheXmlResource->LoadDialog(&dlg, this, "dlg2"); + wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg2"); dlg.ShowModal(); } diff --git a/contrib/src/xrc/xmlres.cpp b/contrib/src/xrc/xmlres.cpp index a802efe57c..649a55320d 100644 --- a/contrib/src/xrc/xmlres.cpp +++ b/contrib/src/xrc/xmlres.cpp @@ -40,6 +40,22 @@ WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords); +wxXmlResource *wxXmlResource::ms_instance = NULL; + +/*static*/ wxXmlResource *wxXmlResource::Get() +{ + if ( !ms_instance ) + ms_instance = new wxXmlResource; + return ms_instance; +} + +/*static*/ wxXmlResource *wxXmlResource::Set(wxXmlResource *res) +{ + wxXmlResource *old = ms_instance; + ms_instance = res; + return old; +} + wxXmlResource::wxXmlResource(int flags) { m_handlers.DeleteContents(TRUE); @@ -1085,21 +1101,18 @@ static void CleanXMLID_Records() // --------------- module and globals ----------------------------- - -static wxXmlResource gs_XmlResource; - -wxXmlResource *wxTheXmlResource = &gs_XmlResource; - - class wxXmlResourceModule: public wxModule { DECLARE_DYNAMIC_CLASS(wxXmlResourceModule) public: wxXmlResourceModule() {} - bool OnInit() {return TRUE;} + bool OnInit() + { + return TRUE; + } void OnExit() { - wxTheXmlResource->ClearHandlers(); + delete wxXmlResource::Get(); CleanXMLID_Records(); } }; diff --git a/contrib/utils/wxrc/wxrc.cpp b/contrib/utils/wxrc/wxrc.cpp index 5741a87450..9acaeef5e4 100644 --- a/contrib/utils/wxrc/wxrc.cpp +++ b/contrib/utils/wxrc/wxrc.cpp @@ -392,7 +392,7 @@ void " + parFuncname + "()\n\ { wxString name, ext, path; wxSplitPath(parFiles[i], &path, &name, &ext); - file.Write(" wxTheXmlResource->Load(\"memory:xml_resource/" + + file.Write(" wxXmlResource::Get()->Load(\"memory:xml_resource/" + name + ".xrc" + "\");\n"); } diff --git a/include/wx/xrc/xmlres.h b/include/wx/xrc/xmlres.h index 01f70f294b..8566b230f7 100644 --- a/include/wx/xrc/xmlres.h +++ b/include/wx/xrc/xmlres.h @@ -101,7 +101,7 @@ public: wxXmlResource(int flags = wxXRC_USE_LOCALE); wxXmlResource(const wxString& filemask, int flags = wxXRC_USE_LOCALE); ~wxXmlResource(); - + // Loads resources from XML files that match given filemask. // This method understands VFS (see filesys.h). bool Load(const wxString& filemask); @@ -172,6 +172,13 @@ public: int CompareVersion(int major, int minor, int release, int revision) const { return GetVersion() - (major*256*256*256 + minor*256*256 + release*256 + revision); } + + // Singleton accessors: + + // Gets global resources object or create one if none exists + static wxXmlResource *Get(); + // Sets global resources object and returns pointer to previous one (may be NULL). + static wxXmlResource *Set(wxXmlResource *res); protected: // Scans resources list for unloaded files and loads them. Also reloads @@ -199,11 +206,14 @@ private: #endif friend class wxXmlResourceHandler; + + // singleton instance: + static wxXmlResource *ms_instance; }; -// Global instance of resource class. For your convenience. -extern WXXMLDLLEXPORT wxXmlResource *wxTheXmlResource; +// This is here only for backward compatibility. Do NOT use!! +#define wxTheXmlResource wxXmlResource::Get() // This macro translates string identifier (as used in XML resource, // e.g. ...) to integer id that is needed by diff --git a/samples/xrc/xrcdemo.cpp b/samples/xrc/xrcdemo.cpp index be7bb1b41c..91cc6b70a8 100644 --- a/samples/xrc/xrcdemo.cpp +++ b/samples/xrc/xrcdemo.cpp @@ -111,8 +111,8 @@ IMPLEMENT_APP(MyApp) bool MyApp::OnInit() { wxImage::AddHandler(new wxGIFHandler); - wxTheXmlResource->InitAllHandlers(); - wxTheXmlResource->Load("rc/resource.xrc"); + wxXmlResource::Get()->InitAllHandlers(); + wxXmlResource::Get()->Load("rc/resource.xrc"); MyFrame *frame = new MyFrame("XML resources demo", wxPoint(50, 50), wxSize(450, 340)); @@ -130,8 +130,8 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size) { SetIcon(wxICON(appicon)); - SetMenuBar(wxTheXmlResource->LoadMenuBar("mainmenu")); - SetToolBar(wxTheXmlResource->LoadToolBar(this, "toolbar")); + SetMenuBar(wxXmlResource::Get()->LoadMenuBar("mainmenu")); + SetToolBar(wxXmlResource::Get()->LoadToolBar(this, "toolbar")); } @@ -155,7 +155,7 @@ void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event)) { wxDialog dlg; - wxTheXmlResource->LoadDialog(&dlg, this, "dlg1"); + wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg1"); dlg.ShowModal(); } @@ -163,6 +163,6 @@ void MyFrame::OnDlg1(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnDlg2(wxCommandEvent& WXUNUSED(event)) { wxDialog dlg; - wxTheXmlResource->LoadDialog(&dlg, this, "dlg2"); + wxXmlResource::Get()->LoadDialog(&dlg, this, "dlg2"); dlg.ShowModal(); } diff --git a/src/xrc/xmlres.cpp b/src/xrc/xmlres.cpp index a802efe57c..649a55320d 100644 --- a/src/xrc/xmlres.cpp +++ b/src/xrc/xmlres.cpp @@ -40,6 +40,22 @@ WX_DEFINE_OBJARRAY(wxXmlResourceDataRecords); +wxXmlResource *wxXmlResource::ms_instance = NULL; + +/*static*/ wxXmlResource *wxXmlResource::Get() +{ + if ( !ms_instance ) + ms_instance = new wxXmlResource; + return ms_instance; +} + +/*static*/ wxXmlResource *wxXmlResource::Set(wxXmlResource *res) +{ + wxXmlResource *old = ms_instance; + ms_instance = res; + return old; +} + wxXmlResource::wxXmlResource(int flags) { m_handlers.DeleteContents(TRUE); @@ -1085,21 +1101,18 @@ static void CleanXMLID_Records() // --------------- module and globals ----------------------------- - -static wxXmlResource gs_XmlResource; - -wxXmlResource *wxTheXmlResource = &gs_XmlResource; - - class wxXmlResourceModule: public wxModule { DECLARE_DYNAMIC_CLASS(wxXmlResourceModule) public: wxXmlResourceModule() {} - bool OnInit() {return TRUE;} + bool OnInit() + { + return TRUE; + } void OnExit() { - wxTheXmlResource->ClearHandlers(); + delete wxXmlResource::Get(); CleanXMLID_Records(); } }; diff --git a/utils/wxrc/wxrc.cpp b/utils/wxrc/wxrc.cpp index 5741a87450..9acaeef5e4 100644 --- a/utils/wxrc/wxrc.cpp +++ b/utils/wxrc/wxrc.cpp @@ -392,7 +392,7 @@ void " + parFuncname + "()\n\ { wxString name, ext, path; wxSplitPath(parFiles[i], &path, &name, &ext); - file.Write(" wxTheXmlResource->Load(\"memory:xml_resource/" + + file.Write(" wxXmlResource::Get()->Load(\"memory:xml_resource/" + name + ".xrc" + "\");\n"); }