16 #ifndef INCLUDE_NN_NLIB_OUTPUTSTREAM_H_ 17 #define INCLUDE_NN_NLIB_OUTPUTSTREAM_H_ 33 kBufferingModeBlockBuffered = 0,
36 BUFFERINGMODE_BLOCKBUFFERED = kBufferingModeBlockBuffered,
37 BUFFERINGMODE_LINEBUFFERED = kBufferingModeLineBuffered,
38 BUFFERINGMODE_UNBUFFERED = kBufferingModeUnbuffered
47 m_BufferingMode(kBufferingModeBlockBuffered) {}
67 os_buf_[os_buf_curidx_++] =
static_cast<unsigned char>(b & 0xff);
71 #ifndef NLIB_NONNULL_ENABLED 73 this->SetError(EINVAL);
78 this->SetError(EINVAL);
81 if (os_buf_curidx_ + n <= os_bufsize_) {
86 return this->Write_(p, n);
89 #ifndef NLIB_NONNULL_ENABLED 91 this->SetError(EINVAL);
96 this->SetError(EINVAL);
99 return this->WriteGather_(iov, iovcnt);
111 os_buf_ =
reinterpret_cast<unsigned char*
>(p);
112 os_bufsize_ =
static_cast<int>(nbytes);
115 if (errno_ == 0) errno_ = e;
119 virtual bool PushBuffer_(
const void* p,
size_t nbytes,
bool do_flush) NLIB_NOEXCEPT = 0;
120 virtual bool Close_() NLIB_NOEXCEPT = 0;
122 virtual bool WriteGather_(
const nlib_fd_iovec* iov,
int iovcnt)
NLIB_NOEXCEPT;
131 size_t os_buf_curidx_;
132 unsigned char* os_buf_;
154 unsigned char dummy_buf_[256];
156 virtual bool Close_()
NLIB_NOEXCEPT NLIB_OVERRIDE {
return true; }
168 size_t n = cur_ - &buf_[0];
170 return stream->Write(&buf_[0], n);
174 if (cur_ + n > &buf_[0] + N) {
176 return Flush(stream);
215 class MiniReallocOut {
218 : mem_(mem), cur_(cur), memsize_(memsize) {}
221 if (n <= *memsize_) {
225 if (!p)
return false;
226 *mem_ =
reinterpret_cast<T*
>(p);
236 const uint32_t t = 4096 /
sizeof(**mem_);
237 if (*cur_ + n >= t) {
238 newsize = ((*cur_ + n) + t - 1) & ~(t - 1);
240 newsize = 1 << (32 -
nlib_clz32(*cur_ + n - 1));
242 void* p =
nlib_realloc(*mem_, newsize *
sizeof(**mem_));
243 if (!p)
return false;
244 *mem_ =
reinterpret_cast<T*
>(p);
251 return (*mem_)[*cur_ + n];
274 #endif // INCLUDE_NN_NLIB_OUTPUTSTREAM_H_ bool Flush() noexcept
Flushes the stream.
#define NLIB_OVERRIDE
Defines override if it is available for use. If not, holds an empty string.
uint64_t Pos64() const noexcept
Returns the current position in the stream as a 64-bit integer.
void SetError(errno_t e) const noexcept
Sets an error to OutputStream.
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
#define NLIB_SAFE_BOOL(class_name, exp)
Defines a safe operator bool function in the class. Uses the C++11 explicit bool if it is available f...
The output is line buffered. This mode is set when data is output to the console. ...
size_t Pos() const noexcept
Returns the current position in the stream.
constexpr OutputStream() noexcept
Instantiates the object with default parameters (default constructor).
The output is not buffered.
The class for OutputStream objects for which no actual writing takes place.
bool Write(int b) noexcept
Writes one byte of data to the stream.
Class that wraps errno_t. This class improves visual representations in the Visual Studio debugger...
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
#define NLIB_CEXPR
Defines constexpr if it is available for use. If not, holds an empty string.
virtual ~OutputStream() noexcept
Destructor. Does not do anything.
A file that contains the configuration information for each development environment.
BufferingMode GetBufferingMode() const noexcept
Gets the buffering mode.
BufferingMode m_BufferingMode
Stores the buffering mode.
void ResetBuffer(void *p, size_t nbytes) noexcept
Sets the buffer held by OutputStream.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
errno_t GetErrorValue() const noexcept
Gets the error value.
bool Write(const void *p, size_t n) noexcept
Writes n bytes of data to the stream.
BufferingMode
The buffering mode for OutputStream.
The base class for output streams. This class cannot be instantiated.
bool WriteGather(const nlib_fd_iovec *iov, int iovcnt) noexcept
Writes data from multiple non-continuous buffers to a stream.