COMMS
Template library intended to help with implementation of communication protocols.
BasicSum.h
Go to the documentation of this file.
1 //
2 // Copyright 2015 - 2024 (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 
15 namespace comms
16 {
17 
18 namespace protocol
19 {
20 
21 namespace checksum
22 {
23 
30 template <typename TResult = std::uint8_t, TResult TInitValue = 0>
31 class BasicSum
32 {
33 public:
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 protocol
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.