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