COMMS
Template library intended to help with implementation of communication protocols.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BasicSum.h
Go to the documentation of this file.
1//
2// Copyright 2015 - 2025 (C). Alex Robenko. All rights reserved.
3//
4// This Source Code Form is subject to the terms of the Mozilla Public
5// License, v. 2.0. If a copy of the MPL was not distributed with this
6// file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
10
11#pragma once
12
13#include <cstdint>
14
15namespace comms
16{
17
18namespace frame
19{
20
21namespace checksum
22{
23
30template <typename TResult = std::uint8_t, TResult TInitValue = 0>
32{
33public:
39 template <typename TIter>
40 TResult operator()(TIter& iter, std::size_t len) const
41 {
42 using ByteType = typename std::make_unsigned<
43 typename std::decay<decltype(*iter)>::type
44 >::type;
45
46 TResult checksum = TInitValue;
47 for (auto idx = 0U; idx < len; ++idx) {
48 checksum = static_cast<TResult>(checksum + static_cast<ByteType>(*iter));
49 ++iter;
50 }
51 return checksum;
52 }
53};
54
55} // namespace checksum
56
57} // namespace frame
58
59} // namespace comms
60
61
62
Summary of all bytes checksum calculator.
Definition BasicSum.h:32
TResult operator()(TIter &iter, std::size_t len) const
Operator that is invoked to calculate the checksum value.
Definition BasicSum.h:40
Main namespace for all classes / functions of COMMS library.