cpp-common: template ArrayValueIterator for reuse with other containers
authorFrancis Deslauriers <francis.deslauriers@efficios.com>
Tue, 8 Mar 2022 17:16:27 +0000 (12:16 -0500)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Mon, 11 Sep 2023 15:24:02 +0000 (11:24 -0400)
This pattern will be reused for Structure and Variant field class.

Signed-off-by: Francis Deslauriers <francis.deslauriers@efficios.com>
Change-Id: I8b6521e41dcc2ebdb81b6348de916544176dbf20
Reviewed-on: https://review.lttng.org/c/babeltrace/+/7518
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/10759
CI-Build: Philippe Proulx <eeppeliteloop@gmail.com>
Tested-by: jenkins <jenkins@lttng.org>
src/cpp-common/bt2/common-iter.hpp [new file with mode: 0644]
src/cpp-common/bt2/value.hpp

diff --git a/src/cpp-common/bt2/common-iter.hpp b/src/cpp-common/bt2/common-iter.hpp
new file mode 100644 (file)
index 0000000..5ebade3
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+ * Copyright (c) 2022 Francis Deslauriers <francis.deslauriers@efficios.com>
+ *
+ * SPDX-License-Identifier: MIT
+ */
+
+#ifndef BABELTRACE_CPP_COMMON_BT2_COMMON_ITER_HPP
+#define BABELTRACE_CPP_COMMON_BT2_COMMON_ITER_HPP
+
+#include <iterator>
+#include <type_traits>
+
+#include "cpp-common/optional.hpp"
+
+namespace bt2 {
+
+template <typename ContainerT, typename ElemT>
+class CommonIterator
+{
+    friend ContainerT;
+
+public:
+    using difference_type = std::ptrdiff_t;
+    using value_type = ElemT;
+    using pointer = value_type *;
+    using reference = value_type&;
+    using iterator_category = std::input_iterator_tag;
+
+private:
+    explicit CommonIterator(const ContainerT container, const uint64_t idx) :
+        _mContainer {container}, _mIdx {idx}
+    {
+        this->_updateCurrentValue();
+    }
+
+public:
+    CommonIterator(const CommonIterator&) = default;
+    CommonIterator(CommonIterator&&) = default;
+    CommonIterator& operator=(const CommonIterator&) = default;
+    CommonIterator& operator=(CommonIterator&&) = default;
+
+    CommonIterator& operator++() noexcept
+    {
+        ++_mIdx;
+        this->_updateCurrentValue();
+        return *this;
+    }
+
+    CommonIterator operator++(int) noexcept
+    {
+        const auto tmp = *this;
+
+        ++(*this);
+        return tmp;
+    }
+
+    bool operator==(const CommonIterator& other) const noexcept
+    {
+        return _mIdx == other._mIdx;
+    }
+
+    bool operator!=(const CommonIterator& other) const noexcept
+    {
+        return !(*this == other);
+    }
+
+    reference operator*() noexcept
+    {
+        return *_mCurrVal;
+    }
+
+    pointer operator->() noexcept
+    {
+        return &(*_mCurrVal);
+    }
+
+private:
+    void _updateCurrentValue() noexcept
+    {
+        if (_mIdx < _mContainer.size()) {
+            _mCurrVal = _mContainer[_mIdx];
+        } else {
+            _mCurrVal = nonstd::nullopt;
+        }
+    }
+
+    nonstd::optional<value_type> _mCurrVal;
+    ContainerT _mContainer;
+    uint64_t _mIdx;
+};
+} /* namespace bt2 */
+
+#endif /* BABELTRACE_CPP_COMMON_BT2_COMMON_ITER_HPP */
index a1a7d5010e22cfb9760abcb0ed2c8fd7f153f34f..fab17ca3a6981a6c191b3ec89af7f79f74532dd2 100644 (file)
@@ -15,6 +15,7 @@
 
 #include "common/assert.h"
 #include "common/common.h"
+#include "common-iter.hpp"
 #include "internal/borrowed-obj.hpp"
 #include "internal/shared-obj.hpp"
 #include "internal/utils.hpp"
@@ -623,82 +624,6 @@ 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>
 {
@@ -708,7 +633,7 @@ private:
 
 public:
     using Shared = internal::SharedValue<CommonArrayValue<LibObjT>, LibObjT>;
-    using Iterator = CommonArrayValueIterator<LibObjT>;
+    using Iterator = CommonIterator<CommonArrayValue<LibObjT>, CommonValue<LibObjT>>;
 
     explicit CommonArrayValue(const _LibObjPtr libObjPtr) noexcept : _ThisCommonValue {libObjPtr}
     {
@@ -740,6 +665,12 @@ public:
         return bt_value_array_get_length(this->_libObjPtr());
     }
 
+    /* Required by the `CommonIterator` template class */
+    std::uint64_t size() const noexcept
+    {
+        return this->length();
+    }
+
     Iterator begin() const noexcept
     {
         return Iterator {*this, 0};
This page took 0.027157 seconds and 4 git commands to generate.