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