COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
SequenceLengthForcing.h
1//
2// Copyright 2017 - 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 SequenceLengthForcing : public TBase
30{
31 using BaseImpl = TBase;
32public:
33 using ValueType = typename BaseImpl::ValueType;
34 using ElementType = typename BaseImpl::ElementType;
35
36 SequenceLengthForcing() = default;
37
38 explicit SequenceLengthForcing(const ValueType& val)
39 : BaseImpl(val)
40 {
41 }
42
43 explicit SequenceLengthForcing(ValueType&& val)
44 : BaseImpl(std::move(val))
45 {
46 }
47
48 SequenceLengthForcing(const SequenceLengthForcing&) = default;
49 SequenceLengthForcing(SequenceLengthForcing&&) = default;
50 SequenceLengthForcing& operator=(const SequenceLengthForcing&) = default;
51 SequenceLengthForcing& operator=(SequenceLengthForcing&&) = default;
52
53 void forceReadLength(std::size_t val)
54 {
55 COMMS_ASSERT(val != Cleared);
56 m_forced = val;
57 }
58
59 void clearReadLengthForcing()
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 if (len < m_forced) {
73 }
74
75 return BaseImpl::read(iter, m_forced);
76 }
77
78 template <typename TIter>
79 ErrorStatus readN(std::size_t count, TIter& iter, std::size_t& len)
80 {
81 if (m_forced == Cleared) {
82 return BaseImpl::read(iter, len);
83 }
84
85 if (len < m_forced) {
87 }
88
89 return BaseImpl::readN(count, iter, m_forced);
90 }
91
92 static constexpr bool hasReadNoStatus()
93 {
94 return false;
95 }
96
97 template <typename TIter>
98 void readNoStatus(TIter& iter) = delete;
99
100 template <typename TIter>
101 void readNoStatusN(std::size_t count, TIter& iter) = delete;
102
103private:
104 static const std::size_t Cleared = std::numeric_limits<std::size_t>::max();
105 std::size_t m_forced = Cleared;
106};
107
108} // namespace adapter
109
110} // namespace field
111
112} // namespace comms
113
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.