--- /dev/null
+/*
+ * 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 */
#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"
} /* 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>
{
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}
{
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};