COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
SerOffset.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/ErrorStatus.h"
13#include "comms/util/access.h"
14
15#include <cstddef>
16#include <utility>
17
18namespace comms
19{
20
21namespace field
22{
23
24namespace adapter
25{
26
27template <long long int TOffset, typename TBase>
28class SerOffset : public TBase
29{
30 using BaseImpl = TBase;
31 static const auto Offset = TOffset;
32public:
33
34 using ValueType = typename BaseImpl::ValueType;
35 using SerialisedType = typename BaseImpl::SerialisedType;
36 using Endian = typename BaseImpl::Endian;
37
38 SerOffset() = default;
39
40 explicit SerOffset(const ValueType& val)
41 : BaseImpl(val)
42 {
43 }
44
45 explicit SerOffset(ValueType&& val)
46 : BaseImpl(std::move(val))
47 {
48 }
49
50 SerOffset(const SerOffset&) = default;
51 SerOffset(SerOffset&&) = default;
52 SerOffset& operator=(const SerOffset&) = default;
53 SerOffset& operator=(SerOffset&&) = default;
54
55 template <typename TIter>
56 ErrorStatus read(TIter& iter, std::size_t size)
57 {
58 if (size < BaseImpl::length()) {
60 }
61
62 readNoStatus(iter);
64 }
65
66 template <typename TIter>
67 void readNoStatus(TIter& iter)
68 {
69 auto serialisedValue =
70 comms::util::readData<SerialisedType>(iter, Endian());
71 BaseImpl::setValue(fromSerialised(serialisedValue));
72 }
73
74 template <typename TIter>
75 ErrorStatus write(TIter& iter, std::size_t size) const
76 {
77 if (size < BaseImpl::length()) {
79 }
80
81 writeNoStatus(iter);
83 }
84
85 template <typename TIter>
86 void writeNoStatus(TIter& iter) const
87 {
88 comms::util::writeData(toSerialised(BaseImpl::getValue()), iter, Endian());
89 }
90
91 static constexpr SerialisedType toSerialised(ValueType val)
92 {
93 return adjustToSerialised(BaseImpl::toSerialised(val));
94 }
95
96 static constexpr ValueType fromSerialised(SerialisedType val)
97 {
98 return BaseImpl::fromSerialised(adjustFromSerialised(val));
99 }
100
101private:
102 static SerialisedType adjustToSerialised(SerialisedType val)
103 {
104 return static_cast<SerialisedType>(Offset + val);
105 }
106
107 static SerialisedType adjustFromSerialised(SerialisedType val)
108 {
109 return static_cast<SerialisedType>((-Offset) + val);
110 }
111};
112
113} // namespace adapter
114
115} // namespace field
116
117} // namespace comms
118
This file contain definition of error statuses used by comms module.
Contains functions for raw data access / (de)serialization.
comms::option::def::Endian< TEndian > Endian
Same as comms::option::def::Endian.
Definition options.h:1473
void writeData(T value, TIter &iter, const traits::endian::Big &endian)
Same as writeBig<T, TIter>()
Definition access.h:707
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.