1/*
2 * Copyright 2022 WebAssembly Community Group participants
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef wasm_support_parent_index_iterator_h
18#define wasm_support_parent_index_iterator_h
19
20#include <cstddef>
21#include <iterator>
22
23namespace wasm {
24
25// A helper for defining iterators that contain references to some parent object
26// and indices into that parent object. Provides operator implementations for
27// equality, inequality, index updates, and index differences, but not for
28// dereferencing.
29//
30// Users of this utility should subclass ParentIndexIterator<...> and define the
31// following members in the subclass:
32//
33// - using value_type = ...
34// - using pointer = ...
35// - using reference = ...
36// - reference operator*() const
37//
38// `Parent` is the parent type that the iterator contains and is being indexed
39// into and `Iterator` is the subclass of ParentIndexIterator<...> that is being
40// defined.
41template<typename Parent, typename Iterator> struct ParentIndexIterator {
42 using iterator_category = std::random_access_iterator_tag;
43 using difference_type = std::ptrdiff_t;
44
45 Parent parent;
46 size_t index;
47
48 const Iterator& self() const { return *static_cast<const Iterator*>(this); }
49
50 Iterator& self() { return *static_cast<Iterator*>(this); }
51
52 bool operator==(const ParentIndexIterator& other) const {
53 return index == other.index && parent == other.parent;
54 }
55 bool operator!=(const ParentIndexIterator& other) const {
56 return !(*this == other);
57 }
58 Iterator& operator++() {
59 ++index;
60 return self();
61 }
62 Iterator& operator--() {
63 --index;
64 return self();
65 }
66 Iterator operator++(int) {
67 auto it = self();
68 index++;
69 return it;
70 }
71 Iterator operator--(int) {
72 auto it = self();
73 index--;
74 return it;
75 }
76 Iterator& operator+=(difference_type off) {
77 index += off;
78 return self();
79 }
80 Iterator operator+(difference_type off) const {
81 return Iterator(self()) += off;
82 }
83 Iterator& operator-=(difference_type off) {
84 index -= off;
85 return self();
86 }
87 Iterator operator-(difference_type off) const {
88 return Iterator(self()) -= off;
89 }
90 difference_type operator-(const Iterator& other) const {
91 assert(parent == other.parent);
92 return index - other.index;
93 }
94};
95
96} // namespace wasm
97
98#endif // wasm_support_parent_index_iterator_h
99

source code of dart_sdk/third_party/binaryen/src/src/support/parent_index_iterator.h