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