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