SeqAn3 3.2.0
The Modern C++ library for sequence analysis.
in_file_iterator.hpp
Go to the documentation of this file.
1// -----------------------------------------------------------------------------------------------------
2// Copyright (c) 2006-2022, Knut Reinert & Freie Universität Berlin
3// Copyright (c) 2016-2022, Knut Reinert & MPI für molekulare Genetik
4// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
5// shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
6// -----------------------------------------------------------------------------------------------------
7
13#pragma once
14
15#include <cassert>
16#include <ranges>
17
19
20namespace seqan3::detail
21{
22
39template <typename file_type>
40class in_file_iterator
41{
42 static_assert(!std::is_const_v<file_type>,
43 "You cannot iterate over const files, because the iterator changes the file.");
44
45public:
52 using value_type = typename file_type::value_type;
54 using reference = typename file_type::reference;
56 using const_reference = typename file_type::reference;
58 using size_type = typename file_type::size_type;
60 using difference_type = typename file_type::difference_type;
62 using pointer = typename file_type::value_type *;
64 using iterator_category = std::input_iterator_tag;
66
71 constexpr in_file_iterator() = default;
73 constexpr in_file_iterator(in_file_iterator const &) = default;
75 constexpr in_file_iterator & operator=(in_file_iterator const &) = default;
77 constexpr in_file_iterator(in_file_iterator &&) = default;
79 constexpr in_file_iterator & operator=(in_file_iterator &&) = default;
81 ~in_file_iterator() = default;
82
84 constexpr in_file_iterator(file_type & _host) noexcept : host{&_host}
85 {}
87
92 in_file_iterator & operator++()
93 {
94 assert(host != nullptr);
95 host->read_next_record();
96 return *this;
97 }
98
100 void operator++(int)
101 {
102 assert(host != nullptr);
103 ++(*this);
104 }
105
107 reference operator*() noexcept
108 {
109 assert(host != nullptr);
110 return host->record_buffer;
111 }
112
114 reference operator*() const noexcept
115 {
116 assert(host != nullptr);
117 return host->record_buffer;
118 }
120
127 constexpr bool operator==(std::default_sentinel_t const &) const noexcept
128 {
129 assert(host != nullptr);
130 return host->at_end;
131 }
132
134 constexpr bool operator!=(std::default_sentinel_t const &) const noexcept
135 {
136 assert(host != nullptr);
137 return !host->at_end;
138 }
139
141 constexpr friend bool operator==(std::default_sentinel_t const &, in_file_iterator const & it) noexcept
142 {
143 return (it == std::default_sentinel);
144 }
145
147 constexpr friend bool operator!=(std::default_sentinel_t const &, in_file_iterator const & it) noexcept
148 {
149 return (it != std::default_sentinel);
150 }
152
159 std::streampos file_position() const
160 {
161 assert(host != nullptr);
162 return host->position_buffer;
163 }
164
166 in_file_iterator & seek_to(std::streampos const & pos)
167 {
168 assert(host != nullptr);
169 host->secondary_stream->seekg(pos);
170 if (host->secondary_stream->fail())
171 {
172 throw std::runtime_error{"Seeking to file position failed!"};
173 }
174 host->at_end = false; // iterator will not be at end if seeking to a specific record
175 host->read_next_record();
176 return *this;
177 }
179
180private:
182 file_type * host{};
183};
184
185} // namespace seqan3::detail
T operator!=(T... args)
Provides platform and dependency checks.