tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

iterator_traits_test_helper.h (2971B)


      1 // Copyright 2025 The Abseil Authors
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //     https://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 #ifndef ABSL_BASE_INTERNAL_ITERATOR_TRAITS_TEST_HELPER_H_
     16 #define ABSL_BASE_INTERNAL_ITERATOR_TRAITS_TEST_HELPER_H_
     17 
     18 #include <iterator>
     19 #include <utility>
     20 
     21 #include "absl/base/config.h"
     22 
     23 namespace absl {
     24 ABSL_NAMESPACE_BEGIN
     25 namespace base_internal {
     26 
     27 // This would be a forward_iterator in C++20, but it's only an input iterator
     28 // before that, since it has a non-reference `reference`.
     29 template <typename Iterator>
     30 class Cpp20ForwardZipIterator {
     31  using IteratorReference = typename std::iterator_traits<Iterator>::reference;
     32 
     33 public:
     34  Cpp20ForwardZipIterator() = default;
     35  explicit Cpp20ForwardZipIterator(Iterator left, Iterator right)
     36      : left_(left), right_(right) {}
     37 
     38  Cpp20ForwardZipIterator& operator++() {
     39    ++left_;
     40    ++right_;
     41    return *this;
     42  }
     43 
     44  Cpp20ForwardZipIterator operator++(int) {
     45    Cpp20ForwardZipIterator tmp(*this);
     46    ++*this;
     47    return *this;
     48  }
     49 
     50  std::pair<IteratorReference, IteratorReference> operator*() const {
     51    return {*left_, *right_};
     52  }
     53 
     54  // C++17 input iterators require `operator->`, but this isn't  possible to
     55  // implement. C++20 dropped the requirement.
     56 
     57  friend bool operator==(const Cpp20ForwardZipIterator& lhs,
     58                         const Cpp20ForwardZipIterator& rhs) {
     59    return lhs.left_ == rhs.left_ && lhs.right_ == rhs.right_;
     60  }
     61 
     62  friend bool operator!=(const Cpp20ForwardZipIterator& lhs,
     63                         const Cpp20ForwardZipIterator& rhs) {
     64    return !(lhs == rhs);
     65  }
     66 
     67 private:
     68  Iterator left_{};
     69  Iterator right_{};
     70 };
     71 
     72 }  // namespace base_internal
     73 ABSL_NAMESPACE_END
     74 }  // namespace absl
     75 
     76 template <typename Iterator>
     77 struct std::iterator_traits<
     78    absl::base_internal::Cpp20ForwardZipIterator<Iterator>> {
     79 private:
     80  using IteratorReference = typename std::iterator_traits<Iterator>::reference;
     81 
     82 public:
     83  using iterator_category = std::input_iterator_tag;
     84  using iterator_concept = std::forward_iterator_tag;
     85  using value_type = std::pair<IteratorReference, IteratorReference>;
     86  using difference_type =
     87      typename std::iterator_traits<Iterator>::difference_type;
     88  using reference = value_type;
     89  using pointer = void;
     90 };
     91 
     92 #if defined(__cpp_lib_concepts)
     93 static_assert(
     94    std::forward_iterator<absl::base_internal::Cpp20ForwardZipIterator<int*>>);
     95 #endif  // defined(__cpp_lib_concepts)
     96 
     97 #endif  // ABSL_BASE_INTERNAL_ITERATOR_TRAITS_TEST_HELPER_H_