iostream
ストリームにオンザフライで書き込んだり、ストリームから読み取ったりするデータを変更できるアダプタクラスを作成したいと思います。アダプター自体は、iostream
サードパーティのコードに対して真の透過性を実現するためのものである必要があります。
StreamEncoder
から派生したクラスの例std::ostream
:
// External algorithm, creates large amounts of log data
int foo(int bar, std::ostream& logOutput);
int main()
{
// The target file
std::ofstream file("logfile.lzma");
// A StreamEncoder compressing the output via LZMA
StreamEncoder lzmaEncoder(file, &encodeLzma);
// A StreamEncoder converting the UTF-8 log data to UTF-16
StreamEncoder utf16Encoder(lzmaEncoder, &utf8ToUtf16);
// Call foo(), but write the log data to an LZMA-compressed UTF-16 file
cout << foo(42, utf16Encoder);
}
私の知る限り、新しい派生物を作成してサブクラスにbasic_streambuf
埋め込む必要がありますが、それはかなり複雑なようです。basic_ostream
これを達成するためのより簡単な方法はありますか?