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