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 - 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
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
71template <typename TFieldBase, typename TEnum, typename... TOptions>
72class EnumValue : public details::AdaptBasicFieldT<basic::EnumValue<TFieldBase, TEnum>, TOptions...>
73{
74 using BaseImpl = details::AdaptBasicFieldT<basic::EnumValue<TFieldBase, TEnum>, TOptions...>;
75 static_assert(std::is_enum<TEnum>::value, "TEnum must be enum type");
76
77public:
79 using FieldBase = TFieldBase;
80
82 using Endian = typename BaseImpl::Endian;
83
85 using VersionType = typename BaseImpl::VersionType;
86
88 using ParsedOptions = details::OptionsParser<TOptions...>;
89
91 using CommsTag = typename BaseImpl::CommsTag;
92
95 using ValueType = typename BaseImpl::ValueType;
96
100 using FieldType = typename ParsedOptions::FieldType;
101
103 EnumValue() = default;
104
106 explicit EnumValue(const ValueType& val)
107 : BaseImpl(val)
108 {
109 }
110
112 EnumValue(const EnumValue&) = default;
113
115 ~EnumValue() noexcept = default;
116
118 EnumValue& operator=(const EnumValue&) = default;
119
122 static constexpr bool hasFailOnInvalid()
123 {
124 return ParsedOptions::HasFailOnInvalid;
125 }
126
129 static constexpr bool hasIgnoreInvalid()
130 {
131 return ParsedOptions::HasIgnoreInvalid;
132 }
133
136 static constexpr bool hasEmptySerialization()
137 {
138 return ParsedOptions::HasEmptySerialization;
139 }
140
143 static constexpr bool hasFieldType()
144 {
145 return ParsedOptions::HasFieldType;
146 }
147
149 const ValueType& value() const
150 {
151 return BaseImpl::value();
152 }
153
156 {
157 return BaseImpl::value();
158 }
159
162 const ValueType& getValue() const
163 {
164 return BaseImpl::getValue();
165 }
166
169 template <typename U>
170 void setValue(U&& val)
171 {
172 BaseImpl::setValue(std::forward<U>(val));
173 }
174
177 constexpr std::size_t length() const
178 {
179 return BaseImpl::length();
180 }
181
184 static constexpr std::size_t minLength()
185 {
186 return BaseImpl::minLength();
187 }
188
191 static constexpr std::size_t maxLength()
192 {
193 return BaseImpl::maxLength();
194 }
195
201 template <typename TIter>
202 ErrorStatus read(TIter& iter, std::size_t size)
203 {
204 return BaseImpl::read(iter, size);
205 }
206
209 static constexpr bool hasReadNoStatus()
210 {
211 return BaseImpl::hasReadNoStatus();
212 }
213
219 template <typename TIter>
220 void readNoStatus(TIter& iter)
221 {
222 BaseImpl::readNoStatus(iter);
223 }
224
226 bool canWrite() const
227 {
228 return BaseImpl::canWrite();
229 }
230
236 template <typename TIter>
237 ErrorStatus write(TIter& iter, std::size_t size) const
238 {
239 return BaseImpl::write(iter, size);
240 }
241
244 static constexpr bool hasWriteNoStatus()
245 {
246 return BaseImpl::hasWriteNoStatus();
247 }
248
254 template <typename TIter>
255 void writeNoStatus(TIter& iter) const
256 {
257 BaseImpl::writeNoStatus(iter);
258 }
259
261 bool valid() const
262 {
263 return BaseImpl::valid();
264 }
265
268 bool refresh()
269 {
270 return BaseImpl::refresh();
271 }
272
274 static constexpr bool isVersionDependent()
275 {
276 return ParsedOptions::HasCustomVersionUpdate || BaseImpl::isVersionDependent();
277 }
278
280 static constexpr bool hasNonDefaultRefresh()
281 {
282 return BaseImpl::hasNonDefaultRefresh();
283 }
284
288 {
289 return BaseImpl::getVersion();
290 }
291
295 {
296 return BaseImpl::setVersion(version);
297 }
298
306 void setForcedLength(int len)
307 {
308 BaseImpl::setForcedLength(len);
309 }
310
313 int getForcedLength() const
314 {
315 return BaseImpl::getForcedLength();
316 }
317
318protected:
319 using BaseImpl::readData;
320 using BaseImpl::writeData;
321private:
322 static_assert(!ParsedOptions::HasSequenceElemLengthForcing,
323 "comms::option::def::SequenceElemLengthForcingEnabled option is not applicable to EnumValue field");
324 static_assert(!ParsedOptions::HasSequenceSizeForcing,
325 "comms::option::def::SequenceSizeForcingEnabled option is not applicable to EnumValue field");
326 static_assert(!ParsedOptions::HasSequenceLengthForcing,
327 "comms::option::def::SequenceLengthForcingEnabled option is not applicable to EnumValue field");
328 static_assert(!ParsedOptions::HasSequenceFixedSize,
329 "comms::option::def::SequenceFixedSize option is not applicable to EnumValue field");
330 static_assert(!ParsedOptions::HasSequenceFixedSizeUseFixedSizeStorage,
331 "comms::option::app::SequenceFixedSizeUseFixedSizeStorage option is not applicable to EnumValue field");
332 static_assert(!ParsedOptions::HasSequenceSizeFieldPrefix,
333 "comms::option::def::SequenceSizeFieldPrefix option is not applicable to EnumValue field");
334 static_assert(!ParsedOptions::HasSequenceSerLengthFieldPrefix,
335 "comms::option::def::SequenceSerLengthFieldPrefix option is not applicable to EnumValue field");
336 static_assert(!ParsedOptions::HasSequenceElemSerLengthFieldPrefix,
337 "comms::option::def::SequenceElemSerLengthFieldPrefix option is not applicable to EnumValue field");
338 static_assert(!ParsedOptions::HasSequenceElemFixedSerLengthFieldPrefix,
339 "comms::option::def::SequenceElemSerLengthFixedFieldPrefix option is not applicable to EnumValue field");
340 static_assert(!ParsedOptions::HasSequenceTrailingFieldSuffix,
341 "comms::option::def::SequenceTrailingFieldSuffix option is not applicable to EnumValue field");
342 static_assert(!ParsedOptions::HasSequenceTerminationFieldSuffix,
343 "comms::option::def::SequenceTerminationFieldSuffix option is not applicable to EnumValue field");
344 static_assert(!ParsedOptions::HasFixedSizeStorage,
345 "comms::option::app::FixedSizeStorage option is not applicable to EnumValue field");
346 static_assert(!ParsedOptions::HasCustomStorageType,
347 "comms::option::app::CustomStorageType option is not applicable to EnumValue field");
348 static_assert(!ParsedOptions::HasScalingRatio,
349 "comms::option::def::ScalingRatio option is not applicable to EnumValue field");
350 static_assert(!ParsedOptions::HasUnits,
351 "comms::option::def::Units option is not applicable to EnumValue field");
352 static_assert(!ParsedOptions::HasOrigDataView,
353 "comms::option::app::OrigDataView option is not applicable to EnumValue field");
354 static_assert(!ParsedOptions::HasVersionsRange,
355 "comms::option::def::ExistsBetweenVersions (or similar) option is not applicable to EnumValue field");
356 static_assert(!ParsedOptions::HasMissingOnReadFail,
357 "comms::option::def::MissingOnReadFail option is not applicable to EnumValue field");
358 static_assert(!ParsedOptions::HasMissingOnInvalid,
359 "comms::option::def::MissingOnInvalid option is not applicable to EnumValue field");
360};
361
362// Implementation
363
369template <typename TFieldBase, typename TEnum, typename... TOptions>
372 const EnumValue<TFieldBase, TEnum, TOptions...>& field2) noexcept
373{
374 return field1.value() == field2.value();
375}
376
382template <typename TFieldBase, typename TEnum, typename... TOptions>
385 const EnumValue<TFieldBase, TEnum, TOptions...>& field2) noexcept
386{
387 return field1.value() != field2.value();
388}
389
395template <typename TFieldBase, typename TEnum, typename... TOptions>
398 const EnumValue<TFieldBase, TEnum, TOptions...>& field2) noexcept
399{
400 return field1.value() < field2.value();
401}
402
408template <typename T>
409constexpr bool isEnumValue()
410{
411 return std::is_same<typename T::CommsTag, tag::Enum>::value;
412}
413
417template <typename TFieldBase, typename TEnum, typename... TOptions>
418inline
419EnumValue<TFieldBase, TEnum, TOptions...>&
421{
422 return field;
423}
424
428template <typename TFieldBase, typename TEnum, typename... TOptions>
429inline
430const EnumValue<TFieldBase, TEnum, TOptions...>&
432{
433 return field;
434}
435
436} // namespace field
437
438} // namespace comms
439
440
441
442
443
Enumerator value field.
Definition EnumValue.h:73
ValueType & value()
Get access to enum value storage.
Definition EnumValue.h:155
constexpr bool isEnumValue()
Compile time check function of whether a provided type is any variant of comms::field::EnumValue.
Definition EnumValue.h:409
bool operator!=(const EnumValue< TFieldBase, TEnum, TOptions... > &field1, const EnumValue< TFieldBase, TEnum, TOptions... > &field2) noexcept
Non-equality comparison operator.
Definition EnumValue.h:383
void writeNoStatus(TIter &iter) const
Write current field value to output data sequence without error check and status report.
Definition EnumValue.h:255
typename BaseImpl::VersionType VersionType
Version type.
Definition EnumValue.h:85
static constexpr std::size_t maxLength()
Get maximal length that is required to serialise field of this type.
Definition EnumValue.h:191
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:431
VersionType getVersion() const
Get version of the field.
Definition EnumValue.h:287
static constexpr bool hasNonDefaultRefresh()
Compile time check if this class has non-default refresh functionality.
Definition EnumValue.h:280
bool valid() const
Check validity of the field value.
Definition EnumValue.h:261
void readNoStatus(TIter &iter)
Read field value from input data sequence without error check and status report.
Definition EnumValue.h:220
typename BaseImpl::CommsTag CommsTag
Tag indicating type of the field.
Definition EnumValue.h:91
void setValue(U &&val)
Set value.
Definition EnumValue.h:170
const ValueType & getValue() const
Get value.
Definition EnumValue.h:162
static constexpr bool hasReadNoStatus()
Compile time check of whether the field has proper readNoStatus() member function.
Definition EnumValue.h:209
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:396
TFieldBase FieldBase
Base class provided in the first template parameter.
Definition EnumValue.h:79
static constexpr bool hasIgnoreInvalid()
Compile time inquiry of whether comms::option::def::IgnoreInvalid option has been used.
Definition EnumValue.h:129
int getForcedLength() const
Get forced serialization length.
Definition EnumValue.h:313
ErrorStatus read(TIter &iter, std::size_t size)
Read field value from input data sequence.
Definition EnumValue.h:202
constexpr std::size_t length() const
Get length required to serialise the current field value.
Definition EnumValue.h:177
bool refresh()
Refresh the field's value.
Definition EnumValue.h:268
typename ParsedOptions::FieldType FieldType
Type of actual extending field specified via comms::option::def::FieldType.
Definition EnumValue.h:100
static constexpr bool hasWriteNoStatus()
Compile time check of whether the field has proper writeNoStatus() member function.
Definition EnumValue.h:244
static constexpr bool hasEmptySerialization()
Compile time inquiry of whether comms::option::def::EmptySerialization option has been used.
Definition EnumValue.h:136
EnumValue(const ValueType &val)
Constructor.
Definition EnumValue.h:106
ErrorStatus write(TIter &iter, std::size_t size) const
Write current field value to output data sequence.
Definition EnumValue.h:237
typename BaseImpl::ValueType ValueType
Type of underlying enum value.
Definition EnumValue.h:95
static constexpr bool isVersionDependent()
Compile time check if this class is version dependent.
Definition EnumValue.h:274
static constexpr bool hasFieldType()
Compile time inquiry of whether comms::option::def::FieldType option has been used.
Definition EnumValue.h:143
static constexpr std::size_t minLength()
Get minimal length that is required to serialise field of this type.
Definition EnumValue.h:184
~EnumValue() noexcept=default
Destructor.
void setForcedLength(int len)
Force serialization length of the field.
Definition EnumValue.h:306
details::OptionsParser< TOptions... > ParsedOptions
All the options provided to this class bundled into struct.
Definition EnumValue.h:88
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:370
bool canWrite() const
Check of whether the field has a consistent value for writing.
Definition EnumValue.h:226
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:420
static constexpr bool hasFailOnInvalid()
Compile time inquiry of whether comms::option::def::FailOnInvalid option has been used.
Definition EnumValue.h:122
typename BaseImpl::Endian Endian
Endian used for serialisation.
Definition EnumValue.h:82
bool setVersion(VersionType version)
Default implementation of version update.
Definition EnumValue.h:294
const ValueType & value() const
Get access to enum value storage.
Definition EnumValue.h:149
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.