Manage AAsett* with std::unique_ptr

This commit is contained in:
Chris Thrasher 2023-05-12 18:06:25 -06:00
parent 1e9161b57e
commit e4db2b4de6
2 changed files with 21 additions and 23 deletions

View File

@ -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(); ActivityStates& states = getActivity();
std::lock_guard lock(states.mutex); std::lock_guard lock(states.mutex);
m_file = AAssetManager_open(states.activity->assetManager, filename.c_str(), AASSET_MODE_UNKNOWN); m_file.reset(AAssetManager_open(states.activity->assetManager, filename.c_str(), AASSET_MODE_UNKNOWN));
}
////////////////////////////////////////////////////////////
ResourceStream::~ResourceStream()
{
if (m_file)
{
AAsset_close(m_file);
}
} }
@ -59,7 +49,7 @@ std::int64_t ResourceStream::read(void* data, std::int64_t size)
{ {
if (m_file) if (m_file)
{ {
return AAsset_read(m_file, data, static_cast<std::size_t>(size)); return AAsset_read(m_file.get(), data, static_cast<std::size_t>(size));
} }
else else
{ {
@ -73,7 +63,7 @@ std::int64_t ResourceStream::seek(std::int64_t position)
{ {
if (m_file) if (m_file)
{ {
return AAsset_seek(m_file, static_cast<off_t>(position), SEEK_SET); return AAsset_seek(m_file.get(), static_cast<off_t>(position), SEEK_SET);
} }
else else
{ {
@ -87,7 +77,7 @@ std::int64_t ResourceStream::tell()
{ {
if (m_file) if (m_file)
{ {
return getSize() - AAsset_getRemainingLength(m_file); return getSize() - AAsset_getRemainingLength(m_file.get());
} }
else else
{ {
@ -101,7 +91,7 @@ std::int64_t ResourceStream::getSize()
{ {
if (m_file) if (m_file)
{ {
return AAsset_getLength(m_file); return AAsset_getLength(m_file.get());
} }
else else
{ {
@ -110,4 +100,10 @@ std::int64_t ResourceStream::getSize()
} }
////////////////////////////////////////////////////////////
void ResourceStream::AAssetDeleter::operator()(AAsset* file)
{
AAsset_close(file);
}
} // namespace sf::priv } // namespace sf::priv

View File

@ -54,12 +54,6 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
ResourceStream(const std::filesystem::path& filename); ResourceStream(const std::filesystem::path& filename);
////////////////////////////////////////////////////////////
/// \brief Destructor
///
////////////////////////////////////////////////////////////
~ResourceStream();
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Read data from the asset /// \brief Read data from the asset
/// ///
@ -98,10 +92,18 @@ public:
std::int64_t getSize() override; std::int64_t getSize() override;
private: private:
////////////////////////////////////////////////////////////
// Types
////////////////////////////////////////////////////////////
struct AAssetDeleter
{
void operator()(AAsset*);
};
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
// Member data // Member data
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
AAsset* m_file; ///< The asset file to read std::unique_ptr<AAsset, AAssetDeleter> m_file; ///< The asset file to read
}; };
} // namespace sf::priv } // namespace sf::priv