C/C++ API Reference
Loading...
Searching...
No Matches
interval_reader.h
1// Copyright 2021 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// 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, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14//
15// The header provides a set of helper utils for protobuf related operations.
16// The APIs may not be finalized yet.
17
18#pragma once
19
20#include <cstddef>
21#include <string_view>
22
23#include "pw_assert/assert.h"
24#include "pw_status/status.h"
25#include "pw_stream/stream.h"
26
27namespace pw::stream {
28
30
31// A reader wrapper that reads from a sub-interval of a given seekable
32// source reader. The IntervalReader tracks and maintains its own read offset.
33// It seeks the source reader to its current read offset before reading. In
34// this way, multiple IntervalReaders can share the same source reader without
35// interfering with each other.
37 public:
38 constexpr IntervalReader() : status_(Status::Unavailable()) {}
39
40 // Create an IntervalReader with an error status.
41 constexpr IntervalReader(Status status) : status_(status) {
42 PW_ASSERT(!status.ok());
43 }
44
45 // source_reader -- The source reader to read from.
46 // start -- starting offset to read in `source_reader`
47 // end -- ending offset in `source_reader`.
48 constexpr IntervalReader(SeekableReader& source_reader,
49 size_t start,
50 size_t end)
51 : source_reader_(&source_reader),
52 start_(start),
53 end_(end),
54 current_(start) {}
55
56 // Reset the read offset to the start of the interval
57 IntervalReader& Reset() {
58 current_ = start_;
59 return *this;
60 }
61
62 // Move the read offset to the end of the interval;
63 IntervalReader& Exhaust() {
64 current_ = end_;
65 return *this;
66 }
67
68 // Get a reference to the source reader.
69 SeekableReader& source_reader() { return *source_reader_; }
70 size_t start() const { return start_; }
71 size_t end() const { return end_; }
72 size_t current() const { return current_; }
73 size_t interval_size() const { return end_ - start_; }
74 bool ok() const { return status_.ok(); }
75 Status status() const { return status_; }
76
77 // For iterator comparison in Message.
78 bool operator==(const IntervalReader& other) const {
79 return source_reader_ == other.source_reader_ && start_ == other.start_ &&
80 end_ == other.end_ && current_ == other.current_;
81 }
82
83 private:
84 StatusWithSize DoRead(ByteSpan destination) final;
85 Status DoSeek(ptrdiff_t offset, Whence origin) final;
86 size_t DoTell() final { return current_ - start_; }
87 size_t ConservativeLimit(LimitType limit) const override {
88 if (limit == LimitType::kRead) {
89 return end_ - current_;
90 }
91 return 0;
92 }
93
94 SeekableReader* source_reader_ = nullptr;
95 size_t start_ = 0;
96 size_t end_ = 0;
97 size_t current_ = 0;
98 Status status_ = OkStatus();
99};
100
102
103} // namespace pw::stream
Definition: status.h:109
constexpr bool ok() const
Definition: status.h:214
static constexpr Status Unavailable()
Requested operation can’t finish now, but may at a later time.
Definition: status.h:187
Definition: status_with_size.h:51
Definition: interval_reader.h:36
size_t DoTell() final
Definition: interval_reader.h:86
size_t ConservativeLimit(LimitType limit) const override
Definition: interval_reader.h:87
Status DoSeek(ptrdiff_t offset, Whence origin) final
Virtual Seek() function implemented by derived classes.
StatusWithSize DoRead(ByteSpan destination) final
Virtual Read() function implemented by derived classes.
Definition: stream.h:400
Whence
Positions from which to seek.
Definition: stream.h:48
constexpr Status OkStatus()
Definition: status.h:297