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