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
ストリームをフラッシュします。
#define NLIB_OVERRIDE
利用可能であればoverrideが定義されます。そうでない場合は空文字列です。
uint64_t Pos64() const noexcept
ストリーム上の現在位置を64bit整数で返します。
void SetError(errno_t e) const noexcept
OutputStreamにエラーを設定します。
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
TypeName で指定されたクラスのコピーコンストラクタと代入演算子を禁止します。
#define NLIB_SAFE_BOOL(class_name, exp)
クラス内に安全なoperator bool()を定義します。 可能であればC++11のexplicit boolを利用します。 ...
size_t Pos() const noexcept
ストリーム上の現在位置を返します。
constexpr OutputStream() noexcept
デフォルトコンストラクタです。
実際の書き込み動作を行わないOutputStreamです。
bool Write(int b) noexcept
ストリームに1バイトのデータを書き込みます。
errno_tをラップするクラスです。Visual Studioのデバッガ上での表示を改善します。
#define NLIB_NOEXCEPT
環境に合わせてnoexcept 又は同等の定義がされます。
#define NLIB_CEXPR
利用可能であればconstexprが定義されます。そうでない場合は空文字列です。
virtual ~OutputStream() noexcept
デストラクタです。何もしません。
BufferingMode GetBufferingMode() const noexcept
バッファリングモードを取得します。
出力をラインバッファします。コンソールに出力する場合に設定されます。
BufferingMode m_BufferingMode
バッファリングモードが格納されています。
void ResetBuffer(void *p, size_t nbytes) noexcept
OutputStreamが持つバッファを設定します。
#define NLIB_FINAL
利用可能であればfinalが定義されます。そうでない場合は空文字列です。
errno_t GetErrorValue() const noexcept
エラー値を取得します。
bool Write(const void *p, size_t n) noexcept
ストリームにn バイトのデータを書き込みます。
BufferingMode
OutputStreamのバッファリングモードです。
出力ストリームの基底クラスです。このクラスを実体化することはできません。
bool WriteGather(const nlib_fd_iovec *iov, int iovcnt) noexcept
複数の非連続のバッファからデータをストリームに書き出します。