Skip to content

Commit 8ae6cec

Browse files
committed
C++17 refactoring
1 parent 5a9297b commit 8ae6cec

File tree

4 files changed

+23
-32
lines changed

4 files changed

+23
-32
lines changed

elfio/elfio.hpp

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,8 @@ THE SOFTWARE.
4141
#include <elfio/elfio_segment.hpp>
4242
#include <elfio/elfio_strings.hpp>
4343

44-
#define ELFIO_HEADER_ACCESS_GET( TYPE, FNAME ) \
45-
TYPE get_##FNAME() const \
46-
{ \
47-
return header ? ( header->get_##FNAME() ) : 0; \
48-
}
44+
#define ELFIO_HEADER_ACCESS_GET( TYPE, FNAME ) \
45+
TYPE get_##FNAME() const { return header ? ( header->get_##FNAME() ) : 0; }
4946

5047
#define ELFIO_HEADER_ACCESS_GET_SET( TYPE, FNAME ) \
5148
TYPE get_##FNAME() const \
@@ -421,9 +418,7 @@ class elfio
421418
//------------------------------------------------------------------------------
422419
section* create_section()
423420
{
424-
unsigned char file_class = get_class();
425-
426-
if ( file_class == ELFCLASS64 ) {
421+
if ( auto file_class = get_class(); file_class == ELFCLASS64 ) {
427422
sections_.emplace_back(
428423
new ( std::nothrow ) section_impl<Elf64_Shdr>(
429424
&convertor, &addr_translator, compression ) );
@@ -447,9 +442,7 @@ class elfio
447442
//------------------------------------------------------------------------------
448443
segment* create_segment()
449444
{
450-
unsigned char file_class = header->get_class();
451-
452-
if ( file_class == ELFCLASS64 ) {
445+
if ( auto file_class = header->get_class(); file_class == ELFCLASS64 ) {
453446
segments_.emplace_back(
454447
new ( std::nothrow )
455448
segment_impl<Elf64_Phdr>( &convertor, &addr_translator ) );
@@ -512,9 +505,8 @@ class elfio
512505
sec->set_address( sec->get_address() );
513506
}
514507

515-
Elf_Half shstrndx = get_section_name_str_index();
516-
517-
if ( SHN_UNDEF != shstrndx ) {
508+
if ( Elf_Half shstrndx = get_section_name_str_index();
509+
SHN_UNDEF != shstrndx ) {
518510
string_section_accessor str_reader( sections[shstrndx] );
519511
for ( Elf_Half i = 0; i < num; ++i ) {
520512
Elf_Word section_offset = sections[i]->get_name_string_offset();
@@ -733,8 +725,8 @@ class elfio
733725
if ( is_section_without_segment( i ) ) {
734726
const auto& sec = sections_[i];
735727

736-
Elf_Xword section_align = sec->get_addr_align();
737-
if ( section_align > 1 &&
728+
if ( Elf_Xword section_align = sec->get_addr_align();
729+
section_align > 1 &&
738730
current_file_pos % section_align != 0 ) {
739731
current_file_pos +=
740732
section_align - current_file_pos % section_align;
@@ -966,7 +958,7 @@ class elfio
966958
}
967959

968960
//------------------------------------------------------------------------------
969-
section* operator[]( const std::string& name ) const
961+
section* operator[]( const std::string_view& name ) const
970962
{
971963
section* sec = nullptr;
972964

elfio/elfio_modinfo.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ THE SOFTWARE.
2323
#ifndef ELFIO_MODINFO_HPP
2424
#define ELFIO_MODINFO_HPP
2525

26-
#include <string>
26+
#include <string_view>
2727
#include <vector>
2828

2929
namespace ELFIO {
@@ -56,12 +56,12 @@ template <class S> class modinfo_section_accessor_template
5656
}
5757

5858
//------------------------------------------------------------------------------
59-
bool get_attribute( const std::string& field_name,
60-
std::string& value ) const
59+
bool get_attribute( const std::string_view& field_name,
60+
std::string& value ) const
6161
{
62-
for ( auto& i : content ) {
63-
if ( field_name == i.first ) {
64-
value = i.second;
62+
for ( const auto [first, second] : content ) {
63+
if ( field_name == first ) {
64+
value = second;
6565
return true;
6666
}
6767
}

elfio/elfio_section.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ template <class T> class section_impl : public section
256256
Elf_Xword size = get_size();
257257
Elf_Xword uncompressed_size = 0;
258258
auto decompressed_data = compression->inflate(
259-
data.get(), convertor, size, uncompressed_size );
259+
data.get(), convertor, size, uncompressed_size );
260260
if ( decompressed_data != nullptr ) {
261261
set_size( uncompressed_size );
262262
data = std::move( decompressed_data );
@@ -338,7 +338,7 @@ template <class T> class section_impl : public section
338338
Elf_Xword decompressed_size = get_size();
339339
Elf_Xword compressed_size = 0;
340340
auto compressed_ptr = compression->deflate(
341-
data.get(), convertor, decompressed_size, compressed_size );
341+
data.get(), convertor, decompressed_size, compressed_size );
342342
stream.write( compressed_ptr.get(), compressed_size );
343343
}
344344
else {

elfio/elfio_utils.hpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,11 @@ class address_translator
177177
{
178178
addr_translations = addr_trans;
179179

180-
std::sort(
181-
addr_translations.begin(), addr_translations.end(),
182-
[]( address_translation& a, address_translation& b ) -> bool {
183-
return a.start < b.start;
184-
} );
180+
std::sort( addr_translations.begin(), addr_translations.end(),
181+
[]( const address_translation& a,
182+
const address_translation& b ) -> bool {
183+
return a.start < b.start;
184+
} );
185185
}
186186

187187
//------------------------------------------------------------------------------
@@ -236,8 +236,7 @@ inline std::string to_hex_string( uint64_t value )
236236
std::string str;
237237

238238
while ( value ) {
239-
auto digit = value & 0xF;
240-
if ( digit < 0xA ) {
239+
if ( auto digit = value & 0xF; digit < 0xA ) {
241240
str = char( '0' + digit ) + str;
242241
}
243242
else {

0 commit comments

Comments
 (0)