COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
IntValue.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 <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
37 IntValue() = default;
38
39 explicit IntValue(ValueType val)
40 : value_(val)
41 {
42 }
43
44 IntValue(const IntValue&) = default;
45 IntValue(IntValue&&) = default;
46 ~IntValue() noexcept = default;
47
48 IntValue& operator=(const IntValue&) = default;
49 IntValue& operator=(IntValue&&) = default;
50
51 const ValueType& value() const
52 {
53 return value_;
54 }
55
56 ValueType& value()
57 {
58 return value_;
59 }
60
61 const ValueType& getValue() const
62 {
63 return value();
64 }
65
66 template <typename U>
67 void setValue(U&& val)
68 {
69 value() = static_cast<ValueType>(std::forward<U>(val));
70 }
71
72 static constexpr std::size_t length()
73 {
74 return sizeof(SerialisedType);
75 }
76
77 static constexpr std::size_t minLength()
78 {
79 return length();
80 }
81
82 static constexpr std::size_t maxLength()
83 {
84 return length();
85 }
86
87 static constexpr SerialisedType toSerialised(ValueType val)
88 {
89 return static_cast<SerialisedType>(val);
90 }
91
92 static constexpr ValueType fromSerialised(SerialisedType val)
93 {
94 return static_cast<ValueType>(val);
95 }
96
97 template <typename TIter>
98 ErrorStatus read(TIter& iter, std::size_t size)
99 {
100 if (size < length()) {
102 }
103
104 readNoStatus(iter);
106 }
107
108 template <typename TIter>
109 void readNoStatus(TIter& iter)
110 {
111 auto serialisedValue =
112 BaseImpl::template readData<SerialisedType>(iter);
113 value_ = fromSerialised(serialisedValue);
114 }
115
116 template <typename TIter>
117 ErrorStatus write(TIter& iter, std::size_t size) const
118 {
119 if (size < length()) {
121 }
122
123 writeNoStatus(iter);
125 }
126
127 template <typename TIter>
128 void writeNoStatus(TIter& iter) const
129 {
130 BaseImpl::writeData(toSerialised(value_), iter);
131 }
132
133private:
134 ValueType value_ = static_cast<ValueType>(0);
135};
136
137} // namespace basic
138
139} // namespace field
140
141} // namespace comms
142
143
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:1504
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.