Skip to content

Commit 6f2e6ff

Browse files
committed
Edited documentation according to new API design
1 parent a68462a commit 6f2e6ff

1 file changed

Lines changed: 110 additions & 40 deletions

File tree

source/centipede/reader/binary.hpp

Lines changed: 110 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -18,45 +18,52 @@ namespace centipede::reader
1818
{
1919
/**
2020
* @class Binary
21-
* @brief Class for reading binary files.
22-
* Data is read from the binary file entry-wise, for each entry contains multiple entrypoints (of type
23-
* #centipede::EntryPoint). Before the initial read operation, #Binary::init() function needs to be called, where
24-
* the file handler is opened and internal buffers get reset.
25-
* To read one entry, #Binary::read_one_entry() needs to be called, which parses the file into the internal buffers.
26-
* To obtain the current entry, #Binary::get_current_entry() needs to be called. Note that the internal buffers will
27-
* be reset after every read, therefore #Binary::get_current_entry() has to be called before the next read.
21+
* @brief Class for reading binary files entry-wise.
2822
*
29-
* Configuration of the class is done via Binary::Config struct
23+
* Data is read from the binary file entry-wise. Each entry contains multiple
24+
* entrypoints of type #centipede::EntryPoint. Before reading, #Binary::init()
25+
* must be called to open the file and initialize the internal buffers.
26+
*
27+
* The reader can be used as a input range. Each iteration reads one
28+
* entry from the file and returns an #EntryResult. On success, the result
29+
* contains an #EntrySpan referencing the current entry. On failure, it contains
30+
* an #ErrorCode.
31+
*
32+
* Note that manual reading via #Binary::read_one_entry() and #Binary::get_current_entry()
33+
* is also supported.
34+
*
35+
* Configuration of the class is done via Binary::Config.
3036
*
3137
* #### Example usage
3238
*
33-
* ``` cpp
34-
*auto reader = centipede::reader::Binary{ centipede::reader::Binary::Config{ .in_filename = "output.bin" } };
35-
*auto init_err = reader.init();
39+
* ```cpp
40+
* auto reader = centipede::reader::Binary{
41+
* centipede::reader::Binary::Config{ .in_filename = "output.bin" }
42+
* };
3643
*
37-
* if (not init_err.has_value())
38-
* {
39-
* std::println(stderr, "Error: {}", init_err.error());
40-
* return EXIT_FAILURE;
41-
* }
44+
* auto init_err = reader.init();
45+
* if (not init_err.has_value())
46+
* {
47+
* std::println(stderr, "Error: {}", init_err.error());
48+
* return EXIT_FAILURE;
49+
* }
4250
*
43-
* auto read_err = centipede::EnumError<std::size_t>{};
51+
* for (const auto& entry : reader)
52+
* {
53+
* if (not entry.has_value())
54+
* {
55+
* std::println(stderr, "Error: {}", entry.error());
56+
* }
4457
*
45-
* while (not reader.is_end_of_file())
46-
* {
47-
* if (reader.read_one_entry())
48-
* {
49-
* const auto& current_entry = reader.get_current_entry();
50-
* }
51-
* else
52-
* {
53-
* std::println(stderr, "Error: {}", read_error())
54-
* }
55-
* }
58+
* for (const auto& entrypoint : *entry)
59+
* {
60+
* // handle entryoint
61+
* }
62+
* }
5663
*
57-
* std::println("N Entries: {}", reader.get_n_entries());
64+
* std::println("N Entries: {}", reader.get_n_entries());
5865
* ```
59-
**/
66+
*/
6067
class Binary
6168
{
6269
public:
@@ -132,7 +139,7 @@ namespace centipede::reader
132139
* - ErrorCode::reader_buffer_overflow if buffer size is too small.
133140
* - ErrorCode::reader_file_fail_to_read if the file stream is broken or file format is corrupted.
134141
* - #size_ on success
135-
**/
142+
*/
136143
[[maybe_unused]] auto read_one_entry() -> EnumError<std::size_t>;
137144

138145
/**
@@ -154,38 +161,78 @@ namespace centipede::reader
154161
* @brief Getter of n_entries_
155162
*
156163
* @return Total number of entries read by the current instance
157-
**/
164+
*/
158165
[[nodiscard]] constexpr auto get_n_entries() const -> std::size_t { return n_entries_; }
159166

160167
/**
161168
* @brief Checks if last read operation reached end of file.
162169
* @return Returns true if end of file is reached.
163-
**/
170+
*/
164171
[[nodiscard]] auto is_end_of_file() const -> bool { return end_of_file_; }
165172

166173
using EntrySpan = std::span<const BufferType::value_type>;
167174
using EntryResult = std::expected<EntrySpan, ErrorCode>;
168175

176+
/**
177+
* @brief Sentinel type marking the end of a Binary reader range.
178+
*
179+
* The sentinel does not store state itself. End detection is handled by
180+
* Binary::Iterator, which stops once EOF or an empty read is reached.
181+
*/
169182
class Sentinel
170183
{
171184
};
172185

186+
/**
187+
* @brief Input iterator for entry-wise reading of a binary file.
188+
*
189+
* The iterator reads entries from the associated Binary reader. On
190+
* construction, the first entry is read. Each increment reads the next entry.
191+
*
192+
* Dereferencing returns an EntryResult:
193+
* - on success, the result contains an EntrySpan pointing to the current entry
194+
* stored in the reader's internal buffer;
195+
* - on failure, the result contains the corresponding ErrorCode.
196+
*
197+
* The returned span is valid only until the iterator is incremented, because
198+
* incrementing reads the next entry and resets/reuses the internal buffers.
199+
*/
173200
class Iterator
174201
{
175202
public:
203+
/**
204+
* @brief Constructs an iterator for the given Binary reader.
205+
*
206+
* Construction performs the first read operation.
207+
*
208+
* @param reader_ptr Pointer to the associated Binary reader instance.
209+
*/
176210
explicit Iterator(Binary* reader_ptr)
177211
: reader_{ reader_ptr }
178212
{
179213
++(*this);
180214
}
181215

182-
using iterator_category = std::input_iterator_tag;
183-
using difference_type = std::ptrdiff_t;
184-
using value_type = EntryResult;
185-
using reference = const EntryResult&;
216+
using iterator_category = std::input_iterator_tag; //!< Iterator category type.
217+
using difference_type = std::ptrdiff_t; //!< Difference type.
218+
using value_type = EntryResult; //!< Dereferenced value type.
219+
using reference = const EntryResult&; //!< Dereference reference type.
186220

221+
/**
222+
* @brief Dereferences the iterator.
223+
*
224+
* @return Returns the current EntryResult.
225+
*/
187226
auto operator*() const -> const EntryResult& { return current_; }
188227

228+
/**
229+
* @brief Advances the iterator to the next entry.
230+
*
231+
* Reads the next entry from the underlying Binary reader and updates the
232+
* internal state accordingly.
233+
*
234+
* @return Reference to the incremented iterator.
235+
*/
189236
auto operator++() -> Iterator&
190237
{
191238
auto result = reader_->read_one_entry();
@@ -208,21 +255,44 @@ namespace centipede::reader
208255
return *this;
209256
}
210257

258+
/**
259+
* @brief Post-increment operator.
260+
*
261+
* @return Copy of the iterator before incrementing.
262+
*/
211263
auto operator++(int) -> Iterator
212264
{
213265
auto tmp = *this;
214266
++(*this);
215267
return tmp;
216268
}
217269

270+
/**
271+
* @brief Compares iterator against the end sentinel.
272+
*
273+
* @param sentinel End sentinel.
274+
* @return Returns true while iteration is not finished.
275+
*/
218276
auto operator!=(const Sentinel&) const -> bool { return not done_; }
219277

220278
private:
221-
Binary* reader_{};
222-
EntryResult current_{ EntrySpan{} };
223-
bool done_{ false };
279+
Binary* reader_{}; //!< Associated Binary reader instance.
280+
EntryResult current_{ EntrySpan{} }; //!< Current iterator value.
281+
bool done_{ false }; //!< Indicates if iteration reached end-of-range.
224282
};
283+
284+
/**
285+
* @brief Returns an iterator positioned at the first readable entry.
286+
*
287+
* @return Iterator initialized with the first entry read from file.
288+
*/
225289
auto begin() -> Iterator { return Iterator{ this }; }
290+
291+
/**
292+
* @brief Returns the end sentinel of the Binary reader range.
293+
*
294+
* @return Sentinel representing the end of the range.
295+
*/
226296
auto end() const -> Sentinel { return Sentinel{}; }
227297

228298
private:

0 commit comments

Comments
 (0)