简体   繁体   English

拥有一个具有写入 function 的 class,如何将其传递给接受 std::ostream& 的 function

[英]Having a class that has a write function, how to I pass it to a function accepting std::ostream&

I have an instance of a class which implements a writing interface such as this one:我有一个 class 的实例,它实现了一个这样的写入接口:

struct Writer {
    virtual size_t write(const char* buffer, size_t size, size_t offset) = 0;
};

I also have a function that takes the std::ostream& as an argument and writes to it.我还有一个 function 将std::ostream&作为参数并写入它。 Like so:像这样:

void foo(std::ostream& os) {
    os << "hello world"; // Say, I don't control this code
}

What is the simplest way to make foo write into my instance of the Writer interface?foo写入我的Writer接口实例的最简单方法是什么?

Bonus points for a Reader interface and std::istream . Reader接口和std::istream的奖励积分。

EDIT:编辑:

Maybe I should also mention, I must assume that foo writes a huge amount of data to the std::ostream , thus a solution where foo first writes into std::stringstream which I then write into the Writer won't work.也许我还应该提到,我必须假设foo将大量数据写入std::ostream ,因此foo首先写入std::stringstream然后我写入Writer的解决方案将不起作用。

Working using code from A custom ostream , I was able to write:使用来自A custom ostream 的代码,我能够编写:

#include <iostream>
#include <sstream>

struct Writer {
    virtual size_t write(const char* buffer, size_t size, size_t offset) = 0;
};
Writer& getWriter();
void foo(std::ostream& os);

struct WriteToWriter : public std::stringbuf {
    Writer& w;
    std::size_t offset{0};
    WriteToWriter(Writer& w) : w(w) {}
    virtual std::streamsize xsputn(const char_type* s, std::streamsize n) override {        
        const size_t r = w.write(s, n, offset);
        offset += r;
        return r;
    }
    virtual int_type overflow(int_type c) override {
        if (c == EOF) return EOF;
        char tmp = c;
        const size_t r = w.write(&tmp, 1, offset);
        offset += r;
        return r == 1 ? c : EOF ;
    }
};

int main()
{
    WriteToWriter wtw(getWriter());
    std::ostream out(&wtw);
    foo(out);
}

that looks like it should write directly to Writer::write看起来它应该直接Writer::write

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以将 std::ostream&amp; 传递给期望 std::ofstream 的 function - can i pass std::ostream& to a function expecting std::ofstream 如何解决这些错误? “&#39;template std :: ostream&operator &lt;&lt;的重新定义”,“未解析的重载函数类型” - How can I fix these errors? “redefinition of 'template std::ostream& operator<<” , “unresolved overloaded function type” 如何理解函数ostream&operator &lt;&lt;(ostream&os,const unsigned char * s) - How to understand function ostream& operator<< (ostream& os, const unsigned char* s) 无法将std :: cout传递给ostream&构造函数 - Cannot pass std::cout to ostream& constructor 类函数参数C ++中的(ostream&outs) - (ostream& outs) in class function parameters C++ 动态数组模板类:ostream和操作员好友功能问题 - Dynamic Array Template Class: problem with ostream& operator friend function 对于这个朋友重载运算符 function 而不仅仅是 std::cout,std::ostream&amp; out 的目的是什么? - What is the purpose of std::ostream& out for this friend overloaded operator function and not just std::cout? 如何编写一个接受std :: vector或std :: list的函数? - How to write a function accepting a std::vector or std::list? 如何在std:ostream&member中正确使用,而“this”是const? - how to use use properly in std:ostream& member, while “this” is a const? 什么是重载`&lt;&lt;`时`std :: ostream&(* f)(std :: ostream&)`,为什么我需要它? - What is `std::ostream& (*f)(std::ostream &)` when overloading `<<` and why I would need it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM