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 - 2024 (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
190template <typename TFieldBase, typename TElement, typename... TOptions>
191class ArrayList : public details::ArrayListBase<TFieldBase, TElement, TOptions...>
192{
193 using BaseImpl = details::ArrayListBase<TFieldBase, TElement, TOptions...>;
194public:
196 using FieldBase = TFieldBase;
197
199 using Endian = typename BaseImpl::Endian;
200
202 using VersionType = typename BaseImpl::VersionType;
203
205 using ParsedOptions = details::OptionsParser<TOptions...>;
206
208 using CommsTag = typename BaseImpl::CommsTag;
209
215 using ValueType = typename BaseImpl::ValueType;
216
218 using ElementType = typename BaseImpl::ElementType;
219
223 using FieldType = typename ParsedOptions::FieldType;
224
227 using SizeFieldPrefix = typename ParsedOptions::SequenceSizeFieldPrefix;
228
231 using SerLengthFieldPrefix = typename ParsedOptions::SequenceSerLengthFieldPrefix;
232
235 using ElemSerLengthFieldPrefix = typename ParsedOptions::SequenceElemSerLengthFieldPrefix;
236
239 using ElemFixedSerLengthFieldPrefix = typename ParsedOptions::SequenceElemFixedSerLengthFieldPrefix;
240
243 using TerminationFieldSuffix = typename ParsedOptions::SequenceTerminationFieldSuffix;
244
247 using TrailingFieldSuffix = typename ParsedOptions::SequenceTrailingFieldSuffix;
248
250 ArrayList() = default;
251
253 explicit ArrayList(const ValueType& val)
254 : BaseImpl(val)
255 {
256 }
257
259 explicit ArrayList(ValueType&& val)
260 : BaseImpl(std::move(val))
261 {
262 }
263
265 ArrayList(const ArrayList&) = default;
266
268 ArrayList(ArrayList&&) = default;
269
271 ~ArrayList() noexcept = default;
272
274 ArrayList& operator=(const ArrayList&) = default;
275
277 ArrayList& operator=(ArrayList&&) = default;
278
281 static constexpr bool hasFailOnInvalid()
282 {
283 return ParsedOptions::HasFailOnInvalid;
284 }
285
288 static constexpr bool hasIgnoreInvalid()
289 {
290 return ParsedOptions::HasIgnoreInvalid;
291 }
292
295 static constexpr bool hasEmptySerialization()
296 {
297 return ParsedOptions::HasEmptySerialization;
298 }
299
302 static constexpr bool hasFieldType()
303 {
304 return ParsedOptions::HasFieldType;
305 }
306
309 static constexpr bool hasSizeFieldPrefix()
310 {
311 return ParsedOptions::HasSequenceSizeFieldPrefix;
312 }
313
316 static constexpr bool hasSerLengthFieldPrefix()
317 {
318 return ParsedOptions::HasSequenceSerLengthFieldPrefix;
319 }
320
323 static constexpr bool hasElemSerLengthFieldPrefix()
324 {
325 return ParsedOptions::HasSequenceElemSerLengthFieldPrefix;
326 }
327
330 static constexpr bool hasElemFixedSerLengthFieldPrefix()
331 {
332 return ParsedOptions::HasSequenceElemFixedSerLengthFieldPrefix;
333 }
334
337 static constexpr bool hasTerminationFieldSuffix()
338 {
339 return ParsedOptions::HasSequenceTerminationFieldSuffix;
340 }
341
344 static constexpr bool hasTrailingFieldSuffix()
345 {
346 return ParsedOptions::HasSequenceTrailingFieldSuffix;
347 }
348
351 static constexpr bool hasFixedSize()
352 {
353 return ParsedOptions::HasSequenceFixedSize;
354 }
355
359 static constexpr std::size_t fixedSize()
360 {
361 return ParsedOptions::SequenceFixedSize;
362 }
363
366 {
367 return BaseImpl::value();
368 }
369
371 const ValueType& value() const
372 {
373 return BaseImpl::value();
374 }
375
378 const ValueType& getValue() const
379 {
380 return BaseImpl::getValue();
381 }
382
385 template <typename U>
386 void setValue(U&& val)
387 {
388 BaseImpl::setValue(std::forward<U>(val));
389 }
390
392 constexpr std::size_t length() const
393 {
394 return BaseImpl::length();
395 }
396
407 template <typename TIter>
408 ErrorStatus read(TIter& iter, std::size_t len)
409 {
410 return BaseImpl::read(iter, len);
411 }
412
415 static constexpr bool hasReadNoStatus()
416 {
417 return BaseImpl::hasReadNoStatus();
418 }
419
425 template <typename TIter>
426 void readNoStatus(TIter& iter)
427 {
428 BaseImpl::readNoStatus(iter);
429 }
430
432 bool canWrite() const
433 {
434 return BaseImpl::canWrite();
435 }
436
449 template <typename TIter>
450 ErrorStatus write(TIter& iter, std::size_t len) const
451 {
452 return BaseImpl::write(iter, len);
453 }
454
457 static constexpr bool hasWriteNoStatus()
458 {
459 return BaseImpl::hasWriteNoStatus();
460 }
461
467 template <typename TIter>
468 void writeNoStatus(TIter& iter) const
469 {
470 BaseImpl::writeNoStatus(iter);
471 }
472
476 bool valid() const
477 {
478 return BaseImpl::valid();
479 }
480
484 bool refresh()
485 {
486 return BaseImpl::refresh();
487 }
488
490 static constexpr std::size_t minLength()
491 {
492 return BaseImpl::minLength();
493 }
494
496 static constexpr std::size_t maxLength()
497 {
498 return BaseImpl::maxLength();
499 }
500
506 void forceReadElemCount(std::size_t count)
507 {
508 return BaseImpl::forceReadElemCount(count);
509 }
510
516 {
517 return BaseImpl::clearReadElemCount();
518 }
519
524 void forceReadLength(std::size_t count)
525 {
526 return BaseImpl::forceReadLength(count);
527 }
528
534 {
535 return BaseImpl::clearReadLengthForcing();
536 }
537
544 void forceReadElemLength(std::size_t count)
545 {
546 return BaseImpl::forceReadElemLength(count);
547 }
548
553 {
554 return BaseImpl::clearReadElemLengthForcing();
555 }
556
558 static constexpr bool isVersionDependent()
559 {
560 return ParsedOptions::HasCustomVersionUpdate || BaseImpl::isVersionDependent();
561 }
562
564 static constexpr bool hasNonDefaultRefresh()
565 {
566 return BaseImpl::hasNonDefaultRefresh();
567 }
568
572 {
573 return BaseImpl::getVersion();
574 }
575
579 {
580 return BaseImpl::setVersion(version);
581 }
582
583#ifdef FOR_DOXYGEN_DOC_ONLY
589 auto terminationFieldSuffix() -> decltype(BaseImpl::terminationFieldSuffix())
590 {
591 return BaseImpl::terminationFieldSuffix();
592 }
593
599 auto terminationFieldSuffix() const -> decltype(BaseImpl::terminationFieldSuffix())
600 {
601 return BaseImpl::terminationFieldSuffix();
602 }
603
604#endif // #ifdef FOR_DOXYGEN_DOC_ONLY
605
606protected:
607 using BaseImpl::readData;
608 using BaseImpl::writeData;
609
610private:
611 static_assert(!ParsedOptions::HasSerOffset,
612 "comms::option::def::NumValueSerOffset option is not applicable to ArrayList field");
613 static_assert(!ParsedOptions::HasFixedLengthLimit,
614 "comms::option::def::FixedLength option is not applicable to ArrayList field");
615 static_assert(!ParsedOptions::HasFixedBitLengthLimit,
616 "comms::option::def::FixedBitLength option is not applicable to ArrayList field");
617 static_assert(!ParsedOptions::HasVarLengthLimits,
618 "comms::option::def::VarLength option is not applicable to ArrayList field");
619 static_assert(!ParsedOptions::HasAvailableLengthLimit,
620 "comms::option::def::AvailableLengthLimit option is not applicable to ArrayList field");
621 static_assert(!ParsedOptions::HasScalingRatio,
622 "comms::option::def::ScalingRatio option is not applicable to ArrayList field");
623 static_assert(!ParsedOptions::HasUnits,
624 "comms::option::def::Units option is not applicable to ArrayList field");
625 static_assert(!ParsedOptions::HasMultiRangeValidation,
626 "comms::option::def::ValidNumValueRange (or similar) option is not applicable to ArrayList field");
627 static_assert((!ParsedOptions::HasOrigDataView) || (std::is_integral<TElement>::value && (sizeof(TElement) == sizeof(std::uint8_t))),
628 "Usage of comms::option::app::OrigDataView option is allowed only for raw binary data (std::uint8_t) types.");
629 static_assert(!ParsedOptions::HasVersionsRange,
630 "comms::option::def::ExistsBetweenVersions (or similar) option is not applicable to ArrayList field");
631 static_assert(!ParsedOptions::HasInvalidByDefault,
632 "comms::option::def::InvalidByDefault option is not applicable to ArrayList field");
633 static_assert(!ParsedOptions::HasMissingOnReadFail,
634 "comms::option::def::MissingOnReadFail option is not applicable to ArrayList field");
635 static_assert(!ParsedOptions::HasMissingOnInvalid,
636 "comms::option::def::MissingOnInvalid option is not applicable to ArrayList field");
637};
638
645template <typename TFieldBase, typename TElement, typename... TOptions>
649{
650 return std::lexicographical_compare(
651 field1.value().begin(), field1.value().end(),
652 field2.value().begin(), field2.value().end());
653}
654
660template <typename TFieldBase, typename TElement, typename... TOptions>
664{
665 auto& vec1 = field1.value();
666 auto& vec2 = field2.value();
667 if (vec1.size() != vec2.size()) {
668 return false;
669 }
670
671 for (auto idx = 0U; idx < vec1.size(); ++idx) {
672 if (vec1[idx] != vec2[idx]) {
673 return false;
674 }
675 }
676 return true;
677}
678
684template <typename TFieldBase, typename TElement, typename... TOptions>
688{
689 return !(field1 == field2);
690}
691
697template <typename T>
698constexpr bool isArrayList()
699{
700 return
701 std::is_same<typename T::CommsTag, tag::ArrayList>::value ||
702 std::is_same<typename T::CommsTag, tag::RawArrayList>::value;
703}
704
708template <typename TFieldBase, typename TElement, typename... TOptions>
709inline
710ArrayList<TFieldBase, TElement, TOptions...>&
715
719template <typename TFieldBase, typename TElement, typename... TOptions>
720inline
721const ArrayList<TFieldBase, TElement, TOptions...>&
723{
724 return field;
725}
726
727
728} // namespace field
729
730} // namespace comms
731
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:192
static constexpr bool hasEmptySerialization()
Compile time inquiry of whether comms::option::def::EmptySerialization option has been used.
Definition ArrayList.h:295
const ValueType & value() const
Get access to the value storage.
Definition ArrayList.h:371
static constexpr bool hasFixedSize()
Compile time inquiry of whether comms::option::def::SequenceFixedSize option has been used.
Definition ArrayList.h:351
typename BaseImpl::ElementType ElementType
Type of the element.
Definition ArrayList.h:218
typename BaseImpl::VersionType VersionType
Version type.
Definition ArrayList.h:202
static constexpr bool hasTrailingFieldSuffix()
Compile time inquiry of whether comms::option::def::SequenceTrailingFieldSuffix option has been used.
Definition ArrayList.h:344
void clearReadElemLengthForcing()
Clear forcing the serialisation length of the single element.
Definition ArrayList.h:552
static constexpr bool hasElemSerLengthFieldPrefix()
Compile time inquiry of whether comms::option::def::SequenceElemSerLengthFieldPrefix option has been ...
Definition ArrayList.h:323
void writeNoStatus(TIter &iter) const
Write current field value to output data sequence without error check and status report.
Definition ArrayList.h:468
typename ParsedOptions::SequenceTrailingFieldSuffix TrailingFieldSuffix
Type of trailing field suffix specified via comms::option::def::SequenceTrailingFieldSuffix.
Definition ArrayList.h:247
static constexpr bool hasFailOnInvalid()
Compile time inquiry of whether comms::option::def::FailOnInvalid option has been used.
Definition ArrayList.h:281
ErrorStatus read(TIter &iter, std::size_t len)
Read field value from input data sequence.
Definition ArrayList.h:408
static constexpr bool hasReadNoStatus()
Compile time check of whether the field has proper readNoStatus() member function.
Definition ArrayList.h:415
static constexpr bool isVersionDependent()
Compile time check if this class is version dependent.
Definition ArrayList.h:558
VersionType getVersion() const
Get version of the field.
Definition ArrayList.h:571
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:722
typename BaseImpl::Endian Endian
Endian used for serialisation.
Definition ArrayList.h:199
void clearReadLengthForcing()
Clear forcing of the available length in the next read() invocation.
Definition ArrayList.h:533
static constexpr bool hasTerminationFieldSuffix()
Compile time inquiry of whether comms::option::def::SequenceTerminationFieldSuffix option has been us...
Definition ArrayList.h:337
void setValue(U &&val)
Set value.
Definition ArrayList.h:386
ValueType & value()
Get access to the value storage.
Definition ArrayList.h:365
bool operator<(const ArrayList< TFieldBase, TElement, TOptions... > &field1, const ArrayList< TFieldBase, TElement, TOptions... > &field2) noexcept
Equivalence comparison operator.
Definition ArrayList.h:646
ArrayList(const ValueType &val)
Value constructor.
Definition ArrayList.h:253
void readNoStatus(TIter &iter)
Read field value from input data sequence without error check and status report.
Definition ArrayList.h:426
typename ParsedOptions::SequenceSizeFieldPrefix SizeFieldPrefix
Type of size field prefix specified via comms::option::def::SequenceSizeFieldPrefix.
Definition ArrayList.h:227
void forceReadElemCount(std::size_t count)
Force number of elements that must be read in the next read() invocation.
Definition ArrayList.h:506
typename ParsedOptions::SequenceElemSerLengthFieldPrefix ElemSerLengthFieldPrefix
Type of element's length field prefix specified via comms::option::def::SequenceElemSerLengthFieldPre...
Definition ArrayList.h:235
ArrayList(const ArrayList &)=default
Copy constructor.
constexpr std::size_t length() const
Get length of serialised data.
Definition ArrayList.h:392
void forceReadElemLength(std::size_t count)
Force serialisation length of a single element.
Definition ArrayList.h:544
static constexpr std::size_t minLength()
Get minimal length that is required to serialise field of this type.
Definition ArrayList.h:490
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:711
~ArrayList() noexcept=default
Destructor.
bool refresh()
Refresh the field.
Definition ArrayList.h:484
static constexpr std::size_t fixedSize()
Compile time inquiry of fixed size provided via comms::option::def::SequenceFixedSize option.
Definition ArrayList.h:359
typename BaseImpl::ValueType ValueType
Type of underlying value.
Definition ArrayList.h:215
constexpr bool isArrayList()
Compile time check function of whether a provided type is any variant of comms::field::ArrayList.
Definition ArrayList.h:698
bool setVersion(VersionType version)
Default implementation of version update.
Definition ArrayList.h:578
static constexpr bool hasSerLengthFieldPrefix()
Compile time inquiry of whether comms::option::def::SequenceSerLengthFieldPrefix option has been used...
Definition ArrayList.h:316
auto terminationFieldSuffix() const -> decltype(BaseImpl::terminationFieldSuffix())
Access list termination field (const variant)
Definition ArrayList.h:599
typename ParsedOptions::FieldType FieldType
Type of actual extending field specified via comms::option::def::FieldType.
Definition ArrayList.h:223
const ValueType & getValue() const
Get value.
Definition ArrayList.h:378
static constexpr bool hasElemFixedSerLengthFieldPrefix()
Compile time inquiry of whether comms::option::def::SequenceElemFixedSerLengthFieldPrefix option has ...
Definition ArrayList.h:330
static constexpr bool hasWriteNoStatus()
Compile time check of whether the field has proper writeNoStatus() member function.
Definition ArrayList.h:457
bool valid() const
Check validity of the field value.
Definition ArrayList.h:476
static constexpr bool hasIgnoreInvalid()
Compile time inquiry of whether comms::option::def::IgnoreInvalid option has been used.
Definition ArrayList.h:288
typename ParsedOptions::SequenceSerLengthFieldPrefix SerLengthFieldPrefix
Type of length field prefix specified via comms::option::def::SequenceSerLengthFieldPrefix.
Definition ArrayList.h:231
typename ParsedOptions::SequenceElemFixedSerLengthFieldPrefix ElemFixedSerLengthFieldPrefix
Type of element's fixed length field prefix specified via comms::option::def::SequenceElemFixedSerLen...
Definition ArrayList.h:239
TFieldBase FieldBase
Base class provided in the first template parameter.
Definition ArrayList.h:196
ErrorStatus write(TIter &iter, std::size_t len) const
Write current field value to output data sequence.
Definition ArrayList.h:450
auto terminationFieldSuffix() -> decltype(BaseImpl::terminationFieldSuffix())
Access list termination field.
Definition ArrayList.h:589
static constexpr bool hasFieldType()
Compile time inquiry of whether comms::option::def::FieldType option has been used.
Definition ArrayList.h:302
ArrayList(ArrayList &&)=default
Move constructor.
bool canWrite() const
Check of whether the field has a consistent value for writing.
Definition ArrayList.h:432
void clearReadElemCount()
Clear forcing of the number of elements that must be read in the next read() invocation.
Definition ArrayList.h:515
void forceReadLength(std::size_t count)
Force available length for the next read() invocation.
Definition ArrayList.h:524
static constexpr bool hasNonDefaultRefresh()
Compile time check if this class has non-default refresh functionality.
Definition ArrayList.h:564
static constexpr std::size_t maxLength()
Get maximal length that is required to serialise field of this type.
Definition ArrayList.h:496
bool operator!=(const ArrayList< TFieldBase, TElement, TOptions... > &field1, const ArrayList< TFieldBase, TElement, TOptions... > &field2) noexcept
Non-equality comparison operator.
Definition ArrayList.h:685
details::OptionsParser< TOptions... > ParsedOptions
All the options provided to this class bundled into struct.
Definition ArrayList.h:205
typename BaseImpl::CommsTag CommsTag
Tag indicating type of the field.
Definition ArrayList.h:208
typename ParsedOptions::SequenceTerminationFieldSuffix TerminationFieldSuffix
Type of termination field suffix specified via comms::option::def::SequenceTerminationFieldSuffix.
Definition ArrayList.h:243
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:661
ArrayList(ValueType &&val)
Value constructor.
Definition ArrayList.h:259
static constexpr bool hasSizeFieldPrefix()
Compile time inquiry of whether comms::option::def::SequenceSizeFieldPrefix option has been used.
Definition ArrayList.h:309
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.