COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
BasicXor.h
Go to the documentation of this file.
1//
2// Copyright 2021 - 2026 (C). Alex Robenko. All rights reserved.
3//
4// SPDX-License-Identifier: MPL-2.0
5//
6// This Source Code Form is subject to the terms of the Mozilla Public
7// License, v. 2.0. If a copy of the MPL was not distributed with this
8// file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
12
13#pragma once
14
15#include <cstddef>
16#include <cstdint>
17#include <type_traits>
18
19namespace comms
20{
21
22namespace frame
23{
24
25namespace checksum
26{
27
34template <typename TResult = std::uint8_t, TResult TInitValue = 0>
36{
37public:
43 template <typename TIter>
44 TResult operator()(TIter& iter, std::size_t len) const
45 {
46 using ByteType = typename std::make_unsigned<
47 typename std::decay<decltype(*iter)>::type
48 >::type;
49
50 TResult checksum = TInitValue;
51 for (auto idx = 0U; idx < len; ++idx) {
52 checksum = static_cast<TResult>(checksum ^ static_cast<ByteType>(*iter));
53 ++iter;
54 }
55 return checksum;
56 }
57};
58
59} // namespace checksum
60
61} // namespace frame
62
63} // namespace comms
64
Exclusive OR (XOR) of all bytes checksum calculator.
Definition BasicXor.h:36
TResult operator()(TIter &iter, std::size_t len) const
Operator that is invoked to calculate the checksum value.
Definition BasicXor.h:44
Main namespace for all classes / functions of COMMS library.