3 #ifndef INCLUDE_NN_NLIB_OUTPUTSTREAM_H_ 4 #define INCLUDE_NN_NLIB_OUTPUTSTREAM_H_ 20 BUFFERINGMODE_BLOCKBUFFERED = 0,
22 BUFFERINGMODE_UNBUFFERED
31 m_BufferingMode(BUFFERINGMODE_BLOCKBUFFERED) {}
51 os_buf_[os_buf_curidx_++] =
static_cast<unsigned char>(b & 0xff);
55 #ifndef NLIB_NONNULL_ENABLED 57 this->SetError(EINVAL);
62 this->SetError(EINVAL);
65 if (os_buf_curidx_ + n <= os_bufsize_) {
70 return this->Write_(p, n);
73 #ifndef NLIB_NONNULL_ENABLED 75 this->SetError(EINVAL);
80 this->SetError(EINVAL);
83 return this->WriteGather_(iov, iovcnt);
95 os_buf_ =
reinterpret_cast<unsigned char*
>(p);
96 os_bufsize_ =
static_cast<int>(nbytes);
99 if (errno_ == 0) errno_ = e;
103 virtual bool PushBuffer_(
const void* p,
size_t nbytes,
bool do_flush) NLIB_NOEXCEPT = 0;
104 virtual bool Close_() NLIB_NOEXCEPT = 0;
106 virtual bool WriteGather_(
const nlib_fd_iovec* iov,
int iovcnt)
NLIB_NOEXCEPT;
115 size_t os_buf_curidx_;
116 unsigned char* os_buf_;
138 unsigned char dummy_buf_[256];
140 virtual bool Close_()
NLIB_NOEXCEPT NLIB_OVERRIDE {
return true; }
152 size_t n = cur_ - &buf_[0];
154 return stream->Write(&buf_[0], n);
158 if (cur_ + n > &buf_[0] + N) {
160 return Flush(stream);
199 class MiniReallocOut {
202 : mem_(mem), cur_(cur), memsize_(memsize) {}
205 if (n <= *memsize_) {
209 if (!p)
return false;
210 *mem_ =
reinterpret_cast<T*
>(p);
220 const uint32_t t = 4096 /
sizeof(**mem_);
221 if (*cur_ + n >= t) {
222 newsize = ((*cur_ + n) + t - 1) & ~(t - 1);
224 newsize = 1 << (32 - nlib_clz(*cur_ + n - 1));
226 void* p =
nlib_realloc(*mem_, newsize *
sizeof(**mem_));
227 if (!p)
return false;
228 *mem_ =
reinterpret_cast<T*
>(p);
235 return (*mem_)[*cur_ + n];
258 #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.