|
1 | 1 | ///////////////////////////////////////////////////////////////// |
2 | | -// Copyright Christopher Kormanyos 2018 - 2023. |
| 2 | +// Copyright Christopher Kormanyos 2018 - 2025. |
3 | 3 | // Distributed under the Boost Software License, |
4 | 4 | // Version 1.0. (See accompanying file LICENSE_1_0.txt |
5 | 5 | // or copy at http://www.boost.org/LICENSE_1_0.txt) |
6 | 6 | // |
7 | 7 |
|
| 8 | +// Repaired in: https://github.com/ckormanyos/real-time-cpp/issues/658 |
| 9 | + |
8 | 10 | // chapter03_21-001_span.cpp |
9 | 11 |
|
| 12 | +#include <algorithm> |
10 | 13 | #include <cstdint> |
| 14 | +#include <iomanip> |
11 | 15 | #include <iostream> |
12 | 16 | #include <iterator> |
13 | 17 | #include <span> |
| 18 | +#include <sstream> |
14 | 19 |
|
15 | | -void clear_buffer(std::span<std::uint8_t> pb) |
| 20 | +namespace |
16 | 21 | { |
17 | | - for(auto u : pb) |
| 22 | + void clear_buffer(std::span<std::uint8_t> buf) |
18 | 23 | { |
19 | | - u = static_cast<std::uint8_t>(UINT8_C(0)); |
20 | | - |
21 | | - static_cast<void>(u); |
| 24 | + for(auto& next_byte : buf) |
| 25 | + { |
| 26 | + next_byte = static_cast<std::uint8_t>(UINT8_C(0)); |
| 27 | + } |
22 | 28 | } |
23 | 29 | } |
24 | 30 |
|
25 | | -#define LEGACY_BUFFER_LENGTH 64U |
| 31 | +#define LEGACY_BUF_LEN 64U |
26 | 32 |
|
27 | | -std::uint8_t legacy_buffer[LEGACY_BUFFER_LENGTH]; |
| 33 | +std::uint8_t legacy_buffer[LEGACY_BUF_LEN]; |
28 | 34 |
|
29 | 35 | void do_something() |
30 | 36 | { |
31 | | - clear_buffer(legacy_buffer); |
| 37 | + std::span<std::uint8_t> buf_span(legacy_buffer); |
| 38 | + |
| 39 | + // First off, fill the legacy buffer with a pattern. |
| 40 | + std::fill(buf_span.begin(), buf_span.end(), UINT8_C(0x5A)); |
| 41 | + |
| 42 | + std::stringstream strm { }; |
| 43 | + |
| 44 | + // Ensure that the buffer has the pattern. |
| 45 | + strm << "filled: " << std::hex << std::uppercase << std::setw(std::streamsize { 2 }) << std::setfill('0') << unsigned(buf_span.back()); |
| 46 | + |
| 47 | + // Now clear the buffer and the buffer has been cleared to 0. |
| 48 | + clear_buffer(buf_span); |
| 49 | + |
| 50 | + strm << "\ncleared: " << std::hex << std::uppercase << std::setw(std::streamsize { 2 }) << std::setfill('0') << unsigned(buf_span.back()); |
| 51 | + |
| 52 | + std::cout << strm.str() << std::endl; |
32 | 53 | } |
33 | 54 |
|
34 | 55 | int main() |
35 | 56 | { |
36 | | - // 0 |
37 | | - std::cout << unsigned(*(std::cend(legacy_buffer) - 1U)) << std::endl; |
| 57 | + do_something(); |
38 | 58 | } |
0 commit comments