COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
SequenceSizeForcing.h
1//
2// Copyright 2015 - 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
10#pragma once
11
12#include "comms/Assert.h"
13#include "comms/ErrorStatus.h"
14
15#include <cstddef>
16#include <limits>
17#include <utility>
18
19namespace comms
20{
21
22namespace field
23{
24
25namespace adapter
26{
27
28template <typename TBase>
29class SequenceSizeForcing : public TBase
30{
31 using BaseImpl = TBase;
32public:
33 using ValueType = typename BaseImpl::ValueType;
34 using ElementType = typename BaseImpl::ElementType;
35
36 SequenceSizeForcing() = default;
37
38 explicit SequenceSizeForcing(const ValueType& val)
39 : BaseImpl(val)
40 {
41 }
42
43 explicit SequenceSizeForcing(ValueType&& val)
44 : BaseImpl(std::move(val))
45 {
46 }
47
48 SequenceSizeForcing(const SequenceSizeForcing&) = default;
49 SequenceSizeForcing(SequenceSizeForcing&&) = default;
50 SequenceSizeForcing& operator=(const SequenceSizeForcing&) = default;
51 SequenceSizeForcing& operator=(SequenceSizeForcing&&) = default;
52
53 void forceReadElemCount(std::size_t val)
54 {
55 COMMS_ASSERT(val != Cleared);
56 m_forced = val;
57 }
58
59 void clearReadElemCount()
60 {
61 m_forced = Cleared;
62 }
63
64 template <typename TIter>
65 comms::ErrorStatus read(TIter& iter, std::size_t len)
66 {
67 if (m_forced == Cleared) {
68 return BaseImpl::read(iter, len);
69 }
70
71 return BaseImpl::readN(m_forced, iter, len);
72 }
73
74 template <typename TIter>
75 ErrorStatus readN(std::size_t count, TIter& iter, std::size_t& len) = delete;
76
77 template <typename TIter>
78 void readNoStatus(TIter& iter)
79 {
80 if (m_forced == Cleared) {
81 BaseImpl::readNoStatus(iter);
82 return;
83 }
84
85 BaseImpl::readNoStatusN(m_forced, iter);
86 }
87
88 template <typename TIter>
89 void readNoStatusN(std::size_t count, TIter& iter) = delete;
90
91private:
92 static const std::size_t Cleared = std::numeric_limits<std::size_t>::max();
93 std::size_t m_forced = Cleared;
94};
95
96} // namespace adapter
97
98} // namespace field
99
100} // namespace comms
101
This file contains classes required for generic custom assertion functionality.
#define COMMS_ASSERT(expr)
Generic assert macro.
Definition Assert.h:170
This file contain definition of error statuses used by comms module.
Main namespace for all classes / functions of COMMS library.
ErrorStatus
Error statuses reported by the Communication module.
Definition ErrorStatus.h:19
STL namespace.