Skip to content

Commit

Permalink
C++17 refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
serge1 committed Jun 2, 2023
1 parent 5a9297b commit 8ae6cec
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 32 deletions.
26 changes: 9 additions & 17 deletions elfio/elfio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,8 @@ THE SOFTWARE.
#include <elfio/elfio_segment.hpp>
#include <elfio/elfio_strings.hpp>

#define ELFIO_HEADER_ACCESS_GET( TYPE, FNAME ) \
TYPE get_##FNAME() const \
{ \
return header ? ( header->get_##FNAME() ) : 0; \
}
#define ELFIO_HEADER_ACCESS_GET( TYPE, FNAME ) \
TYPE get_##FNAME() const { return header ? ( header->get_##FNAME() ) : 0; }

#define ELFIO_HEADER_ACCESS_GET_SET( TYPE, FNAME ) \
TYPE get_##FNAME() const \
Expand Down Expand Up @@ -421,9 +418,7 @@ class elfio
//------------------------------------------------------------------------------
section* create_section()
{
unsigned char file_class = get_class();

if ( file_class == ELFCLASS64 ) {
if ( auto file_class = get_class(); file_class == ELFCLASS64 ) {
sections_.emplace_back(
new ( std::nothrow ) section_impl<Elf64_Shdr>(
&convertor, &addr_translator, compression ) );
Expand All @@ -447,9 +442,7 @@ class elfio
//------------------------------------------------------------------------------
segment* create_segment()
{
unsigned char file_class = header->get_class();

if ( file_class == ELFCLASS64 ) {
if ( auto file_class = header->get_class(); file_class == ELFCLASS64 ) {
segments_.emplace_back(
new ( std::nothrow )
segment_impl<Elf64_Phdr>( &convertor, &addr_translator ) );
Expand Down Expand Up @@ -512,9 +505,8 @@ class elfio
sec->set_address( sec->get_address() );
}

Elf_Half shstrndx = get_section_name_str_index();

if ( SHN_UNDEF != shstrndx ) {
if ( Elf_Half shstrndx = get_section_name_str_index();
SHN_UNDEF != shstrndx ) {
string_section_accessor str_reader( sections[shstrndx] );
for ( Elf_Half i = 0; i < num; ++i ) {
Elf_Word section_offset = sections[i]->get_name_string_offset();
Expand Down Expand Up @@ -733,8 +725,8 @@ class elfio
if ( is_section_without_segment( i ) ) {
const auto& sec = sections_[i];

Elf_Xword section_align = sec->get_addr_align();
if ( section_align > 1 &&
if ( Elf_Xword section_align = sec->get_addr_align();
section_align > 1 &&
current_file_pos % section_align != 0 ) {
current_file_pos +=
section_align - current_file_pos % section_align;
Expand Down Expand Up @@ -966,7 +958,7 @@ class elfio
}

//------------------------------------------------------------------------------
section* operator[]( const std::string& name ) const
section* operator[]( const std::string_view& name ) const
{
section* sec = nullptr;

Expand Down
12 changes: 6 additions & 6 deletions elfio/elfio_modinfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ THE SOFTWARE.
#ifndef ELFIO_MODINFO_HPP
#define ELFIO_MODINFO_HPP

#include <string>
#include <string_view>
#include <vector>

namespace ELFIO {
Expand Down Expand Up @@ -56,12 +56,12 @@ template <class S> class modinfo_section_accessor_template
}

//------------------------------------------------------------------------------
bool get_attribute( const std::string& field_name,
std::string& value ) const
bool get_attribute( const std::string_view& field_name,
std::string& value ) const
{
for ( auto& i : content ) {
if ( field_name == i.first ) {
value = i.second;
for ( const auto [first, second] : content ) {
if ( field_name == first ) {
value = second;
return true;
}
}
Expand Down
4 changes: 2 additions & 2 deletions elfio/elfio_section.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ template <class T> class section_impl : public section
Elf_Xword size = get_size();
Elf_Xword uncompressed_size = 0;
auto decompressed_data = compression->inflate(
data.get(), convertor, size, uncompressed_size );
data.get(), convertor, size, uncompressed_size );
if ( decompressed_data != nullptr ) {
set_size( uncompressed_size );
data = std::move( decompressed_data );
Expand Down Expand Up @@ -338,7 +338,7 @@ template <class T> class section_impl : public section
Elf_Xword decompressed_size = get_size();
Elf_Xword compressed_size = 0;
auto compressed_ptr = compression->deflate(
data.get(), convertor, decompressed_size, compressed_size );
data.get(), convertor, decompressed_size, compressed_size );
stream.write( compressed_ptr.get(), compressed_size );
}
else {
Expand Down
13 changes: 6 additions & 7 deletions elfio/elfio_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,11 @@ class address_translator
{
addr_translations = addr_trans;

std::sort(
addr_translations.begin(), addr_translations.end(),
[]( address_translation& a, address_translation& b ) -> bool {
return a.start < b.start;
} );
std::sort( addr_translations.begin(), addr_translations.end(),
[]( const address_translation& a,
const address_translation& b ) -> bool {
return a.start < b.start;
} );
}

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -236,8 +236,7 @@ inline std::string to_hex_string( uint64_t value )
std::string str;

while ( value ) {
auto digit = value & 0xF;
if ( digit < 0xA ) {
if ( auto digit = value & 0xF; digit < 0xA ) {
str = char( '0' + digit ) + str;
}
else {
Expand Down

0 comments on commit 8ae6cec

Please sign in to comment.