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/commandLine.cpp | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'base/commandLine.cpp') diff --git a/base/commandLine.cpp b/base/commandLine.cpp index 640daa1793..2e981fb75a 100644 --- a/base/commandLine.cpp +++ b/base/commandLine.cpp @@ -673,9 +673,9 @@ static void listGames() { printf("Game ID Full Title \n" "-------------------- ------------------------------------------------------\n"); - const EnginePlugin::List &plugins = EngineMan.getPlugins(); - for (EnginePlugin::List::const_iterator iter = plugins.begin(); iter != plugins.end(); ++iter) { - GameList list = (**iter)->getSupportedGames(); + const PluginList &plugins = EngineMan.getPlugins(); + for (PluginList::const_iterator iter = plugins.begin(); iter != plugins.end(); ++iter) { + GameList list = (*iter)->get().getSupportedGames(); for (GameList::iterator v = list.begin(); v != list.end(); ++v) { printf("%-20s %s\n", v->gameid().c_str(), v->description().c_str()); } @@ -741,7 +741,7 @@ static Common::Error listSaves(const char *target) { gameid.toLowercase(); // Normalize it to lower case // Find the plugin that will handle the specified gameid - const EnginePlugin *plugin = 0; + const Plugin *plugin = nullptr; GameDescriptor game = EngineMan.findGame(gameid, &plugin); if (!plugin) { @@ -749,13 +749,15 @@ static Common::Error listSaves(const char *target) { Common::String::format("target '%s', gameid '%s", target, gameid.c_str())); } - if (!(*plugin)->hasFeature(MetaEngine::kSupportsListSaves)) { + const MetaEngine &metaEngine = plugin->get(); + + if (!metaEngine.hasFeature(MetaEngine::kSupportsListSaves)) { // TODO: Include more info about the target (desc, engine name, ...) ??? return Common::Error(Common::kEnginePluginNotSupportSaves, Common::String::format("target '%s', gameid '%s", target, gameid.c_str())); } else { // Query the plugin for a list of saved games - SaveStateList saveList = (*plugin)->listSaves(target); + SaveStateList saveList = metaEngine.listSaves(target); if (saveList.size() > 0) { // TODO: Include more info about the target (desc, engine name, ...) ??? @@ -793,13 +795,14 @@ static void listThemes() { /** Lists all output devices */ static void listAudioDevices() { - MusicPlugin::List pluginList = MusicMan.getPlugins(); + PluginList pluginList = MusicMan.getPlugins(); printf("ID Description\n"); printf("------------------------------ ------------------------------------------------\n"); - for (MusicPlugin::List::const_iterator i = pluginList.begin(), iend = pluginList.end(); i != iend; ++i) { - MusicDevices deviceList = (**i)->getDevices(); + for (PluginList::const_iterator i = pluginList.begin(), iend = pluginList.end(); i != iend; ++i) { + const MusicPluginObject &musicObject = (*i)->get(); + MusicDevices deviceList = musicObject.getDevices(); for (MusicDevices::iterator j = deviceList.begin(), jend = deviceList.end(); j != jend; ++j) { printf("%-30s %s\n", Common::String::format("\"%s\"", j->getCompleteId().c_str()).c_str(), j->getCompleteName().c_str()); } -- cgit v1.2.3