Phosphor
iterator.h
1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  * Copyright 2016 Couchbase, Inc
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 namespace gsl_p {
19 
48  template <typename T>
51  using U = decltype((*((T*)nullptr))->begin());
52  using child_value_type = typename std::iterator_traits<U>::value_type;
53 
54  public:
55  multidimensional_iterator(T parent_, T parent_end_)
56  : parent(parent_), parent_end(parent_end_), child(parent->begin()) {
57  seekChildForward(); // Move forward until a valid child is found
58  }
59 
60  const child_value_type& operator*() const {
61  return *child;
62  }
63 
64  const child_value_type* operator->() const {
65  return &(*child);
66  }
67 
68  __self& operator++() {
69  ++child;
70  seekChildForward(); // Move forward until a valid child is found
71  return *this;
72  }
73 
74  bool operator==(const __self& other) const {
75  // Special case if we're at the end as the child might
76  // not be valid
77  if (parent == parent_end && parent == other.parent) {
78  return true;
79  }
80  return (parent == other.parent) && (child == other.child);
81  }
82 
83  bool operator!=(const __self& other) const {
84  return !(*this == other);
85  }
86 
87  protected:
88  void seekChildForward() {
89  if (parent == parent_end) {
90  return; // Already reached the last parent
91  }
92  while (child == parent->end()) {
93  ++parent;
94  if (parent == parent_end) {
95  break; // Reached the last parent
96  }
97  child = parent->begin();
98  }
99  }
100 
101  private:
102  T parent;
103  T parent_end;
104  U child;
105  };
106 }
Definition: dyn_array.h:337
Definition: iterator.h:49