C/C++ API Reference
Loading...
Searching...
No Matches
null_stream.h
1// Copyright 2020 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#pragma once
15
16#include <cstddef>
17
18#include "pw_bytes/span.h"
19#include "pw_polyfill/language_feature_macros.h"
20#include "pw_span/span.h"
21#include "pw_status/status.h"
22#include "pw_status/status_with_size.h"
23#include "pw_stream/stream.h"
24
25namespace pw::stream {
26
28
31class NullStream final : public SeekableReaderWriter {
32 public:
33 // Gives access to a global NullStream instance. It is not necessary to have
34 // multiple NullStream instances since they have no state and do nothing.
35 static NullStream& Instance() {
36 PW_CONSTINIT static NullStream stream;
37 return stream;
38 }
39
40 private:
41 Status DoWrite(ConstByteSpan) final { return OkStatus(); }
42 StatusWithSize DoRead(ByteSpan) final { return StatusWithSize::OutOfRange(); }
43 Status DoSeek(ptrdiff_t, Whence) final { return OkStatus(); }
44};
45
48 public:
49 constexpr CountingNullStream() : bytes_written_(0) {}
50
52 size_t bytes_written() const { return bytes_written_; }
53
54 private:
56 bytes_written_ += data.size();
57 return OkStatus();
58 }
59
60 StatusWithSize DoRead(ByteSpan) final { return StatusWithSize::OutOfRange(); }
61 Status DoSeek(ptrdiff_t, Whence) final { return OkStatus(); }
62
63 size_t bytes_written_;
64};
65
67
68} // namespace pw::stream
Definition: status.h:109
Definition: status_with_size.h:51
Same as pw::stream::NullStream, but tracks the number of bytes written.
Definition: null_stream.h:47
Status DoWrite(ConstByteSpan data) final
Virtual Write() function implemented by derived classes.
Definition: null_stream.h:55
StatusWithSize DoRead(ByteSpan) final
Virtual Read() function implemented by derived classes.
Definition: null_stream.h:60
Status DoSeek(ptrdiff_t, Whence) final
Virtual Seek() function implemented by derived classes.
Definition: null_stream.h:61
size_t bytes_written() const
Definition: null_stream.h:52
Definition: null_stream.h:31
Status DoWrite(ConstByteSpan) final
Virtual Write() function implemented by derived classes.
Definition: null_stream.h:41
StatusWithSize DoRead(ByteSpan) final
Virtual Read() function implemented by derived classes.
Definition: null_stream.h:42
Status DoSeek(ptrdiff_t, Whence) final
Virtual Seek() function implemented by derived classes.
Definition: null_stream.h:43
Definition: stream.h:612
Whence
Positions from which to seek.
Definition: stream.h:48
#define PW_CONSTINIT
Definition: language_feature_macros.h:52
constexpr Status OkStatus()
Definition: status.h:297