2 * Copyright (c) 2022 Francis Deslauriers <francis.deslauriers@efficios.com>
4 * SPDX-License-Identifier: MIT
7 #ifndef BABELTRACE_CPP_COMMON_BT2_COMMON_ITER_HPP
8 #define BABELTRACE_CPP_COMMON_BT2_COMMON_ITER_HPP
13 #include "cpp-common/optional.hpp"
17 template <typename ContainerT, typename ElemT>
23 using difference_type = std::ptrdiff_t;
24 using value_type = ElemT;
25 using pointer = value_type *;
26 using reference = value_type&;
27 using iterator_category = std::input_iterator_tag;
30 explicit CommonIterator(const ContainerT container, const uint64_t idx) :
31 _mContainer {container}, _mIdx {idx}
33 this->_updateCurrentValue();
37 CommonIterator(const CommonIterator&) = default;
38 CommonIterator(CommonIterator&&) = default;
39 CommonIterator& operator=(const CommonIterator&) = default;
40 CommonIterator& operator=(CommonIterator&&) = default;
42 CommonIterator& operator++() noexcept
45 this->_updateCurrentValue();
49 CommonIterator operator++(int) noexcept
51 const auto tmp = *this;
57 bool operator==(const CommonIterator& other) const noexcept
59 return _mIdx == other._mIdx;
62 bool operator!=(const CommonIterator& other) const noexcept
64 return !(*this == other);
67 reference operator*() noexcept
72 pointer operator->() noexcept
78 void _updateCurrentValue() noexcept
80 if (_mIdx < _mContainer.size()) {
81 _mCurrVal = _mContainer[_mIdx];
83 _mCurrVal = nonstd::nullopt;
87 nonstd::optional<value_type> _mCurrVal;
88 ContainerT _mContainer;
93 #endif /* BABELTRACE_CPP_COMMON_BT2_COMMON_ITER_HPP */