Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Stream.hpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2021 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#ifndef TOOLBOX_IO_STREAM_HPP
18#define TOOLBOX_IO_STREAM_HPP
19
20#include <toolbox/io/Buffer.hpp>
22
23namespace toolbox {
24inline namespace io {
25
26class TOOLBOX_API StreamBuf final : public std::streambuf {
27 public:
28 explicit StreamBuf(Buffer& buf) noexcept
29 : buf_{buf}
30 {
31 }
32 ~StreamBuf() override;
33
34 // Copy.
35 StreamBuf(const StreamBuf&) = delete;
36 StreamBuf& operator=(const StreamBuf&) = delete;
37
38 // Move.
39 StreamBuf(StreamBuf&&) = delete;
41
42 void commit() noexcept { buf_.commit(pcount_); }
43 void reset() noexcept
44 {
45 pbase_ = nullptr;
46 pcount_ = 0;
47 }
48
49 protected:
50 int_type overflow(int_type c) noexcept override;
51 std::streamsize xsputn(const char_type* s, std::streamsize count) noexcept override;
52
53 private:
54 Buffer& buf_;
55 char* pbase_{nullptr};
56 std::streamsize pcount_{0};
57};
58
59class TOOLBOX_API OStream final : public std::ostream {
60 public:
61 explicit OStream(Buffer& buf) noexcept
62 : std::ostream{nullptr}
63 , buf_{buf}
64 {
65 rdbuf(&buf_);
66 }
67 ~OStream() override;
68
69 // Copy.
70 OStream(const OStream&) = delete;
71 OStream& operator=(const OStream&) = delete;
72
73 // Move.
74 OStream(OStream&&) = delete;
76
77 void commit() noexcept { buf_.commit(); }
78 void reset() noexcept
79 {
80 buf_.reset();
81 *this << reset_state;
82 }
83
84 private:
85 StreamBuf buf_;
86};
87
88} // namespace io
89} // namespace toolbox
90
91#endif // TOOLBOX_IO_STREAM_HPP
#define TOOLBOX_API
Definition Config.h:39
void reset() noexcept
Definition Stream.hpp:78
OStream & operator=(OStream &&)=delete
OStream(const OStream &)=delete
void commit() noexcept
Definition Stream.hpp:77
OStream(Buffer &buf) noexcept
Definition Stream.hpp:61
OStream & operator=(const OStream &)=delete
OStream(OStream &&)=delete
StreamBuf & operator=(const StreamBuf &)=delete
StreamBuf(const StreamBuf &)=delete
StreamBuf(StreamBuf &&)=delete
void reset() noexcept
Definition Stream.hpp:43
std::streamsize xsputn(const char_type *s, std::streamsize count) noexcept override
void commit() noexcept
Definition Stream.hpp:42
StreamBuf & operator=(StreamBuf &&)=delete
StreamBuf(Buffer &buf) noexcept
Definition Stream.hpp:28
constexpr detail::ResetState reset_state
I/O manipulator that resets I/O state.
Definition Stream.hpp:34
constexpr auto bind() noexcept
Definition Slot.hpp:92