COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
IntValue.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/field/tag.h"
14
15#include <cstddef>
16#include <ratio>
17#include <type_traits>
18#include <utility>
19
20namespace comms
21{
22
23namespace field
24{
25
26namespace basic
27{
28
29template <typename TFieldBase, typename T>
30class IntValue : public TFieldBase
31{
32 static_assert(std::is_integral<T>::value, "T must be integral value");
33
34 using BaseImpl = TFieldBase;
35public:
36
37 using ValueType = T;
38 using SerialisedType = ValueType;
39 using ScalingRatio = std::ratio<1, 1>;
40 using CommsTag = comms::field::tag::Int;
41 using DisplayOffsetType = typename std::make_signed<ValueType>::type;
42
43 IntValue() = default;
44
45 explicit IntValue(ValueType val)
46 : m_value(val)
47 {
48 }
49
50 IntValue(const IntValue&) = default;
51 IntValue(IntValue&&) = default;
52 ~IntValue() noexcept = default;
53
54 IntValue& operator=(const IntValue&) = default;
55 IntValue& operator=(IntValue&&) = default;
56
57 const ValueType& value() const
58 {
59 return m_value;
60 }
61
62 ValueType& value()
63 {
64 return m_value;
65 }
66
67 const ValueType& getValue() const
68 {
69 return value();
70 }
71
72 template <typename U>
73 void setValue(U&& val)
74 {
75 value() = static_cast<ValueType>(std::forward<U>(val));
76 }
77
78 static constexpr std::size_t length()
79 {
80 return sizeof(SerialisedType);
81 }
82
83 static constexpr std::size_t minLength()
84 {
85 return length();
86 }
87
88 static constexpr std::size_t maxLength()
89 {
90 return length();
91 }
92
93 static constexpr SerialisedType toSerialised(ValueType val)
94 {
95 return static_cast<SerialisedType>(val);
96 }
97
98 static constexpr ValueType fromSerialised(SerialisedType val)
99 {
100 return static_cast<ValueType>(val);
101 }
102
103 template <typename TIter>
104 ErrorStatus read(TIter& iter, std::size_t size)
105 {
106 if (size < length()) {
108 }
109
110 readNoStatus(iter);
112 }
113
114 template <typename TIter>
115 void readNoStatus(TIter& iter)
116 {
117 auto serialisedValue =
118 BaseImpl::template readData<SerialisedType>(iter);
119 m_value = fromSerialised(serialisedValue);
120 }
121
122 template <typename TIter>
123 ErrorStatus write(TIter& iter, std::size_t size) const
124 {
125 if (size < length()) {
127 }
128
129 writeNoStatus(iter);
131 }
132
133 template <typename TIter>
134 void writeNoStatus(TIter& iter) const
135 {
136 BaseImpl::writeData(toSerialised(m_value), iter);
137 }
138
139 static constexpr DisplayOffsetType displayOffset()
140 {
141 return static_cast<DisplayOffsetType>(0);
142 }
143
144private:
145 ValueType m_value = static_cast<ValueType>(0);
146};
147
148} // namespace basic
149
150} // namespace field
151
152} // namespace comms
153
This file contain definition of error statuses used by comms module.
Contains definition of various tag classes.
comms::option::def::ScalingRatio< TNum, TDenom > ScalingRatio
Same as comms::option::def::ScalingRatio.
Definition options.h:1557
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.