基本上,它归结为我是否可以使用自定义缓冲区初始化std::basic_fstream
。我想为std::basic_streambuf
使用受保护的继承,所以我可以访问get区域指针。然后简单地将我的新对象传递给basic_fstream
,以便使用它来代替默认缓冲区。
使用此:
#include <streambuf>
class strbuffer : protected std::streambuf {
public:
strbuffer();
char_type* operator+(unsigned int slots);
char_type* cur;
char_type* beg;
char_type* eob;
};
strbuffer::strbuffer() : std::streambuf() {
cur = strbuffer::gptr();
beg = strbuffer::eback();
eob = strbuffer::egptr();
}
// returns val of cur
std::basic_streambuf<char, std::char_traits<char>>::char_type* strbuffer::operator+(unsigned int slots) {
int_type t;
for (int i = 0; i < slots; i++)
t = this->sbumpc();
return new char_type(t);
}
有类似的东西:
class fstream0 : public std::basic_fstream<char, std::char_traits<char>> {
public:
fstream0();
void fInit();
void format();
bool connectFile(std::string fname);
bool connectFile(std::string path, std::string fname);
void closeFile();
std::ios_base::iostate get8(std::string& out); //32 bit size estimation?
}
fstream0
的构造函数将采用stream_buf
(cast?)对象的参数:
fstream0::fstream0(arg) : std::basic_fstream<char, std::char_traits<char>>() {
// create bare bones file stream
// buffer is provided, constructor overloaded with support for many file types
// char*, std::array, etc...
strBuf = fstream0::rdbuf();
fBuf = fstream0::rdbuf();
}
我是否必须使用pubsetbuf
来写入已初始化的缓冲区,以便为我的自定义缓冲区使用许多不同的数据类型?包括一个继承自std::streambuf
?