COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
Optional.h
Go to the documentation of this file.
1//
2// Copyright 2015 - 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/Assert.h"
14#include "comms/ErrorStatus.h"
15#include "details/OptionsParser.h"
16#include "OptionalMode.h"
17#include "basic/Optional.h"
18#include "details/AdaptBasicField.h"
19
20namespace comms
21{
22
23namespace field
24{
25
45template <typename TField, typename... TOptions>
46class Optional : public details::AdaptBasicFieldT<basic::Optional<TField>, TOptions...>
47{
48 using BaseImpl = details::AdaptBasicFieldT<basic::Optional<TField>, TOptions...>;
49public:
51 using Endian = typename BaseImpl::Endian;
52
54 using VersionType = typename BaseImpl::VersionType;
55
57 using ParsedOptions = details::OptionsParser<TOptions...>;
58
60 using CommsTag = typename BaseImpl::CommsTag;
61
63 using Field = TField;
64
67
71
75 using FieldType = typename ParsedOptions::FieldType;
76
79 Optional() = default;
80
83 explicit Optional(const Field& fieldSrc)
84 : BaseImpl(fieldSrc)
85 {
86 }
87
90 explicit Optional(Field&& fieldSrc)
91 : BaseImpl(std::move(fieldSrc))
92 {
93 }
94
96 Optional(const Optional&) = default;
97
99 Optional(Optional&&) = default;
100
102 ~Optional() noexcept = default;
103
105 Optional& operator=(const Optional&) = default;
106
108 Optional& operator=(Optional&&) = default;
109
112 static constexpr bool hasFailOnInvalid()
113 {
114 return ParsedOptions::HasFailOnInvalid;
115 }
116
119 static constexpr bool hasIgnoreInvalid()
120 {
121 return ParsedOptions::HasIgnoreInvalid;
122 }
123
126 static constexpr bool hasEmptySerialization()
127 {
128 return ParsedOptions::HasEmptySerialization;
129 }
130
133 static constexpr bool hasFieldType()
134 {
135 return ParsedOptions::HasFieldType;
136 }
137
140 static constexpr bool hasFixedValue()
141 {
142 return ParsedOptions::HasFixedValue;
143 }
144
147 static constexpr bool hasName()
148 {
149 return ParsedOptions::HasName;
150 }
151
155 bool isTentative() const
156 {
157 return BaseImpl::getMode() == Mode::Tentative;
158 }
159
164 {
165 BaseImpl::setMode(Mode::Tentative);
166 }
167
171 bool isMissing() const
172 {
173 return BaseImpl::getMode() == Mode::Missing;
174 }
175
180 {
181 BaseImpl::setMode(Mode::Missing);
182 }
183
187 bool doesExist() const
188 {
189 return BaseImpl::getMode() == Mode::Exists;
190 }
191
196 {
197 BaseImpl::setMode(Mode::Exists);
198 }
199
202 {
203 return BaseImpl::field();
204 }
205
207 const Field& field() const
208 {
209 return BaseImpl::field();
210 }
211
214 {
215 return BaseImpl::value();
216 }
217
219 const ValueType& value() const
220 {
221 return BaseImpl::value();
222 }
223
226 const ValueType& getValue() const
227 {
228 return BaseImpl::getValue();
229 }
230
233 template <typename U>
234 void setValue(U&& val)
235 {
236 BaseImpl::setValue(std::forward<U>(val));
237 }
238
240 Mode getMode() const
241 {
242 return BaseImpl::getMode();
243 }
244
246 void setMode(Mode val)
247 {
248 BaseImpl::setMode(val);
249 }
250
256 std::size_t length() const
257 {
258 return BaseImpl::length();
259 }
260
263 static constexpr std::size_t minLength()
264 {
265 return BaseImpl::minLength();
266 }
267
270 static constexpr std::size_t maxLength()
271 {
272 return BaseImpl::maxLength();
273 }
274
279 bool valid() const
280 {
281 return BaseImpl::valid();
282 }
283
289 bool refresh()
290 {
291 return BaseImpl::refresh();
292 }
293
309 template <typename TIter>
310 ErrorStatus read(TIter& iter, std::size_t len)
311 {
312 return BaseImpl::read(iter, len);
313 }
314
317 static constexpr bool hasReadNoStatus()
318 {
319 return BaseImpl::hasReadNoStatus();
320 }
321
327 template <typename TIter>
328 void readNoStatus(TIter& iter)
329 {
330 BaseImpl::readNoStatus(iter);
331 }
332
334 bool canWrite() const
335 {
336 return BaseImpl::canWrite();
337 }
338
349 template <typename TIter>
350 ErrorStatus write(TIter& iter, std::size_t len) const
351 {
352 return BaseImpl::write(iter, len);
353 }
354
357 static constexpr bool hasWriteNoStatus()
358 {
359 return BaseImpl::hasWriteNoStatus();
360 }
361
367 template <typename TIter>
368 void writeNoStatus(TIter& iter) const
369 {
370 BaseImpl::writeNoStatus(iter);
371 }
372
374 static constexpr bool isVersionDependent()
375 {
376 return ParsedOptions::HasCustomVersionUpdate || BaseImpl::isVersionDependent();
377 }
378
380 static constexpr bool hasNonDefaultRefresh()
381 {
382 return BaseImpl::hasNonDefaultRefresh();
383 }
384
388 {
389 return BaseImpl::getVersion();
390 }
391
395 {
396 return BaseImpl::setVersion(version);
397 }
398
399protected:
400 using BaseImpl::readData;
401 using BaseImpl::writeData;
402
403private:
404 static_assert(!ParsedOptions::HasInvalidByDefault,
405 "comms::option::def::InvalidByDefault option is not applicable to Optional field");
406};
407
413template <typename TField, typename... TOptions>
415 const Optional<TField, TOptions...>& field1,
416 const Optional<TField, TOptions...>& field2) noexcept
417{
418 if (field1.getMode() != field2.getMode()) {
419 return false;
420 }
421
422 if (field1.isMissing()) {
423 return true;
424 }
425
426 return field1.field() == field2.field();
427}
428
434template <typename TField, typename... TOptions>
436 const Optional<TField, TOptions...>& field1,
437 const Optional<TField, TOptions...>& field2) noexcept
438{
439 return !(field1 == field2);
440}
441
447template <typename TField, typename... TOptions>
449 const Optional<TField, TOptions...>& field1,
450 const Optional<TField, TOptions...>& field2) noexcept
451{
452 if (field1.isMissing()) {
453 return !field2.isMissing();
454 }
455
456 if (field2.isMissing()) {
457 return false;
458 }
459
460 return field1.field() < field2.field();
461}
462
468template <typename TField, typename... TOptions>
470 const Optional<TField, TOptions...>& field1,
471 const Optional<TField, TOptions...>& field2) noexcept
472{
473 return (field2 < field1);
474}
475
481template <typename TField, typename... TOptions>
483 const Optional<TField, TOptions...>& field1,
484 const Optional<TField, TOptions...>& field2) noexcept
485{
486 return (field1 < field2) || (field1 == field2);
487}
488
494template <typename TField, typename... TOptions>
496 const Optional<TField, TOptions...>& field1,
497 const Optional<TField, TOptions...>& field2) noexcept
498{
499 return field2 <= field1;
500}
501
507template <typename T>
508constexpr bool isOptional()
509{
510 return std::is_same<typename T::CommsTag, tag::Optional>::value;
511}
512
516template <typename TField, typename... TOptions>
517inline
518Optional<TField, TOptions...>&
520{
521 return field;
522}
523
527template <typename TField, typename... TOptions>
528inline
529const Optional<TField, TOptions...>&
531{
532 return field;
533}
534
535} // namespace field
536
537} // namespace comms
538
539
This file contains classes required for generic custom assertion functionality.
This file contain definition of error statuses used by comms module.
Contains definition of the mode used for comms::field::Optional fields.
Adaptor class to any other field, that makes the field optional.
Definition Optional.h:47
bool operator<(const Optional< TField, TOptions... > &field1, const Optional< TField, TOptions... > &field2) noexcept
Equivalence comparison operator.
Definition Optional.h:448
bool operator<=(const Optional< TField, TOptions... > &field1, const Optional< TField, TOptions... > &field2) noexcept
Equivalence comparison operator.
Definition Optional.h:482
ErrorStatus read(TIter &iter, std::size_t len)
Read field value from input data sequence.
Definition Optional.h:310
Optional()=default
Default constructor.
Optional(const Field &fieldSrc)
Construct the field.
Definition Optional.h:83
bool operator>(const Optional< TField, TOptions... > &field1, const Optional< TField, TOptions... > &field2) noexcept
Equivalence comparison operator.
Definition Optional.h:469
VersionType getVersion() const
Get version of the field.
Definition Optional.h:387
constexpr bool isOptional()
Compile time check function of whether a provided type is any variant of comms::field::Optional.
Definition Optional.h:508
void setExists()
Set mode to Mode::Exists.
Definition Optional.h:195
TField Field
Type of the field.
Definition Optional.h:63
const ValueType & getValue() const
Get value.
Definition Optional.h:226
const Field & field() const
Get an access to the wrapped field object.
Definition Optional.h:207
void writeNoStatus(TIter &iter) const
Write current field value to output data sequence without error check and status report.
Definition Optional.h:368
Optional(Field &&fieldSrc)
Construct the field.
Definition Optional.h:90
bool isTentative() const
Check whether mode is equivalent to Mode::Tentative.
Definition Optional.h:155
void readNoStatus(TIter &iter)
Read field value from input data sequence without error check and status report.
Definition Optional.h:328
bool operator>=(const Optional< TField, TOptions... > &field1, const Optional< TField, TOptions... > &field2) noexcept
Equivalence comparison operator.
Definition Optional.h:495
Field & field()
Get an access to the wrapped field object.
Definition Optional.h:201
details::OptionsParser< TOptions... > ParsedOptions
All the options provided to this class bundled into struct.
Definition Optional.h:57
bool operator!=(const Optional< TField, TOptions... > &field1, const Optional< TField, TOptions... > &field2) noexcept
Non-equality comparison operator.
Definition Optional.h:435
void setMissing()
Set mode to Mode::Missing.
Definition Optional.h:179
typename BaseImpl::CommsTag CommsTag
Tag indicating type of the field.
Definition Optional.h:60
static constexpr bool hasFieldType()
Compile time inquiry of whether comms::option::def::FieldType option has been used.
Definition Optional.h:133
Optional< TField, TOptions... > & toFieldBase(Optional< TField, TOptions... > &field)
Upcast type of the field definition to its parent comms::field::Optional type in order to have access...
Definition Optional.h:519
static constexpr bool isVersionDependent()
Compile time check if this class is version dependent.
Definition Optional.h:374
static constexpr std::size_t maxLength()
Get maximal length that is required to serialise field of this type.
Definition Optional.h:270
void setValue(U &&val)
Set value.
Definition Optional.h:234
static constexpr bool hasIgnoreInvalid()
Compile time inquiry of whether comms::option::def::IgnoreInvalid option has been used.
Definition Optional.h:119
static constexpr bool hasName()
Compile time inquiry of whether comms::option::def::HasName option has been used.
Definition Optional.h:147
bool canWrite() const
Check of whether the field has a consistent value for writing.
Definition Optional.h:334
static constexpr bool hasReadNoStatus()
Compile time check of whether the field has proper readNoStatus() member function.
Definition Optional.h:317
static constexpr bool hasNonDefaultRefresh()
Compile time check if this class has non-default refresh functionality.
Definition Optional.h:380
const ValueType & value() const
Get an access to the wrapped field object.
Definition Optional.h:219
ValueType & value()
Get an access to the wrapped field object.
Definition Optional.h:213
bool doesExist() const
Check whether mode is equivalent to Mode::Exists.
Definition Optional.h:187
static constexpr bool hasFixedValue()
Compile time inquiry of whether comms::option::def::FixedValue option has been used.
Definition Optional.h:140
Mode getMode() const
Get current optional mode.
Definition Optional.h:240
Optional(const Optional &)=default
Copy constructor.
const Optional< TField, TOptions... > & toFieldBase(const Optional< TField, TOptions... > &field)
Upcast type of the field definition to its parent comms::field::Optional type in order to have access...
Definition Optional.h:530
typename BaseImpl::Endian Endian
Endian used for serialisation.
Definition Optional.h:51
std::size_t length() const
Get length required to serialise the current field value.
Definition Optional.h:256
~Optional() noexcept=default
Destructor.
void setMode(Mode val)
Get optional mode.
Definition Optional.h:246
bool operator==(const Optional< TField, TOptions... > &field1, const Optional< TField, TOptions... > &field2) noexcept
Equality comparison operator.
Definition Optional.h:414
static constexpr bool hasFailOnInvalid()
Compile time inquiry of whether comms::option::def::FailOnInvalid option has been used.
Definition Optional.h:112
bool valid() const
Check validity of the field value.
Definition Optional.h:279
bool isMissing() const
Check whether mode is equivalent to Mode::Missing.
Definition Optional.h:171
Field ValueType
Value type of this field, equal to Field.
Definition Optional.h:66
typename BaseImpl::VersionType VersionType
Version type.
Definition Optional.h:54
ErrorStatus write(TIter &iter, std::size_t len) const
Write current field value to output data sequence.
Definition Optional.h:350
bool refresh()
Refresh the field's value.
Definition Optional.h:289
static constexpr bool hasWriteNoStatus()
Compile time check of whether the field has proper writeNoStatus() member function.
Definition Optional.h:357
static constexpr std::size_t minLength()
Get minimal length that is required to serialise field of this type.
Definition Optional.h:263
static constexpr bool hasEmptySerialization()
Compile time inquiry of whether comms::option::def::EmptySerialization option has been used.
Definition Optional.h:126
bool setVersion(VersionType version)
Default implementation of version update.
Definition Optional.h:394
Optional(Optional &&)=default
Move constructor.
typename ParsedOptions::FieldType FieldType
Type of actual extending field specified via comms::option::def::FieldType.
Definition Optional.h:75
void setTentative()
Set mode to Mode::Tentative.
Definition Optional.h:163
OptionalMode
Mode to be used by comms::field::Optional.
Definition OptionalMode.h:22
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
STL namespace.