From d087c9605ffffdc9053c5efdc83f53658afbe9a6 Mon Sep 17 00:00:00 2001 From: Colin Snover Date: Fri, 10 Nov 2017 23:06:42 -0600 Subject: BASE: Remove bad casts between incompatible Plugin types Previously, a C-style cast was used to convert a Common::Array, populated with pointers to StaticPlugin and DynamicPlugin instances, to a Common::Array *>, but PluginSubclass is a *sibling* class to StaticPlugin/DynamicPlugin, so this cast was invalid and the results undefined. The methods for retrieving subclasses of plugins can't be easily changed to just generate an array of temporary wrapper objects that expose an identical API which dereferences to the preferred PluginObject subclass because pointers to these objects are retained by other parts of ScummVM, so the wrappers would needed to be persisted or they would need to just re-expose the underlying Plugin object again. This indicated that a way to solve this problem is to have the callers receive Plugin objects and get the PluginObject from the Plugin by explicitly stating their desired type, in a similar manner to std::get(std::variant), so that the pattern used by this patch to solve the problem. Closes gh-1051. --- base/plugins.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'base/plugins.cpp') diff --git a/base/plugins.cpp b/base/plugins.cpp index 39aaf2f73e..18755003b4 100644 --- a/base/plugins.cpp +++ b/base/plugins.cpp @@ -457,7 +457,7 @@ DECLARE_SINGLETON(EngineManager); * For the uncached version, we first try to find the plugin using the gameId * and only if we can't find it there, we loop through the plugins. **/ -GameDescriptor EngineManager::findGame(const Common::String &gameName, const EnginePlugin **plugin) const { +GameDescriptor EngineManager::findGame(const Common::String &gameName, const Plugin **plugin) const { GameDescriptor result; // First look for the game using the plugins in memory. This is critical @@ -493,18 +493,18 @@ GameDescriptor EngineManager::findGame(const Common::String &gameName, const Eng /** * Find the game within the plugins loaded in memory **/ -GameDescriptor EngineManager::findGameInLoadedPlugins(const Common::String &gameName, const EnginePlugin **plugin) const { +GameDescriptor EngineManager::findGameInLoadedPlugins(const Common::String &gameName, const Plugin **plugin) const { // Find the GameDescriptor for this target - const EnginePlugin::List &plugins = getPlugins(); + const PluginList &plugins = getPlugins(); GameDescriptor result; if (plugin) *plugin = 0; - EnginePlugin::List::const_iterator iter; + PluginList::const_iterator iter; for (iter = plugins.begin(); iter != plugins.end(); ++iter) { - result = (**iter)->findGame(gameName.c_str()); + result = (*iter)->get().findGame(gameName.c_str()); if (!result.gameid().empty()) { if (plugin) *plugin = *iter; @@ -516,22 +516,22 @@ GameDescriptor EngineManager::findGameInLoadedPlugins(const Common::String &game GameList EngineManager::detectGames(const Common::FSList &fslist) const { GameList candidates; - EnginePlugin::List plugins; - EnginePlugin::List::const_iterator iter; + PluginList plugins; + PluginList::const_iterator iter; PluginManager::instance().loadFirstPlugin(); do { plugins = getPlugins(); // Iterate over all known games and for each check if it might be // the game in the presented directory. for (iter = plugins.begin(); iter != plugins.end(); ++iter) { - candidates.push_back((**iter)->detectGames(fslist)); + candidates.push_back((*iter)->get().detectGames(fslist)); } } while (PluginManager::instance().loadNextPlugin()); return candidates; } -const EnginePlugin::List &EngineManager::getPlugins() const { - return (const EnginePlugin::List &)PluginManager::instance().getPlugins(PLUGIN_TYPE_ENGINE); +const PluginList &EngineManager::getPlugins() const { + return PluginManager::instance().getPlugins(PLUGIN_TYPE_ENGINE); } @@ -543,6 +543,6 @@ namespace Common { DECLARE_SINGLETON(MusicManager); } -const MusicPlugin::List &MusicManager::getPlugins() const { - return (const MusicPlugin::List &)PluginManager::instance().getPlugins(PLUGIN_TYPE_MUSIC); +const PluginList &MusicManager::getPlugins() const { + return PluginManager::instance().getPlugins(PLUGIN_TYPE_MUSIC); } -- cgit v1.2.3