CppCommon 1.0.5.0
C++ Common Library
Loading...
Searching...
No Matches
stream.h
Go to the documentation of this file.
1
9#ifndef CPPCOMMON_SYSTEM_STREAM_H
10#define CPPCOMMON_SYSTEM_STREAM_H
11
12#include "common/reader.h"
13#include "common/writer.h"
14#include "errors/exceptions.h"
15
16#include <memory>
17
18namespace CppCommon {
19
21
24class StdInput : public Reader
25{
26public:
27 StdInput();
28 StdInput(const StdInput&) = delete;
30 virtual ~StdInput();
31
32 StdInput& operator=(const StdInput&) = delete;
34
36 explicit operator bool() const noexcept { return IsValid(); }
37
39 void* stream() const noexcept;
40
42 bool IsValid() const noexcept;
43
45
50 size_t Read(void* buffer, size_t size) override;
51
52 using Reader::ReadAllBytes;
53 using Reader::ReadAllText;
54 using Reader::ReadAllLines;
55
57 void swap(StdInput& stream) noexcept;
58 friend void swap(StdInput& stream1, StdInput& stream2) noexcept;
59
60private:
61 class Impl;
62
63 Impl& impl() noexcept { return reinterpret_cast<Impl&>(_storage); }
64 const Impl& impl() const noexcept { return reinterpret_cast<Impl const&>(_storage); }
65
66 static const size_t StorageSize = 8;
67 static const size_t StorageAlign = 8;
68 alignas(StorageAlign) std::byte _storage[StorageSize];
69};
70
72
75class StdOutput : public Writer
76{
77public:
78 StdOutput();
79 StdOutput(const StdOutput&) = delete;
81 virtual ~StdOutput();
82
83 StdOutput& operator=(const StdOutput&) = delete;
85
87 explicit operator bool() const noexcept { return IsValid(); }
88
90 void* stream() const noexcept;
91
93 bool IsValid() const noexcept;
94
96
101 size_t Write(const void* buffer, size_t size) override;
102
103 using Writer::Write;
104
106 void Flush() override;
107
109 void swap(StdOutput& stream) noexcept;
110 friend void swap(StdOutput& stream1, StdOutput& stream2) noexcept;
111
112private:
113 class Impl;
114
115 Impl& impl() noexcept { return reinterpret_cast<Impl&>(_storage); }
116 const Impl& impl() const noexcept { return reinterpret_cast<Impl const&>(_storage); }
117
118 static const size_t StorageSize = 8;
119 static const size_t StorageAlign = 8;
120 alignas(StorageAlign) std::byte _storage[StorageSize];
121};
122
124
127class StdError : public Writer
128{
129public:
130 StdError();
131 StdError(const StdError&) = delete;
133 virtual ~StdError();
134
135 StdError& operator=(const StdError&) = delete;
137
139 explicit operator bool() const { return IsValid(); }
140
142 void* stream() const noexcept;
143
145 bool IsValid() const noexcept;
146
148
153 size_t Write(const void* buffer, size_t size) override;
154
155 using Writer::Write;
156
158 void Flush() override;
159
161 void swap(StdError& stream) noexcept;
162 friend void swap(StdError& stream1, StdError& stream2) noexcept;
163
164private:
165 class Impl;
166
167 Impl& impl() noexcept { return reinterpret_cast<Impl&>(_storage); }
168 const Impl& impl() const noexcept { return reinterpret_cast<Impl const&>(_storage); }
169
170 static const size_t StorageSize = 8;
171 static const size_t StorageAlign = 8;
172 alignas(StorageAlign) std::byte _storage[StorageSize];
173};
174
175} // namespace CppCommon
176
177#include "stream.inl"
178
179#endif // CPPCOMMON_SYSTEM_STREAM_H
Reader interface.
Definition reader.h:24
Standard error stream.
Definition stream.h:128
size_t Write(const void *buffer, size_t size) override
Write a byte buffer into the stream.
Definition stream.cpp:309
void * stream() const noexcept
Get the native stream handler.
Definition stream.cpp:307
StdError & operator=(StdError &&stream)=delete
virtual ~StdError()
Definition stream.cpp:301
void Flush() override
Flush the stream.
Definition stream.cpp:310
friend void swap(StdError &stream1, StdError &stream2) noexcept
Definition stream.inl:21
StdError(StdError &&stream)=delete
StdError & operator=(const StdError &)=delete
StdError(const StdError &)=delete
bool IsValid() const noexcept
Is stream valid?
Definition stream.cpp:308
Standard input stream.
Definition stream.h:25
friend void swap(StdInput &stream1, StdInput &stream2) noexcept
Definition stream.inl:11
StdInput(const StdInput &)=delete
bool IsValid() const noexcept
Is stream valid?
Definition stream.cpp:253
virtual ~StdInput()
Definition stream.cpp:246
StdInput & operator=(const StdInput &)=delete
std::vector< std::string > ReadAllLines()
Read all text lines.
Definition reader.cpp:38
void * stream() const noexcept
Get the native stream handler.
Definition stream.cpp:252
StdInput(StdInput &&stream)=delete
size_t Read(void *buffer, size_t size) override
Read a bytes buffer from the stream.
Definition stream.cpp:254
std::vector< uint8_t > ReadAllBytes()
Read all bytes.
Definition reader.cpp:15
StdInput & operator=(StdInput &&stream)=delete
std::string ReadAllText()
Read all text.
Definition reader.cpp:32
Standard output stream.
Definition stream.h:76
StdOutput & operator=(const StdOutput &)=delete
virtual ~StdOutput()
Definition stream.cpp:273
StdOutput(const StdOutput &)=delete
void * stream() const noexcept
Get the native stream handler.
Definition stream.cpp:279
friend void swap(StdOutput &stream1, StdOutput &stream2) noexcept
Definition stream.inl:16
void Flush() override
Flush the stream.
Definition stream.cpp:282
StdOutput & operator=(StdOutput &&stream)=delete
size_t Write(const void *buffer, size_t size) override
Write a byte buffer into the stream.
Definition stream.cpp:281
StdOutput(StdOutput &&stream)=delete
bool IsValid() const noexcept
Is stream valid?
Definition stream.cpp:280
Writer interface.
Definition writer.h:23
Exceptions definition.
C++ Common project definitions.
Reader interface definition.
Standard input/output/error stream inline implementation.
Writer interface definition.