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