Move constructor and assignment has been implemented

This commit is contained in:
Serge Lamikhov-Center 2021-09-19 11:04:16 +03:00 committed by Serge Lamikhov-Center
parent 3a76e30a92
commit 24d29a213a
2 changed files with 59 additions and 5 deletions

View File

@ -68,19 +68,48 @@ class elfio
{
public:
//------------------------------------------------------------------------------
elfio() : sections( this ), segments( this )
elfio() noexcept : sections( this ), segments( this )
{
header = nullptr;
current_file_pos = 0;
create( ELFCLASS32, ELFDATA2LSB );
}
elfio( elfio&& other ) noexcept : sections( this ), segments( this )
{
header = std::move(other.header);
sections_ = std::move(other.sections_);
segments_ = std::move(other.segments_);
convertor = std::move(other.convertor);
current_file_pos = std::move(other.current_file_pos);
other.header = nullptr;
other.sections_.clear();
other.segments_.clear();
}
elfio& operator=( elfio&& other ) noexcept
{
if ( this != &other ) {
clean();
header = std::move(other.header);
sections_ = std::move(other.sections_);
segments_ = std::move(other.segments_);
convertor = std::move(other.convertor);
current_file_pos = std::move(other.current_file_pos);
other.header = nullptr;
other.sections_.clear();
other.segments_.clear();
}
return *this;
}
//------------------------------------------------------------------------------
// clang-format off
elfio( const elfio& ) = delete;
elfio& operator=( const elfio& ) = delete;
elfio( elfio&& ) = default;
elfio& operator=( elfio&& ) = default;
// clang-format on
//------------------------------------------------------------------------------
@ -779,7 +808,7 @@ class elfio
align = 1;
}
Elf64_Off error = current_file_pos % align;
section_align = ( align - error ) % align;
section_align = ( align - error ) % align;
}
else if ( section_generated[index] ) {
// Alignment for already generated sections

View File

@ -399,7 +399,32 @@ BOOST_AUTO_TEST_CASE( gnu_version_64_le )
if ( verindex > 1 ) {
Elf64_Addr value = 0;
vern.get_entry( verindex * 8, value );
std::cout << value << std::endl;
// std::cout << value << std::endl;
}
}
}
////////////////////////////////////////////////////////////////////////////////
BOOST_AUTO_TEST_CASE( move_constructor_and_assignment )
{
elfio r1;
// Load ELF data
BOOST_REQUIRE_EQUAL( r1.load( "elf_examples/hello_64" ), true );
Elf64_Addr entry = r1.get_entry();
std::string sec_name = r1.sections[".text"]->get_name();
Elf_Xword seg_size = r1.segments[1]->get_memory_size();
// Move to a vector element
std::vector<elfio> v;
v.emplace_back( std::move( r1 ) );
BOOST_CHECK_EQUAL( v[0].get_entry(), entry );
BOOST_CHECK_EQUAL( v[0].sections[".text"]->get_name(), sec_name );
BOOST_CHECK_EQUAL( v[0].segments[1]->get_memory_size(), seg_size );
elfio r2;
r2 = std::move( v[0] );
BOOST_CHECK_EQUAL( r2.get_entry(), entry );
BOOST_CHECK_EQUAL( r2.sections[".text"]->get_name(), sec_name );
BOOST_CHECK_EQUAL( r2.segments[1]->get_memory_size(), seg_size );
}