派生 std::ofstream 并重载 operator<<
Deriving std::ofstream and overloading operator<<
如何从 std::ofstream class 导出,在写入文件之前添加一些操作?
换句话说,替换像
这样的代码
int main()
{
std::ofstream file("file.txt");
file << "something" << std::endl;
return 0;
}
来自
class MyFile : public std::ofstream
{
MyFile(std::string filename) : std::ofstream(filename) {}
??? operator<< ??? <-- HERE1
{
// 1. do some stuff with the input
// 2. if wanted,
// flush it to the base class operator<<
}
};
class MyFileC
{
private:
std::ofstream intern_os;
public:
MyFileC(std::string filename) : intern_os(filename) {}
MyFileC& operator<<( input ??? <-- HERE2 )
{
// 1. do some stuff with the input
// e.g (a) IF the input is a std::string,
// THEN, if it contains "signal",
// OnSignal();
// or (b) if file size is too big, clear it ...
// 2. Flush it (for all possible types):
intern_os << input;
}
};
int main()
{
MyFile file("file2.txt"); // (or with MyFileC)
file << "something" << std::endl;
return 0;
}
我们可以在写入之前即时过滤等等
在 ??? 行中添加什么以享受现有的所有 std::ofstream.operator<<(),以及我们的个人添加,好吗?
- HERE1 :更优雅,采用继承方法;
或者如果不可能(在下面的评论之后),
- HERE2 : 要传递什么类型 "whatever that could passed to the internal std::ofstream" (strings, ints, ...)
operator<<
不打算被派生流 classes 覆盖。相反,您应该覆盖 std::basic_streambuf::overflow
and/or std::basic_streambuf::xsputn
.
overflow
特别是在缓冲区溢出时在内部调用。如果你想提供你自己的缓冲,你可以通过在覆盖流 class 中的 std::basic_streambuf::setp
初始化流指针来实现。这些链接附带了如何执行此操作的示例。
这里只有一个外观问题:这些都是 buffer 方法。您想要覆盖 std::fstream
,这是一个 流 class,而不是 buffer class。因此,您需要做的是双重的:
- 如上所述覆盖
std::basic_filebuf
。
- 使用
std::fstream::rdbuf
将文件流对象的关联缓冲区设置为覆盖文件缓冲区的实例 class。您可以在现有 std::fstream
的实例或该类型的自定义子 class 上执行此操作。
如何从 std::ofstream class 导出,在写入文件之前添加一些操作? 换句话说,替换像
这样的代码int main()
{
std::ofstream file("file.txt");
file << "something" << std::endl;
return 0;
}
来自
class MyFile : public std::ofstream
{
MyFile(std::string filename) : std::ofstream(filename) {}
??? operator<< ??? <-- HERE1
{
// 1. do some stuff with the input
// 2. if wanted,
// flush it to the base class operator<<
}
};
class MyFileC
{
private:
std::ofstream intern_os;
public:
MyFileC(std::string filename) : intern_os(filename) {}
MyFileC& operator<<( input ??? <-- HERE2 )
{
// 1. do some stuff with the input
// e.g (a) IF the input is a std::string,
// THEN, if it contains "signal",
// OnSignal();
// or (b) if file size is too big, clear it ...
// 2. Flush it (for all possible types):
intern_os << input;
}
};
int main()
{
MyFile file("file2.txt"); // (or with MyFileC)
file << "something" << std::endl;
return 0;
}
我们可以在写入之前即时过滤等等
在 ??? 行中添加什么以享受现有的所有 std::ofstream.operator<<(),以及我们的个人添加,好吗?
- HERE1 :更优雅,采用继承方法; 或者如果不可能(在下面的评论之后),
- HERE2 : 要传递什么类型 "whatever that could passed to the internal std::ofstream" (strings, ints, ...)
operator<<
不打算被派生流 classes 覆盖。相反,您应该覆盖 std::basic_streambuf::overflow
and/or std::basic_streambuf::xsputn
.
overflow
特别是在缓冲区溢出时在内部调用。如果你想提供你自己的缓冲,你可以通过在覆盖流 class 中的 std::basic_streambuf::setp
初始化流指针来实现。这些链接附带了如何执行此操作的示例。
这里只有一个外观问题:这些都是 buffer 方法。您想要覆盖 std::fstream
,这是一个 流 class,而不是 buffer class。因此,您需要做的是双重的:
- 如上所述覆盖
std::basic_filebuf
。 - 使用
std::fstream::rdbuf
将文件流对象的关联缓冲区设置为覆盖文件缓冲区的实例 class。您可以在现有std::fstream
的实例或该类型的自定义子 class 上执行此操作。