From 3a34361189fd69c82934ae4b95713b272ccc5e55 Mon Sep 17 00:00:00 2001 From: Francis Deslauriers Date: Tue, 1 Feb 2022 09:05:18 -0500 Subject: [PATCH] Add `bt2::CommonArrayValueIterator` to iterate on `bt2::CommonArrayValue` container This commit implements an array value input iterator and adds `begin()` and `end()` methods to the `CommonArrayValue` container class. Signed-off-by: Francis Deslauriers Change-Id: I2e3c502bd5b11a507fc4643cee27b48b06a6c4a8 Reviewed-on: https://review.lttng.org/c/babeltrace/+/7322 Reviewed-by: Philippe Proulx Reviewed-on: https://review.lttng.org/c/babeltrace/+/7193 Tested-by: jenkins CI-Build: Philippe Proulx --- src/cpp-common/bt2/value.hpp | 88 ++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/src/cpp-common/bt2/value.hpp b/src/cpp-common/bt2/value.hpp index 4b34a16e..feedde27 100644 --- a/src/cpp-common/bt2/value.hpp +++ b/src/cpp-common/bt2/value.hpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include "common/assert.h" @@ -622,6 +623,82 @@ struct CommonArrayValueSpec final } // namespace internal +template +class CommonArrayValueIterator +{ + friend CommonArrayValue; + + using difference_type = std::ptrdiff_t; + using value_type = CommonValue; + using pointer = value_type *; + using reference = value_type&; + using iterator_category = std::input_iterator_tag; + +private: + explicit CommonArrayValueIterator(const CommonArrayValue& arrayVal, + const uint64_t idx) : + _mArrayVal {arrayVal}, + _mIdx {idx} + { + this->_updateCurrentValue(); + } + +public: + CommonArrayValueIterator(const CommonArrayValueIterator&) = default; + CommonArrayValueIterator(CommonArrayValueIterator&&) = default; + CommonArrayValueIterator& operator=(const CommonArrayValueIterator&) = default; + CommonArrayValueIterator& operator=(CommonArrayValueIterator&&) = default; + + CommonArrayValueIterator& operator++() noexcept + { + ++_mIdx; + this->_updateCurrentValue(); + return *this; + } + + CommonArrayValueIterator operator++(int) noexcept + { + const auto tmp = *this; + + ++(*this); + return tmp; + } + + bool operator==(const CommonArrayValueIterator& other) const noexcept + { + return _mIdx == other._mIdx; + } + + bool operator!=(const CommonArrayValueIterator& other) const noexcept + { + return !(*this == other); + } + + reference operator*() noexcept + { + return *_mCurrVal; + } + + pointer operator->() noexcept + { + return &(*_mCurrVal); + } + +private: + void _updateCurrentValue() noexcept + { + if (_mIdx < _mArrayVal.length()) { + _mCurrVal = _mArrayVal[_mIdx]; + } else { + _mCurrVal = nonstd::nullopt; + } + } + + nonstd::optional> _mCurrVal; + CommonArrayValue _mArrayVal; + uint64_t _mIdx; +}; + template class CommonArrayValue final : public CommonValue { @@ -631,6 +708,7 @@ private: public: using Shared = internal::SharedValue, LibObjT>; + using Iterator = CommonArrayValueIterator; explicit CommonArrayValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr} { @@ -662,6 +740,16 @@ public: return bt_value_array_get_length(this->_libObjPtr()); } + Iterator begin() const noexcept + { + return Iterator {*this, 0}; + } + + Iterator end() const noexcept + { + return Iterator {*this, this->length()}; + } + bool isEmpty() const noexcept { return this->length() == 0; -- 2.34.1