std :: Ostream类成员变量将用作std :: ofstream或std :: ostringstream

发布于 2025-01-23 07:24:28 字数 1099 浏览 3 评论 0原文

我正在挣扎着溪流。我想拥有将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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

分分钟 2025-01-30 07:24:29

您为什么不能简单地提供两个单独的类-Filewriter 写入ofStreamStringwriter,将写入ostringstream < /代码>?让用户确定要使用哪种类型的作者,例如:

class FileWriter : public IObjectWriter {
public:
    FileWriter(const std::string& filePath) : stream(filePath) { }

    void write(MyObject& o) override {
        stream << o.getSomeValue();
    }   

private:
    std::ofstream stream;
};

class StringWriter : public IObjectWriter {
public:
    StringWriter(std::ostringstream &os) : stream(os) { }

    void write(MyObject& o) override {
        stream << o.getSomeValue();
    }   

private:
    std::ostringstream &stream;
};
FileWriter fw("path");
fw.write(obj);

std::ostringstream oss;
StringWriter sw(oss);
sw.write(obj);

然后,您可以通过添加公共基类重复使用一些代码,例如:

class StreamWriter : public IObjectWriter {
public:
    StreamWriter(std::ostream& os) : stream(os) { }

    void write(MyObject& o) override {
        stream << o.getSomeValue();
    }   

private:
    std::ostream &stream;
};


class FileWriter : public StreamWriter {
public:
    FileWriter(const std::string& filePath) : StreamWriter(ofs), ofs(filePath) { }

private:
    std::ofstream ofs;
};

class StringWriter : public StreamWriter {
public:
    StringWriter(std::ostringstream &oss) : StreamWriter(oss) { }
};

但是,在这种情况下,您不妨使用streamwriter本身,让用户通过它要写入的任何ostream衍生的对象,例如:

std::ofstream ofs("path");
StreamWriter sw1(ofs);
sw1.write(obj);

std::ostringstream oss;
StreamWriter sw2(oss);
sw2.write(obj);

Why can't you simply have two separate classes - FileWriter that writes to an ofstream, and StringWriter that writes to an ostringstream? Let the user decide which type of writer it wants to use, eg:

class FileWriter : public IObjectWriter {
public:
    FileWriter(const std::string& filePath) : stream(filePath) { }

    void write(MyObject& o) override {
        stream << o.getSomeValue();
    }   

private:
    std::ofstream stream;
};

class StringWriter : public IObjectWriter {
public:
    StringWriter(std::ostringstream &os) : stream(os) { }

    void write(MyObject& o) override {
        stream << o.getSomeValue();
    }   

private:
    std::ostringstream &stream;
};
FileWriter fw("path");
fw.write(obj);

std::ostringstream oss;
StringWriter sw(oss);
sw.write(obj);

You could then reuse some code by adding a common base class, eg:

class StreamWriter : public IObjectWriter {
public:
    StreamWriter(std::ostream& os) : stream(os) { }

    void write(MyObject& o) override {
        stream << o.getSomeValue();
    }   

private:
    std::ostream &stream;
};


class FileWriter : public StreamWriter {
public:
    FileWriter(const std::string& filePath) : StreamWriter(ofs), ofs(filePath) { }

private:
    std::ofstream ofs;
};

class StringWriter : public StreamWriter {
public:
    StringWriter(std::ostringstream &oss) : StreamWriter(oss) { }
};

But, in that case, you may as well just use StreamWriter by itself, and let the user pass in whatever ostream-derived object it wants to write to, eg:

std::ofstream ofs("path");
StreamWriter sw1(ofs);
sw1.write(obj);

std::ostringstream oss;
StreamWriter sw2(oss);
sw2.write(obj);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文