mirror of
https://github.com/loot/libloot.git
synced 2026-07-27 14:16:01 -07:00
Replace unnecessary usage of std::shared_ptr
- CreateGameHandle() should return a unique_ptr because it transfers ownership. - GameInterface::GetDatabase() should return a reference because it returns an existing object that will always be valid at call time. - GameInterface::GetPlugin() and GameInterface::GetLoadedPlugins() return raw pointers because they do not transfer or share ownership. It's unfortunately that references can't be used instead (ignoring std::reference_wrapper), as std::optional<const Plugin&> and std::vector<const Plugin&> would be more meaningful return types. Internally, there were a few uses of shared_ptr that could be unique_ptr and a few that could be non-pointer types. A few uses of shared_ptr remain: - Game and ApiDatabase share a ConditionEvaluator, so it's kept inside a shared_ptr. Technically ApiDatabase is used such that the ConditionEvaluator it uses will always outlive it, but that's not guaranteed, so shared_ptr is used for safety. - Plugin objects are cached inside shared_ptr so that the map of them can be iterated over. Ideally they'd be stored in unique_ptr, but unique_ptr not being copyable means the map entries can't be iterated over.
This commit is contained in:
+1
-1
@@ -105,7 +105,7 @@ LOOT_API bool IsCompatible(const unsigned int major,
|
||||
* variable (eg. Linux) can still use the API.
|
||||
* @returns The new game handle.
|
||||
*/
|
||||
LOOT_API std::shared_ptr<GameInterface> CreateGameHandle(
|
||||
LOOT_API std::unique_ptr<GameInterface> CreateGameHandle(
|
||||
const GameType game,
|
||||
const std::filesystem::path& game_path,
|
||||
const std::filesystem::path& game_local_path = "");
|
||||
|
||||
@@ -33,6 +33,8 @@ namespace loot {
|
||||
/** @brief The interface provided for accessing game-specific functionality. */
|
||||
class GameInterface {
|
||||
public:
|
||||
virtual ~GameInterface() = default;
|
||||
|
||||
/**
|
||||
* @name Metadata Access
|
||||
* @{
|
||||
@@ -41,9 +43,10 @@ public:
|
||||
/**
|
||||
* @brief Get the database interface used for accessing metadata-related
|
||||
* functionality.
|
||||
* @returns A shared pointer to the game's DatabaseInterface
|
||||
* @returns A reference to the game's DatabaseInterface. The reference remains
|
||||
* valid for the lifetime of the GameInterface instance.
|
||||
*/
|
||||
virtual std::shared_ptr<DatabaseInterface> GetDatabase() = 0;
|
||||
virtual DatabaseInterface& GetDatabase() = 0;
|
||||
|
||||
/**
|
||||
* @}
|
||||
@@ -84,7 +87,7 @@ public:
|
||||
* @returns A shared 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 const PluginInterface* GetPlugin(
|
||||
const std::string& pluginName) const = 0;
|
||||
|
||||
/**
|
||||
@@ -94,8 +97,7 @@ public:
|
||||
* valid until the ``LoadPlugins()`` or ``SortPlugins()`` functions
|
||||
* are next called or this GameInterface is destroyed.
|
||||
*/
|
||||
virtual std::vector<std::shared_ptr<const PluginInterface>> GetLoadedPlugins()
|
||||
const = 0;
|
||||
virtual std::vector<const PluginInterface*> GetLoadedPlugins() const = 0;
|
||||
|
||||
/**
|
||||
* @}
|
||||
|
||||
Reference in New Issue
Block a user