Use unique_ptr instead of shared_ptr for PluginInterface pointers

unique_ptr is simpler, and can be easily converted to shared_ptr if needed, and makes it obvious that each call will return a new pointer.

This does mean that if I want to hold onto the objects again in the future for some reason, that'll need the API to change back, but that seems unlikely.

unique_ptr not being copyable does make using it a little more awkward, but that only happened in one place in LOOT's code, and there the pointer needs to become shared anyway.
This commit is contained in:
Oliver Hamlet
2026-01-03 13:32:57 +00:00
parent b7363b0a29
commit 84ffe39a37
3 changed files with 12 additions and 16 deletions
+5 -9
View File
@@ -141,22 +141,18 @@ public:
* @brief Get data for a loaded plugin.
* @param pluginName
* The filename of the plugin to get data for.
* @returns A shared pointer to a const PluginInterface implementation. The
* pointer is null if the given plugin has not been loaded. Repeated
* calls given the same filename may return different pointers to
* equivalent objects.
* @returns A pointer to a const PluginInterface implementation. The
* pointer is null if the given plugin has not been loaded.
*/
virtual std::shared_ptr<const PluginInterface> GetPlugin(
virtual std::unique_ptr<const PluginInterface> GetPlugin(
std::string_view pluginName) const = 0;
/**
* @brief Get a set of const references to all loaded plugins' PluginInterface
* objects.
* @returns A set of shared pointers to const PluginInterface objects.
* Repeated calls may return different pointers to equivalent
* objects.
* @returns A set of pointers to const PluginInterface objects.
*/
virtual std::vector<std::shared_ptr<const PluginInterface>> GetLoadedPlugins()
virtual std::vector<std::unique_ptr<const PluginInterface>> GetLoadedPlugins()
const = 0;
/**
+5 -5
View File
@@ -171,7 +171,7 @@ void Game::LoadPlugins(const std::vector<std::filesystem::path>& pluginPaths,
void Game::ClearLoadedPlugins() { game_->clear_loaded_plugins(); }
std::shared_ptr<const PluginInterface> Game::GetPlugin(
std::unique_ptr<const PluginInterface> Game::GetPlugin(
std::string_view pluginName) const {
const auto pluginOpt = game_->plugin(convert(pluginName));
if (!pluginOpt->is_some()) {
@@ -179,19 +179,19 @@ std::shared_ptr<const PluginInterface> Game::GetPlugin(
}
try {
return std::make_shared<Plugin>(
return std::make_unique<Plugin>(
std::move(pluginOpt->as_ref().boxed_clone()));
} catch (const ::rust::Error& e) {
std::rethrow_exception(mapError(e));
}
}
std::vector<std::shared_ptr<const PluginInterface>> Game::GetLoadedPlugins()
std::vector<std::unique_ptr<const PluginInterface>> Game::GetLoadedPlugins()
const {
std::vector<std::shared_ptr<const PluginInterface>> plugins;
std::vector<std::unique_ptr<const PluginInterface>> plugins;
for (const auto& pluginRef : game_->loaded_plugins()) {
plugins.push_back(
std::make_shared<Plugin>(std::move(pluginRef.boxed_clone())));
std::make_unique<Plugin>(std::move(pluginRef.boxed_clone())));
}
return plugins;
+2 -2
View File
@@ -35,10 +35,10 @@ public:
void ClearLoadedPlugins() override;
std::shared_ptr<const PluginInterface> GetPlugin(
std::unique_ptr<const PluginInterface> GetPlugin(
std::string_view pluginName) const override;
std::vector<std::shared_ptr<const PluginInterface>> GetLoadedPlugins()
std::vector<std::unique_ptr<const PluginInterface>> GetLoadedPlugins()
const override;
std::vector<std::string> SortPlugins(