From 32bcde9e4850ca6192cd74b1f4a3fcfd7c2512df Mon Sep 17 00:00:00 2001 From: codereader Date: Sun, 27 Sep 2020 16:13:01 +0200 Subject: [PATCH] #5343: Use a proxy object to cache the reference in the Global*() module accessor wrappers. When the MODULE_REFERENCES_SUPPORT_INVALIDATION preprocessor symbol is defined, it will check the cached reference for validity on every access. --- include/iaasfile.h | 8 +--- include/ibrush.h | 9 +--- include/icameraview.h | 9 +--- include/iclipboard.h | 9 +--- include/iclipper.h | 9 +--- include/icolourscheme.h | 9 +--- include/icommandsystem.h | 9 +--- include/icounter.h | 9 +--- include/ieclass.h | 12 ++--- include/ieditstopwatch.h | 9 +--- include/ientity.h | 9 +--- include/ientityinspector.h | 11 ++--- include/ieventmanager.h | 12 ++--- include/ifilesystem.h | 9 +--- include/ifiletypes.h | 9 +--- include/ifilter.h | 9 +--- include/ifonts.h | 11 ++--- include/igame.h | 9 +--- include/igl.h | 18 ++------ include/igrid.h | 9 +--- include/igui.h | 8 +--- include/iimage.h | 9 +--- include/ilayer.h | 9 +--- include/imainframe.h | 9 +--- include/imainframelayout.h | 19 +++----- include/imap.h | 9 +--- include/imapformat.h | 9 +--- include/imapinfofile.h | 8 +--- include/imapresource.h | 9 +--- include/imd5anim.h | 9 +--- include/imediabrowser.h | 9 +--- include/imodel.h | 8 +--- include/imodelcache.h | 8 +--- include/imodule.h | 50 +++++++++++++++++++++ include/imousetoolmanager.h | 9 +--- include/imru.h | 9 +--- include/inamespace.h | 9 +--- include/iorthocontextmenu.h | 9 +--- include/iorthoview.h | 9 +--- include/iparticles.h | 9 +--- include/ipatch.h | 9 +--- include/ipreferencesystem.h | 9 +--- include/iradiant.h | 9 +--- include/iregion.h | 9 +--- include/iregistry.h | 19 ++++---- include/irender.h | 11 ++--- include/irendersystemfactory.h | 14 ++---- include/iscenegraph.h | 11 ++--- include/iscenegraphfactory.h | 9 +--- include/iscript.h | 9 +--- include/iselection.h | 12 ++--- include/iselectiongroup.h | 9 +--- include/iselectionset.h | 9 +--- include/ishaderclipboard.h | 9 +--- include/ishaders.h | 9 +--- include/isound.h | 9 +--- include/iuimanager.h | 9 +--- include/iundo.h | 9 +--- include/iwxgl.h | 9 +--- include/modelskin.h | 10 ++--- radiantcore/modulesystem/ModuleRegistry.cpp | 5 +++ radiantcore/modulesystem/ModuleRegistry.h | 2 + 62 files changed, 199 insertions(+), 436 deletions(-) diff --git a/include/iaasfile.h b/include/iaasfile.h index 46c61cba3b..b6cda7f026 100644 --- a/include/iaasfile.h +++ b/include/iaasfile.h @@ -153,10 +153,6 @@ const char* const MODULE_AASFILEMANAGER("ZAasFileManager"); // Application-wide Accessor to the global AAS file manager inline map::IAasFileManager& GlobalAasFileManager() { - // Cache the reference locally - static map::IAasFileManager& _manager( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_AASFILEMANAGER)) - ); - return _manager; + static module::InstanceReference _reference(MODULE_AASFILEMANAGER); + return _reference; } diff --git a/include/ibrush.h b/include/ibrush.h index 84b0890a0d..10870b8f8a 100644 --- a/include/ibrush.h +++ b/include/ibrush.h @@ -322,11 +322,6 @@ const char* const MODULE_BRUSHCREATOR("Doom3BrushCreator"); inline brush::BrushCreator& GlobalBrushCreator() { - // Cache the reference locally - static brush::BrushCreator& _brushCreator( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_BRUSHCREATOR) - ) - ); - return _brushCreator; + static module::InstanceReference _reference(MODULE_BRUSHCREATOR); + return _reference; } diff --git a/include/icameraview.h b/include/icameraview.h index b178a5a53a..fa7748e951 100644 --- a/include/icameraview.h +++ b/include/icameraview.h @@ -101,11 +101,6 @@ const char* const MODULE_CAMERA_MANAGER("CameraManager"); // Module accessor inline camera::ICameraViewManager& GlobalCameraManager() { - // Cache the reference locally - static camera::ICameraViewManager& _instance( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_CAMERA_MANAGER) - ) - ); - return _instance; + static module::InstanceReference _reference(MODULE_CAMERA_MANAGER); + return _reference; } diff --git a/include/iclipboard.h b/include/iclipboard.h index fc989e25f5..8984276f49 100644 --- a/include/iclipboard.h +++ b/include/iclipboard.h @@ -33,11 +33,6 @@ const char* const MODULE_CLIPBOARD("Clipboard"); inline radiant::IClipboard& GlobalClipboard() { - // Cache the reference locally - static radiant::IClipboard& _instance( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_CLIPBOARD) - ) - ); - return _instance; + static module::InstanceReference _reference(MODULE_CLIPBOARD); + return _reference; } diff --git a/include/iclipper.h b/include/iclipper.h index 2c2897b021..c8631a1160 100644 --- a/include/iclipper.h +++ b/include/iclipper.h @@ -73,11 +73,6 @@ class IClipper : // The accessor for the clipper module inline IClipper& GlobalClipper() { - // Cache the reference locally - static IClipper& _clipper( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_CLIPPER) - ) - ); - return _clipper; + static module::InstanceReference _reference(MODULE_CLIPPER); + return _reference; } diff --git a/include/icolourscheme.h b/include/icolourscheme.h index db16cde833..40d3b3acc2 100644 --- a/include/icolourscheme.h +++ b/include/icolourscheme.h @@ -82,11 +82,6 @@ const char* const MODULE_COLOURSCHEME_MANAGER("ColourSchemeManager"); inline colours::IColourSchemeManager& GlobalColourSchemeManager() { - // Cache the reference locally - static colours::IColourSchemeManager& _instance( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_COLOURSCHEME_MANAGER) - ) - ); - return _instance; + static module::InstanceReference _reference(MODULE_COLOURSCHEME_MANAGER); + return _reference; } diff --git a/include/icommandsystem.h b/include/icommandsystem.h index 6a0a002b19..5205af4557 100644 --- a/include/icommandsystem.h +++ b/include/icommandsystem.h @@ -319,11 +319,6 @@ const char* const MODULE_COMMANDSYSTEM("CommandSystem"); // This is the accessor for the commandsystem inline cmd::ICommandSystem& GlobalCommandSystem() { - // Cache the reference locally - static cmd::ICommandSystem& _cmdSystem( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_COMMANDSYSTEM) - ) - ); - return _cmdSystem; + static module::InstanceReference _reference(MODULE_COMMANDSYSTEM); + return _reference; } diff --git a/include/icounter.h b/include/icounter.h index 5878075784..e3d1350cc4 100644 --- a/include/icounter.h +++ b/include/icounter.h @@ -44,11 +44,6 @@ class ICounterManager : inline ICounterManager& GlobalCounters() { - // Cache the reference locally - static ICounterManager& _counters( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_COUNTER) - ) - ); - return _counters; + static module::InstanceReference _reference(MODULE_COUNTER); + return _reference; } diff --git a/include/ieclass.h b/include/ieclass.h index 2d4f3e4486..67f5007bee 100644 --- a/include/ieclass.h +++ b/include/ieclass.h @@ -446,12 +446,8 @@ class IEntityClassManager : * * \ingroup eclass */ -inline IEntityClassManager& GlobalEntityClassManager() { - // Cache the reference locally - static IEntityClassManager& _eclassMgr( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_ECLASSMANAGER) - ) - ); - return _eclassMgr; +inline IEntityClassManager& GlobalEntityClassManager() +{ + static module::InstanceReference _reference(MODULE_ECLASSMANAGER); + return _reference; } diff --git a/include/ieditstopwatch.h b/include/ieditstopwatch.h index aa1d2ad108..e600a8eea6 100644 --- a/include/ieditstopwatch.h +++ b/include/ieditstopwatch.h @@ -48,11 +48,6 @@ const char* const MODULE_EDITING_STOPWATCH("EditingStopwatch"); inline map::IMapEditStopwatch& GlobalMapEditStopwatch() { - // Cache the reference locally - static map::IMapEditStopwatch& _instance( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_EDITING_STOPWATCH) - ) - ); - return _instance; + static module::InstanceReference _reference(MODULE_EDITING_STOPWATCH); + return _reference; } diff --git a/include/ientity.h b/include/ientity.h index 6dc0d7c9cf..d81d79e873 100644 --- a/include/ientity.h +++ b/include/ientity.h @@ -403,11 +403,6 @@ class IEntityModule : inline IEntityModule& GlobalEntityModule() { - // Cache the reference locally - static IEntityModule& _entityCreator( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_ENTITY) - ) - ); - return _entityCreator; + static module::InstanceReference _reference(MODULE_ENTITY); + return _reference; } diff --git a/include/ientityinspector.h b/include/ientityinspector.h index e8d44813e0..c29104e825 100644 --- a/include/ientityinspector.h +++ b/include/ientityinspector.h @@ -110,15 +110,10 @@ class IEntityInspector : } // namespace ui -const std::string MODULE_ENTITYINSPECTOR("EntityInspector"); +const char* const MODULE_ENTITYINSPECTOR("EntityInspector"); inline ui::IEntityInspector& GlobalEntityInspector() { - // Cache the reference locally - static ui::IEntityInspector& _inspector( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_ENTITYINSPECTOR) - ) - ); - return _inspector; + static module::InstanceReference _reference(MODULE_ENTITYINSPECTOR); + return _reference; } diff --git a/include/ieventmanager.h b/include/ieventmanager.h index a9e3a9d321..917d488387 100644 --- a/include/ieventmanager.h +++ b/include/ieventmanager.h @@ -179,12 +179,8 @@ class IEventManager : typedef std::shared_ptr IEventManagerPtr; // This is the accessor for the event manager -inline IEventManager& GlobalEventManager() { - // Cache the reference locally - static IEventManager& _eventManager( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_EVENTMANAGER) - ) - ); - return _eventManager; +inline IEventManager& GlobalEventManager() +{ + static module::InstanceReference _reference(MODULE_EVENTMANAGER); + return _reference; } diff --git a/include/ifilesystem.h b/include/ifilesystem.h index b248836fd8..4eb57fe707 100644 --- a/include/ifilesystem.h +++ b/include/ifilesystem.h @@ -200,11 +200,6 @@ const char* const MODULE_VIRTUALFILESYSTEM("VirtualFileSystem"); inline vfs::VirtualFileSystem& GlobalFileSystem() { - // Cache the reference locally - static vfs::VirtualFileSystem& _vfs( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_VIRTUALFILESYSTEM) - ) - ); - return _vfs; + static module::InstanceReference _reference(MODULE_VIRTUALFILESYSTEM); + return _reference; } diff --git a/include/ifiletypes.h b/include/ifiletypes.h index 67524a42df..af7b327572 100644 --- a/include/ifiletypes.h +++ b/include/ifiletypes.h @@ -84,11 +84,6 @@ const char* const TYPE_MODEL_EXPORT = "modelexport"; inline IFileTypeRegistry& GlobalFiletypes() { - // Cache the reference locally - static IFileTypeRegistry& _fileTypes( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_FILETYPES) - ) - ); - return _fileTypes; + static module::InstanceReference _reference(MODULE_FILETYPES); + return _reference; } diff --git a/include/ifilter.h b/include/ifilter.h index 183f287b1b..1c0f73ce3a 100644 --- a/include/ifilter.h +++ b/include/ifilter.h @@ -220,11 +220,6 @@ class IFilterSystem : inline filters::IFilterSystem& GlobalFilterSystem() { - // Cache the reference locally - static filters::IFilterSystem& _filterSystem( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_FILTERSYSTEM) - ) - ); - return _filterSystem; + static module::InstanceReference _reference(MODULE_FILTERSYSTEM); + return _reference; } diff --git a/include/ifonts.h b/include/ifonts.h index 9fb48bfeef..49389b6198 100644 --- a/include/ifonts.h +++ b/include/ifonts.h @@ -136,15 +136,10 @@ class IFontManager : } -const std::string MODULE_FONTMANAGER("FontManager"); +const char* const MODULE_FONTMANAGER("FontManager"); inline fonts::IFontManager& GlobalFontManager() { - // Cache the reference locally - static fonts::IFontManager& _fontManager( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_FONTMANAGER) - ) - ); - return _fontManager; + static module::InstanceReference _reference(MODULE_FONTMANAGER); + return _reference; } diff --git a/include/igame.h b/include/igame.h index 6d86ece5f8..df2449c7ec 100644 --- a/include/igame.h +++ b/include/igame.h @@ -133,11 +133,6 @@ typedef std::shared_ptr IGameManagerPtr; // This is the accessor for the game manager inline game::IGameManager& GlobalGameManager() { - // Cache the reference locally - static game::IGameManager& _gameManager( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_GAMEMANAGER) - ) - ); - return _gameManager; + static module::InstanceReference _reference(MODULE_GAMEMANAGER); + return _reference; } diff --git a/include/igl.h b/include/igl.h index 883e9aeb61..229f89747f 100644 --- a/include/igl.h +++ b/include/igl.h @@ -49,13 +49,8 @@ const char* const MODULE_SHARED_GL_CONTEXT("SharedGLContextHolder"); inline gl::ISharedGLContextHolder& GlobalOpenGLContext() { - // Cache the reference locally - static gl::ISharedGLContextHolder& _instance( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_SHARED_GL_CONTEXT) - ) - ); - return _instance; + static module::InstanceReference _reference(MODULE_SHARED_GL_CONTEXT); + return _reference; } const char* const MODULE_OPENGL("OpenGL"); @@ -80,11 +75,6 @@ class OpenGLBinding : inline OpenGLBinding& GlobalOpenGL() { - // Cache the reference locally - static OpenGLBinding& _openGL( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_OPENGL) - ) - ); - return _openGL; + static module::InstanceReference _reference(MODULE_OPENGL); + return _reference; } diff --git a/include/igrid.h b/include/igrid.h index 328cbb334d..47d3cb3ec6 100644 --- a/include/igrid.h +++ b/include/igrid.h @@ -89,11 +89,6 @@ class IGridManager : // This is the accessor for the grid module inline IGridManager& GlobalGrid() { - // Cache the reference locally - static IGridManager& _grid( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_GRID) - ) - ); - return _grid; + static module::InstanceReference _reference(MODULE_GRID); + return _reference; } diff --git a/include/igui.h b/include/igui.h index 5f3280b828..ba30eb1722 100644 --- a/include/igui.h +++ b/include/igui.h @@ -389,10 +389,6 @@ const char* const MODULE_GUIMANAGER("GuiManager"); // Application-wide Accessor to the global GUI manager inline gui::IGuiManager& GlobalGuiManager() { - // Cache the reference locally - static gui::IGuiManager& _manager( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_GUIMANAGER)) - ); - return _manager; + static module::InstanceReference _reference(MODULE_GUIMANAGER); + return _reference; } diff --git a/include/iimage.h b/include/iimage.h index 4bffb9fc78..ce6627fe15 100644 --- a/include/iimage.h +++ b/include/iimage.h @@ -89,11 +89,6 @@ const char* const MODULE_IMAGELOADER("ImageLoader"); inline const IImageLoader& GlobalImageLoader() { - static IImageLoader& _imageLoader( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_IMAGELOADER) - ) - ); - - return _imageLoader; + static module::InstanceReference _reference(MODULE_IMAGELOADER); + return _reference; } diff --git a/include/ilayer.h b/include/ilayer.h index e5667e1334..b08b4d4012 100644 --- a/include/ilayer.h +++ b/include/ilayer.h @@ -219,11 +219,6 @@ const char* const MODULE_LAYERS("LayerModule"); inline scene::ILayerModule& GlobalLayerModule() { - // Cache the reference locally - static scene::ILayerModule& _layerModule( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_LAYERS) - ) - ); - return _layerModule; + static module::InstanceReference _reference(MODULE_LAYERS); + return _reference; } diff --git a/include/imainframe.h b/include/imainframe.h index 5e3fc6dc97..271ed2ffa6 100644 --- a/include/imainframe.h +++ b/include/imainframe.h @@ -139,11 +139,6 @@ class IMainFrame : // This is the accessor for the mainframe module inline IMainFrame& GlobalMainFrame() { - // Cache the reference locally - static IMainFrame& _mainFrame( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_MAINFRAME) - ) - ); - return _mainFrame; + static module::InstanceReference _reference(MODULE_MAINFRAME); + return _reference; } diff --git a/include/imainframelayout.h b/include/imainframelayout.h index 026c8b505b..1199abbbff 100644 --- a/include/imainframelayout.h +++ b/include/imainframelayout.h @@ -1,5 +1,4 @@ -#ifndef IMAINFRAME_LAYOUT_H_ -#define IMAINFRAME_LAYOUT_H_ +#pragma once #include "imodule.h" #include @@ -53,7 +52,7 @@ typedef std::shared_ptr IMainFrameLayoutPtr; */ typedef std::function CreateMainFrameLayoutFunc; -const std::string MODULE_MAINFRAME_LAYOUT_MANAGER("MainFrameLayoutManager"); +const char* const MODULE_MAINFRAME_LAYOUT_MANAGER("MainFrameLayoutManager"); class IMainFrameLayoutManager : public RegisterableModule @@ -76,14 +75,8 @@ class IMainFrameLayoutManager : }; // This is the accessor for the mainframe module -inline IMainFrameLayoutManager& GlobalMainFrameLayoutManager() { - // Cache the reference locally - static IMainFrameLayoutManager& _mainFrameLayoutManager( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_MAINFRAME_LAYOUT_MANAGER) - ) - ); - return _mainFrameLayoutManager; +inline IMainFrameLayoutManager& GlobalMainFrameLayoutManager() +{ + static module::InstanceReference _reference(MODULE_MAINFRAME_LAYOUT_MANAGER); + return _reference; } - -#endif /* IMAINFRAME_LAYOUT_H_ */ diff --git a/include/imap.h b/include/imap.h index 9b56a5f26d..77518af9a1 100644 --- a/include/imap.h +++ b/include/imap.h @@ -160,12 +160,7 @@ const char* const MODULE_MAP("Map"); // Application-wide Accessor to the currently active map inline IMap& GlobalMapModule() { - // Cache the reference locally - static IMap& _mapModule( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_MAP) - ) - ); - return _mapModule; + static module::InstanceReference _reference(MODULE_MAP); + return _reference; } diff --git a/include/imapformat.h b/include/imapformat.h index 7981734223..e2bf35cd53 100644 --- a/include/imapformat.h +++ b/include/imapformat.h @@ -288,11 +288,6 @@ const char* const MODULE_MAPFORMATMANAGER("MapFormatManager"); // Application-wide Accessor to the global map format manager inline map::IMapFormatManager& GlobalMapFormatManager() { - // Cache the reference locally - static map::IMapFormatManager& _mapFormatManager( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_MAPFORMATMANAGER) - ) - ); - return _mapFormatManager; + static module::InstanceReference _reference(MODULE_MAPFORMATMANAGER); + return _reference; } diff --git a/include/imapinfofile.h b/include/imapinfofile.h index fb3b3cfe2f..28e51d8d58 100644 --- a/include/imapinfofile.h +++ b/include/imapinfofile.h @@ -159,10 +159,6 @@ const char* const MODULE_MAPINFOFILEMANAGER("MapInfoFileManager"); // Application-wide Accessor to the global map info file manager inline map::IMapInfoFileManager& GlobalMapInfoFileManager() { - // Cache the reference locally - static map::IMapInfoFileManager& _manager( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_MAPINFOFILEMANAGER)) - ); - return _manager; + static module::InstanceReference _reference(MODULE_MAPINFOFILEMANAGER); + return _reference; } diff --git a/include/imapresource.h b/include/imapresource.h index 3c036745d8..e8d3790159 100644 --- a/include/imapresource.h +++ b/include/imapresource.h @@ -81,11 +81,6 @@ class IMapResourceManager : inline IMapResourceManager& GlobalMapResourceManager() { - // Cache the reference locally - static IMapResourceManager& _mapResourceManager( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_MAPRESOURCEMANAGER) - ) - ); - return _mapResourceManager; + static module::InstanceReference _reference(MODULE_MAPRESOURCEMANAGER); + return _reference; } diff --git a/include/imd5anim.h b/include/imd5anim.h index d3e9354334..f800609a85 100644 --- a/include/imd5anim.h +++ b/include/imd5anim.h @@ -106,11 +106,6 @@ const char* const MODULE_ANIMATIONCACHE("MD5AnimationCache"); inline md5::IAnimationCache& GlobalAnimationCache() { - // Cache the reference locally - static md5::IAnimationCache& _animationCache( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(md5::MODULE_ANIMATIONCACHE) - ) - ); - return _animationCache; + static module::InstanceReference _reference(md5::MODULE_ANIMATIONCACHE); + return _reference; } diff --git a/include/imediabrowser.h b/include/imediabrowser.h index bbfcc98415..60a647b429 100644 --- a/include/imediabrowser.h +++ b/include/imediabrowser.h @@ -47,11 +47,6 @@ const char* const MODULE_MEDIABROWSER = "MediaBrowser"; inline ui::IMediaBrowser& GlobalMediaBrowser() { - // Cache the reference locally - static ui::IMediaBrowser& _mediaBrowser( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_MEDIABROWSER) - ) - ); - return _mediaBrowser; + static module::InstanceReference _reference(MODULE_MEDIABROWSER); + return _reference; } diff --git a/include/imodel.h b/include/imodel.h index f580957004..22852e4fea 100644 --- a/include/imodel.h +++ b/include/imodel.h @@ -246,10 +246,6 @@ const char* const MODULE_MODELFORMATMANAGER("ModelFormatManager"); inline model::IModelFormatManager& GlobalModelFormatManager() { - std::shared_ptr _modelFormatManager( - std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_MODELFORMATMANAGER) - ) - ); - return *_modelFormatManager; + static module::InstanceReference _reference(MODULE_MODELFORMATMANAGER); + return _reference; } diff --git a/include/imodelcache.h b/include/imodelcache.h index ff75f0ce86..1b1ab78cf7 100644 --- a/include/imodelcache.h +++ b/include/imodelcache.h @@ -55,10 +55,6 @@ const char* const MODULE_MODELCACHE("ModelCache"); inline model::IModelCache& GlobalModelCache() { - static model::IModelCache& _modelCache( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_MODELCACHE) - ) - ); - return _modelCache; + static module::InstanceReference _reference(MODULE_MODELCACHE); + return _reference; } diff --git a/include/imodule.h b/include/imodule.h index 1d797441bd..4d853863bd 100644 --- a/include/imodule.h +++ b/include/imodule.h @@ -326,6 +326,12 @@ class IModuleRegistry // on top of that they can actively query this number from the registry // to check whether they are being loaded into an incompatible binary. virtual std::size_t getCompatibilityLevel() const = 0; + + typedef std::uintptr_t InstanceId; + + // Returns the instance ID of this registry. This is a numeric type used by + // InstanceReference classes to check if their references are still valid. + virtual InstanceId getInstanceId() const = 0; }; namespace module @@ -337,6 +343,50 @@ namespace module * \ingroup module */ + // Reference container to hold the cached module references. + // It automatically invalidates its reference as soon as the IModuleRegistry + // changes its instance ID. + template + class InstanceReference + { + private: + const char* const _moduleName; + ModuleType* _instancePtr; + IModuleRegistry::InstanceId _registryInstanceId; + public: + InstanceReference(const char* const moduleName) : + _moduleName(moduleName), + _instancePtr(nullptr), + _registryInstanceId(0) + { + acquireReference(); + } + + // Cast-operator used to access the module reference + inline operator ModuleType&() + { +#ifdef MODULE_REFERENCES_SUPPORT_INVALIDATION + // Check if we have an instance or if it is outdated + if (_instancePtr == nullptr || + _registryInstanceId != GlobalModuleRegistry().getInstanceId()) + { + acquireReference(); + } +#endif + return *_instancePtr; + } + + private: + void acquireReference() + { + _instancePtr = std::dynamic_pointer_cast( + GlobalModuleRegistry().getModule(_moduleName)).get(); + // Save the instance ID of the registry - if this ever changes + // the above reference is treated as invalid + _registryInstanceId = GlobalModuleRegistry().getInstanceId(); + } + }; + /** greebo: This is a container holding a reference to the registry. * The getRegistry() symbol above is not exported to the * modules in Win32 compiles. That's why this structure diff --git a/include/imousetoolmanager.h b/include/imousetoolmanager.h index ee557c3fdf..3d2633a2b2 100644 --- a/include/imousetoolmanager.h +++ b/include/imousetoolmanager.h @@ -122,11 +122,6 @@ const char* const MODULE_MOUSETOOLMANAGER = "MouseToolManager"; inline ui::IMouseToolManager& GlobalMouseToolManager() { - // Cache the reference locally - static ui::IMouseToolManager& _mtManager( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_MOUSETOOLMANAGER) - ) - ); - return _mtManager; + static module::InstanceReference _reference(MODULE_MOUSETOOLMANAGER); + return _reference; } diff --git a/include/imru.h b/include/imru.h index 69a5f1eef5..7f3124f965 100644 --- a/include/imru.h +++ b/include/imru.h @@ -38,11 +38,6 @@ const char* const MODULE_MRU_MANAGER = "MRUManager"; inline map::IMRUManager& GlobalMRU() { - // Cache the reference locally - static map::IMRUManager& _manager( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_MRU_MANAGER) - ) - ); - return _manager; + static module::InstanceReference _reference(MODULE_MRU_MANAGER); + return _reference; } diff --git a/include/inamespace.h b/include/inamespace.h index efe6571e99..c4ac39bfd3 100644 --- a/include/inamespace.h +++ b/include/inamespace.h @@ -168,11 +168,6 @@ const char* const MODULE_NAMESPACE_FACTORY("NamespaceFactory"); // Factory accessor inline INamespaceFactory& GlobalNamespaceFactory() { - // Cache the reference locally - static INamespaceFactory& _namespaceFactory( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_NAMESPACE_FACTORY) - ) - ); - return _namespaceFactory; + static module::InstanceReference _reference(MODULE_NAMESPACE_FACTORY); + return _reference; } diff --git a/include/iorthocontextmenu.h b/include/iorthocontextmenu.h index 459d94c6a7..d4d5116cc6 100644 --- a/include/iorthocontextmenu.h +++ b/include/iorthocontextmenu.h @@ -43,11 +43,6 @@ const char* const MODULE_ORTHOCONTEXTMENU = "OrthoContextMenu"; inline ui::IOrthoContextMenu& GlobalOrthoContextMenu() { - // Cache the reference locally - static ui::IOrthoContextMenu& _menu( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_ORTHOCONTEXTMENU) - ) - ); - return _menu; + static module::InstanceReference _reference(MODULE_ORTHOCONTEXTMENU); + return _reference; } diff --git a/include/iorthoview.h b/include/iorthoview.h index c67470be84..efd6854bd9 100644 --- a/include/iorthoview.h +++ b/include/iorthoview.h @@ -110,11 +110,6 @@ const char* const MODULE_ORTHOVIEWMANAGER = "OrthoviewManager"; // This is the accessor for the xy window manager module inline ui::IXWndManager& GlobalXYWndManager() { - // Cache the reference locally - static ui::IXWndManager& _xyWndManager( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_ORTHOVIEWMANAGER) - ) - ); - return _xyWndManager; + static module::InstanceReference _reference(MODULE_ORTHOVIEWMANAGER); + return _reference; } diff --git a/include/iparticles.h b/include/iparticles.h index a73a7981a4..9138dfa0a4 100644 --- a/include/iparticles.h +++ b/include/iparticles.h @@ -239,11 +239,6 @@ const char* const MODULE_PARTICLESMANAGER = "ParticlesManager"; // Accessor inline particles::IParticlesManager& GlobalParticlesManager() { - // Cache the reference locally - static particles::IParticlesManager& _particlesManager( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_PARTICLESMANAGER) - ) - ); - return _particlesManager; + static module::InstanceReference _reference(MODULE_PARTICLESMANAGER); + return _reference; } diff --git a/include/ipatch.h b/include/ipatch.h index b29e36221b..6223903e27 100644 --- a/include/ipatch.h +++ b/include/ipatch.h @@ -319,11 +319,6 @@ const char* const MODULE_PATCH = "PatchModule"; inline patch::IPatchModule& GlobalPatchModule() { - static patch::IPatchModule& _patchCreator( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_PATCH) - ) - ); - - return _patchCreator; + static module::InstanceReference _reference(MODULE_PATCH); + return _reference; } diff --git a/include/ipreferencesystem.h b/include/ipreferencesystem.h index 25e851a37a..a6a8d27623 100644 --- a/include/ipreferencesystem.h +++ b/include/ipreferencesystem.h @@ -195,11 +195,6 @@ class IPreferenceSystem : inline IPreferenceSystem& GlobalPreferenceSystem() { - // Cache the reference locally - static IPreferenceSystem& _prefSystem( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_PREFERENCESYSTEM) - ) - ); - return _prefSystem; + static module::InstanceReference _reference(MODULE_PREFERENCESYSTEM); + return _reference; } diff --git a/include/iradiant.h b/include/iradiant.h index 6f09975d82..2aba8a5f29 100644 --- a/include/iradiant.h +++ b/include/iradiant.h @@ -76,11 +76,6 @@ class IRadiant : inline radiant::IRadiant& GlobalRadiantCore() { - // Cache the reference locally - static radiant::IRadiant& _radiant( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_RADIANT_CORE) - ) - ); - return _radiant; + static module::InstanceReference _reference(MODULE_RADIANT_CORE); + return _reference; } diff --git a/include/iregion.h b/include/iregion.h index 3a03b466c4..50eb2fdd43 100644 --- a/include/iregion.h +++ b/include/iregion.h @@ -26,11 +26,6 @@ const char* const MODULE_REGION_MANAGER = "RegionManager"; inline map::IRegionManager& GlobalRegionManager() { - // Cache the reference locally - static map::IRegionManager& _module( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_REGION_MANAGER) - ) - ); - return _module; + static module::InstanceReference _reference(MODULE_REGION_MANAGER); + return _reference; } diff --git a/include/iregistry.h b/include/iregistry.h index d06690b68f..64db9388a9 100644 --- a/include/iregistry.h +++ b/include/iregistry.h @@ -7,8 +7,9 @@ #include #include -namespace { - const std::string RKEY_SKIP_REGISTRY_SAVE = "user/skipRegistrySaveOnShutdown"; +namespace +{ + const char* const RKEY_SKIP_REGISTRY_SAVE = "user/skipRegistrySaveOnShutdown"; } /** @@ -16,7 +17,7 @@ namespace { */ // String identifier for the registry module -const std::string MODULE_XMLREGISTRY("XMLRegistry"); +const char* const MODULE_XMLREGISTRY("XMLRegistry"); /** * Abstract base class for the registry module. @@ -107,12 +108,8 @@ class Registry : typedef std::shared_ptr RegistryPtr; // This is the accessor for the registry -inline Registry& GlobalRegistry() { - // Cache the reference locally - static Registry& _registry( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_XMLREGISTRY) - ) - ); - return _registry; +inline Registry& GlobalRegistry() +{ + static module::InstanceReference _reference(MODULE_XMLREGISTRY); + return _reference; } diff --git a/include/irender.h b/include/irender.h index 62667525bf..48b7b99a59 100644 --- a/include/irender.h +++ b/include/irender.h @@ -499,7 +499,7 @@ class Shader */ typedef std::shared_ptr ShaderPtr; -const std::string MODULE_RENDERSYSTEM("ShaderCache"); +const char* const MODULE_RENDERSYSTEM("ShaderCache"); /** * \brief @@ -664,11 +664,6 @@ typedef std::weak_ptr RenderSystemWeakPtr; */ inline RenderSystem& GlobalRenderSystem() { - // Cache the reference locally - static RenderSystem& _instance( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_RENDERSYSTEM) - ) - ); - return _instance; + static module::InstanceReference _reference(MODULE_RENDERSYSTEM); + return _reference; } diff --git a/include/irendersystemfactory.h b/include/irendersystemfactory.h index a852d905e3..7c94775b19 100644 --- a/include/irendersystemfactory.h +++ b/include/irendersystemfactory.h @@ -1,5 +1,4 @@ -#ifndef _IRENDERSYSTEM_FACTORY_H_ -#define _IRENDERSYSTEM_FACTORY_H_ +#pragma once #include "imodule.h" #include "irender.h" @@ -34,13 +33,6 @@ const char* const MODULE_RENDERSYSTEMFACTORY = "RenderSystemFactory"; // Global accessor to the rendersystem factory module inline render::IRenderSystemFactory& GlobalRenderSystemFactory() { - // Cache the reference locally - static render::IRenderSystemFactory& _instance( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_RENDERSYSTEMFACTORY) - ) - ); - return _instance; + static module::InstanceReference _reference(MODULE_RENDERSYSTEMFACTORY); + return _reference; } - -#endif /* _IRENDERSYSTEM_FACTORY_H_ */ diff --git a/include/iscenegraph.h b/include/iscenegraph.h index e776bfd54f..53aebf2953 100644 --- a/include/iscenegraph.h +++ b/include/iscenegraph.h @@ -16,7 +16,7 @@ */ // String identifier for the registry module -const std::string MODULE_SCENEGRAPH("SceneGraph"); +const char* const MODULE_SCENEGRAPH("SceneGraph"); class VolumeTest; @@ -134,13 +134,8 @@ typedef std::shared_ptr CloneablePtr; // Accessor to the singleton scenegraph, used for the main map inline scene::Graph& GlobalSceneGraph() { - // Cache the reference locally - static scene::Graph& _sceneGraph( - *std::dynamic_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_SCENEGRAPH) - ) - ); - return _sceneGraph; + static module::InstanceReference _reference(MODULE_SCENEGRAPH); + return _reference; } inline void SceneChangeNotify() diff --git a/include/iscenegraphfactory.h b/include/iscenegraphfactory.h index b510fd093d..ec5936e469 100644 --- a/include/iscenegraphfactory.h +++ b/include/iscenegraphfactory.h @@ -31,11 +31,6 @@ const char* const MODULE_SCENEGRAPHFACTORY = "SceneGraphFactory"; // Global accessor to the rendersystem factory module inline scene::ISceneGraphFactory& GlobalSceneGraphFactory() { - // Cache the reference locally - static scene::ISceneGraphFactory& _instance( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_SCENEGRAPHFACTORY) - ) - ); - return _instance; + static module::InstanceReference _reference(MODULE_SCENEGRAPHFACTORY); + return _reference; } diff --git a/include/iscript.h b/include/iscript.h index f283e6c203..a7e6888793 100644 --- a/include/iscript.h +++ b/include/iscript.h @@ -92,11 +92,6 @@ const char* const MODULE_SCRIPTING_SYSTEM("ScriptingSystem"); // This is the accessor for the scripting system inline IScriptingSystem& GlobalScriptingSystem() { - // Cache the reference locally - static IScriptingSystem& _scriptingSystem( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_SCRIPTING_SYSTEM) - ) - ); - return _scriptingSystem; + static module::InstanceReference _reference(MODULE_SCRIPTING_SYSTEM); + return _reference; } diff --git a/include/iselection.h b/include/iselection.h index 54ecaa4df7..e5ca0fd9ad 100644 --- a/include/iselection.h +++ b/include/iselection.h @@ -318,12 +318,8 @@ class SelectionSystem : const char* const MODULE_SELECTIONSYSTEM("SelectionSystem"); -inline SelectionSystem& GlobalSelectionSystem() { - // Cache the reference locally - static SelectionSystem& _selectionSystem( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_SELECTIONSYSTEM) - ) - ); - return _selectionSystem; +inline SelectionSystem& GlobalSelectionSystem() +{ + static module::InstanceReference _reference(MODULE_SELECTIONSYSTEM); + return _reference; } diff --git a/include/iselectiongroup.h b/include/iselectiongroup.h index d44e9b3901..7c627dc41a 100644 --- a/include/iselectiongroup.h +++ b/include/iselectiongroup.h @@ -127,11 +127,6 @@ const char* const MODULE_SELECTIONGROUPMODULE = "SelectionGroupModule"; inline selection::ISelectionGroupModule& GlobalSelectionGroupModule() { - // Cache the reference locally - static selection::ISelectionGroupModule& _module( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_SELECTIONGROUPMODULE) - ) - ); - return _module; + static module::InstanceReference _reference(MODULE_SELECTIONGROUPMODULE); + return _reference; } diff --git a/include/iselectionset.h b/include/iselectionset.h index fea0e57244..4f5e0e924d 100644 --- a/include/iselectionset.h +++ b/include/iselectionset.h @@ -112,11 +112,6 @@ const char* const MODULE_SELECTIONSETS = "SelectionSetModule"; inline selection::ISelectionSetModule& GlobalSelectionSetModule() { - // Cache the reference locally - static selection::ISelectionSetModule& _module( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_SELECTIONSETS) - ) - ); - return _module; + static module::InstanceReference _reference(MODULE_SELECTIONSETS); + return _reference; } diff --git a/include/ishaderclipboard.h b/include/ishaderclipboard.h index 1a59e9fd3e..5a43b968b1 100644 --- a/include/ishaderclipboard.h +++ b/include/ishaderclipboard.h @@ -83,11 +83,6 @@ const char* const MODULE_SHADERCLIPBOARD("ShaderClipboard"); inline selection::IShaderClipboard& GlobalShaderClipboard() { - // Cache the reference locally - static selection::IShaderClipboard& _clipboard( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_SHADERCLIPBOARD) - ) - ); - return _clipboard; + static module::InstanceReference _reference(MODULE_SHADERCLIPBOARD); + return _reference; } diff --git a/include/ishaders.h b/include/ishaders.h index 853f5a4126..5e7a0c5041 100644 --- a/include/ishaders.h +++ b/include/ishaders.h @@ -448,11 +448,6 @@ class MaterialManager inline MaterialManager& GlobalMaterialManager() { - // Cache the reference locally - static MaterialManager& _shaderSystem( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_SHADERSYSTEM) - ) - ); - return _shaderSystem; + static module::InstanceReference _reference(MODULE_SHADERSYSTEM); + return _reference; } diff --git a/include/isound.h b/include/isound.h index b9be572315..095c9eb616 100644 --- a/include/isound.h +++ b/include/isound.h @@ -118,11 +118,6 @@ class ISoundManager : // Accessor method inline ISoundManager& GlobalSoundManager() { - // Cache the reference locally - static ISoundManager& _soundManager( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_SOUNDMANAGER) - ) - ); - return _soundManager; + static module::InstanceReference _reference(MODULE_SOUNDMANAGER); + return _reference; } diff --git a/include/iuimanager.h b/include/iuimanager.h index b1bbbec319..b3dbd1cb8e 100644 --- a/include/iuimanager.h +++ b/include/iuimanager.h @@ -229,13 +229,8 @@ class IUIManager : // This is the accessor for the UI manager inline IUIManager& GlobalUIManager() { - // Cache the reference locally - static IUIManager& _uiManager( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_UIMANAGER) - ) - ); - return _uiManager; + static module::InstanceReference _reference(MODULE_UIMANAGER); + return _reference; } inline IGroupDialog& GlobalGroupDialog() { diff --git a/include/iundo.h b/include/iundo.h index afc75d6fc7..7a9a77cdbc 100644 --- a/include/iundo.h +++ b/include/iundo.h @@ -107,13 +107,8 @@ class IUndoSystem : // The accessor function inline IUndoSystem& GlobalUndoSystem() { - // Cache the reference locally - static IUndoSystem& _undoSystem( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_UNDOSYSTEM) - ) - ); - return _undoSystem; + static module::InstanceReference _reference(MODULE_UNDOSYSTEM); + return _reference; } class UndoableCommand diff --git a/include/iwxgl.h b/include/iwxgl.h index fc41d85b6b..2ad615b626 100644 --- a/include/iwxgl.h +++ b/include/iwxgl.h @@ -28,11 +28,6 @@ const char* const MODULE_WXGLWIDGET_MANAGER("wxGLWidgetManager"); inline ui::IWxGLWidgetManager& GlobalWxGlWidgetManager() { - // Cache the reference locally - static ui::IWxGLWidgetManager& _instance( - *std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_WXGLWIDGET_MANAGER) - ) - ); - return _instance; + static module::InstanceReference _reference(MODULE_WXGLWIDGET_MANAGER); + return _reference; } diff --git a/include/modelskin.h b/include/modelskin.h index 985854f92a..954e0def19 100644 --- a/include/modelskin.h +++ b/include/modelskin.h @@ -43,7 +43,7 @@ typedef std::shared_ptr SkinnedModelPtr; // Model skinlist typedef typedef std::vector StringList; -const std::string MODULE_MODELSKINCACHE("ModelSkinCache"); +const char* const MODULE_MODELSKINCACHE("ModelSkinCache"); /** * Interface class for the skin manager. @@ -88,10 +88,6 @@ class ModelSkinCache : inline ModelSkinCache& GlobalModelSkinCache() { - std::shared_ptr _skinCache( - std::static_pointer_cast( - module::GlobalModuleRegistry().getModule(MODULE_MODELSKINCACHE) - ) - ); - return *_skinCache; + static module::InstanceReference _reference(MODULE_MODELSKINCACHE); + return _reference; } diff --git a/radiantcore/modulesystem/ModuleRegistry.cpp b/radiantcore/modulesystem/ModuleRegistry.cpp index 78929b5ad4..c6efb24bfd 100644 --- a/radiantcore/modulesystem/ModuleRegistry.cpp +++ b/radiantcore/modulesystem/ModuleRegistry.cpp @@ -297,6 +297,11 @@ std::size_t ModuleRegistry::getCompatibilityLevel() const return MODULE_COMPATIBILITY_LEVEL; } +ModuleRegistry::InstanceId ModuleRegistry::getInstanceId() const +{ + return reinterpret_cast(this); +} + std::string ModuleRegistry::getModuleList(const std::string& separator) { std::string returnValue; diff --git a/radiantcore/modulesystem/ModuleRegistry.h b/radiantcore/modulesystem/ModuleRegistry.h index c34350b55b..727b0dff3f 100644 --- a/radiantcore/modulesystem/ModuleRegistry.h +++ b/radiantcore/modulesystem/ModuleRegistry.h @@ -83,6 +83,8 @@ class ModuleRegistry : std::size_t getCompatibilityLevel() const override; + InstanceId getInstanceId() const override; + // Returns a list of modules std::string getModuleList(const std::string& separator = "\n");