1 #ifndef MARNAV_NMEA_CHECKSUM_HPP
2 #define MARNAV_NMEA_CHECKSUM_HPP
11 /// Exception for cases where the checksum is wrong.
13 /// This exception carries the actual as well as the expected
14 /// checksum and will provide this information in the explanation.
15 class checksum_error
: public std::exception
18 checksum_error() = delete;
19 explicit checksum_error(uint8_t exp
, uint8_t act
);
20 checksum_error(const checksum_error
&) = default;
21 checksum_error(checksum_error
&&) = default;
23 checksum_error
& operator=(const checksum_error
&) = default;
24 checksum_error
& operator=(checksum_error
&&) = default;
26 const char * what() const noexcept override
{ return text_
; }
28 uint8_t expected() const noexcept
{ return expected_
; }
29 uint8_t actual() const noexcept
{ return actual_
; }
32 uint8_t expected_
= 0u;
37 /// Computes and returns the checksum of the specified range.
39 /// @tparam Iterator Iterator type which dereferences to a \c char.
40 /// @param[in] a The starting point to compute the checksum.
41 /// @param[in] b The ending point (exclusive) to compute the checksum.
42 /// @return The computed checksum.
44 template <class Iterator
>
45 uint8_t checksum(Iterator a
, Iterator b
) noexcept
48 std::for_each(a
, b
, [&sum
](char c
) { sum
^= static_cast<uint8_t>(c
); });
52 std::string
checksum_to_string(uint8_t sum
);