COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
SequenceTrailingFieldSuffix.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 <utility>
17
18namespace comms
19{
20
21namespace field
22{
23
24namespace adapter
25{
26
27template <typename TTrailField, typename TBase>
28class SequenceTrailingFieldSuffix : public TBase
29{
30 using BaseImpl = TBase;
31 using TrailField = TTrailField;
32
33 static_assert(!TrailField::isVersionDependent(),
34 "Suffix fields must not be version dependent");
35
36public:
37 using ValueType = typename BaseImpl::ValueType;
38 using ElementType = typename BaseImpl::ElementType;
39
41
42 explicit SequenceTrailingFieldSuffix(const ValueType& val)
43 : BaseImpl(val)
44 {
45 }
46
47 explicit SequenceTrailingFieldSuffix(ValueType&& val)
48 : BaseImpl(std::move(val))
49 {
50 }
51
52 SequenceTrailingFieldSuffix(const SequenceTrailingFieldSuffix&) = default;
53 SequenceTrailingFieldSuffix(SequenceTrailingFieldSuffix&&) = default;
54 SequenceTrailingFieldSuffix& operator=(const SequenceTrailingFieldSuffix&) = default;
55 SequenceTrailingFieldSuffix& operator=(SequenceTrailingFieldSuffix&&) = default;
56
57 constexpr std::size_t length() const
58 {
59 return m_trailField.length() + BaseImpl::length();
60 }
61
62 static constexpr std::size_t minLength()
63 {
64 return TrailField::minLength() + BaseImpl::minLength();
65 }
66
67 static constexpr std::size_t maxLength()
68 {
69 return TrailField::maxLength() + BaseImpl::maxLength();
70 }
71
72 bool valid() const
73 {
74 return m_trailField.valid() && BaseImpl::valid();
75 }
76
77 template <typename TIter>
78 comms::ErrorStatus read(TIter& iter, std::size_t len)
79 {
80 auto es = BaseImpl::read(iter, len - TrailField::minLength());
82 return es;
83 }
84
85 return m_trailField.read(iter, len - BaseImpl::length());
86 }
87
88 static constexpr bool hasReadNoStatus()
89 {
90 return false;
91 }
92
93 template <typename TIter>
94 void readNoStatus(TIter& iter) = delete;
95
96 template <typename TIter>
97 comms::ErrorStatus write(TIter& iter, std::size_t len) const
98 {
99 auto trailLen = m_trailField.length();
100 auto es = BaseImpl::write(iter, len - trailLen);
101 if (es != comms::ErrorStatus::Success) {
102 return es;
103 }
104
105 return m_trailField.write(iter, trailLen);
106 }
107
108 template <typename TIter>
109 void writeNoStatus(TIter& iter) const
110 {
111 BaseImpl::writeNoStatus(iter);
112 m_trailField.writeNoStatus(iter);
113 }
114
115private:
116 TrailField m_trailField;
117};
118
119} // namespace adapter
120
121} // namespace field
122
123} // namespace comms
124
This file contains classes required for generic custom assertion functionality.
This file contain definition of error statuses used by comms module.
comms::option::def::SequenceTrailingFieldSuffix< TField > SequenceTrailingFieldSuffix
Same as comms::option::def::SequenceTrailingFieldSuffix.
Definition options.h:1590
Main namespace for all classes / functions of COMMS library.
ErrorStatus
Error statuses reported by the Communication module.
Definition ErrorStatus.h:19
@ Success
Used to indicate successful outcome of the operation.
STL namespace.