COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
EnumValue.h
Go to the documentation of this file.
1//
2// Copyright 2014 - 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
12
13#pragma once
14
15#include "comms/field/basic/EnumValue.h"
16#include "comms/field/details/AdaptBasicField.h"
17#include "comms/field/details/OptionsParser.h"
18#include "comms/options.h"
19
20#include <cstddef>
21#include <type_traits>
22#include <utility>
23
24namespace comms
25{
26namespace field
27{
28
77template <typename TFieldBase, typename TEnum, typename... TOptions>
78class EnumValue : public details::AdaptBasicFieldT<basic::EnumValue<TFieldBase, TEnum>, TOptions...>
79{
80 using BaseImpl = details::AdaptBasicFieldT<basic::EnumValue<TFieldBase, TEnum>, TOptions...>;
81 static_assert(std::is_enum<TEnum>::value, "TEnum must be enum type");
82
83public:
85 using FieldBase = TFieldBase;
86
88 using Endian = typename BaseImpl::Endian;
89
91 using VersionType = typename BaseImpl::VersionType;
92
94 using ParsedOptions = details::OptionsParser<TOptions...>;
95
97 using CommsTag = typename BaseImpl::CommsTag;
98
101 using ValueType = typename BaseImpl::ValueType;
102
106 using FieldType = typename ParsedOptions::FieldType;
107
109 EnumValue() = default;
110
112 explicit EnumValue(const ValueType& val)
113 : BaseImpl(val)
114 {
115 }
116
118 EnumValue(const EnumValue&) = default;
119
121 ~EnumValue() noexcept = default;
122
124 EnumValue& operator=(const EnumValue&) = default;
125
128 static constexpr bool hasFailOnInvalid()
129 {
130 return ParsedOptions::HasFailOnInvalid;
131 }
132
135 static constexpr bool hasIgnoreInvalid()
136 {
137 return ParsedOptions::HasIgnoreInvalid;
138 }
139
142 static constexpr bool hasEmptySerialization()
143 {
144 return ParsedOptions::HasEmptySerialization;
145 }
146
149 static constexpr bool hasFieldType()
150 {
151 return ParsedOptions::HasFieldType;
152 }
153
156 static constexpr bool hasFixedValue()
157 {
158 return ParsedOptions::HasFixedValue;
159 }
160
163 static constexpr bool hasName()
164 {
165 return ParsedOptions::HasName;
166 }
167
169 const ValueType& value() const
170 {
171 return BaseImpl::value();
172 }
173
176 {
177 return BaseImpl::value();
178 }
179
182 const ValueType& getValue() const
183 {
184 return BaseImpl::getValue();
185 }
186
189 template <typename U>
190 void setValue(U&& val)
191 {
192 BaseImpl::setValue(std::forward<U>(val));
193 }
194
197 constexpr std::size_t length() const
198 {
199 return BaseImpl::length();
200 }
201
204 static constexpr std::size_t minLength()
205 {
206 return BaseImpl::minLength();
207 }
208
211 static constexpr std::size_t maxLength()
212 {
213 return BaseImpl::maxLength();
214 }
215
221 template <typename TIter>
222 ErrorStatus read(TIter& iter, std::size_t size)
223 {
224 return BaseImpl::read(iter, size);
225 }
226
229 static constexpr bool hasReadNoStatus()
230 {
231 return BaseImpl::hasReadNoStatus();
232 }
233
239 template <typename TIter>
240 void readNoStatus(TIter& iter)
241 {
242 BaseImpl::readNoStatus(iter);
243 }
244
246 bool canWrite() const
247 {
248 return BaseImpl::canWrite();
249 }
250
256 template <typename TIter>
257 ErrorStatus write(TIter& iter, std::size_t size) const
258 {
259 return BaseImpl::write(iter, size);
260 }
261
264 static constexpr bool hasWriteNoStatus()
265 {
266 return BaseImpl::hasWriteNoStatus();
267 }
268
274 template <typename TIter>
275 void writeNoStatus(TIter& iter) const
276 {
277 BaseImpl::writeNoStatus(iter);
278 }
279
281 bool valid() const
282 {
283 return BaseImpl::valid();
284 }
285
288 bool refresh()
289 {
290 return BaseImpl::refresh();
291 }
292
294 static constexpr bool isVersionDependent()
295 {
296 return ParsedOptions::HasCustomVersionUpdate || BaseImpl::isVersionDependent();
297 }
298
300 static constexpr bool hasNonDefaultRefresh()
301 {
302 return BaseImpl::hasNonDefaultRefresh();
303 }
304
308 {
309 return BaseImpl::getVersion();
310 }
311
315 {
316 return BaseImpl::setVersion(version);
317 }
318
326 void setForcedLength(int len)
327 {
328 BaseImpl::setForcedLength(len);
329 }
330
333 int getForcedLength() const
334 {
335 return BaseImpl::getForcedLength();
336 }
337
338protected:
339 using BaseImpl::readData;
340 using BaseImpl::writeData;
341private:
342 static_assert(!ParsedOptions::HasSequenceElemLengthForcing,
343 "comms::option::def::SequenceElemLengthForcingEnabled option is not applicable to EnumValue field");
344 static_assert(!ParsedOptions::HasSequenceSizeForcing,
345 "comms::option::def::SequenceSizeForcingEnabled option is not applicable to EnumValue field");
346 static_assert(!ParsedOptions::HasSequenceLengthForcing,
347 "comms::option::def::SequenceLengthForcingEnabled option is not applicable to EnumValue field");
348 static_assert(!ParsedOptions::HasSequenceFixedSize,
349 "comms::option::def::SequenceFixedSize option is not applicable to EnumValue field");
350 static_assert(!ParsedOptions::HasSequenceFixedSizeUseFixedSizeStorage,
351 "comms::option::app::SequenceFixedSizeUseFixedSizeStorage option is not applicable to EnumValue field");
352 static_assert(!ParsedOptions::HasSequenceSizeFieldPrefix,
353 "comms::option::def::SequenceSizeFieldPrefix option is not applicable to EnumValue field");
354 static_assert(!ParsedOptions::HasSequenceSerLengthFieldPrefix,
355 "comms::option::def::SequenceSerLengthFieldPrefix option is not applicable to EnumValue field");
356 static_assert(!ParsedOptions::HasSequenceElemSerLengthFieldPrefix,
357 "comms::option::def::SequenceElemSerLengthFieldPrefix option is not applicable to EnumValue field");
358 static_assert(!ParsedOptions::HasSequenceElemFixedSerLengthFieldPrefix,
359 "comms::option::def::SequenceElemSerLengthFixedFieldPrefix option is not applicable to EnumValue field");
360 static_assert(!ParsedOptions::HasSequenceTrailingFieldSuffix,
361 "comms::option::def::SequenceTrailingFieldSuffix option is not applicable to EnumValue field");
362 static_assert(!ParsedOptions::HasSequenceTerminationFieldSuffix,
363 "comms::option::def::SequenceTerminationFieldSuffix option is not applicable to EnumValue field");
364 static_assert(!ParsedOptions::HasFixedSizeStorage,
365 "comms::option::app::FixedSizeStorage option is not applicable to EnumValue field");
366 static_assert(!ParsedOptions::HasCustomStorageType,
367 "comms::option::app::CustomStorageType option is not applicable to EnumValue field");
368 static_assert(!ParsedOptions::HasScalingRatio,
369 "comms::option::def::ScalingRatio option is not applicable to EnumValue field");
370 static_assert(!ParsedOptions::HasUnits,
371 "comms::option::def::Units option is not applicable to EnumValue field");
372 static_assert(!ParsedOptions::HasOrigDataView,
373 "comms::option::app::OrigDataView option is not applicable to EnumValue field");
374 static_assert(!ParsedOptions::HasVersionsRange,
375 "comms::option::def::ExistsBetweenVersions (or similar) option is not applicable to EnumValue field");
376 static_assert(!ParsedOptions::HasMissingOnReadFail,
377 "comms::option::def::MissingOnReadFail option is not applicable to EnumValue field");
378 static_assert(!ParsedOptions::HasMissingOnInvalid,
379 "comms::option::def::MissingOnInvalid option is not applicable to EnumValue field");
380};
381
382// Implementation
383
389template <typename TFieldBase, typename TEnum, typename... TOptions>
392 const EnumValue<TFieldBase, TEnum, TOptions...>& field2) noexcept
393{
394 return field1.value() == field2.value();
395}
396
402template <typename TFieldBase, typename TEnum, typename... TOptions>
405 const EnumValue<TFieldBase, TEnum, TOptions...>& field2) noexcept
406{
407 return field1.value() != field2.value();
408}
409
415template <typename TFieldBase, typename TEnum, typename... TOptions>
418 const EnumValue<TFieldBase, TEnum, TOptions...>& field2) noexcept
419{
420 return field1.value() < field2.value();
421}
422
428template <typename T>
429constexpr bool isEnumValue()
430{
431 return std::is_same<typename T::CommsTag, tag::Enum>::value;
432}
433
437template <typename TFieldBase, typename TEnum, typename... TOptions>
438inline
439EnumValue<TFieldBase, TEnum, TOptions...>&
441{
442 return field;
443}
444
448template <typename TFieldBase, typename TEnum, typename... TOptions>
449inline
450const EnumValue<TFieldBase, TEnum, TOptions...>&
452{
453 return field;
454}
455
456} // namespace field
457
458} // namespace comms
459
Enumerator value field.
Definition EnumValue.h:79
ValueType & value()
Get access to enum value storage.
Definition EnumValue.h:175
constexpr bool isEnumValue()
Compile time check function of whether a provided type is any variant of comms::field::EnumValue.
Definition EnumValue.h:429
bool operator!=(const EnumValue< TFieldBase, TEnum, TOptions... > &field1, const EnumValue< TFieldBase, TEnum, TOptions... > &field2) noexcept
Non-equality comparison operator.
Definition EnumValue.h:403
void writeNoStatus(TIter &iter) const
Write current field value to output data sequence without error check and status report.
Definition EnumValue.h:275
typename BaseImpl::VersionType VersionType
Version type.
Definition EnumValue.h:91
static constexpr std::size_t maxLength()
Get maximal length that is required to serialise field of this type.
Definition EnumValue.h:211
const EnumValue< TFieldBase, TEnum, TOptions... > & toFieldBase(const EnumValue< TFieldBase, TEnum, TOptions... > &field)
Upcast type of the field definition to its parent comms::field::EnumValue type in order to have acces...
Definition EnumValue.h:451
VersionType getVersion() const
Get version of the field.
Definition EnumValue.h:307
static constexpr bool hasNonDefaultRefresh()
Compile time check if this class has non-default refresh functionality.
Definition EnumValue.h:300
bool valid() const
Check validity of the field value.
Definition EnumValue.h:281
void readNoStatus(TIter &iter)
Read field value from input data sequence without error check and status report.
Definition EnumValue.h:240
typename BaseImpl::CommsTag CommsTag
Tag indicating type of the field.
Definition EnumValue.h:97
void setValue(U &&val)
Set value.
Definition EnumValue.h:190
const ValueType & getValue() const
Get value.
Definition EnumValue.h:182
static constexpr bool hasReadNoStatus()
Compile time check of whether the field has proper readNoStatus() member function.
Definition EnumValue.h:229
EnumValue(const EnumValue &)=default
Copy constructor.
bool operator<(const EnumValue< TFieldBase, TEnum, TOptions... > &field1, const EnumValue< TFieldBase, TEnum, TOptions... > &field2) noexcept
Equivalence comparison operator.
Definition EnumValue.h:416
TFieldBase FieldBase
Base class provided in the first template parameter.
Definition EnumValue.h:85
static constexpr bool hasFixedValue()
Compile time inquiry of whether comms::option::def::FixedValue option has been used.
Definition EnumValue.h:156
static constexpr bool hasIgnoreInvalid()
Compile time inquiry of whether comms::option::def::IgnoreInvalid option has been used.
Definition EnumValue.h:135
int getForcedLength() const
Get forced serialization length.
Definition EnumValue.h:333
ErrorStatus read(TIter &iter, std::size_t size)
Read field value from input data sequence.
Definition EnumValue.h:222
constexpr std::size_t length() const
Get length required to serialise the current field value.
Definition EnumValue.h:197
bool refresh()
Refresh the field's value.
Definition EnumValue.h:288
typename ParsedOptions::FieldType FieldType
Type of actual extending field specified via comms::option::def::FieldType.
Definition EnumValue.h:106
static constexpr bool hasWriteNoStatus()
Compile time check of whether the field has proper writeNoStatus() member function.
Definition EnumValue.h:264
static constexpr bool hasEmptySerialization()
Compile time inquiry of whether comms::option::def::EmptySerialization option has been used.
Definition EnumValue.h:142
EnumValue(const ValueType &val)
Constructor.
Definition EnumValue.h:112
ErrorStatus write(TIter &iter, std::size_t size) const
Write current field value to output data sequence.
Definition EnumValue.h:257
typename BaseImpl::ValueType ValueType
Type of underlying enum value.
Definition EnumValue.h:101
static constexpr bool isVersionDependent()
Compile time check if this class is version dependent.
Definition EnumValue.h:294
static constexpr bool hasFieldType()
Compile time inquiry of whether comms::option::def::FieldType option has been used.
Definition EnumValue.h:149
static constexpr std::size_t minLength()
Get minimal length that is required to serialise field of this type.
Definition EnumValue.h:204
~EnumValue() noexcept=default
Destructor.
void setForcedLength(int len)
Force serialization length of the field.
Definition EnumValue.h:326
details::OptionsParser< TOptions... > ParsedOptions
All the options provided to this class bundled into struct.
Definition EnumValue.h:94
EnumValue()=default
Default constructor.
bool operator==(const EnumValue< TFieldBase, TEnum, TOptions... > &field1, const EnumValue< TFieldBase, TEnum, TOptions... > &field2) noexcept
Equality comparison operator.
Definition EnumValue.h:390
bool canWrite() const
Check of whether the field has a consistent value for writing.
Definition EnumValue.h:246
EnumValue< TFieldBase, TEnum, TOptions... > & toFieldBase(EnumValue< TFieldBase, TEnum, TOptions... > &field)
Upcast type of the field definition to its parent comms::field::EnumValue type in order to have acces...
Definition EnumValue.h:440
static constexpr bool hasFailOnInvalid()
Compile time inquiry of whether comms::option::def::FailOnInvalid option has been used.
Definition EnumValue.h:128
static constexpr bool hasName()
Compile time inquiry of whether comms::option::def::HasName option has been used.
Definition EnumValue.h:163
typename BaseImpl::Endian Endian
Endian used for serialisation.
Definition EnumValue.h:88
bool setVersion(VersionType version)
Default implementation of version update.
Definition EnumValue.h:314
const ValueType & value() const
Get access to enum value storage.
Definition EnumValue.h:169
Main namespace for all classes / functions of COMMS library.
ErrorStatus
Error statuses reported by the Communication module.
Definition ErrorStatus.h:19
constexpr unsigned version()
Version of the COMMS library as single numeric value.
Definition version.h:66
Contains definition of all the options used by the COMMS library.