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 - 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/Assert.h"
16#include "comms/ErrorStatus.h"
17#include "comms/field/basic/Optional.h"
18#include "comms/field/details/AdaptBasicField.h"
19#include "comms/field/details/OptionsParser.h"
21
22#include <cstddef>
23#include <type_traits>
24#include <utility>
25
26namespace comms
27{
28
29namespace field
30{
31
51template <typename TField, typename... TOptions>
52class Optional : public details::AdaptBasicFieldT<basic::Optional<TField>, TOptions...>
53{
54 using BaseImpl = details::AdaptBasicFieldT<basic::Optional<TField>, TOptions...>;
55public:
57 using Endian = typename BaseImpl::Endian;
58
60 using VersionType = typename BaseImpl::VersionType;
61
63 using ParsedOptions = details::OptionsParser<TOptions...>;
64
66 using CommsTag = typename BaseImpl::CommsTag;
67
69 using Field = TField;
70
73
77
81 using FieldType = typename ParsedOptions::FieldType;
82
85 Optional() = default;
86
89 explicit Optional(const Field& fieldSrc)
90 : BaseImpl(fieldSrc)
91 {
92 }
93
96 explicit Optional(Field&& fieldSrc)
97 : BaseImpl(std::move(fieldSrc))
98 {
99 }
100
102 Optional(const Optional&) = default;
103
105 Optional(Optional&&) = default;
106
108 ~Optional() noexcept = default;
109
111 Optional& operator=(const Optional&) = default;
112
114 Optional& operator=(Optional&&) = default;
115
118 static constexpr bool hasFailOnInvalid()
119 {
120 return ParsedOptions::HasFailOnInvalid;
121 }
122
125 static constexpr bool hasIgnoreInvalid()
126 {
127 return ParsedOptions::HasIgnoreInvalid;
128 }
129
132 static constexpr bool hasEmptySerialization()
133 {
134 return ParsedOptions::HasEmptySerialization;
135 }
136
139 static constexpr bool hasFieldType()
140 {
141 return ParsedOptions::HasFieldType;
142 }
143
146 static constexpr bool hasFixedValue()
147 {
148 return ParsedOptions::HasFixedValue;
149 }
150
153 static constexpr bool hasName()
154 {
155 return ParsedOptions::HasName;
156 }
157
161 bool isTentative() const
162 {
163 return BaseImpl::getMode() == Mode::Tentative;
164 }
165
170 {
171 BaseImpl::setMode(Mode::Tentative);
172 }
173
177 bool isMissing() const
178 {
179 return BaseImpl::getMode() == Mode::Missing;
180 }
181
186 {
187 BaseImpl::setMode(Mode::Missing);
188 }
189
193 bool doesExist() const
194 {
195 return BaseImpl::getMode() == Mode::Exists;
196 }
197
202 {
203 BaseImpl::setMode(Mode::Exists);
204 }
205
208 {
209 return BaseImpl::field();
210 }
211
213 const Field& field() const
214 {
215 return BaseImpl::field();
216 }
217
220 {
221 return BaseImpl::value();
222 }
223
225 const ValueType& value() const
226 {
227 return BaseImpl::value();
228 }
229
232 const ValueType& getValue() const
233 {
234 return BaseImpl::getValue();
235 }
236
239 template <typename U>
240 void setValue(U&& val)
241 {
242 BaseImpl::setValue(std::forward<U>(val));
243 }
244
246 Mode getMode() const
247 {
248 return BaseImpl::getMode();
249 }
250
252 void setMode(Mode val)
253 {
254 BaseImpl::setMode(val);
255 }
256
262 std::size_t length() const
263 {
264 return BaseImpl::length();
265 }
266
269 static constexpr std::size_t minLength()
270 {
271 return BaseImpl::minLength();
272 }
273
276 static constexpr std::size_t maxLength()
277 {
278 return BaseImpl::maxLength();
279 }
280
285 bool valid() const
286 {
287 return BaseImpl::valid();
288 }
289
295 bool refresh()
296 {
297 return BaseImpl::refresh();
298 }
299
315 template <typename TIter>
316 ErrorStatus read(TIter& iter, std::size_t len)
317 {
318 return BaseImpl::read(iter, len);
319 }
320
323 static constexpr bool hasReadNoStatus()
324 {
325 return BaseImpl::hasReadNoStatus();
326 }
327
333 template <typename TIter>
334 void readNoStatus(TIter& iter)
335 {
336 BaseImpl::readNoStatus(iter);
337 }
338
340 bool canWrite() const
341 {
342 return BaseImpl::canWrite();
343 }
344
355 template <typename TIter>
356 ErrorStatus write(TIter& iter, std::size_t len) const
357 {
358 return BaseImpl::write(iter, len);
359 }
360
363 static constexpr bool hasWriteNoStatus()
364 {
365 return BaseImpl::hasWriteNoStatus();
366 }
367
373 template <typename TIter>
374 void writeNoStatus(TIter& iter) const
375 {
376 BaseImpl::writeNoStatus(iter);
377 }
378
380 static constexpr bool isVersionDependent()
381 {
382 return ParsedOptions::HasCustomVersionUpdate || BaseImpl::isVersionDependent();
383 }
384
386 static constexpr bool hasNonDefaultRefresh()
387 {
388 return BaseImpl::hasNonDefaultRefresh();
389 }
390
394 {
395 return BaseImpl::getVersion();
396 }
397
401 {
402 return BaseImpl::setVersion(version);
403 }
404
405protected:
406 using BaseImpl::readData;
407 using BaseImpl::writeData;
408
409private:
410 static_assert(!ParsedOptions::HasInvalidByDefault,
411 "comms::option::def::InvalidByDefault option is not applicable to Optional field");
412};
413
419template <typename TField, typename... TOptions>
421 const Optional<TField, TOptions...>& field1,
422 const Optional<TField, TOptions...>& field2) noexcept
423{
424 if (field1.getMode() != field2.getMode()) {
425 return false;
426 }
427
428 if (field1.isMissing()) {
429 return true;
430 }
431
432 return field1.field() == field2.field();
433}
434
440template <typename TField, typename... TOptions>
442 const Optional<TField, TOptions...>& field1,
443 const Optional<TField, TOptions...>& field2) noexcept
444{
445 return !(field1 == field2);
446}
447
453template <typename TField, typename... TOptions>
455 const Optional<TField, TOptions...>& field1,
456 const Optional<TField, TOptions...>& field2) noexcept
457{
458 if (field1.isMissing()) {
459 return !field2.isMissing();
460 }
461
462 if (field2.isMissing()) {
463 return false;
464 }
465
466 return field1.field() < field2.field();
467}
468
474template <typename TField, typename... TOptions>
476 const Optional<TField, TOptions...>& field1,
477 const Optional<TField, TOptions...>& field2) noexcept
478{
479 return (field2 < field1);
480}
481
487template <typename TField, typename... TOptions>
489 const Optional<TField, TOptions...>& field1,
490 const Optional<TField, TOptions...>& field2) noexcept
491{
492 return (field1 < field2) || (field1 == field2);
493}
494
500template <typename TField, typename... TOptions>
502 const Optional<TField, TOptions...>& field1,
503 const Optional<TField, TOptions...>& field2) noexcept
504{
505 return field2 <= field1;
506}
507
513template <typename T>
514constexpr bool isOptional()
515{
516 return std::is_same<typename T::CommsTag, tag::Optional>::value;
517}
518
522template <typename TField, typename... TOptions>
523inline
524Optional<TField, TOptions...>&
526{
527 return field;
528}
529
533template <typename TField, typename... TOptions>
534inline
535const Optional<TField, TOptions...>&
537{
538 return field;
539}
540
541} // namespace field
542
543} // namespace comms
544
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:53
bool operator<(const Optional< TField, TOptions... > &field1, const Optional< TField, TOptions... > &field2) noexcept
Equivalence comparison operator.
Definition Optional.h:454
bool operator<=(const Optional< TField, TOptions... > &field1, const Optional< TField, TOptions... > &field2) noexcept
Equivalence comparison operator.
Definition Optional.h:488
ErrorStatus read(TIter &iter, std::size_t len)
Read field value from input data sequence.
Definition Optional.h:316
Optional()=default
Default constructor.
Optional(const Field &fieldSrc)
Construct the field.
Definition Optional.h:89
bool operator>(const Optional< TField, TOptions... > &field1, const Optional< TField, TOptions... > &field2) noexcept
Equivalence comparison operator.
Definition Optional.h:475
VersionType getVersion() const
Get version of the field.
Definition Optional.h:393
constexpr bool isOptional()
Compile time check function of whether a provided type is any variant of comms::field::Optional.
Definition Optional.h:514
void setExists()
Set mode to Mode::Exists.
Definition Optional.h:201
TField Field
Type of the field.
Definition Optional.h:69
const ValueType & getValue() const
Get value.
Definition Optional.h:232
const Field & field() const
Get an access to the wrapped field object.
Definition Optional.h:213
void writeNoStatus(TIter &iter) const
Write current field value to output data sequence without error check and status report.
Definition Optional.h:374
Optional(Field &&fieldSrc)
Construct the field.
Definition Optional.h:96
bool isTentative() const
Check whether mode is equivalent to Mode::Tentative.
Definition Optional.h:161
void readNoStatus(TIter &iter)
Read field value from input data sequence without error check and status report.
Definition Optional.h:334
bool operator>=(const Optional< TField, TOptions... > &field1, const Optional< TField, TOptions... > &field2) noexcept
Equivalence comparison operator.
Definition Optional.h:501
Field & field()
Get an access to the wrapped field object.
Definition Optional.h:207
details::OptionsParser< TOptions... > ParsedOptions
All the options provided to this class bundled into struct.
Definition Optional.h:63
bool operator!=(const Optional< TField, TOptions... > &field1, const Optional< TField, TOptions... > &field2) noexcept
Non-equality comparison operator.
Definition Optional.h:441
void setMissing()
Set mode to Mode::Missing.
Definition Optional.h:185
typename BaseImpl::CommsTag CommsTag
Tag indicating type of the field.
Definition Optional.h:66
static constexpr bool hasFieldType()
Compile time inquiry of whether comms::option::def::FieldType option has been used.
Definition Optional.h:139
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:525
static constexpr bool isVersionDependent()
Compile time check if this class is version dependent.
Definition Optional.h:380
static constexpr std::size_t maxLength()
Get maximal length that is required to serialise field of this type.
Definition Optional.h:276
void setValue(U &&val)
Set value.
Definition Optional.h:240
static constexpr bool hasIgnoreInvalid()
Compile time inquiry of whether comms::option::def::IgnoreInvalid option has been used.
Definition Optional.h:125
static constexpr bool hasName()
Compile time inquiry of whether comms::option::def::HasName option has been used.
Definition Optional.h:153
bool canWrite() const
Check of whether the field has a consistent value for writing.
Definition Optional.h:340
static constexpr bool hasReadNoStatus()
Compile time check of whether the field has proper readNoStatus() member function.
Definition Optional.h:323
static constexpr bool hasNonDefaultRefresh()
Compile time check if this class has non-default refresh functionality.
Definition Optional.h:386
const ValueType & value() const
Get an access to the wrapped field object.
Definition Optional.h:225
ValueType & value()
Get an access to the wrapped field object.
Definition Optional.h:219
bool doesExist() const
Check whether mode is equivalent to Mode::Exists.
Definition Optional.h:193
static constexpr bool hasFixedValue()
Compile time inquiry of whether comms::option::def::FixedValue option has been used.
Definition Optional.h:146
Mode getMode() const
Get current optional mode.
Definition Optional.h:246
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:536
typename BaseImpl::Endian Endian
Endian used for serialisation.
Definition Optional.h:57
std::size_t length() const
Get length required to serialise the current field value.
Definition Optional.h:262
~Optional() noexcept=default
Destructor.
void setMode(Mode val)
Get optional mode.
Definition Optional.h:252
bool operator==(const Optional< TField, TOptions... > &field1, const Optional< TField, TOptions... > &field2) noexcept
Equality comparison operator.
Definition Optional.h:420
static constexpr bool hasFailOnInvalid()
Compile time inquiry of whether comms::option::def::FailOnInvalid option has been used.
Definition Optional.h:118
bool valid() const
Check validity of the field value.
Definition Optional.h:285
bool isMissing() const
Check whether mode is equivalent to Mode::Missing.
Definition Optional.h:177
Field ValueType
Value type of this field, equal to Field.
Definition Optional.h:72
typename BaseImpl::VersionType VersionType
Version type.
Definition Optional.h:60
ErrorStatus write(TIter &iter, std::size_t len) const
Write current field value to output data sequence.
Definition Optional.h:356
bool refresh()
Refresh the field's value.
Definition Optional.h:295
static constexpr bool hasWriteNoStatus()
Compile time check of whether the field has proper writeNoStatus() member function.
Definition Optional.h:363
static constexpr std::size_t minLength()
Get minimal length that is required to serialise field of this type.
Definition Optional.h:269
static constexpr bool hasEmptySerialization()
Compile time inquiry of whether comms::option::def::EmptySerialization option has been used.
Definition Optional.h:132
bool setVersion(VersionType version)
Default implementation of version update.
Definition Optional.h:400
Optional(Optional &&)=default
Move constructor.
typename ParsedOptions::FieldType FieldType
Type of actual extending field specified via comms::option::def::FieldType.
Definition Optional.h:81
void setTentative()
Set mode to Mode::Tentative.
Definition Optional.h:169
OptionalMode
Mode to be used by comms::field::Optional.
Definition OptionalMode.h:24
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
STL namespace.