Skip to content

Commit 7442d4f

Browse files
authored
Merge pull request #680 from ckormanyos/more_modernization
More modernization
2 parents a097985 + 287a66e commit 7442d4f

24 files changed

Lines changed: 278 additions & 274 deletions

code_snippets/chapter06/chapter06.sh

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ $GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -Wconversion -Wsign-conversion
3030
$GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -Wconversion -Wsign-conversion -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter06_06-002_name_mangling_get_event.cpp -o ./bin/chapter06_06-002_name_mangling_get_event.exe
3131
$GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -Wconversion -Wsign-conversion -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter06_07-001_assembly_sequences.cpp -o ./bin/chapter06_07-001_assembly_sequences.exe
3232
$GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -Wconversion -Wsign-conversion -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter06_08-001_comments.cpp -o ./bin/chapter06_08-001_comments.exe
33-
$GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -Wconversion -Wsign-conversion -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter06_09-001_typedef_reg_access.cpp -o ./bin/chapter06_09-001_typedef_reg_access.exe
34-
$GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -Wconversion -Wsign-conversion -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter06_09-002_typedef_led_template.cpp -o ./bin/chapter06_09-002_typedef_led_template.exe
35-
$GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -Wconversion -Wsign-conversion -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter06_09-003_alias_reg_access.cpp -o ./bin/chapter06_09-003_alias_reg_access.exe
33+
$GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -Wconversion -Wsign-conversion -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter06_09-001_alias_reg_access.cpp -o ./bin/chapter06_09-001_alias_reg_access.exe
34+
$GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -Wconversion -Wsign-conversion -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter06_09-002_alias_led_template.cpp -o ./bin/chapter06_09-002_alias_led_template.exe
3635
$GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -Wconversion -Wsign-conversion -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter06_10-001_checksum_uint8_t.cpp -o ./bin/chapter06_10-001_checksum_uint8_t.exe
3736
$GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -Wconversion -Wsign-conversion -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter06_10-002_checksum_uint_fast8_t.cpp -o ./bin/chapter06_10-002_checksum_uint_fast8_t.exe
3837
$GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -Wconversion -Wsign-conversion -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter06_11-001_scale_with_powers_of_two.cpp -o ./bin/chapter06_11-001_scale_with_powers_of_two.exe
@@ -63,9 +62,8 @@ ls -la \
6362
./bin/chapter06_06-002_name_mangling_get_event.exe \
6463
./bin/chapter06_07-001_assembly_sequences.exe \
6564
./bin/chapter06_08-001_comments.exe \
66-
./bin/chapter06_09-001_typedef_reg_access.exe \
67-
./bin/chapter06_09-002_typedef_led_template.exe \
68-
./bin/chapter06_09-003_alias_reg_access.exe \
65+
./bin/chapter06_09-001_alias_reg_access.exe \
66+
./bin/chapter06_09-002_alias_led_template.exe \
6967
./bin/chapter06_10-001_checksum_uint8_t.exe \
7068
./bin/chapter06_10-002_checksum_uint_fast8_t.exe \
7169
./bin/chapter06_11-001_scale_with_powers_of_two.exe \
@@ -92,9 +90,8 @@ ls -la \
9290
./bin/chapter06_06-002_name_mangling_get_event.exe \
9391
./bin/chapter06_07-001_assembly_sequences.exe \
9492
./bin/chapter06_08-001_comments.exe \
95-
./bin/chapter06_09-001_typedef_reg_access.exe \
96-
./bin/chapter06_09-002_typedef_led_template.exe \
97-
./bin/chapter06_09-003_alias_reg_access.exe \
93+
./bin/chapter06_09-001_alias_reg_access.exe \
94+
./bin/chapter06_09-002_alias_led_template.exe \
9895
./bin/chapter06_10-001_checksum_uint8_t.exe \
9996
./bin/chapter06_10-002_checksum_uint_fast8_t.exe \
10097
./bin/chapter06_11-001_scale_with_powers_of_two.exe \

code_snippets/chapter06/chapter06_08-001_comments.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
///////////////////////////////////////////////////////////////////////////////
2-
// Copyright Christopher Kormanyos 2019.
2+
// Copyright Christopher Kormanyos 2019 - 2025.
33
// Distributed under the Boost Software License,
44
// Version 1.0. (See accompanying file LICENSE_1_0.txt
55
// or copy at http://www.boost.org/LICENSE_1_0.txt)
66
//
77

88
// chapter06_08-001_comments_sparingly.cpp
99

10+
void CalculateSpeed();
11+
1012
void CalculateSpeed() { }
1113

12-
int main()
14+
auto main() -> int;
15+
16+
auto main() -> int
1317
{
1418
// This is an example of a potentially redundant comment.
1519

code_snippets/chapter06/chapter06_09-003_alias_reg_access.cpp renamed to code_snippets/chapter06/chapter06_09-001_alias_reg_access.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
///////////////////////////////////////////////////////////////////////////////
2-
// Copyright Christopher Kormanyos 2019 - 2023.
2+
// Copyright Christopher Kormanyos 2019 - 2025.
33
// Distributed under the Boost Software License,
44
// Version 1.0. (See accompanying file LICENSE_1_0.txt
55
// or copy at http://www.boost.org/LICENSE_1_0.txt)
66
//
77

8-
// chapter06_09-001_typedef_reg_access.cpp
8+
// chapter06_09-001_alias_reg_access.cpp
99

1010
#include <cstdint>
1111
#include <iomanip>
1212
#include <iostream>
13+
#include <sstream>
1314

1415
template<typename RegisterAddressType,
1516
typename RegisterValueType>
@@ -53,7 +54,7 @@ auto do_something() -> void;
5354

5455
auto do_something() -> void
5556
{
56-
// Use an alias to save tedious typing work.
57+
// Use a convenient alias to save tedious typing work.
5758
using port_type = reg_access_dynamic<std::uintptr_t, std::uint8_t>;
5859

5960
// Toggle the simulated portb.5.
@@ -64,20 +65,19 @@ auto main() -> int;
6465

6566
auto main() -> int
6667
{
67-
const auto flg = std::cout.flags();
68-
6968
for(auto i = static_cast<unsigned>(UINT8_C(0)); i < static_cast<unsigned>(UINT8_C(4)); ++i)
7069
{
7170
do_something();
7271

73-
std::cout << "simulated_register_portb: "
74-
<< std::hex
75-
<< "0x"
76-
<< std::setw(static_cast<std::streamsize>(INT8_C(2)))
77-
<< std::setfill('0')
78-
<< static_cast<unsigned>(simulated_register_portb)
79-
<< std::endl;
80-
}
72+
std::stringstream strm { };
8173

82-
std::cout.flags(flg);
74+
strm << "simulated_register_portb: "
75+
<< std::hex
76+
<< "0x"
77+
<< std::setw(static_cast<std::streamsize>(INT8_C(2)))
78+
<< std::setfill('0')
79+
<< static_cast<unsigned>(simulated_register_portb);
80+
81+
std::cout << strm.str() << std::endl;
82+
}
8383
}

code_snippets/chapter06/chapter06_09-001_typedef_reg_access.cpp

Lines changed: 0 additions & 83 deletions
This file was deleted.

code_snippets/chapter06/chapter06_09-002_typedef_led_template.cpp renamed to code_snippets/chapter06/chapter06_09-002_alias_led_template.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
///////////////////////////////////////////////////////////////////////////////
2-
// Copyright Christopher Kormanyos 2019 - 2023.
2+
// Copyright Christopher Kormanyos 2019 - 2025.
33
// Distributed under the Boost Software License,
44
// Version 1.0. (See accompanying file LICENSE_1_0.txt
55
// or copy at http://www.boost.org/LICENSE_1_0.txt)
66
//
77

8-
// chapter06_09-002_typedef_led_template.cpp
8+
// chapter06_09-002_alias_led_template.cpp
99

1010
#include <cstdint>
11-
#include <iomanip>
1211
#include <iostream>
1312

1413
template<typename RegisterAddressType,
@@ -64,40 +63,36 @@ class led_template
6463
{
6564
// Toggle the LED.
6665
port_pin_type::bit_not(my_port, my_bval);
66+
67+
is_on = (!is_on);
6768
}
6869

70+
auto get_is_on() const -> bool { return is_on; }
71+
6972
private:
7073
const port_type my_port;
7174
const bval_type my_bval;
75+
bool is_on { };
7276
};
7377

7478
// The simulated portb.
75-
auto simulated_register_portb = std::uint8_t { };
79+
std::uint8_t simulated_register_portb { };
7680

77-
const auto address = reinterpret_cast<std::uintptr_t>(&simulated_register_portb);
81+
const auto address { reinterpret_cast<std::uintptr_t>(&simulated_register_portb) };
7882

7983
auto main() -> int;
8084

8185
auto main() -> int
8286
{
87+
// Use a convenient alias to save tedious typing work.
8388
using led_b5_type = led_template<std::uintptr_t, std::uint8_t>;
8489

8590
led_b5_type led_b5 { address, static_cast<std::uint8_t>(UINT8_C(5)) };
8691

87-
const auto flg = std::cout.flags();
88-
8992
for(auto i = static_cast<unsigned>(UINT8_C(0)); i < static_cast<unsigned>(UINT8_C(4)); ++i)
9093
{
9194
led_b5.toggle();
9295

93-
std::cout << "simulated_register_portb: "
94-
<< std::hex
95-
<< "0x"
96-
<< std::setw(static_cast<std::streamsize>(INT8_C(2)))
97-
<< std::setfill('0')
98-
<< static_cast<unsigned>(simulated_register_portb)
99-
<< std::endl;
96+
std::cout << "LED is: " << (led_b5.get_is_on() ? "on" : "off") << std::endl;
10097
}
101-
102-
std::cout.flags(flg);
10398
}
Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,47 @@
11
///////////////////////////////////////////////////////////////////////////////
2-
// Copyright Christopher Kormanyos 2019.
2+
// Copyright Christopher Kormanyos 2019 - 2025.
33
// Distributed under the Boost Software License,
44
// Version 1.0. (See accompanying file LICENSE_1_0.txt
55
// or copy at http://www.boost.org/LICENSE_1_0.txt)
66
//
77

88
// chapter06_10-001_checksum_uint8_t.cpp
99

10+
#include <array>
11+
#include <cstddef>
1012
#include <cstdint>
1113
#include <iomanip>
1214
#include <iostream>
1315

14-
std::uint8_t checksum(const std::uint8_t* p,
15-
const std::uint8_t len)
16+
constexpr auto checksum(const std::uint8_t* p, const std::uint8_t len) -> std::uint8_t;
17+
18+
constexpr auto checksum(const std::uint8_t* p, const std::uint8_t len) -> std::uint8_t
1619
{
17-
std::uint8_t sum = UINT8_C(0);
20+
std::uint8_t sum { UINT8_C(0) };
1821

19-
for(std::uint8_t i = UINT8_C(0); i < len; i++)
22+
for(std::uint8_t i = UINT8_C(0); i < len; ++i)
2023
{
2124
sum += *p;
25+
2226
++p;
2327
};
2428

25-
return sum;
29+
return static_cast<std::uint8_t>(~sum + UINT8_C(1));
2630
}
2731

28-
int main()
32+
auto main() -> int;
33+
34+
auto main() -> int
2935
{
30-
const std::uint8_t data[3U] = { 1U, 2U, 3U };
36+
const std::array<std::uint8_t, std::size_t { UINT8_C(3) }> data =
37+
{{
38+
UINT8_C(1),
39+
UINT8_C(2),
40+
UINT8_C(3)
41+
}};
3142

32-
const std::uint8_t sum = checksum(data, 3U);
43+
const std::uint8_t sum { checksum(data.data(), static_cast<std::uint8_t>(data.size())) };
3344

34-
// 6
35-
std::cout << "sum: " << static_cast<std::uint32_t>(sum) << std::endl;
45+
// 250
46+
std::cout << "sum: " << static_cast<unsigned>(sum) << std::endl;
3647
}
Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,47 @@
11
///////////////////////////////////////////////////////////////////////////////
2-
// Copyright Christopher Kormanyos 2019.
2+
// Copyright Christopher Kormanyos 2019 - 2025.
33
// Distributed under the Boost Software License,
44
// Version 1.0. (See accompanying file LICENSE_1_0.txt
55
// or copy at http://www.boost.org/LICENSE_1_0.txt)
66
//
77

88
// chapter06_10-002_checksum_uint_fast8_t.cpp
99

10+
#include <array>
11+
#include <cstddef>
1012
#include <cstdint>
1113
#include <iomanip>
1214
#include <iostream>
1315

14-
std::uint8_t checksum(const std::uint8_t* p,
15-
const std::uint_fast8_t len)
16+
constexpr auto checksum(const std::uint8_t* p, const std::uint_fast8_t len) -> std::uint8_t;
17+
18+
constexpr auto checksum(const std::uint8_t* p, const std::uint_fast8_t len) -> std::uint8_t
1619
{
17-
std::uint_fast8_t sum = UINT8_C(0);
20+
std::uint_fast8_t sum { UINT8_C(0) };
1821

19-
for(std::uint_fast8_t i = UINT8_C(0); i < len; i++)
22+
for(std::uint_fast8_t i = UINT8_C(0); i < len; ++i)
2023
{
2124
sum += *p;
25+
2226
++p;
2327
};
2428

25-
return sum;
29+
return static_cast<std::uint8_t>(~sum + UINT8_C(1));
2630
}
2731

28-
int main()
32+
auto main() -> int;
33+
34+
auto main() -> int
2935
{
30-
const std::uint8_t data[3U] = { 1U, 2U, 3U };
36+
const std::array<std::uint8_t, std::size_t { UINT8_C(3) }> data =
37+
{{
38+
UINT8_C(1),
39+
UINT8_C(2),
40+
UINT8_C(3)
41+
}};
3142

32-
const std::uint8_t sum = checksum(data, 3U);
43+
const std::uint8_t sum { checksum(data.data(), static_cast<std::uint8_t>(data.size())) };
3344

34-
// 6
35-
std::cout << "sum: " << static_cast<std::uint32_t>(sum) << std::endl;
45+
// 250
46+
std::cout << "sum: " << static_cast<unsigned>(sum) << std::endl;
3647
}

0 commit comments

Comments
 (0)