Add `bt2::CommonArrayValueIterator` to iterate on `bt2::CommonArrayValue` container
authorFrancis Deslauriers <francis.deslauriers@efficios.com>
Tue, 1 Feb 2022 14:05:18 +0000 (09:05 -0500)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Mon, 11 Sep 2023 15:24:02 +0000 (11:24 -0400)
This commit implements an array value input iterator and adds `begin()` and
`end()` methods to the `CommonArrayValue` container class.

Signed-off-by: Francis Deslauriers <francis.deslauriers@efficios.com>
Change-Id: I2e3c502bd5b11a507fc4643cee27b48b06a6c4a8
Reviewed-on: https://review.lttng.org/c/babeltrace/+/7322
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/7193
Tested-by: jenkins <jenkins@lttng.org>
CI-Build: Philippe Proulx <eeppeliteloop@gmail.com>

src/cpp-common/bt2/value.hpp

index 4b34a16ebf2038a57d944e47662b862d529935ba..feedde27ba985688411ceef69d8c0c2af5c29bfb 100644 (file)
@@ -10,6 +10,7 @@
 #include <type_traits>
 #include <cstdint>
 #include <functional>
+#include <iterator>
 #include <babeltrace2/babeltrace.h>
 
 #include "common/assert.h"
@@ -622,6 +623,82 @@ struct CommonArrayValueSpec<const bt_value> final
 
 } // namespace internal
 
+template <typename LibObjT>
+class CommonArrayValueIterator
+{
+    friend CommonArrayValue<LibObjT>;
+
+    using difference_type = std::ptrdiff_t;
+    using value_type = CommonValue<LibObjT>;
+    using pointer = value_type *;
+    using reference = value_type&;
+    using iterator_category = std::input_iterator_tag;
+
+private:
+    explicit CommonArrayValueIterator(const CommonArrayValue<LibObjT>& 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<CommonValue<LibObjT>> _mCurrVal;
+    CommonArrayValue<LibObjT> _mArrayVal;
+    uint64_t _mIdx;
+};
+
 template <typename LibObjT>
 class CommonArrayValue final : public CommonValue<LibObjT>
 {
@@ -631,6 +708,7 @@ private:
 
 public:
     using Shared = internal::SharedValue<CommonArrayValue<LibObjT>, LibObjT>;
+    using Iterator = CommonArrayValueIterator<LibObjT>;
 
     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;
This page took 0.02621 seconds and 4 git commands to generate.