CppCommon  1.0.4.1
C++ Common Library
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 
18 namespace CppCommon {
19 
21 
24 class StdInput : public Reader
25 {
26 public:
27  StdInput();
28  StdInput(const StdInput&) = delete;
29  StdInput(StdInput&& stream) = 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 
60 private:
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 
75 class StdOutput : public Writer
76 {
77 public:
78  StdOutput();
79  StdOutput(const StdOutput&) = delete;
80  StdOutput(StdOutput&& stream) = 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 
112 private:
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 
127 class StdError : public Writer
128 {
129 public:
130  StdError();
131  StdError(const StdError&) = delete;
132  StdError(StdError&& stream) = 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 
164 private:
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
void * stream() const noexcept
Get the native stream handler.
Definition: stream.cpp:307
virtual ~StdError()
Definition: stream.cpp:301
StdError & operator=(const StdError &)=delete
virtual size_t Write(const void *buffer, size_t size)=0
Write a byte buffer base method.
void Flush() override
Flush the stream.
Definition: stream.cpp:310
StdError(StdError &&stream)=delete
void swap(StdError &stream) noexcept
Swap two instances.
Definition: stream.cpp:312
StdError(const StdError &)=delete
StdError & operator=(StdError &&stream)=delete
bool IsValid() const noexcept
Is stream valid?
Definition: stream.cpp:308
Standard input stream.
Definition: stream.h:25
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
void swap(StdInput &stream) noexcept
Swap two instances.
Definition: stream.cpp:256
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
StdInput & operator=(StdInput &&stream)=delete
std::vector< uint8_t > ReadAllBytes()
Read all bytes.
Definition: reader.cpp:15
std::string ReadAllText()
Read all text.
Definition: reader.cpp:32
Standard output stream.
Definition: stream.h:76
StdOutput & operator=(StdOutput &&stream)=delete
void swap(StdOutput &stream) noexcept
Swap two instances.
Definition: stream.cpp:284
virtual ~StdOutput()
Definition: stream.cpp:273
StdOutput & operator=(const StdOutput &)=delete
virtual size_t Write(const void *buffer, size_t size)=0
Write a byte buffer base method.
StdOutput(const StdOutput &)=delete
void * stream() const noexcept
Get the native stream handler.
Definition: stream.cpp:279
void Flush() override
Flush the stream.
Definition: stream.cpp:282
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.
Definition: token_bucket.h:15
Reader interface definition.
Standard input/output/error stream inline implementation.
Writer interface definition.