libcppwrap
A collection of C++ wrappers for native APIs
Loading...
Searching...
No Matches
iterators.hpp
Go to the documentation of this file.
1//
2// libcppwrap - A collection of C++ wrappers for native APIs
3// Copyright (C) 2021-2023 David A. Norris <danorris@gmail.com>
4// Published under the MIT license - https://opensource.org/licenses/MIT
5//
6
7#pragma once
8
9#include <iterator>
10
11namespace w
12{
20 template <typename ListNode, const ListNode * (*Next)(const ListNode *)>
22 {
26 const_list_iterator() noexcept : _current(nullptr) { }
27
33 const_list_iterator(const ListNode *current) noexcept : _current(current) { }
34
42 {
43 _current = Next(_current);
44 return *this;
45 }
46
53 const ListNode& operator*() const noexcept { return *_current; }
54
61 const ListNode *operator->() const noexcept { return _current; }
62
70 bool operator==(const_list_iterator const& rhs) const noexcept { return _current == rhs._current; }
71
79 bool operator!=(const_list_iterator const& rhs) const noexcept { return _current != rhs._current; }
80
81 private: const ListNode *_current;
82 };
83}
84
85template <typename ListNode, const ListNode * (*Next)(const ListNode *)>
86struct std::iterator_traits<w::const_list_iterator<ListNode, Next>>
87{
89 typedef ListNode value_type;
90 typedef const ListNode * pointer;
91 typedef const ListNode& reference;
93};
Definition: assert.hpp:13
const ListNode * pointer
Definition: iterators.hpp:90
std::forward_iterator_tag iterator_category
Definition: iterators.hpp:92
std::ptrdiff_t difference_type
Definition: iterators.hpp:88
const ListNode & reference
Definition: iterators.hpp:91
A forward iterator for linked lists.
Definition: iterators.hpp:22
const_list_iterator & operator++() noexcept
Advances to the next linked list node.
Definition: iterators.hpp:41
const ListNode & operator*() const noexcept
Gets a reference to the linked list node pointed to by this iterator.
Definition: iterators.hpp:53
const_list_iterator(const ListNode *current) noexcept
Constructs an iterator pointing to the specified linked list node.
Definition: iterators.hpp:33
bool operator!=(const_list_iterator const &rhs) const noexcept
Tests if this object and rhs point to the same linked list node.
Definition: iterators.hpp:79
bool operator==(const_list_iterator const &rhs) const noexcept
Tests if this object and rhs point to the same linked list node.
Definition: iterators.hpp:70
const_list_iterator() noexcept
Constructs an end iterator.
Definition: iterators.hpp:26
const ListNode * operator->() const noexcept
Gets a pointer to the linked list node pointed to by this iterator.
Definition: iterators.hpp:61