Add 'lazy' load for own opened streams

This commit is contained in:
Serge Lamikhov-Center 2022-11-12 18:19:48 +02:00
parent 4320ea915e
commit 94505dd637
2 changed files with 12 additions and 6 deletions

View File

@ -46,7 +46,7 @@ target_include_directories(
if(IS_TOP_PROJECT) if(IS_TOP_PROJECT)
# Enable C++11 for examples and tests # Enable C++11 for examples and tests
if(ELFIO_BUILD_EXAMPLES OR ELFIO_BUILD_TESTS) if(ELFIO_BUILD_EXAMPLES OR ELFIO_BUILD_TESTS)
set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD 14)
endif() endif()
if(ELFIO_BUILD_EXAMPLES) if(ELFIO_BUILD_EXAMPLES)

View File

@ -141,13 +141,19 @@ class elfio
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
bool load( const std::string& file_name, bool is_lazy = false ) noexcept bool load( const std::string& file_name, bool is_lazy = false ) noexcept
{ {
std::ifstream stream; pstream = std::make_unique<std::ifstream>();
stream.open( file_name.c_str(), std::ios::in | std::ios::binary ); pstream->open( file_name.c_str(), std::ios::in | std::ios::binary );
if ( !stream ) { if ( pstream == nullptr || !*pstream ) {
return false; return false;
} }
return load( stream, is_lazy ); bool ret = load( *pstream, is_lazy );
if ( !is_lazy ) {
pstream.release();
}
return ret;
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@ -1085,7 +1091,7 @@ class elfio
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
private: private:
std::ifstream* pstream = nullptr; std::unique_ptr<std::ifstream> pstream = nullptr;
std::unique_ptr<elf_header> header = nullptr; std::unique_ptr<elf_header> header = nullptr;
std::vector<std::unique_ptr<section>> sections_; std::vector<std::unique_ptr<section>> sections_;
std::vector<std::unique_ptr<segment>> segments_; std::vector<std::unique_ptr<segment>> segments_;