COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
SequenceLengthForcing.h
1//
2// Copyright 2017 - 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
8#pragma once
9
10#include <limits>
11#include <algorithm>
12
13#include "comms/Assert.h"
14#include "comms/ErrorStatus.h"
15
16namespace comms
17{
18
19namespace field
20{
21
22namespace adapter
23{
24
25template <typename TBase>
26class SequenceLengthForcing : public TBase
27{
28 using BaseImpl = TBase;
29public:
30 using ValueType = typename BaseImpl::ValueType;
31 using ElementType = typename BaseImpl::ElementType;
32
33 SequenceLengthForcing() = default;
34
35 explicit SequenceLengthForcing(const ValueType& val)
36 : BaseImpl(val)
37 {
38 }
39
40 explicit SequenceLengthForcing(ValueType&& val)
41 : BaseImpl(std::move(val))
42 {
43 }
44
45 SequenceLengthForcing(const SequenceLengthForcing&) = default;
46 SequenceLengthForcing(SequenceLengthForcing&&) = default;
47 SequenceLengthForcing& operator=(const SequenceLengthForcing&) = default;
48 SequenceLengthForcing& operator=(SequenceLengthForcing&&) = default;
49
50 void forceReadLength(std::size_t val)
51 {
52 COMMS_ASSERT(val != Cleared);
53 forced_ = val;
54 }
55
56 void clearReadLengthForcing()
57 {
58 forced_ = Cleared;
59 }
60
61 template <typename TIter>
62 comms::ErrorStatus read(TIter& iter, std::size_t len)
63 {
64 if (forced_ == Cleared) {
65 return BaseImpl::read(iter, len);
66 }
67
68 if (len < forced_) {
70 }
71
72 return BaseImpl::read(iter, forced_);
73 }
74
75 template <typename TIter>
76 ErrorStatus readN(std::size_t count, TIter& iter, std::size_t& len)
77 {
78 if (forced_ == Cleared) {
79 return BaseImpl::read(iter, len);
80 }
81
82 if (len < forced_) {
84 }
85
86 return BaseImpl::readN(count, iter, forced_);
87 }
88
89 static constexpr bool hasReadNoStatus()
90 {
91 return false;
92 }
93
94 template <typename TIter>
95 void readNoStatus(TIter& iter) = delete;
96
97 template <typename TIter>
98 void readNoStatusN(std::size_t count, TIter& iter) = delete;
99
100
101private:
102 static const std::size_t Cleared = std::numeric_limits<std::size_t>::max();
103 std::size_t forced_ = Cleared;
104};
105
106} // namespace adapter
107
108} // namespace field
109
110} // namespace comms
111
112
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:17
STL namespace.