16 #ifndef INCLUDE_NN_NLIB_OUTPUTSTREAM_H_ 17 #define INCLUDE_NN_NLIB_OUTPUTSTREAM_H_ 33 kBufferingModeBlockBuffered = 0,
35 kBufferingModeUnbuffered
44 m_BufferingMode(kBufferingModeBlockBuffered) {}
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_) {
79 nlib_memcpy(&os_buf_[os_buf_curidx_], n, p, n);
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_ =
static_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;
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];
158 template<
size_t N,
class Ch = u
int8_t>
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);
185 NLIB_ASSERT(cur_ <= ptr && ptr <= &buf_[0] + N);
194 template<
class OS,
class T>
204 template<
class OS,
class T>
207 (void)nlib_memcpy(static_cast<void*>(cur_), n,
static_cast<const void*
>(data), n);
221 class MiniReallocOut {
229 if (n <= *memsize_) {
233 if (!p)
return false;
234 *mem_ =
reinterpret_cast<T*
>(p);
244 const uint32_t t = 4096 /
sizeof(**mem_);
245 if (*cur_ + n >= t) {
246 newsize = ((*cur_ + n) + t - 1) & ~(t - 1);
248 newsize = 1 << (32 -
nlib_clz32(*cur_ + n - 1));
250 void* p =
nlib_realloc(*mem_, newsize *
sizeof(**mem_));
251 if (!p)
return false;
252 *mem_ =
reinterpret_cast<T*
>(p);
259 return (*mem_)[*cur_ + n];
282 #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 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.
A file that contains the configuration information for each development environment.
BufferingMode GetBufferingMode() const noexcept
Gets the buffering mode.
#define NLIB_FINAL
Defines final if it is available for use. If not, holds an empty string.
#define NLIB_STATIC_ASSERT(exp)
Defines a static assertion. Uses static_assert if it is available for use.
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.