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