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_HTTP_STREAM_HPP
18#define TOOLBOX_HTTP_STREAM_HPP
19
21#include <toolbox/io/Buffer.hpp>
23
24namespace toolbox {
25inline namespace http {
26
27constexpr char ApplicationJson[]{"application/json"};
28constexpr char TextHtml[]{"text/html"};
29constexpr char TextPlain[]{"text/plain"};
30
31class TOOLBOX_API StreamBuf final : public std::streambuf {
32 public:
33 explicit StreamBuf(Buffer& buf) noexcept
34 : buf_{buf}
35 {
36 }
37 ~StreamBuf() override;
38
39 // Copy.
40 StreamBuf(const StreamBuf&) = delete;
41 StreamBuf& operator=(const StreamBuf&) = delete;
42
43 // Move.
44 StreamBuf(StreamBuf&&) = delete;
46
47 std::streamsize pcount() const noexcept { return pcount_; }
48 void commit() noexcept { buf_.commit(pcount_); }
49 void reset() noexcept
50 {
51 pbase_ = nullptr;
52 pcount_ = 0;
53 }
54 void set_content_length(std::streamsize pos, std::streamsize len) noexcept;
55
56 protected:
57 int_type overflow(int_type c) noexcept override;
58 std::streamsize xsputn(const char_type* s, std::streamsize count) noexcept override;
59
60 private:
61 Buffer& buf_;
62 char* pbase_{nullptr};
63 std::streamsize pcount_{0};
64};
65
66class TOOLBOX_API OStream final : public std::ostream {
67 public:
68 explicit OStream(Buffer& buf) noexcept
69 : std::ostream{nullptr}
70 , buf_{buf}
71 {
72 rdbuf(&buf_);
73 }
74 ~OStream() override;
75
76 // Copy.
77 OStream(const OStream&) = delete;
78 OStream& operator=(const OStream&) = delete;
79
80 // Move.
81 OStream(OStream&&) = delete;
83
84 void commit() noexcept;
85 void reset() noexcept
86 {
87 buf_.reset();
88 *this << reset_state;
89 cloff_ = hcount_ = 0;
90 }
91 void reset(Status status, const char* content_type, NoCache no_cache = NoCache::Yes);
92
93 private:
94 StreamBuf buf_;
96 std::streamsize cloff_{0};
98 std::streamsize hcount_{0};
99};
100
101} // namespace http
102} // namespace toolbox
103
104#endif // TOOLBOX_HTTP_STREAM_HPP
#define TOOLBOX_API
Definition Config.h:39
OStream & operator=(OStream &&)=delete
void reset() noexcept
Definition Stream.hpp:85
OStream & operator=(const OStream &)=delete
OStream(const OStream &)=delete
OStream(Buffer &buf) noexcept
Definition Stream.hpp:68
OStream(OStream &&)=delete
StreamBuf & operator=(const StreamBuf &)=delete
StreamBuf(Buffer &buf) noexcept
Definition Stream.hpp:33
StreamBuf(const StreamBuf &)=delete
StreamBuf & operator=(StreamBuf &&)=delete
StreamBuf(StreamBuf &&)=delete
std::streamsize xsputn(const char_type *s, std::streamsize count) noexcept override
void reset() noexcept
Definition Stream.hpp:49
void commit() noexcept
Definition Stream.hpp:48
std::streamsize pcount() const noexcept
Definition Stream.hpp:47
constexpr char ApplicationJson[]
Definition Stream.hpp:27
constexpr char TextHtml[]
Definition Stream.hpp:28
constexpr char TextPlain[]
Definition Stream.hpp:29
constexpr detail::ResetState reset_state
I/O manipulator that resets I/O state.
Definition Stream.hpp:34
TOOLBOX_API void reset(std::ostream &os) noexcept
constexpr auto bind() noexcept
Definition Slot.hpp:92