Skip to content

Commit

Permalink
Move the sync operation to deleter
Browse files Browse the repository at this point in the history
  • Loading branch information
oleg-derevenetz committed Jan 3, 2025
1 parent 7ba3604 commit 50bec98
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
24 changes: 13 additions & 11 deletions src/engine/serialize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,11 +399,6 @@ std::string_view ROStreamBuf::getStringView( const size_t size /* = 0 */ )
return { reinterpret_cast<const char *>( strBeg ), static_cast<size_t>( strEnd - strBeg ) };
}

StreamFile::~StreamFile()
{
close();
}

bool StreamFile::open( const std::string & fn, const std::string & mode )
{
_file.reset( std::fopen( fn.c_str(), mode.c_str() ) );
Expand All @@ -419,12 +414,6 @@ bool StreamFile::open( const std::string & fn, const std::string & mode )
void StreamFile::close()
{
_file.reset();

#ifdef __EMSCRIPTEN__
// clang-format off
EM_ASM( FS.syncfs( err => err && console.warn( "FS.syncfs() error:", err ) ) );
// clang-format on
#endif
}

size_t StreamFile::size()
Expand Down Expand Up @@ -637,3 +626,16 @@ std::string StreamFile::getString( const size_t size /* = 0 */ )

return { buf.begin(), std::find( buf.begin(), buf.end(), 0 ) };
}

int StreamFile::closeFile( std::FILE * f )
{
const int res = std::fclose( f );

#ifdef __EMSCRIPTEN__
// clang-format off
EM_ASM( FS.syncfs( err => err && console.warn( "FS.syncfs() error:", err ) ) );
// clang-format on
#endif

return res;
}
6 changes: 4 additions & 2 deletions src/engine/serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ class StreamFile final : public IStreamBase, public OStreamBase

StreamFile( const StreamFile & ) = delete;

~StreamFile() override;
~StreamFile() = default;

StreamFile & operator=( const StreamFile & ) = delete;

Expand Down Expand Up @@ -669,7 +669,9 @@ class StreamFile final : public IStreamBase, public OStreamBase
}
}

std::unique_ptr<std::FILE, int ( * )( std::FILE * )> _file{ nullptr, []( std::FILE * f ) { return std::fclose( f ); } };
static int closeFile( std::FILE * f );

std::unique_ptr<std::FILE, int ( * )( std::FILE * )> _file{ nullptr, closeFile };
};

namespace fheroes2
Expand Down

0 comments on commit 50bec98

Please sign in to comment.