From 24d29a213ad236e3c535bcde7bb2f54b66078a48 Mon Sep 17 00:00:00 2001 From: Serge Lamikhov-Center Date: Sun, 19 Sep 2021 11:04:16 +0300 Subject: [PATCH] Move constructor and assignment has been implemented --- elfio/elfio.hpp | 37 +++++++++++++++++++++++++++++++++---- tests/ELFIOTest2.cpp | 27 ++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/elfio/elfio.hpp b/elfio/elfio.hpp index 92e8fd3..e84bd7f 100644 --- a/elfio/elfio.hpp +++ b/elfio/elfio.hpp @@ -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 diff --git a/tests/ELFIOTest2.cpp b/tests/ELFIOTest2.cpp index 5d1ef99..72cf726 100644 --- a/tests/ELFIOTest2.cpp +++ b/tests/ELFIOTest2.cpp @@ -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 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 ); +}