COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
ArrayList.h
Go to the documentation of this file.
1//
2// Copyright 2014 - 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 "basic/ArrayList.h"
17#include "comms/ErrorStatus.h"
18#include "comms/options.h"
22#include "details/AdaptBasicField.h"
23#include "details/OptionsParser.h"
24
25#include <cstddef>
26#include <cstdint>
27#include <type_traits>
28#include <utility>
29#include <vector>
30
31#if COMMS_HAS_CPP20_SPAN
32#include <span>
33#endif // #if COMMS_HAS_CPP20_SPAN
34
35namespace comms
36{
37
38namespace field
39{
40
41namespace details
42{
43
44template <bool THasOrigDataViewStorage>
45struct ArrayListOrigDataViewStorageType;
46
47template <>
48struct ArrayListOrigDataViewStorageType<true>
49{
50#if COMMS_HAS_CPP20_SPAN
51 template <typename TElement>
52 using Type = std::span<TElement>;
53#else // #if COMMS_HAS_CPP20_SPAN
54 template <typename TElement>
56#endif // #if COMMS_HAS_CPP20_SPAN
57};
58
59template <>
60struct ArrayListOrigDataViewStorageType<false>
61{
62 template <typename TElement>
63 using Type = std::vector<TElement>;
64};
65
66template <bool THasSequenceFixedSizeUseFixedSizeStorage>
67struct ArrayListSequenceFixedSizeUseFixedSizeStorageType;
68
69template <>
70struct ArrayListSequenceFixedSizeUseFixedSizeStorageType<true>
71{
72 template <typename TElement, typename TOpt>
74};
75
76template <>
77struct ArrayListSequenceFixedSizeUseFixedSizeStorageType<false>
78{
79 template <typename TElement, typename TOpt>
80 using Type =
81 typename ArrayListOrigDataViewStorageType<
82 TOpt::HasOrigDataView && std::is_integral<TElement>::value && (sizeof(TElement) == sizeof(std::uint8_t))
83 >::template Type<TElement>;
84};
85
86template <bool THasFixedSizeStorage>
87struct ArrayListFixedSizeStorageType;
88
89template <>
90struct ArrayListFixedSizeStorageType<true>
91{
92 template <typename TElement, typename TOpt>
94};
95
96template <>
97struct ArrayListFixedSizeStorageType<false>
98{
99 template <typename TElement, typename TOpt>
100 using Type =
101 typename ArrayListSequenceFixedSizeUseFixedSizeStorageType<TOpt::HasSequenceFixedSizeUseFixedSizeStorage>
102 ::template Type<TElement, TOpt>;
103};
104
105template <bool THasCustomStorage>
106struct ArrayListCustomArrayListStorageType;
107
108template <>
109struct ArrayListCustomArrayListStorageType<true>
110{
111 template <typename TElement, typename TOpt>
112 using Type = typename TOpt::CustomStorageType;
113};
114
115template <>
116struct ArrayListCustomArrayListStorageType<false>
117{
118 template <typename TElement, typename TOpt>
119 using Type =
120 typename ArrayListFixedSizeStorageType<TOpt::HasFixedSizeStorage>::template Type<TElement, TOpt>;
121};
122
123template <typename TElement, typename TOpt>
124using ArrayListStorageTypeT =
125 typename ArrayListCustomArrayListStorageType<TOpt::HasCustomStorageType>::template Type<TElement, TOpt>;
126
127template <typename TFieldBase, typename TElement, typename... TOptions>
128using ArrayListBase =
129 AdaptBasicFieldT<
130 comms::field::basic::ArrayList<
131 TFieldBase,
132 ArrayListStorageTypeT<TElement, OptionsParser<TOptions...> >
133 >,
134 TOptions...
135 >;
136
137} // namespace details
138
195
198template <typename TFieldBase, typename TElement, typename... TOptions>
199class ArrayList : public details::ArrayListBase<TFieldBase, TElement, TOptions...>
200{
201 using BaseImpl = details::ArrayListBase<TFieldBase, TElement, TOptions...>;
202public:
204 using FieldBase = TFieldBase;
205
207 using Endian = typename BaseImpl::Endian;
208
210 using VersionType = typename BaseImpl::VersionType;
211
213 using ParsedOptions = details::OptionsParser<TOptions...>;
214
216 using CommsTag = typename BaseImpl::CommsTag;
217
223 using ValueType = typename BaseImpl::ValueType;
224
226 using ElementType = typename BaseImpl::ElementType;
227
231 using FieldType = typename ParsedOptions::FieldType;
232
235 using SizeFieldPrefix = typename ParsedOptions::SequenceSizeFieldPrefix;
236
239 using SerLengthFieldPrefix = typename ParsedOptions::SequenceSerLengthFieldPrefix;
240
243 using ElemSerLengthFieldPrefix = typename ParsedOptions::SequenceElemSerLengthFieldPrefix;
244
247 using ElemFixedSerLengthFieldPrefix = typename ParsedOptions::SequenceElemFixedSerLengthFieldPrefix;
248
251 using TerminationFieldSuffix = typename ParsedOptions::SequenceTerminationFieldSuffix;
252
255 using TrailingFieldSuffix = typename ParsedOptions::SequenceTrailingFieldSuffix;
256
258 ArrayList() = default;
259
261 explicit ArrayList(const ValueType& val)
262 : BaseImpl(val)
263 {
264 }
265
267 explicit ArrayList(ValueType&& val)
268 : BaseImpl(std::move(val))
269 {
270 }
271
273 ArrayList(const ArrayList&) = default;
274
276 ArrayList(ArrayList&&) = default;
277
279 ~ArrayList() noexcept = default;
280
282 ArrayList& operator=(const ArrayList&) = default;
283
285 ArrayList& operator=(ArrayList&&) = default;
286
289 static constexpr bool hasFailOnInvalid()
290 {
291 return ParsedOptions::HasFailOnInvalid;
292 }
293
296 static constexpr bool hasIgnoreInvalid()
297 {
298 return ParsedOptions::HasIgnoreInvalid;
299 }
300
303 static constexpr bool hasEmptySerialization()
304 {
305 return ParsedOptions::HasEmptySerialization;
306 }
307
310 static constexpr bool hasFieldType()
311 {
312 return ParsedOptions::HasFieldType;
313 }
314
317 static constexpr bool hasSizeFieldPrefix()
318 {
319 return ParsedOptions::HasSequenceSizeFieldPrefix;
320 }
321
324 static constexpr bool hasSerLengthFieldPrefix()
325 {
326 return ParsedOptions::HasSequenceSerLengthFieldPrefix;
327 }
328
331 static constexpr bool hasElemSerLengthFieldPrefix()
332 {
333 return ParsedOptions::HasSequenceElemSerLengthFieldPrefix;
334 }
335
338 static constexpr bool hasElemFixedSerLengthFieldPrefix()
339 {
340 return ParsedOptions::HasSequenceElemFixedSerLengthFieldPrefix;
341 }
342
345 static constexpr bool hasTerminationFieldSuffix()
346 {
347 return ParsedOptions::HasSequenceTerminationFieldSuffix;
348 }
349
352 static constexpr bool hasTrailingFieldSuffix()
353 {
354 return ParsedOptions::HasSequenceTrailingFieldSuffix;
355 }
356
359 static constexpr bool hasFixedSize()
360 {
361 return ParsedOptions::HasSequenceFixedSize;
362 }
363
366 static constexpr bool hasFixedValue()
367 {
368 return ParsedOptions::HasFixedValue;
369 }
370
373 static constexpr bool hasName()
374 {
375 return ParsedOptions::HasName;
376 }
377
381 static constexpr std::size_t fixedSize()
382 {
383 return ParsedOptions::SequenceFixedSize;
384 }
385
388 {
389 return BaseImpl::value();
390 }
391
393 const ValueType& value() const
394 {
395 return BaseImpl::value();
396 }
397
400 const ValueType& getValue() const
401 {
402 return BaseImpl::getValue();
403 }
404
407 template <typename U>
408 void setValue(U&& val)
409 {
410 BaseImpl::setValue(std::forward<U>(val));
411 }
412
414 constexpr std::size_t length() const
415 {
416 return BaseImpl::length();
417 }
418
429 template <typename TIter>
430 ErrorStatus read(TIter& iter, std::size_t len)
431 {
432 return BaseImpl::read(iter, len);
433 }
434
437 static constexpr bool hasReadNoStatus()
438 {
439 return BaseImpl::hasReadNoStatus();
440 }
441
447 template <typename TIter>
448 void readNoStatus(TIter& iter)
449 {
450 BaseImpl::readNoStatus(iter);
451 }
452
454 bool canWrite() const
455 {
456 return BaseImpl::canWrite();
457 }
458
471 template <typename TIter>
472 ErrorStatus write(TIter& iter, std::size_t len) const
473 {
474 return BaseImpl::write(iter, len);
475 }
476
479 static constexpr bool hasWriteNoStatus()
480 {
481 return BaseImpl::hasWriteNoStatus();
482 }
483
489 template <typename TIter>
490 void writeNoStatus(TIter& iter) const
491 {
492 BaseImpl::writeNoStatus(iter);
493 }
494
498 bool valid() const
499 {
500 return BaseImpl::valid();
501 }
502
506 bool refresh()
507 {
508 return BaseImpl::refresh();
509 }
510
512 static constexpr std::size_t minLength()
513 {
514 return BaseImpl::minLength();
515 }
516
518 static constexpr std::size_t maxLength()
519 {
520 return BaseImpl::maxLength();
521 }
522
528 void forceReadElemCount(std::size_t count)
529 {
530 return BaseImpl::forceReadElemCount(count);
531 }
532
538 {
539 return BaseImpl::clearReadElemCount();
540 }
541
546 void forceReadLength(std::size_t count)
547 {
548 return BaseImpl::forceReadLength(count);
549 }
550
556 {
557 return BaseImpl::clearReadLengthForcing();
558 }
559
566 void forceReadElemLength(std::size_t count)
567 {
568 return BaseImpl::forceReadElemLength(count);
569 }
570
575 {
576 return BaseImpl::clearReadElemLengthForcing();
577 }
578
580 static constexpr bool isVersionDependent()
581 {
582 return ParsedOptions::HasCustomVersionUpdate || BaseImpl::isVersionDependent();
583 }
584
586 static constexpr bool hasNonDefaultRefresh()
587 {
588 return BaseImpl::hasNonDefaultRefresh();
589 }
590
594 {
595 return BaseImpl::getVersion();
596 }
597
601 {
602 return BaseImpl::setVersion(version);
603 }
604
605#ifdef FOR_DOXYGEN_DOC_ONLY
611 auto terminationFieldSuffix() -> decltype(BaseImpl::terminationFieldSuffix())
612 {
613 return BaseImpl::terminationFieldSuffix();
614 }
615
621 auto terminationFieldSuffix() const -> decltype(BaseImpl::terminationFieldSuffix())
622 {
623 return BaseImpl::terminationFieldSuffix();
624 }
625
626#endif // #ifdef FOR_DOXYGEN_DOC_ONLY
627
628protected:
629 using BaseImpl::readData;
630 using BaseImpl::writeData;
631
632private:
633 static_assert(!ParsedOptions::HasSerOffset,
634 "comms::option::def::NumValueSerOffset option is not applicable to ArrayList field");
635 static_assert(!ParsedOptions::HasFixedLengthLimit,
636 "comms::option::def::FixedLength option is not applicable to ArrayList field");
637 static_assert(!ParsedOptions::HasFixedBitLengthLimit,
638 "comms::option::def::FixedBitLength option is not applicable to ArrayList field");
639 static_assert(!ParsedOptions::HasVarLengthLimits,
640 "comms::option::def::VarLength option is not applicable to ArrayList field");
641 static_assert(!ParsedOptions::HasAvailableLengthLimit,
642 "comms::option::def::AvailableLengthLimit option is not applicable to ArrayList field");
643 static_assert(!ParsedOptions::HasScalingRatio,
644 "comms::option::def::ScalingRatio option is not applicable to ArrayList field");
645 static_assert(!ParsedOptions::HasUnits,
646 "comms::option::def::Units option is not applicable to ArrayList field");
647 static_assert(!ParsedOptions::HasMultiRangeValidation,
648 "comms::option::def::ValidNumValueRange (or similar) option is not applicable to ArrayList field");
649 static_assert((!ParsedOptions::HasOrigDataView) || (std::is_integral<TElement>::value && (sizeof(TElement) == sizeof(std::uint8_t))),
650 "Usage of comms::option::app::OrigDataView option is allowed only for raw binary data (std::uint8_t) types.");
651 static_assert(!ParsedOptions::HasVersionsRange,
652 "comms::option::def::ExistsBetweenVersions (or similar) option is not applicable to ArrayList field");
653 static_assert(!ParsedOptions::HasInvalidByDefault,
654 "comms::option::def::InvalidByDefault option is not applicable to ArrayList field");
655 static_assert(!ParsedOptions::HasMissingOnReadFail,
656 "comms::option::def::MissingOnReadFail option is not applicable to ArrayList field");
657 static_assert(!ParsedOptions::HasMissingOnInvalid,
658 "comms::option::def::MissingOnInvalid option is not applicable to ArrayList field");
659};
660
667template <typename TFieldBase, typename TElement, typename... TOptions>
671{
672 return std::lexicographical_compare(
673 field1.value().begin(), field1.value().end(),
674 field2.value().begin(), field2.value().end());
675}
676
682template <typename TFieldBase, typename TElement, typename... TOptions>
686{
687 auto& vec1 = field1.value();
688 auto& vec2 = field2.value();
689 if (vec1.size() != vec2.size()) {
690 return false;
691 }
692
693 for (auto idx = 0U; idx < vec1.size(); ++idx) {
694 if (vec1[idx] != vec2[idx]) {
695 return false;
696 }
697 }
698 return true;
699}
700
706template <typename TFieldBase, typename TElement, typename... TOptions>
710{
711 return !(field1 == field2);
712}
713
719template <typename T>
720constexpr bool isArrayList()
721{
722 return
723 std::is_same<typename T::CommsTag, tag::ArrayList>::value ||
724 std::is_same<typename T::CommsTag, tag::RawArrayList>::value;
725}
726
730template <typename TFieldBase, typename TElement, typename... TOptions>
731inline
732ArrayList<TFieldBase, TElement, TOptions...>&
737
741template <typename TFieldBase, typename TElement, typename... TOptions>
742inline
743const ArrayList<TFieldBase, TElement, TOptions...>&
745{
746 return field;
747}
748
749} // namespace field
750
751} // namespace comms
752
Contains comms::util::ArrayView class.
Contains various compiler related definitions.
This file contain definition of error statuses used by comms module.
Contains comms::util::StaticVector class.
Field that represents a sequential collection of fields.
Definition ArrayList.h:200
static constexpr bool hasEmptySerialization()
Compile time inquiry of whether comms::option::def::EmptySerialization option has been used.
Definition ArrayList.h:303
const ValueType & value() const
Get access to the value storage.
Definition ArrayList.h:393
static constexpr bool hasFixedSize()
Compile time inquiry of whether comms::option::def::SequenceFixedSize option has been used.
Definition ArrayList.h:359
typename BaseImpl::ElementType ElementType
Type of the element.
Definition ArrayList.h:226
typename BaseImpl::VersionType VersionType
Version type.
Definition ArrayList.h:210
static constexpr bool hasTrailingFieldSuffix()
Compile time inquiry of whether comms::option::def::SequenceTrailingFieldSuffix option has been used.
Definition ArrayList.h:352
void clearReadElemLengthForcing()
Clear forcing the serialisation length of the single element.
Definition ArrayList.h:574
static constexpr bool hasElemSerLengthFieldPrefix()
Compile time inquiry of whether comms::option::def::SequenceElemSerLengthFieldPrefix option has been ...
Definition ArrayList.h:331
void writeNoStatus(TIter &iter) const
Write current field value to output data sequence without error check and status report.
Definition ArrayList.h:490
typename ParsedOptions::SequenceTrailingFieldSuffix TrailingFieldSuffix
Type of trailing field suffix specified via comms::option::def::SequenceTrailingFieldSuffix.
Definition ArrayList.h:255
static constexpr bool hasFailOnInvalid()
Compile time inquiry of whether comms::option::def::FailOnInvalid option has been used.
Definition ArrayList.h:289
ErrorStatus read(TIter &iter, std::size_t len)
Read field value from input data sequence.
Definition ArrayList.h:430
static constexpr bool hasName()
Compile time inquiry of whether comms::option::def::HasName option has been used.
Definition ArrayList.h:373
static constexpr bool hasReadNoStatus()
Compile time check of whether the field has proper readNoStatus() member function.
Definition ArrayList.h:437
static constexpr bool isVersionDependent()
Compile time check if this class is version dependent.
Definition ArrayList.h:580
VersionType getVersion() const
Get version of the field.
Definition ArrayList.h:593
const ArrayList< TFieldBase, TElement, TOptions... > & toFieldBase(const ArrayList< TFieldBase, TElement, TOptions... > &field)
Upcast type of the field definition to its parent comms::field::ArrayList type in order to have acces...
Definition ArrayList.h:744
typename BaseImpl::Endian Endian
Endian used for serialisation.
Definition ArrayList.h:207
void clearReadLengthForcing()
Clear forcing of the available length in the next read() invocation.
Definition ArrayList.h:555
static constexpr bool hasTerminationFieldSuffix()
Compile time inquiry of whether comms::option::def::SequenceTerminationFieldSuffix option has been us...
Definition ArrayList.h:345
void setValue(U &&val)
Set value.
Definition ArrayList.h:408
static constexpr bool hasFixedValue()
Compile time inquiry of whether comms::option::def::FixedValue option has been used.
Definition ArrayList.h:366
ValueType & value()
Get access to the value storage.
Definition ArrayList.h:387
bool operator<(const ArrayList< TFieldBase, TElement, TOptions... > &field1, const ArrayList< TFieldBase, TElement, TOptions... > &field2) noexcept
Equivalence comparison operator.
Definition ArrayList.h:668
ArrayList(const ValueType &val)
Value constructor.
Definition ArrayList.h:261
void readNoStatus(TIter &iter)
Read field value from input data sequence without error check and status report.
Definition ArrayList.h:448
typename ParsedOptions::SequenceSizeFieldPrefix SizeFieldPrefix
Type of size field prefix specified via comms::option::def::SequenceSizeFieldPrefix.
Definition ArrayList.h:235
void forceReadElemCount(std::size_t count)
Force number of elements that must be read in the next read() invocation.
Definition ArrayList.h:528
typename ParsedOptions::SequenceElemSerLengthFieldPrefix ElemSerLengthFieldPrefix
Type of element's length field prefix specified via comms::option::def::SequenceElemSerLengthFieldPre...
Definition ArrayList.h:243
ArrayList(const ArrayList &)=default
Copy constructor.
constexpr std::size_t length() const
Get length of serialised data.
Definition ArrayList.h:414
void forceReadElemLength(std::size_t count)
Force serialisation length of a single element.
Definition ArrayList.h:566
static constexpr std::size_t minLength()
Get minimal length that is required to serialise field of this type.
Definition ArrayList.h:512
ArrayList< TFieldBase, TElement, TOptions... > & toFieldBase(ArrayList< TFieldBase, TElement, TOptions... > &field)
Upcast type of the field definition to its parent comms::field::ArrayList type in order to have acces...
Definition ArrayList.h:733
~ArrayList() noexcept=default
Destructor.
bool refresh()
Refresh the field.
Definition ArrayList.h:506
static constexpr std::size_t fixedSize()
Compile time inquiry of fixed size provided via comms::option::def::SequenceFixedSize option.
Definition ArrayList.h:381
typename BaseImpl::ValueType ValueType
Type of underlying value.
Definition ArrayList.h:223
constexpr bool isArrayList()
Compile time check function of whether a provided type is any variant of comms::field::ArrayList.
Definition ArrayList.h:720
bool setVersion(VersionType version)
Default implementation of version update.
Definition ArrayList.h:600
static constexpr bool hasSerLengthFieldPrefix()
Compile time inquiry of whether comms::option::def::SequenceSerLengthFieldPrefix option has been used...
Definition ArrayList.h:324
auto terminationFieldSuffix() const -> decltype(BaseImpl::terminationFieldSuffix())
Access list termination field (const variant)
Definition ArrayList.h:621
typename ParsedOptions::FieldType FieldType
Type of actual extending field specified via comms::option::def::FieldType.
Definition ArrayList.h:231
const ValueType & getValue() const
Get value.
Definition ArrayList.h:400
static constexpr bool hasElemFixedSerLengthFieldPrefix()
Compile time inquiry of whether comms::option::def::SequenceElemFixedSerLengthFieldPrefix option has ...
Definition ArrayList.h:338
static constexpr bool hasWriteNoStatus()
Compile time check of whether the field has proper writeNoStatus() member function.
Definition ArrayList.h:479
bool valid() const
Check validity of the field value.
Definition ArrayList.h:498
static constexpr bool hasIgnoreInvalid()
Compile time inquiry of whether comms::option::def::IgnoreInvalid option has been used.
Definition ArrayList.h:296
typename ParsedOptions::SequenceSerLengthFieldPrefix SerLengthFieldPrefix
Type of length field prefix specified via comms::option::def::SequenceSerLengthFieldPrefix.
Definition ArrayList.h:239
typename ParsedOptions::SequenceElemFixedSerLengthFieldPrefix ElemFixedSerLengthFieldPrefix
Type of element's fixed length field prefix specified via comms::option::def::SequenceElemFixedSerLen...
Definition ArrayList.h:247
TFieldBase FieldBase
Base class provided in the first template parameter.
Definition ArrayList.h:204
ErrorStatus write(TIter &iter, std::size_t len) const
Write current field value to output data sequence.
Definition ArrayList.h:472
auto terminationFieldSuffix() -> decltype(BaseImpl::terminationFieldSuffix())
Access list termination field.
Definition ArrayList.h:611
static constexpr bool hasFieldType()
Compile time inquiry of whether comms::option::def::FieldType option has been used.
Definition ArrayList.h:310
ArrayList(ArrayList &&)=default
Move constructor.
bool canWrite() const
Check of whether the field has a consistent value for writing.
Definition ArrayList.h:454
void clearReadElemCount()
Clear forcing of the number of elements that must be read in the next read() invocation.
Definition ArrayList.h:537
void forceReadLength(std::size_t count)
Force available length for the next read() invocation.
Definition ArrayList.h:546
static constexpr bool hasNonDefaultRefresh()
Compile time check if this class has non-default refresh functionality.
Definition ArrayList.h:586
static constexpr std::size_t maxLength()
Get maximal length that is required to serialise field of this type.
Definition ArrayList.h:518
bool operator!=(const ArrayList< TFieldBase, TElement, TOptions... > &field1, const ArrayList< TFieldBase, TElement, TOptions... > &field2) noexcept
Non-equality comparison operator.
Definition ArrayList.h:707
details::OptionsParser< TOptions... > ParsedOptions
All the options provided to this class bundled into struct.
Definition ArrayList.h:213
typename BaseImpl::CommsTag CommsTag
Tag indicating type of the field.
Definition ArrayList.h:216
typename ParsedOptions::SequenceTerminationFieldSuffix TerminationFieldSuffix
Type of termination field suffix specified via comms::option::def::SequenceTerminationFieldSuffix.
Definition ArrayList.h:251
ArrayList()=default
Default constructor.
bool operator==(const ArrayList< TFieldBase, TElement, TOptions... > &field1, const ArrayList< TFieldBase, TElement, TOptions... > &field2) noexcept
Equality comparison operator.
Definition ArrayList.h:683
ArrayList(ValueType &&val)
Value constructor.
Definition ArrayList.h:267
static constexpr bool hasSizeFieldPrefix()
Compile time inquiry of whether comms::option::def::SequenceSizeFieldPrefix option has been used.
Definition ArrayList.h:317
Describes an object that can refer to a constant contiguous sequence of other objects.
Definition ArrayView.h:32
Replacement to std::vector when no dynamic memory allocation is allowed.
Definition StaticVector.h:961
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.
Contains definition of all the options used by the COMMS library.
Replacement to some types from standard type_traits.