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 "comms/field/basic/EnumValue.h"
14#include "comms/field/details/AdaptBasicField.h"
15#include "comms/field/details/OptionsParser.h"
16#include "comms/options.h"
17
18#include <type_traits>
19#include <utility>
20
21namespace comms
22{
23namespace field
24{
25
74template <typename TFieldBase, typename TEnum, typename... TOptions>
75class EnumValue : public details::AdaptBasicFieldT<basic::EnumValue<TFieldBase, TEnum>, TOptions...>
76{
77 using BaseImpl = details::AdaptBasicFieldT<basic::EnumValue<TFieldBase, TEnum>, TOptions...>;
78 static_assert(std::is_enum<TEnum>::value, "TEnum must be enum type");
79
80public:
82 using FieldBase = TFieldBase;
83
85 using Endian = typename BaseImpl::Endian;
86
88 using VersionType = typename BaseImpl::VersionType;
89
91 using ParsedOptions = details::OptionsParser<TOptions...>;
92
94 using CommsTag = typename BaseImpl::CommsTag;
95
98 using ValueType = typename BaseImpl::ValueType;
99
103 using FieldType = typename ParsedOptions::FieldType;
104
106 EnumValue() = default;
107
109 explicit EnumValue(const ValueType& val)
110 : BaseImpl(val)
111 {
112 }
113
115 EnumValue(const EnumValue&) = default;
116
118 ~EnumValue() noexcept = default;
119
121 EnumValue& operator=(const EnumValue&) = default;
122
125 static constexpr bool hasFailOnInvalid()
126 {
127 return ParsedOptions::HasFailOnInvalid;
128 }
129
132 static constexpr bool hasIgnoreInvalid()
133 {
134 return ParsedOptions::HasIgnoreInvalid;
135 }
136
139 static constexpr bool hasEmptySerialization()
140 {
141 return ParsedOptions::HasEmptySerialization;
142 }
143
146 static constexpr bool hasFieldType()
147 {
148 return ParsedOptions::HasFieldType;
149 }
150
153 static constexpr bool hasFixedValue()
154 {
155 return ParsedOptions::HasFixedValue;
156 }
157
160 static constexpr bool hasName()
161 {
162 return ParsedOptions::HasName;
163 }
164
166 const ValueType& value() const
167 {
168 return BaseImpl::value();
169 }
170
173 {
174 return BaseImpl::value();
175 }
176
179 const ValueType& getValue() const
180 {
181 return BaseImpl::getValue();
182 }
183
186 template <typename U>
187 void setValue(U&& val)
188 {
189 BaseImpl::setValue(std::forward<U>(val));
190 }
191
194 constexpr std::size_t length() const
195 {
196 return BaseImpl::length();
197 }
198
201 static constexpr std::size_t minLength()
202 {
203 return BaseImpl::minLength();
204 }
205
208 static constexpr std::size_t maxLength()
209 {
210 return BaseImpl::maxLength();
211 }
212
218 template <typename TIter>
219 ErrorStatus read(TIter& iter, std::size_t size)
220 {
221 return BaseImpl::read(iter, size);
222 }
223
226 static constexpr bool hasReadNoStatus()
227 {
228 return BaseImpl::hasReadNoStatus();
229 }
230
236 template <typename TIter>
237 void readNoStatus(TIter& iter)
238 {
239 BaseImpl::readNoStatus(iter);
240 }
241
243 bool canWrite() const
244 {
245 return BaseImpl::canWrite();
246 }
247
253 template <typename TIter>
254 ErrorStatus write(TIter& iter, std::size_t size) const
255 {
256 return BaseImpl::write(iter, size);
257 }
258
261 static constexpr bool hasWriteNoStatus()
262 {
263 return BaseImpl::hasWriteNoStatus();
264 }
265
271 template <typename TIter>
272 void writeNoStatus(TIter& iter) const
273 {
274 BaseImpl::writeNoStatus(iter);
275 }
276
278 bool valid() const
279 {
280 return BaseImpl::valid();
281 }
282
285 bool refresh()
286 {
287 return BaseImpl::refresh();
288 }
289
291 static constexpr bool isVersionDependent()
292 {
293 return ParsedOptions::HasCustomVersionUpdate || BaseImpl::isVersionDependent();
294 }
295
297 static constexpr bool hasNonDefaultRefresh()
298 {
299 return BaseImpl::hasNonDefaultRefresh();
300 }
301
305 {
306 return BaseImpl::getVersion();
307 }
308
312 {
313 return BaseImpl::setVersion(version);
314 }
315
323 void setForcedLength(int len)
324 {
325 BaseImpl::setForcedLength(len);
326 }
327
330 int getForcedLength() const
331 {
332 return BaseImpl::getForcedLength();
333 }
334
335protected:
336 using BaseImpl::readData;
337 using BaseImpl::writeData;
338private:
339 static_assert(!ParsedOptions::HasSequenceElemLengthForcing,
340 "comms::option::def::SequenceElemLengthForcingEnabled option is not applicable to EnumValue field");
341 static_assert(!ParsedOptions::HasSequenceSizeForcing,
342 "comms::option::def::SequenceSizeForcingEnabled option is not applicable to EnumValue field");
343 static_assert(!ParsedOptions::HasSequenceLengthForcing,
344 "comms::option::def::SequenceLengthForcingEnabled option is not applicable to EnumValue field");
345 static_assert(!ParsedOptions::HasSequenceFixedSize,
346 "comms::option::def::SequenceFixedSize option is not applicable to EnumValue field");
347 static_assert(!ParsedOptions::HasSequenceFixedSizeUseFixedSizeStorage,
348 "comms::option::app::SequenceFixedSizeUseFixedSizeStorage option is not applicable to EnumValue field");
349 static_assert(!ParsedOptions::HasSequenceSizeFieldPrefix,
350 "comms::option::def::SequenceSizeFieldPrefix option is not applicable to EnumValue field");
351 static_assert(!ParsedOptions::HasSequenceSerLengthFieldPrefix,
352 "comms::option::def::SequenceSerLengthFieldPrefix option is not applicable to EnumValue field");
353 static_assert(!ParsedOptions::HasSequenceElemSerLengthFieldPrefix,
354 "comms::option::def::SequenceElemSerLengthFieldPrefix option is not applicable to EnumValue field");
355 static_assert(!ParsedOptions::HasSequenceElemFixedSerLengthFieldPrefix,
356 "comms::option::def::SequenceElemSerLengthFixedFieldPrefix option is not applicable to EnumValue field");
357 static_assert(!ParsedOptions::HasSequenceTrailingFieldSuffix,
358 "comms::option::def::SequenceTrailingFieldSuffix option is not applicable to EnumValue field");
359 static_assert(!ParsedOptions::HasSequenceTerminationFieldSuffix,
360 "comms::option::def::SequenceTerminationFieldSuffix option is not applicable to EnumValue field");
361 static_assert(!ParsedOptions::HasFixedSizeStorage,
362 "comms::option::app::FixedSizeStorage option is not applicable to EnumValue field");
363 static_assert(!ParsedOptions::HasCustomStorageType,
364 "comms::option::app::CustomStorageType option is not applicable to EnumValue field");
365 static_assert(!ParsedOptions::HasScalingRatio,
366 "comms::option::def::ScalingRatio option is not applicable to EnumValue field");
367 static_assert(!ParsedOptions::HasUnits,
368 "comms::option::def::Units option is not applicable to EnumValue field");
369 static_assert(!ParsedOptions::HasOrigDataView,
370 "comms::option::app::OrigDataView option is not applicable to EnumValue field");
371 static_assert(!ParsedOptions::HasVersionsRange,
372 "comms::option::def::ExistsBetweenVersions (or similar) option is not applicable to EnumValue field");
373 static_assert(!ParsedOptions::HasMissingOnReadFail,
374 "comms::option::def::MissingOnReadFail option is not applicable to EnumValue field");
375 static_assert(!ParsedOptions::HasMissingOnInvalid,
376 "comms::option::def::MissingOnInvalid option is not applicable to EnumValue field");
377};
378
379// Implementation
380
386template <typename TFieldBase, typename TEnum, typename... TOptions>
389 const EnumValue<TFieldBase, TEnum, TOptions...>& field2) noexcept
390{
391 return field1.value() == field2.value();
392}
393
399template <typename TFieldBase, typename TEnum, typename... TOptions>
402 const EnumValue<TFieldBase, TEnum, TOptions...>& field2) noexcept
403{
404 return field1.value() != field2.value();
405}
406
412template <typename TFieldBase, typename TEnum, typename... TOptions>
415 const EnumValue<TFieldBase, TEnum, TOptions...>& field2) noexcept
416{
417 return field1.value() < field2.value();
418}
419
425template <typename T>
426constexpr bool isEnumValue()
427{
428 return std::is_same<typename T::CommsTag, tag::Enum>::value;
429}
430
434template <typename TFieldBase, typename TEnum, typename... TOptions>
435inline
436EnumValue<TFieldBase, TEnum, TOptions...>&
438{
439 return field;
440}
441
445template <typename TFieldBase, typename TEnum, typename... TOptions>
446inline
447const EnumValue<TFieldBase, TEnum, TOptions...>&
449{
450 return field;
451}
452
453} // namespace field
454
455} // namespace comms
456
457
458
459
460
Enumerator value field.
Definition EnumValue.h:76
ValueType & value()
Get access to enum value storage.
Definition EnumValue.h:172
constexpr bool isEnumValue()
Compile time check function of whether a provided type is any variant of comms::field::EnumValue.
Definition EnumValue.h:426
bool operator!=(const EnumValue< TFieldBase, TEnum, TOptions... > &field1, const EnumValue< TFieldBase, TEnum, TOptions... > &field2) noexcept
Non-equality comparison operator.
Definition EnumValue.h:400
void writeNoStatus(TIter &iter) const
Write current field value to output data sequence without error check and status report.
Definition EnumValue.h:272
typename BaseImpl::VersionType VersionType
Version type.
Definition EnumValue.h:88
static constexpr std::size_t maxLength()
Get maximal length that is required to serialise field of this type.
Definition EnumValue.h:208
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:448
VersionType getVersion() const
Get version of the field.
Definition EnumValue.h:304
static constexpr bool hasNonDefaultRefresh()
Compile time check if this class has non-default refresh functionality.
Definition EnumValue.h:297
bool valid() const
Check validity of the field value.
Definition EnumValue.h:278
void readNoStatus(TIter &iter)
Read field value from input data sequence without error check and status report.
Definition EnumValue.h:237
typename BaseImpl::CommsTag CommsTag
Tag indicating type of the field.
Definition EnumValue.h:94
void setValue(U &&val)
Set value.
Definition EnumValue.h:187
const ValueType & getValue() const
Get value.
Definition EnumValue.h:179
static constexpr bool hasReadNoStatus()
Compile time check of whether the field has proper readNoStatus() member function.
Definition EnumValue.h:226
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:413
TFieldBase FieldBase
Base class provided in the first template parameter.
Definition EnumValue.h:82
static constexpr bool hasFixedValue()
Compile time inquiry of whether comms::option::def::FixedValue option has been used.
Definition EnumValue.h:153
static constexpr bool hasIgnoreInvalid()
Compile time inquiry of whether comms::option::def::IgnoreInvalid option has been used.
Definition EnumValue.h:132
int getForcedLength() const
Get forced serialization length.
Definition EnumValue.h:330
ErrorStatus read(TIter &iter, std::size_t size)
Read field value from input data sequence.
Definition EnumValue.h:219
constexpr std::size_t length() const
Get length required to serialise the current field value.
Definition EnumValue.h:194
bool refresh()
Refresh the field's value.
Definition EnumValue.h:285
typename ParsedOptions::FieldType FieldType
Type of actual extending field specified via comms::option::def::FieldType.
Definition EnumValue.h:103
static constexpr bool hasWriteNoStatus()
Compile time check of whether the field has proper writeNoStatus() member function.
Definition EnumValue.h:261
static constexpr bool hasEmptySerialization()
Compile time inquiry of whether comms::option::def::EmptySerialization option has been used.
Definition EnumValue.h:139
EnumValue(const ValueType &val)
Constructor.
Definition EnumValue.h:109
ErrorStatus write(TIter &iter, std::size_t size) const
Write current field value to output data sequence.
Definition EnumValue.h:254
typename BaseImpl::ValueType ValueType
Type of underlying enum value.
Definition EnumValue.h:98
static constexpr bool isVersionDependent()
Compile time check if this class is version dependent.
Definition EnumValue.h:291
static constexpr bool hasFieldType()
Compile time inquiry of whether comms::option::def::FieldType option has been used.
Definition EnumValue.h:146
static constexpr std::size_t minLength()
Get minimal length that is required to serialise field of this type.
Definition EnumValue.h:201
~EnumValue() noexcept=default
Destructor.
void setForcedLength(int len)
Force serialization length of the field.
Definition EnumValue.h:323
details::OptionsParser< TOptions... > ParsedOptions
All the options provided to this class bundled into struct.
Definition EnumValue.h:91
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:387
bool canWrite() const
Check of whether the field has a consistent value for writing.
Definition EnumValue.h:243
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:437
static constexpr bool hasFailOnInvalid()
Compile time inquiry of whether comms::option::def::FailOnInvalid option has been used.
Definition EnumValue.h:125
static constexpr bool hasName()
Compile time inquiry of whether comms::option::def::HasName option has been used.
Definition EnumValue.h:160
typename BaseImpl::Endian Endian
Endian used for serialisation.
Definition EnumValue.h:85
bool setVersion(VersionType version)
Default implementation of version update.
Definition EnumValue.h:311
const ValueType & value() const
Get access to enum value storage.
Definition EnumValue.h:166
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.