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