From e4db2b4de67341c3e5c4abbdf5ee70f40c15f9ef Mon Sep 17 00:00:00 2001 From: Chris Thrasher Date: Fri, 12 May 2023 18:06:25 -0600 Subject: [PATCH] Manage `AAsett*` with `std::unique_ptr` --- src/SFML/System/Android/ResourceStream.cpp | 28 ++++++++++------------ src/SFML/System/Android/ResourceStream.hpp | 16 +++++++------ 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/SFML/System/Android/ResourceStream.cpp b/src/SFML/System/Android/ResourceStream.cpp index 870d11c8..892b4fd2 100644 --- a/src/SFML/System/Android/ResourceStream.cpp +++ b/src/SFML/System/Android/ResourceStream.cpp @@ -36,21 +36,11 @@ namespace sf::priv { //////////////////////////////////////////////////////////// -ResourceStream::ResourceStream(const std::filesystem::path& filename) : m_file(nullptr) +ResourceStream::ResourceStream(const std::filesystem::path& filename) { ActivityStates& states = getActivity(); std::lock_guard lock(states.mutex); - m_file = AAssetManager_open(states.activity->assetManager, filename.c_str(), AASSET_MODE_UNKNOWN); -} - - -//////////////////////////////////////////////////////////// -ResourceStream::~ResourceStream() -{ - if (m_file) - { - AAsset_close(m_file); - } + m_file.reset(AAssetManager_open(states.activity->assetManager, filename.c_str(), AASSET_MODE_UNKNOWN)); } @@ -59,7 +49,7 @@ std::int64_t ResourceStream::read(void* data, std::int64_t size) { if (m_file) { - return AAsset_read(m_file, data, static_cast(size)); + return AAsset_read(m_file.get(), data, static_cast(size)); } else { @@ -73,7 +63,7 @@ std::int64_t ResourceStream::seek(std::int64_t position) { if (m_file) { - return AAsset_seek(m_file, static_cast(position), SEEK_SET); + return AAsset_seek(m_file.get(), static_cast(position), SEEK_SET); } else { @@ -87,7 +77,7 @@ std::int64_t ResourceStream::tell() { if (m_file) { - return getSize() - AAsset_getRemainingLength(m_file); + return getSize() - AAsset_getRemainingLength(m_file.get()); } else { @@ -101,7 +91,7 @@ std::int64_t ResourceStream::getSize() { if (m_file) { - return AAsset_getLength(m_file); + return AAsset_getLength(m_file.get()); } else { @@ -110,4 +100,10 @@ std::int64_t ResourceStream::getSize() } +//////////////////////////////////////////////////////////// +void ResourceStream::AAssetDeleter::operator()(AAsset* file) +{ + AAsset_close(file); +} + } // namespace sf::priv diff --git a/src/SFML/System/Android/ResourceStream.hpp b/src/SFML/System/Android/ResourceStream.hpp index d94e3199..a77f2d77 100644 --- a/src/SFML/System/Android/ResourceStream.hpp +++ b/src/SFML/System/Android/ResourceStream.hpp @@ -54,12 +54,6 @@ public: //////////////////////////////////////////////////////////// ResourceStream(const std::filesystem::path& filename); - //////////////////////////////////////////////////////////// - /// \brief Destructor - /// - //////////////////////////////////////////////////////////// - ~ResourceStream(); - //////////////////////////////////////////////////////////// /// \brief Read data from the asset /// @@ -98,10 +92,18 @@ public: std::int64_t getSize() override; private: + //////////////////////////////////////////////////////////// + // Types + //////////////////////////////////////////////////////////// + struct AAssetDeleter + { + void operator()(AAsset*); + }; + //////////////////////////////////////////////////////////// // Member data //////////////////////////////////////////////////////////// - AAsset* m_file; ///< The asset file to read + std::unique_ptr m_file; ///< The asset file to read }; } // namespace sf::priv