COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
SerOffset.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/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
51 template <typename TIter>
52 ErrorStatus read(TIter& iter, std::size_t size)
53 {
54 if (size < BaseImpl::length()) {
56 }
57
58 readNoStatus(iter);
60 }
61
62 template <typename TIter>
63 void readNoStatus(TIter& iter)
64 {
65 auto serialisedValue =
66 comms::util::readData<SerialisedType>(iter, Endian());
67 BaseImpl::setValue(fromSerialised(serialisedValue));
68 }
69
70 template <typename TIter>
71 ErrorStatus write(TIter& iter, std::size_t size) const
72 {
73 if (size < BaseImpl::length()) {
75 }
76
77 writeNoStatus(iter);
79 }
80
81 template <typename TIter>
82 void writeNoStatus(TIter& iter) const
83 {
84 comms::util::writeData(toSerialised(BaseImpl::getValue()), iter, Endian());
85 }
86
87 static constexpr SerialisedType toSerialised(ValueType val)
88 {
89 return adjustToSerialised(BaseImpl::toSerialised(val));
90 }
91
92 static constexpr ValueType fromSerialised(SerialisedType val)
93 {
94 return BaseImpl::fromSerialised(adjustFromSerialised(val));
95 }
96
97private:
98 static SerialisedType adjustToSerialised(SerialisedType val)
99 {
100 return static_cast<SerialisedType>(Offset + val);
101 }
102
103 static SerialisedType adjustFromSerialised(SerialisedType val)
104 {
105 return static_cast<SerialisedType>((-Offset) + val);
106 }
107};
108
109} // namespace adapter
110
111} // namespace field
112
113} // namespace comms
114
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:1438
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.