capture_iterator.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //
  2. // capture_iterator.h
  3. // iterator
  4. //
  5. // Created by Sam Jaffe on 4/1/23.
  6. // Copyright © 2023 Sam Jaffe. All rights reserved.
  7. //
  8. #pragma once
  9. #include <iterator/forwards.h>
  10. #include <iterator/proxy.h>
  11. #include <iterator/detail/macro.h>
  12. namespace iterator {
  13. template <typename It>
  14. class capture_iterator : public proxy<It, capture_iterator<It>> {
  15. public:
  16. using super_t = proxy<It, capture_iterator<It>>;
  17. using value_type = typename std::iterator_traits<It>::value_type;
  18. using difference_type = typename std::iterator_traits<It>::difference_type;
  19. private:
  20. value_type cache_;
  21. public:
  22. capture_iterator() = default;
  23. capture_iterator(It it) : super_t(it) { cache_ = super_t::dereference(); }
  24. value_type const & dereference() const { return cache_; }
  25. SFINAE(super_t::category_enum >= category::forward) void increment() {
  26. super_t::increment();
  27. cache_ = super_t::dereference();
  28. }
  29. SFINAE(super_t::category_enum >= category::bidirectional)
  30. void decrement() {
  31. super_t::decrement();
  32. cache_ = super_t::dereference();
  33. }
  34. SFINAE(super_t::category_enum >= category::random_access)
  35. void advance(difference_type off) {
  36. super_t::advance(off);
  37. cache_ = super_t::dereference();
  38. }
  39. };
  40. template <typename It> capture_iterator(It) -> capture_iterator<It>;
  41. }
  42. #include <iterator/detail/undef.h>