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 - 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
14
15#include <vector>
16
17#if COMMS_HAS_CPP20_SPAN
18#include <span>
19#endif // #if COMMS_HAS_CPP20_SPAN
20
21#include "comms/ErrorStatus.h"
22#include "comms/options.h"
26#include "basic/ArrayList.h"
27#include "details/AdaptBasicField.h"
28#include "details/OptionsParser.h"
29
30namespace comms
31{
32
33namespace field
34{
35
36namespace details
37{
38
39template <bool THasOrigDataViewStorage>
40struct ArrayListOrigDataViewStorageType;
41
42template <>
43struct ArrayListOrigDataViewStorageType<true>
44{
45#if COMMS_HAS_CPP20_SPAN
46 template <typename TElement>
47 using Type = std::span<TElement>;
48#else // #if COMMS_HAS_CPP20_SPAN
49 template <typename TElement>
51#endif // #if COMMS_HAS_CPP20_SPAN
52};
53
54template <>
55struct ArrayListOrigDataViewStorageType<false>
56{
57 template <typename TElement>
58 using Type = std::vector<TElement>;
59};
60
61template <bool THasSequenceFixedSizeUseFixedSizeStorage>
62struct ArrayListSequenceFixedSizeUseFixedSizeStorageType;
63
64template <>
65struct ArrayListSequenceFixedSizeUseFixedSizeStorageType<true>
66{
67 template <typename TElement, typename TOpt>
69};
70
71template <>
72struct ArrayListSequenceFixedSizeUseFixedSizeStorageType<false>
73{
74 template <typename TElement, typename TOpt>
75 using Type =
76 typename ArrayListOrigDataViewStorageType<
77 TOpt::HasOrigDataView && std::is_integral<TElement>::value && (sizeof(TElement) == sizeof(std::uint8_t))
78 >::template Type<TElement>;
79};
80
81template <bool THasFixedSizeStorage>
82struct ArrayListFixedSizeStorageType;
83
84template <>
85struct ArrayListFixedSizeStorageType<true>
86{
87 template <typename TElement, typename TOpt>
89};
90
91template <>
92struct ArrayListFixedSizeStorageType<false>
93{
94 template <typename TElement, typename TOpt>
95 using Type =
96 typename ArrayListSequenceFixedSizeUseFixedSizeStorageType<TOpt::HasSequenceFixedSizeUseFixedSizeStorage>
97 ::template Type<TElement, TOpt>;
98};
99
100template <bool THasCustomStorage>
101struct ArrayListCustomArrayListStorageType;
102
103template <>
104struct ArrayListCustomArrayListStorageType<true>
105{
106 template <typename TElement, typename TOpt>
107 using Type = typename TOpt::CustomStorageType;
108};
109
110template <>
111struct ArrayListCustomArrayListStorageType<false>
112{
113 template <typename TElement, typename TOpt>
114 using Type =
115 typename ArrayListFixedSizeStorageType<TOpt::HasFixedSizeStorage>::template Type<TElement, TOpt>;
116};
117
118template <typename TElement, typename TOpt>
119using ArrayListStorageTypeT =
120 typename ArrayListCustomArrayListStorageType<TOpt::HasCustomStorageType>::template Type<TElement, TOpt>;
121
122template <typename TFieldBase, typename TElement, typename... TOptions>
123using ArrayListBase =
124 AdaptBasicFieldT<
125 comms::field::basic::ArrayList<
126 TFieldBase,
127 ArrayListStorageTypeT<TElement, OptionsParser<TOptions...> >
128 >,
129 TOptions...
130 >;
131
132} // namespace details
133
190
193template <typename TFieldBase, typename TElement, typename... TOptions>
194class ArrayList : public details::ArrayListBase<TFieldBase, TElement, TOptions...>
195{
196 using BaseImpl = details::ArrayListBase<TFieldBase, TElement, TOptions...>;
197public:
199 using FieldBase = TFieldBase;
200
202 using Endian = typename BaseImpl::Endian;
203
205 using VersionType = typename BaseImpl::VersionType;
206
208 using ParsedOptions = details::OptionsParser<TOptions...>;
209
211 using CommsTag = typename BaseImpl::CommsTag;
212
218 using ValueType = typename BaseImpl::ValueType;
219
221 using ElementType = typename BaseImpl::ElementType;
222
226 using FieldType = typename ParsedOptions::FieldType;
227
230 using SizeFieldPrefix = typename ParsedOptions::SequenceSizeFieldPrefix;
231
234 using SerLengthFieldPrefix = typename ParsedOptions::SequenceSerLengthFieldPrefix;
235
238 using ElemSerLengthFieldPrefix = typename ParsedOptions::SequenceElemSerLengthFieldPrefix;
239
242 using ElemFixedSerLengthFieldPrefix = typename ParsedOptions::SequenceElemFixedSerLengthFieldPrefix;
243
246 using TerminationFieldSuffix = typename ParsedOptions::SequenceTerminationFieldSuffix;
247
250 using TrailingFieldSuffix = typename ParsedOptions::SequenceTrailingFieldSuffix;
251
253 ArrayList() = default;
254
256 explicit ArrayList(const ValueType& val)
257 : BaseImpl(val)
258 {
259 }
260
262 explicit ArrayList(ValueType&& val)
263 : BaseImpl(std::move(val))
264 {
265 }
266
268 ArrayList(const ArrayList&) = default;
269
271 ArrayList(ArrayList&&) = default;
272
274 ~ArrayList() noexcept = default;
275
277 ArrayList& operator=(const ArrayList&) = default;
278
280 ArrayList& operator=(ArrayList&&) = default;
281
284 static constexpr bool hasFailOnInvalid()
285 {
286 return ParsedOptions::HasFailOnInvalid;
287 }
288
291 static constexpr bool hasIgnoreInvalid()
292 {
293 return ParsedOptions::HasIgnoreInvalid;
294 }
295
298 static constexpr bool hasEmptySerialization()
299 {
300 return ParsedOptions::HasEmptySerialization;
301 }
302
305 static constexpr bool hasFieldType()
306 {
307 return ParsedOptions::HasFieldType;
308 }
309
312 static constexpr bool hasSizeFieldPrefix()
313 {
314 return ParsedOptions::HasSequenceSizeFieldPrefix;
315 }
316
319 static constexpr bool hasSerLengthFieldPrefix()
320 {
321 return ParsedOptions::HasSequenceSerLengthFieldPrefix;
322 }
323
326 static constexpr bool hasElemSerLengthFieldPrefix()
327 {
328 return ParsedOptions::HasSequenceElemSerLengthFieldPrefix;
329 }
330
333 static constexpr bool hasElemFixedSerLengthFieldPrefix()
334 {
335 return ParsedOptions::HasSequenceElemFixedSerLengthFieldPrefix;
336 }
337
340 static constexpr bool hasTerminationFieldSuffix()
341 {
342 return ParsedOptions::HasSequenceTerminationFieldSuffix;
343 }
344
347 static constexpr bool hasTrailingFieldSuffix()
348 {
349 return ParsedOptions::HasSequenceTrailingFieldSuffix;
350 }
351
354 static constexpr bool hasFixedSize()
355 {
356 return ParsedOptions::HasSequenceFixedSize;
357 }
358
361 static constexpr bool hasFixedValue()
362 {
363 return ParsedOptions::HasFixedValue;
364 }
365
368 static constexpr bool hasName()
369 {
370 return ParsedOptions::HasName;
371 }
372
376 static constexpr std::size_t fixedSize()
377 {
378 return ParsedOptions::SequenceFixedSize;
379 }
380
383 {
384 return BaseImpl::value();
385 }
386
388 const ValueType& value() const
389 {
390 return BaseImpl::value();
391 }
392
395 const ValueType& getValue() const
396 {
397 return BaseImpl::getValue();
398 }
399
402 template <typename U>
403 void setValue(U&& val)
404 {
405 BaseImpl::setValue(std::forward<U>(val));
406 }
407
409 constexpr std::size_t length() const
410 {
411 return BaseImpl::length();
412 }
413
424 template <typename TIter>
425 ErrorStatus read(TIter& iter, std::size_t len)
426 {
427 return BaseImpl::read(iter, len);
428 }
429
432 static constexpr bool hasReadNoStatus()
433 {
434 return BaseImpl::hasReadNoStatus();
435 }
436
442 template <typename TIter>
443 void readNoStatus(TIter& iter)
444 {
445 BaseImpl::readNoStatus(iter);
446 }
447
449 bool canWrite() const
450 {
451 return BaseImpl::canWrite();
452 }
453
466 template <typename TIter>
467 ErrorStatus write(TIter& iter, std::size_t len) const
468 {
469 return BaseImpl::write(iter, len);
470 }
471
474 static constexpr bool hasWriteNoStatus()
475 {
476 return BaseImpl::hasWriteNoStatus();
477 }
478
484 template <typename TIter>
485 void writeNoStatus(TIter& iter) const
486 {
487 BaseImpl::writeNoStatus(iter);
488 }
489
493 bool valid() const
494 {
495 return BaseImpl::valid();
496 }
497
501 bool refresh()
502 {
503 return BaseImpl::refresh();
504 }
505
507 static constexpr std::size_t minLength()
508 {
509 return BaseImpl::minLength();
510 }
511
513 static constexpr std::size_t maxLength()
514 {
515 return BaseImpl::maxLength();
516 }
517
523 void forceReadElemCount(std::size_t count)
524 {
525 return BaseImpl::forceReadElemCount(count);
526 }
527
533 {
534 return BaseImpl::clearReadElemCount();
535 }
536
541 void forceReadLength(std::size_t count)
542 {
543 return BaseImpl::forceReadLength(count);
544 }
545
551 {
552 return BaseImpl::clearReadLengthForcing();
553 }
554
561 void forceReadElemLength(std::size_t count)
562 {
563 return BaseImpl::forceReadElemLength(count);
564 }
565
570 {
571 return BaseImpl::clearReadElemLengthForcing();
572 }
573
575 static constexpr bool isVersionDependent()
576 {
577 return ParsedOptions::HasCustomVersionUpdate || BaseImpl::isVersionDependent();
578 }
579
581 static constexpr bool hasNonDefaultRefresh()
582 {
583 return BaseImpl::hasNonDefaultRefresh();
584 }
585
589 {
590 return BaseImpl::getVersion();
591 }
592
596 {
597 return BaseImpl::setVersion(version);
598 }
599
600#ifdef FOR_DOXYGEN_DOC_ONLY
606 auto terminationFieldSuffix() -> decltype(BaseImpl::terminationFieldSuffix())
607 {
608 return BaseImpl::terminationFieldSuffix();
609 }
610
616 auto terminationFieldSuffix() const -> decltype(BaseImpl::terminationFieldSuffix())
617 {
618 return BaseImpl::terminationFieldSuffix();
619 }
620
621#endif // #ifdef FOR_DOXYGEN_DOC_ONLY
622
623protected:
624 using BaseImpl::readData;
625 using BaseImpl::writeData;
626
627private:
628 static_assert(!ParsedOptions::HasSerOffset,
629 "comms::option::def::NumValueSerOffset option is not applicable to ArrayList field");
630 static_assert(!ParsedOptions::HasFixedLengthLimit,
631 "comms::option::def::FixedLength option is not applicable to ArrayList field");
632 static_assert(!ParsedOptions::HasFixedBitLengthLimit,
633 "comms::option::def::FixedBitLength option is not applicable to ArrayList field");
634 static_assert(!ParsedOptions::HasVarLengthLimits,
635 "comms::option::def::VarLength option is not applicable to ArrayList field");
636 static_assert(!ParsedOptions::HasAvailableLengthLimit,
637 "comms::option::def::AvailableLengthLimit option is not applicable to ArrayList field");
638 static_assert(!ParsedOptions::HasScalingRatio,
639 "comms::option::def::ScalingRatio option is not applicable to ArrayList field");
640 static_assert(!ParsedOptions::HasUnits,
641 "comms::option::def::Units option is not applicable to ArrayList field");
642 static_assert(!ParsedOptions::HasMultiRangeValidation,
643 "comms::option::def::ValidNumValueRange (or similar) option is not applicable to ArrayList field");
644 static_assert((!ParsedOptions::HasOrigDataView) || (std::is_integral<TElement>::value && (sizeof(TElement) == sizeof(std::uint8_t))),
645 "Usage of comms::option::app::OrigDataView option is allowed only for raw binary data (std::uint8_t) types.");
646 static_assert(!ParsedOptions::HasVersionsRange,
647 "comms::option::def::ExistsBetweenVersions (or similar) option is not applicable to ArrayList field");
648 static_assert(!ParsedOptions::HasInvalidByDefault,
649 "comms::option::def::InvalidByDefault option is not applicable to ArrayList field");
650 static_assert(!ParsedOptions::HasMissingOnReadFail,
651 "comms::option::def::MissingOnReadFail option is not applicable to ArrayList field");
652 static_assert(!ParsedOptions::HasMissingOnInvalid,
653 "comms::option::def::MissingOnInvalid option is not applicable to ArrayList field");
654};
655
662template <typename TFieldBase, typename TElement, typename... TOptions>
666{
667 return std::lexicographical_compare(
668 field1.value().begin(), field1.value().end(),
669 field2.value().begin(), field2.value().end());
670}
671
677template <typename TFieldBase, typename TElement, typename... TOptions>
681{
682 auto& vec1 = field1.value();
683 auto& vec2 = field2.value();
684 if (vec1.size() != vec2.size()) {
685 return false;
686 }
687
688 for (auto idx = 0U; idx < vec1.size(); ++idx) {
689 if (vec1[idx] != vec2[idx]) {
690 return false;
691 }
692 }
693 return true;
694}
695
701template <typename TFieldBase, typename TElement, typename... TOptions>
705{
706 return !(field1 == field2);
707}
708
714template <typename T>
715constexpr bool isArrayList()
716{
717 return
718 std::is_same<typename T::CommsTag, tag::ArrayList>::value ||
719 std::is_same<typename T::CommsTag, tag::RawArrayList>::value;
720}
721
725template <typename TFieldBase, typename TElement, typename... TOptions>
726inline
727ArrayList<TFieldBase, TElement, TOptions...>&
732
736template <typename TFieldBase, typename TElement, typename... TOptions>
737inline
738const ArrayList<TFieldBase, TElement, TOptions...>&
740{
741 return field;
742}
743
744
745} // namespace field
746
747} // namespace comms
748
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:195
static constexpr bool hasEmptySerialization()
Compile time inquiry of whether comms::option::def::EmptySerialization option has been used.
Definition ArrayList.h:298
const ValueType & value() const
Get access to the value storage.
Definition ArrayList.h:388
static constexpr bool hasFixedSize()
Compile time inquiry of whether comms::option::def::SequenceFixedSize option has been used.
Definition ArrayList.h:354
typename BaseImpl::ElementType ElementType
Type of the element.
Definition ArrayList.h:221
typename BaseImpl::VersionType VersionType
Version type.
Definition ArrayList.h:205
static constexpr bool hasTrailingFieldSuffix()
Compile time inquiry of whether comms::option::def::SequenceTrailingFieldSuffix option has been used.
Definition ArrayList.h:347
void clearReadElemLengthForcing()
Clear forcing the serialisation length of the single element.
Definition ArrayList.h:569
static constexpr bool hasElemSerLengthFieldPrefix()
Compile time inquiry of whether comms::option::def::SequenceElemSerLengthFieldPrefix option has been ...
Definition ArrayList.h:326
void writeNoStatus(TIter &iter) const
Write current field value to output data sequence without error check and status report.
Definition ArrayList.h:485
typename ParsedOptions::SequenceTrailingFieldSuffix TrailingFieldSuffix
Type of trailing field suffix specified via comms::option::def::SequenceTrailingFieldSuffix.
Definition ArrayList.h:250
static constexpr bool hasFailOnInvalid()
Compile time inquiry of whether comms::option::def::FailOnInvalid option has been used.
Definition ArrayList.h:284
ErrorStatus read(TIter &iter, std::size_t len)
Read field value from input data sequence.
Definition ArrayList.h:425
static constexpr bool hasName()
Compile time inquiry of whether comms::option::def::HasName option has been used.
Definition ArrayList.h:368
static constexpr bool hasReadNoStatus()
Compile time check of whether the field has proper readNoStatus() member function.
Definition ArrayList.h:432
static constexpr bool isVersionDependent()
Compile time check if this class is version dependent.
Definition ArrayList.h:575
VersionType getVersion() const
Get version of the field.
Definition ArrayList.h:588
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:739
typename BaseImpl::Endian Endian
Endian used for serialisation.
Definition ArrayList.h:202
void clearReadLengthForcing()
Clear forcing of the available length in the next read() invocation.
Definition ArrayList.h:550
static constexpr bool hasTerminationFieldSuffix()
Compile time inquiry of whether comms::option::def::SequenceTerminationFieldSuffix option has been us...
Definition ArrayList.h:340
void setValue(U &&val)
Set value.
Definition ArrayList.h:403
static constexpr bool hasFixedValue()
Compile time inquiry of whether comms::option::def::FixedValue option has been used.
Definition ArrayList.h:361
ValueType & value()
Get access to the value storage.
Definition ArrayList.h:382
bool operator<(const ArrayList< TFieldBase, TElement, TOptions... > &field1, const ArrayList< TFieldBase, TElement, TOptions... > &field2) noexcept
Equivalence comparison operator.
Definition ArrayList.h:663
ArrayList(const ValueType &val)
Value constructor.
Definition ArrayList.h:256
void readNoStatus(TIter &iter)
Read field value from input data sequence without error check and status report.
Definition ArrayList.h:443
typename ParsedOptions::SequenceSizeFieldPrefix SizeFieldPrefix
Type of size field prefix specified via comms::option::def::SequenceSizeFieldPrefix.
Definition ArrayList.h:230
void forceReadElemCount(std::size_t count)
Force number of elements that must be read in the next read() invocation.
Definition ArrayList.h:523
typename ParsedOptions::SequenceElemSerLengthFieldPrefix ElemSerLengthFieldPrefix
Type of element's length field prefix specified via comms::option::def::SequenceElemSerLengthFieldPre...
Definition ArrayList.h:238
ArrayList(const ArrayList &)=default
Copy constructor.
constexpr std::size_t length() const
Get length of serialised data.
Definition ArrayList.h:409
void forceReadElemLength(std::size_t count)
Force serialisation length of a single element.
Definition ArrayList.h:561
static constexpr std::size_t minLength()
Get minimal length that is required to serialise field of this type.
Definition ArrayList.h:507
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:728
~ArrayList() noexcept=default
Destructor.
bool refresh()
Refresh the field.
Definition ArrayList.h:501
static constexpr std::size_t fixedSize()
Compile time inquiry of fixed size provided via comms::option::def::SequenceFixedSize option.
Definition ArrayList.h:376
typename BaseImpl::ValueType ValueType
Type of underlying value.
Definition ArrayList.h:218
constexpr bool isArrayList()
Compile time check function of whether a provided type is any variant of comms::field::ArrayList.
Definition ArrayList.h:715
bool setVersion(VersionType version)
Default implementation of version update.
Definition ArrayList.h:595
static constexpr bool hasSerLengthFieldPrefix()
Compile time inquiry of whether comms::option::def::SequenceSerLengthFieldPrefix option has been used...
Definition ArrayList.h:319
auto terminationFieldSuffix() const -> decltype(BaseImpl::terminationFieldSuffix())
Access list termination field (const variant)
Definition ArrayList.h:616
typename ParsedOptions::FieldType FieldType
Type of actual extending field specified via comms::option::def::FieldType.
Definition ArrayList.h:226
const ValueType & getValue() const
Get value.
Definition ArrayList.h:395
static constexpr bool hasElemFixedSerLengthFieldPrefix()
Compile time inquiry of whether comms::option::def::SequenceElemFixedSerLengthFieldPrefix option has ...
Definition ArrayList.h:333
static constexpr bool hasWriteNoStatus()
Compile time check of whether the field has proper writeNoStatus() member function.
Definition ArrayList.h:474
bool valid() const
Check validity of the field value.
Definition ArrayList.h:493
static constexpr bool hasIgnoreInvalid()
Compile time inquiry of whether comms::option::def::IgnoreInvalid option has been used.
Definition ArrayList.h:291
typename ParsedOptions::SequenceSerLengthFieldPrefix SerLengthFieldPrefix
Type of length field prefix specified via comms::option::def::SequenceSerLengthFieldPrefix.
Definition ArrayList.h:234
typename ParsedOptions::SequenceElemFixedSerLengthFieldPrefix ElemFixedSerLengthFieldPrefix
Type of element's fixed length field prefix specified via comms::option::def::SequenceElemFixedSerLen...
Definition ArrayList.h:242
TFieldBase FieldBase
Base class provided in the first template parameter.
Definition ArrayList.h:199
ErrorStatus write(TIter &iter, std::size_t len) const
Write current field value to output data sequence.
Definition ArrayList.h:467
auto terminationFieldSuffix() -> decltype(BaseImpl::terminationFieldSuffix())
Access list termination field.
Definition ArrayList.h:606
static constexpr bool hasFieldType()
Compile time inquiry of whether comms::option::def::FieldType option has been used.
Definition ArrayList.h:305
ArrayList(ArrayList &&)=default
Move constructor.
bool canWrite() const
Check of whether the field has a consistent value for writing.
Definition ArrayList.h:449
void clearReadElemCount()
Clear forcing of the number of elements that must be read in the next read() invocation.
Definition ArrayList.h:532
void forceReadLength(std::size_t count)
Force available length for the next read() invocation.
Definition ArrayList.h:541
static constexpr bool hasNonDefaultRefresh()
Compile time check if this class has non-default refresh functionality.
Definition ArrayList.h:581
static constexpr std::size_t maxLength()
Get maximal length that is required to serialise field of this type.
Definition ArrayList.h:513
bool operator!=(const ArrayList< TFieldBase, TElement, TOptions... > &field1, const ArrayList< TFieldBase, TElement, TOptions... > &field2) noexcept
Non-equality comparison operator.
Definition ArrayList.h:702
details::OptionsParser< TOptions... > ParsedOptions
All the options provided to this class bundled into struct.
Definition ArrayList.h:208
typename BaseImpl::CommsTag CommsTag
Tag indicating type of the field.
Definition ArrayList.h:211
typename ParsedOptions::SequenceTerminationFieldSuffix TerminationFieldSuffix
Type of termination field suffix specified via comms::option::def::SequenceTerminationFieldSuffix.
Definition ArrayList.h:246
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:678
ArrayList(ValueType &&val)
Value constructor.
Definition ArrayList.h:262
static constexpr bool hasSizeFieldPrefix()
Compile time inquiry of whether comms::option::def::SequenceSizeFieldPrefix option has been used.
Definition ArrayList.h:312
Describes an object that can refer to a constant contiguous sequence of other objects.
Definition ArrayView.h:31
Replacement to std::vector when no dynamic memory allocation is allowed.
Definition StaticVector.h:960
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.
Contains definition of all the options used by the COMMS library.
Replacement to some types from standard type_traits.