std :: Ostream类成员变量将用作std :: ofstream或std :: ostringstream
我正在挣扎着溪流。我想拥有将MyObject数据写入文件或弦流的文件撰写者。我不希望write()的接口遵循文件路径或弦乐,因为我可能会有其他混凝土作者序列化,它们可以序列化到某些不同的设备(即插座)。因此,我需要将文件路径或弦乐传递给文件撰写器构造函数。我需要将StringStream作为filepath的替代方案,以便我可以轻松实施单元测试。
这是我描述的类的草图:
class IObjectWriter {
virtual void write(MyObject& o) = 0;
};
class FileWriter : public IObjectWriter {
FileWriter(const std::string& filePath) {
std::ofstream os(filePath, std::ios::out);
// assign in some way os to stream member variable
}
FileWriter(std::ostringstream& so) {
// assign in some way os to stream member variable
}
void write(MyObject& o) override {
stream << o.getSomeValue();
}
private:
std::ostream stream;
};
我无法解决的问题是将Ofstream和Osttream“分配”到Ostream成员,以便Write()调用可以在文件或stingstream上独立写入。
我遇到的问题:
- std :: ostream没有
- 无法分配或不可复制的默认构造函数流,
- 无法将相同的rdbuf用于ofStream和ostringstream
可能的解决方案或想法:
- 将sharone_ptr用于成员变量,但这意味着filewriter filewriter应修改构造函数以接受OSTRINGSTREAM的共享_ptr。
- 我不认为使用std :: ostream&amp;因为会员变量是一个好主意,因为通行证的生命周期处理
I'm struggling with streams. I would like to have FileWriter that writes MyObject data to file or to a stringstream. I don't want the interface of write() to take the file path or the stringstream becouse I may have other concrete writers that serialize to some different devices (i.e. socket). So I need to pass the file path or the stringstream to the FileWriter constructor. I need to have a stringstream as an alternative to a filePath so that I can easily implement unit testing.
Here is a sketch of the classes I have described:
class IObjectWriter {
virtual void write(MyObject& o) = 0;
};
class FileWriter : public IObjectWriter {
FileWriter(const std::string& filePath) {
std::ofstream os(filePath, std::ios::out);
// assign in some way os to stream member variable
}
FileWriter(std::ostringstream& so) {
// assign in some way os to stream member variable
}
void write(MyObject& o) override {
stream << o.getSomeValue();
}
private:
std::ostream stream;
};
The problem I could not solve is to "assign" the ofstream and ostringstream to the ostream member so that the write() call can write independently on file or stingstream.
Problems I've run into:
- std::ostream does not have a default constructor
- streams not assignable or copyable
- not able to use the same rdbuf for ofstream and ostringstream
Possible solutions or ideas:
- use shared_ptr for the member variable, but that means the FileWriter constructor should be modified to accept a shared_ptr for the ostringstream.
- I don't think using a std::ostream& for the member variable is a good idea, because of the lifecycle handling of the passes ostringstream
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您为什么不能简单地提供两个单独的类-Filewriter 写入
ofStream
和Stringwriter
,将写入ostringstream < /代码>?让用户确定要使用哪种类型的作者,例如:
然后,您可以通过添加公共基类重复使用一些代码,例如:
但是,在这种情况下,您不妨使用
streamwriter
本身,让用户通过它要写入的任何ostream
衍生的对象,例如:Why can't you simply have two separate classes -
FileWriter
that writes to anofstream
, andStringWriter
that writes to anostringstream
? Let the user decide which type of writer it wants to use, eg:You could then reuse some code by adding a common base class, eg:
But, in that case, you may as well just use
StreamWriter
by itself, and let the user pass in whateverostream
-derived object it wants to write to, eg: