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 "comms/field/basic/Optional.h"
16#include "comms/field/details/AdaptBasicField.h"
17#include "comms/field/details/OptionsParser.h"
19
20#include <utility>
21
22namespace comms
23{
24
25namespace field
26{
27
47template <typename TField, typename... TOptions>
48class Optional : public details::AdaptBasicFieldT<basic::Optional<TField>, TOptions...>
49{
50 using BaseImpl = details::AdaptBasicFieldT<basic::Optional<TField>, TOptions...>;
51public:
53 using Endian = typename BaseImpl::Endian;
54
56 using VersionType = typename BaseImpl::VersionType;
57
59 using ParsedOptions = details::OptionsParser<TOptions...>;
60
62 using CommsTag = typename BaseImpl::CommsTag;
63
65 using Field = TField;
66
69
73
77 using FieldType = typename ParsedOptions::FieldType;
78
81 Optional() = default;
82
85 explicit Optional(const Field& fieldSrc)
86 : BaseImpl(fieldSrc)
87 {
88 }
89
92 explicit Optional(Field&& fieldSrc)
93 : BaseImpl(std::move(fieldSrc))
94 {
95 }
96
98 Optional(const Optional&) = default;
99
101 Optional(Optional&&) = default;
102
104 ~Optional() noexcept = default;
105
107 Optional& operator=(const Optional&) = default;
108
110 Optional& operator=(Optional&&) = default;
111
114 static constexpr bool hasFailOnInvalid()
115 {
116 return ParsedOptions::HasFailOnInvalid;
117 }
118
121 static constexpr bool hasIgnoreInvalid()
122 {
123 return ParsedOptions::HasIgnoreInvalid;
124 }
125
128 static constexpr bool hasEmptySerialization()
129 {
130 return ParsedOptions::HasEmptySerialization;
131 }
132
135 static constexpr bool hasFieldType()
136 {
137 return ParsedOptions::HasFieldType;
138 }
139
142 static constexpr bool hasFixedValue()
143 {
144 return ParsedOptions::HasFixedValue;
145 }
146
149 static constexpr bool hasName()
150 {
151 return ParsedOptions::HasName;
152 }
153
157 bool isTentative() const
158 {
159 return BaseImpl::getMode() == Mode::Tentative;
160 }
161
166 {
167 BaseImpl::setMode(Mode::Tentative);
168 }
169
173 bool isMissing() const
174 {
175 return BaseImpl::getMode() == Mode::Missing;
176 }
177
182 {
183 BaseImpl::setMode(Mode::Missing);
184 }
185
189 bool doesExist() const
190 {
191 return BaseImpl::getMode() == Mode::Exists;
192 }
193
198 {
199 BaseImpl::setMode(Mode::Exists);
200 }
201
204 {
205 return BaseImpl::field();
206 }
207
209 const Field& field() const
210 {
211 return BaseImpl::field();
212 }
213
216 {
217 return BaseImpl::value();
218 }
219
221 const ValueType& value() const
222 {
223 return BaseImpl::value();
224 }
225
228 const ValueType& getValue() const
229 {
230 return BaseImpl::getValue();
231 }
232
235 template <typename U>
236 void setValue(U&& val)
237 {
238 BaseImpl::setValue(std::forward<U>(val));
239 }
240
242 Mode getMode() const
243 {
244 return BaseImpl::getMode();
245 }
246
248 void setMode(Mode val)
249 {
250 BaseImpl::setMode(val);
251 }
252
258 std::size_t length() const
259 {
260 return BaseImpl::length();
261 }
262
265 static constexpr std::size_t minLength()
266 {
267 return BaseImpl::minLength();
268 }
269
272 static constexpr std::size_t maxLength()
273 {
274 return BaseImpl::maxLength();
275 }
276
281 bool valid() const
282 {
283 return BaseImpl::valid();
284 }
285
291 bool refresh()
292 {
293 return BaseImpl::refresh();
294 }
295
311 template <typename TIter>
312 ErrorStatus read(TIter& iter, std::size_t len)
313 {
314 return BaseImpl::read(iter, len);
315 }
316
319 static constexpr bool hasReadNoStatus()
320 {
321 return BaseImpl::hasReadNoStatus();
322 }
323
329 template <typename TIter>
330 void readNoStatus(TIter& iter)
331 {
332 BaseImpl::readNoStatus(iter);
333 }
334
336 bool canWrite() const
337 {
338 return BaseImpl::canWrite();
339 }
340
351 template <typename TIter>
352 ErrorStatus write(TIter& iter, std::size_t len) const
353 {
354 return BaseImpl::write(iter, len);
355 }
356
359 static constexpr bool hasWriteNoStatus()
360 {
361 return BaseImpl::hasWriteNoStatus();
362 }
363
369 template <typename TIter>
370 void writeNoStatus(TIter& iter) const
371 {
372 BaseImpl::writeNoStatus(iter);
373 }
374
376 static constexpr bool isVersionDependent()
377 {
378 return ParsedOptions::HasCustomVersionUpdate || BaseImpl::isVersionDependent();
379 }
380
382 static constexpr bool hasNonDefaultRefresh()
383 {
384 return BaseImpl::hasNonDefaultRefresh();
385 }
386
390 {
391 return BaseImpl::getVersion();
392 }
393
397 {
398 return BaseImpl::setVersion(version);
399 }
400
401protected:
402 using BaseImpl::readData;
403 using BaseImpl::writeData;
404
405private:
406 static_assert(!ParsedOptions::HasInvalidByDefault,
407 "comms::option::def::InvalidByDefault option is not applicable to Optional field");
408};
409
415template <typename TField, typename... TOptions>
417 const Optional<TField, TOptions...>& field1,
418 const Optional<TField, TOptions...>& field2) noexcept
419{
420 if (field1.getMode() != field2.getMode()) {
421 return false;
422 }
423
424 if (field1.isMissing()) {
425 return true;
426 }
427
428 return field1.field() == field2.field();
429}
430
436template <typename TField, typename... TOptions>
438 const Optional<TField, TOptions...>& field1,
439 const Optional<TField, TOptions...>& field2) noexcept
440{
441 return !(field1 == field2);
442}
443
449template <typename TField, typename... TOptions>
451 const Optional<TField, TOptions...>& field1,
452 const Optional<TField, TOptions...>& field2) noexcept
453{
454 if (field1.isMissing()) {
455 return !field2.isMissing();
456 }
457
458 if (field2.isMissing()) {
459 return false;
460 }
461
462 return field1.field() < field2.field();
463}
464
470template <typename TField, typename... TOptions>
472 const Optional<TField, TOptions...>& field1,
473 const Optional<TField, TOptions...>& field2) noexcept
474{
475 return (field2 < field1);
476}
477
483template <typename TField, typename... TOptions>
485 const Optional<TField, TOptions...>& field1,
486 const Optional<TField, TOptions...>& field2) noexcept
487{
488 return (field1 < field2) || (field1 == field2);
489}
490
496template <typename TField, typename... TOptions>
498 const Optional<TField, TOptions...>& field1,
499 const Optional<TField, TOptions...>& field2) noexcept
500{
501 return field2 <= field1;
502}
503
509template <typename T>
510constexpr bool isOptional()
511{
512 return std::is_same<typename T::CommsTag, tag::Optional>::value;
513}
514
518template <typename TField, typename... TOptions>
519inline
520Optional<TField, TOptions...>&
522{
523 return field;
524}
525
529template <typename TField, typename... TOptions>
530inline
531const Optional<TField, TOptions...>&
533{
534 return field;
535}
536
537} // namespace field
538
539} // namespace comms
540
541
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:49
bool operator<(const Optional< TField, TOptions... > &field1, const Optional< TField, TOptions... > &field2) noexcept
Equivalence comparison operator.
Definition Optional.h:450
bool operator<=(const Optional< TField, TOptions... > &field1, const Optional< TField, TOptions... > &field2) noexcept
Equivalence comparison operator.
Definition Optional.h:484
ErrorStatus read(TIter &iter, std::size_t len)
Read field value from input data sequence.
Definition Optional.h:312
Optional()=default
Default constructor.
Optional(const Field &fieldSrc)
Construct the field.
Definition Optional.h:85
bool operator>(const Optional< TField, TOptions... > &field1, const Optional< TField, TOptions... > &field2) noexcept
Equivalence comparison operator.
Definition Optional.h:471
VersionType getVersion() const
Get version of the field.
Definition Optional.h:389
constexpr bool isOptional()
Compile time check function of whether a provided type is any variant of comms::field::Optional.
Definition Optional.h:510
void setExists()
Set mode to Mode::Exists.
Definition Optional.h:197
TField Field
Type of the field.
Definition Optional.h:65
const ValueType & getValue() const
Get value.
Definition Optional.h:228
const Field & field() const
Get an access to the wrapped field object.
Definition Optional.h:209
void writeNoStatus(TIter &iter) const
Write current field value to output data sequence without error check and status report.
Definition Optional.h:370
Optional(Field &&fieldSrc)
Construct the field.
Definition Optional.h:92
bool isTentative() const
Check whether mode is equivalent to Mode::Tentative.
Definition Optional.h:157
void readNoStatus(TIter &iter)
Read field value from input data sequence without error check and status report.
Definition Optional.h:330
bool operator>=(const Optional< TField, TOptions... > &field1, const Optional< TField, TOptions... > &field2) noexcept
Equivalence comparison operator.
Definition Optional.h:497
Field & field()
Get an access to the wrapped field object.
Definition Optional.h:203
details::OptionsParser< TOptions... > ParsedOptions
All the options provided to this class bundled into struct.
Definition Optional.h:59
bool operator!=(const Optional< TField, TOptions... > &field1, const Optional< TField, TOptions... > &field2) noexcept
Non-equality comparison operator.
Definition Optional.h:437
void setMissing()
Set mode to Mode::Missing.
Definition Optional.h:181
typename BaseImpl::CommsTag CommsTag
Tag indicating type of the field.
Definition Optional.h:62
static constexpr bool hasFieldType()
Compile time inquiry of whether comms::option::def::FieldType option has been used.
Definition Optional.h:135
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:521
static constexpr bool isVersionDependent()
Compile time check if this class is version dependent.
Definition Optional.h:376
static constexpr std::size_t maxLength()
Get maximal length that is required to serialise field of this type.
Definition Optional.h:272
void setValue(U &&val)
Set value.
Definition Optional.h:236
static constexpr bool hasIgnoreInvalid()
Compile time inquiry of whether comms::option::def::IgnoreInvalid option has been used.
Definition Optional.h:121
static constexpr bool hasName()
Compile time inquiry of whether comms::option::def::HasName option has been used.
Definition Optional.h:149
bool canWrite() const
Check of whether the field has a consistent value for writing.
Definition Optional.h:336
static constexpr bool hasReadNoStatus()
Compile time check of whether the field has proper readNoStatus() member function.
Definition Optional.h:319
static constexpr bool hasNonDefaultRefresh()
Compile time check if this class has non-default refresh functionality.
Definition Optional.h:382
const ValueType & value() const
Get an access to the wrapped field object.
Definition Optional.h:221
ValueType & value()
Get an access to the wrapped field object.
Definition Optional.h:215
bool doesExist() const
Check whether mode is equivalent to Mode::Exists.
Definition Optional.h:189
static constexpr bool hasFixedValue()
Compile time inquiry of whether comms::option::def::FixedValue option has been used.
Definition Optional.h:142
Mode getMode() const
Get current optional mode.
Definition Optional.h:242
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:532
typename BaseImpl::Endian Endian
Endian used for serialisation.
Definition Optional.h:53
std::size_t length() const
Get length required to serialise the current field value.
Definition Optional.h:258
~Optional() noexcept=default
Destructor.
void setMode(Mode val)
Get optional mode.
Definition Optional.h:248
bool operator==(const Optional< TField, TOptions... > &field1, const Optional< TField, TOptions... > &field2) noexcept
Equality comparison operator.
Definition Optional.h:416
static constexpr bool hasFailOnInvalid()
Compile time inquiry of whether comms::option::def::FailOnInvalid option has been used.
Definition Optional.h:114
bool valid() const
Check validity of the field value.
Definition Optional.h:281
bool isMissing() const
Check whether mode is equivalent to Mode::Missing.
Definition Optional.h:173
Field ValueType
Value type of this field, equal to Field.
Definition Optional.h:68
typename BaseImpl::VersionType VersionType
Version type.
Definition Optional.h:56
ErrorStatus write(TIter &iter, std::size_t len) const
Write current field value to output data sequence.
Definition Optional.h:352
bool refresh()
Refresh the field's value.
Definition Optional.h:291
static constexpr bool hasWriteNoStatus()
Compile time check of whether the field has proper writeNoStatus() member function.
Definition Optional.h:359
static constexpr std::size_t minLength()
Get minimal length that is required to serialise field of this type.
Definition Optional.h:265
static constexpr bool hasEmptySerialization()
Compile time inquiry of whether comms::option::def::EmptySerialization option has been used.
Definition Optional.h:128
bool setVersion(VersionType version)
Default implementation of version update.
Definition Optional.h:396
Optional(Optional &&)=default
Move constructor.
typename ParsedOptions::FieldType FieldType
Type of actual extending field specified via comms::option::def::FieldType.
Definition Optional.h:77
void setTentative()
Set mode to Mode::Tentative.
Definition Optional.h:165
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.