16 #ifndef INCLUDE_NN_NLIB_OUTPUTSTREAM_H_ 17 #define INCLUDE_NN_NLIB_OUTPUTSTREAM_H_ 33 BUFFERINGMODE_BLOCKBUFFERED = 0,
35 BUFFERINGMODE_UNBUFFERED
44 m_BufferingMode(BUFFERINGMODE_BLOCKBUFFERED) {}
64 os_buf_[os_buf_curidx_++] =
static_cast<unsigned char>(b & 0xff);
68 #ifndef NLIB_NONNULL_ENABLED 70 this->SetError(EINVAL);
75 this->SetError(EINVAL);
78 if (os_buf_curidx_ + n <= os_bufsize_) {
83 return this->Write_(p, n);
86 #ifndef NLIB_NONNULL_ENABLED 88 this->SetError(EINVAL);
93 this->SetError(EINVAL);
96 return this->WriteGather_(iov, iovcnt);
108 os_buf_ =
reinterpret_cast<unsigned char*
>(p);
109 os_bufsize_ =
static_cast<int>(nbytes);
112 if (errno_ == 0) errno_ = e;
116 virtual bool PushBuffer_(
const void* p,
size_t nbytes,
bool do_flush) NLIB_NOEXCEPT = 0;
117 virtual bool Close_() NLIB_NOEXCEPT = 0;
119 virtual bool WriteGather_(
const nlib_fd_iovec* iov,
int iovcnt)
NLIB_NOEXCEPT;
128 size_t os_buf_curidx_;
129 unsigned char* os_buf_;
151 unsigned char dummy_buf_[256];
153 virtual bool Close_()
NLIB_NOEXCEPT NLIB_OVERRIDE {
return true; }
165 size_t n = cur_ - &buf_[0];
167 return stream->Write(&buf_[0], n);
171 if (cur_ + n > &buf_[0] + N) {
173 return Flush(stream);
212 class MiniReallocOut {
215 : mem_(mem), cur_(cur), memsize_(memsize) {}
218 if (n <= *memsize_) {
222 if (!p)
return false;
223 *mem_ =
reinterpret_cast<T*
>(p);
233 const uint32_t t = 4096 /
sizeof(**mem_);
234 if (*cur_ + n >= t) {
235 newsize = ((*cur_ + n) + t - 1) & ~(t - 1);
237 newsize = 1 << (32 - nlib_clz(*cur_ + n - 1));
239 void* p =
nlib_realloc(*mem_, newsize *
sizeof(**mem_));
240 if (!p)
return false;
241 *mem_ =
reinterpret_cast<T*
>(p);
248 return (*mem_)[*cur_ + n];
271 #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...
size_t Pos() const noexcept
Returns the current position in the stream.
constexpr OutputStream() noexcept
Instantiates the object with default parameters (default constructor).
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.
The output is line buffered. This mode is set when data is output to the console. ...
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.