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 - 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
10
11#pragma once
12
13#include <type_traits>
14
15#include "comms/options.h"
16#include "details/OptionsParser.h"
17#include "basic/EnumValue.h"
18#include "details/AdaptBasicField.h"
19
20namespace comms
21{
22namespace field
23{
24
73template <typename TFieldBase, typename TEnum, typename... TOptions>
74class EnumValue : public details::AdaptBasicFieldT<basic::EnumValue<TFieldBase, TEnum>, TOptions...>
75{
76 using BaseImpl = details::AdaptBasicFieldT<basic::EnumValue<TFieldBase, TEnum>, TOptions...>;
77 static_assert(std::is_enum<TEnum>::value, "TEnum must be enum type");
78
79public:
81 using FieldBase = TFieldBase;
82
84 using Endian = typename BaseImpl::Endian;
85
87 using VersionType = typename BaseImpl::VersionType;
88
90 using ParsedOptions = details::OptionsParser<TOptions...>;
91
93 using CommsTag = typename BaseImpl::CommsTag;
94
97 using ValueType = typename BaseImpl::ValueType;
98
102 using FieldType = typename ParsedOptions::FieldType;
103
105 EnumValue() = default;
106
108 explicit EnumValue(const ValueType& val)
109 : BaseImpl(val)
110 {
111 }
112
114 EnumValue(const EnumValue&) = default;
115
117 ~EnumValue() noexcept = default;
118
120 EnumValue& operator=(const EnumValue&) = default;
121
124 static constexpr bool hasFailOnInvalid()
125 {
126 return ParsedOptions::HasFailOnInvalid;
127 }
128
131 static constexpr bool hasIgnoreInvalid()
132 {
133 return ParsedOptions::HasIgnoreInvalid;
134 }
135
138 static constexpr bool hasEmptySerialization()
139 {
140 return ParsedOptions::HasEmptySerialization;
141 }
142
145 static constexpr bool hasFieldType()
146 {
147 return ParsedOptions::HasFieldType;
148 }
149
152 static constexpr bool hasFixedValue()
153 {
154 return ParsedOptions::HasFixedValue;
155 }
156
159 static constexpr bool hasName()
160 {
161 return ParsedOptions::HasName;
162 }
163
165 const ValueType& value() const
166 {
167 return BaseImpl::value();
168 }
169
172 {
173 return BaseImpl::value();
174 }
175
178 const ValueType& getValue() const
179 {
180 return BaseImpl::getValue();
181 }
182
185 template <typename U>
186 void setValue(U&& val)
187 {
188 BaseImpl::setValue(std::forward<U>(val));
189 }
190
193 constexpr std::size_t length() const
194 {
195 return BaseImpl::length();
196 }
197
200 static constexpr std::size_t minLength()
201 {
202 return BaseImpl::minLength();
203 }
204
207 static constexpr std::size_t maxLength()
208 {
209 return BaseImpl::maxLength();
210 }
211
217 template <typename TIter>
218 ErrorStatus read(TIter& iter, std::size_t size)
219 {
220 return BaseImpl::read(iter, size);
221 }
222
225 static constexpr bool hasReadNoStatus()
226 {
227 return BaseImpl::hasReadNoStatus();
228 }
229
235 template <typename TIter>
236 void readNoStatus(TIter& iter)
237 {
238 BaseImpl::readNoStatus(iter);
239 }
240
242 bool canWrite() const
243 {
244 return BaseImpl::canWrite();
245 }
246
252 template <typename TIter>
253 ErrorStatus write(TIter& iter, std::size_t size) const
254 {
255 return BaseImpl::write(iter, size);
256 }
257
260 static constexpr bool hasWriteNoStatus()
261 {
262 return BaseImpl::hasWriteNoStatus();
263 }
264
270 template <typename TIter>
271 void writeNoStatus(TIter& iter) const
272 {
273 BaseImpl::writeNoStatus(iter);
274 }
275
277 bool valid() const
278 {
279 return BaseImpl::valid();
280 }
281
284 bool refresh()
285 {
286 return BaseImpl::refresh();
287 }
288
290 static constexpr bool isVersionDependent()
291 {
292 return ParsedOptions::HasCustomVersionUpdate || BaseImpl::isVersionDependent();
293 }
294
296 static constexpr bool hasNonDefaultRefresh()
297 {
298 return BaseImpl::hasNonDefaultRefresh();
299 }
300
304 {
305 return BaseImpl::getVersion();
306 }
307
311 {
312 return BaseImpl::setVersion(version);
313 }
314
322 void setForcedLength(int len)
323 {
324 BaseImpl::setForcedLength(len);
325 }
326
329 int getForcedLength() const
330 {
331 return BaseImpl::getForcedLength();
332 }
333
334protected:
335 using BaseImpl::readData;
336 using BaseImpl::writeData;
337private:
338 static_assert(!ParsedOptions::HasSequenceElemLengthForcing,
339 "comms::option::def::SequenceElemLengthForcingEnabled option is not applicable to EnumValue field");
340 static_assert(!ParsedOptions::HasSequenceSizeForcing,
341 "comms::option::def::SequenceSizeForcingEnabled option is not applicable to EnumValue field");
342 static_assert(!ParsedOptions::HasSequenceLengthForcing,
343 "comms::option::def::SequenceLengthForcingEnabled option is not applicable to EnumValue field");
344 static_assert(!ParsedOptions::HasSequenceFixedSize,
345 "comms::option::def::SequenceFixedSize option is not applicable to EnumValue field");
346 static_assert(!ParsedOptions::HasSequenceFixedSizeUseFixedSizeStorage,
347 "comms::option::app::SequenceFixedSizeUseFixedSizeStorage option is not applicable to EnumValue field");
348 static_assert(!ParsedOptions::HasSequenceSizeFieldPrefix,
349 "comms::option::def::SequenceSizeFieldPrefix option is not applicable to EnumValue field");
350 static_assert(!ParsedOptions::HasSequenceSerLengthFieldPrefix,
351 "comms::option::def::SequenceSerLengthFieldPrefix option is not applicable to EnumValue field");
352 static_assert(!ParsedOptions::HasSequenceElemSerLengthFieldPrefix,
353 "comms::option::def::SequenceElemSerLengthFieldPrefix option is not applicable to EnumValue field");
354 static_assert(!ParsedOptions::HasSequenceElemFixedSerLengthFieldPrefix,
355 "comms::option::def::SequenceElemSerLengthFixedFieldPrefix option is not applicable to EnumValue field");
356 static_assert(!ParsedOptions::HasSequenceTrailingFieldSuffix,
357 "comms::option::def::SequenceTrailingFieldSuffix option is not applicable to EnumValue field");
358 static_assert(!ParsedOptions::HasSequenceTerminationFieldSuffix,
359 "comms::option::def::SequenceTerminationFieldSuffix option is not applicable to EnumValue field");
360 static_assert(!ParsedOptions::HasFixedSizeStorage,
361 "comms::option::app::FixedSizeStorage option is not applicable to EnumValue field");
362 static_assert(!ParsedOptions::HasCustomStorageType,
363 "comms::option::app::CustomStorageType option is not applicable to EnumValue field");
364 static_assert(!ParsedOptions::HasScalingRatio,
365 "comms::option::def::ScalingRatio option is not applicable to EnumValue field");
366 static_assert(!ParsedOptions::HasUnits,
367 "comms::option::def::Units option is not applicable to EnumValue field");
368 static_assert(!ParsedOptions::HasOrigDataView,
369 "comms::option::app::OrigDataView option is not applicable to EnumValue field");
370 static_assert(!ParsedOptions::HasVersionsRange,
371 "comms::option::def::ExistsBetweenVersions (or similar) option is not applicable to EnumValue field");
372 static_assert(!ParsedOptions::HasMissingOnReadFail,
373 "comms::option::def::MissingOnReadFail option is not applicable to EnumValue field");
374 static_assert(!ParsedOptions::HasMissingOnInvalid,
375 "comms::option::def::MissingOnInvalid option is not applicable to EnumValue field");
376};
377
378// Implementation
379
385template <typename TFieldBase, typename TEnum, typename... TOptions>
388 const EnumValue<TFieldBase, TEnum, TOptions...>& field2) noexcept
389{
390 return field1.value() == field2.value();
391}
392
398template <typename TFieldBase, typename TEnum, typename... TOptions>
401 const EnumValue<TFieldBase, TEnum, TOptions...>& field2) noexcept
402{
403 return field1.value() != field2.value();
404}
405
411template <typename TFieldBase, typename TEnum, typename... TOptions>
414 const EnumValue<TFieldBase, TEnum, TOptions...>& field2) noexcept
415{
416 return field1.value() < field2.value();
417}
418
424template <typename T>
425constexpr bool isEnumValue()
426{
427 return std::is_same<typename T::CommsTag, tag::Enum>::value;
428}
429
433template <typename TFieldBase, typename TEnum, typename... TOptions>
434inline
435EnumValue<TFieldBase, TEnum, TOptions...>&
437{
438 return field;
439}
440
444template <typename TFieldBase, typename TEnum, typename... TOptions>
445inline
446const EnumValue<TFieldBase, TEnum, TOptions...>&
448{
449 return field;
450}
451
452} // namespace field
453
454} // namespace comms
455
456
457
458
459
Enumerator value field.
Definition EnumValue.h:75
ValueType & value()
Get access to enum value storage.
Definition EnumValue.h:171
constexpr bool isEnumValue()
Compile time check function of whether a provided type is any variant of comms::field::EnumValue.
Definition EnumValue.h:425
bool operator!=(const EnumValue< TFieldBase, TEnum, TOptions... > &field1, const EnumValue< TFieldBase, TEnum, TOptions... > &field2) noexcept
Non-equality comparison operator.
Definition EnumValue.h:399
void writeNoStatus(TIter &iter) const
Write current field value to output data sequence without error check and status report.
Definition EnumValue.h:271
typename BaseImpl::VersionType VersionType
Version type.
Definition EnumValue.h:87
static constexpr std::size_t maxLength()
Get maximal length that is required to serialise field of this type.
Definition EnumValue.h:207
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:447
VersionType getVersion() const
Get version of the field.
Definition EnumValue.h:303
static constexpr bool hasNonDefaultRefresh()
Compile time check if this class has non-default refresh functionality.
Definition EnumValue.h:296
bool valid() const
Check validity of the field value.
Definition EnumValue.h:277
void readNoStatus(TIter &iter)
Read field value from input data sequence without error check and status report.
Definition EnumValue.h:236
typename BaseImpl::CommsTag CommsTag
Tag indicating type of the field.
Definition EnumValue.h:93
void setValue(U &&val)
Set value.
Definition EnumValue.h:186
const ValueType & getValue() const
Get value.
Definition EnumValue.h:178
static constexpr bool hasReadNoStatus()
Compile time check of whether the field has proper readNoStatus() member function.
Definition EnumValue.h:225
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:412
TFieldBase FieldBase
Base class provided in the first template parameter.
Definition EnumValue.h:81
static constexpr bool hasFixedValue()
Compile time inquiry of whether comms::option::def::FixedValue option has been used.
Definition EnumValue.h:152
static constexpr bool hasIgnoreInvalid()
Compile time inquiry of whether comms::option::def::IgnoreInvalid option has been used.
Definition EnumValue.h:131
int getForcedLength() const
Get forced serialization length.
Definition EnumValue.h:329
ErrorStatus read(TIter &iter, std::size_t size)
Read field value from input data sequence.
Definition EnumValue.h:218
constexpr std::size_t length() const
Get length required to serialise the current field value.
Definition EnumValue.h:193
bool refresh()
Refresh the field's value.
Definition EnumValue.h:284
typename ParsedOptions::FieldType FieldType
Type of actual extending field specified via comms::option::def::FieldType.
Definition EnumValue.h:102
static constexpr bool hasWriteNoStatus()
Compile time check of whether the field has proper writeNoStatus() member function.
Definition EnumValue.h:260
static constexpr bool hasEmptySerialization()
Compile time inquiry of whether comms::option::def::EmptySerialization option has been used.
Definition EnumValue.h:138
EnumValue(const ValueType &val)
Constructor.
Definition EnumValue.h:108
ErrorStatus write(TIter &iter, std::size_t size) const
Write current field value to output data sequence.
Definition EnumValue.h:253
typename BaseImpl::ValueType ValueType
Type of underlying enum value.
Definition EnumValue.h:97
static constexpr bool isVersionDependent()
Compile time check if this class is version dependent.
Definition EnumValue.h:290
static constexpr bool hasFieldType()
Compile time inquiry of whether comms::option::def::FieldType option has been used.
Definition EnumValue.h:145
static constexpr std::size_t minLength()
Get minimal length that is required to serialise field of this type.
Definition EnumValue.h:200
~EnumValue() noexcept=default
Destructor.
void setForcedLength(int len)
Force serialization length of the field.
Definition EnumValue.h:322
details::OptionsParser< TOptions... > ParsedOptions
All the options provided to this class bundled into struct.
Definition EnumValue.h:90
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:386
bool canWrite() const
Check of whether the field has a consistent value for writing.
Definition EnumValue.h:242
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:436
static constexpr bool hasFailOnInvalid()
Compile time inquiry of whether comms::option::def::FailOnInvalid option has been used.
Definition EnumValue.h:124
static constexpr bool hasName()
Compile time inquiry of whether comms::option::def::HasName option has been used.
Definition EnumValue.h:159
typename BaseImpl::Endian Endian
Endian used for serialisation.
Definition EnumValue.h:84
bool setVersion(VersionType version)
Default implementation of version update.
Definition EnumValue.h:310
const ValueType & value() const
Get access to enum value storage.
Definition EnumValue.h:165
Main namespace for all classes / functions of COMMS library.
ErrorStatus
Error statuses reported by the Communication module.
Definition ErrorStatus.h:17
constexpr unsigned version()
Version of the COMMS library as single numeric value.
Definition version.h:64
Contains definition of all the options used by the COMMS library.