Return 从函数提升 streambuf

Return boost streambuf from function

我正在尝试将读取 gz 文件的代码打包成一个函数,源代码取自 https://techoverflow.net/2013/11/03/c-iterating-lines-in-a-gz-file-using-boostiostreams/

我的尝试

boost::iostreams::filtering_streambuf<boost::iostreams::input> func(std::string filename);
boost::iostreams::filtering_streambuf<boost::iostreams::input> func(std::string filename)
{
  std::ifstream file(filename, std::ios_base::in | std::ios_base::binary);
  boost::iostreams::filtering_streambuf<boost::iostreams::input> inbuf;
  inbuf.push(boost::iostreams::gzip_decompressor());
  inbuf.push(file);
  return inbuf;

}
void mymainfunc(std::string filename)
{

  //Convert streambuf to istream
  std::istream ifstrm( func( filename));
  std::string line;
  while(std::getline(ifstrm, line)) {
        std::cout << line << std::endl;
    }
}

代码 运行 如果不通过函数 运行 没问题,我认为我在 return 类型中做错了。 错误:https://pastebin.com/kFpjYG0M

流不可复制。其实这个filteringstreambuf是连动都动不了的

所以在这种情况下,您需要通过智能指针动态分配和return。然而,即使只是 returning 过滤 streambuf 也不起作用,因为它会保存对 ifstream 的引用。那是本地人。

所以,也许你需要打包它:

Live On Coliru

#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <fstream>

namespace bio = boost::iostreams;

struct MySource {
    using fisb = bio::filtering_istreambuf;

    struct State {
        State(std::string filename) : ifs(filename, std::ios::binary) {
            buf.push(bio::gzip_decompressor());
            buf.push(ifs);
        }

        fisb buf;
        std::ifstream ifs;
        std::istream is { &buf };
    };

    std::unique_ptr<State> _state;

    operator std::istream&() const { return _state->is; }
};

MySource func(std::string filename) {
    auto inbuf = std::make_unique<MySource::State>(filename);
    return {std::move(inbuf)};
}

#include <iostream>
void mymainfunc(std::string filename)
{
    auto source = func(filename);
    std::istream& is = source;

    std::string line;
    while(std::getline(is, line)) {
        std::cout << line << std::endl;
    }
}

int main(){
    mymainfunc("test.cpp.gz");
}

选择 #1

你可以简化一下:

Live On Coliru

struct MySource {
    struct State {
        State(std::string filename) : ifs(filename, std::ios::binary) {
            is.push(bio::gzip_decompressor());
            is.push(ifs);
        }

        std::ifstream ifs;
        bio::filtering_istream is;
    };

    std::unique_ptr<State> _state;

    operator std::istream&() const { return _state->is; }
};

不单独处理流缓冲区使它更简单。

选择#2

不照抄自有其风采:

Live On Coliru

#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <fstream>
#include <iostream>

void mymainfunc(std::istream& is) {
    std::string line;
    while(std::getline(is, line)) {
        std::cout << line << std::endl;
    }
}

namespace bio = boost::iostreams;

int main(){
    std::ifstream ifs("test.cpp.gz", std::ios::binary);

    bio::filtering_istream is;
    is.push(bio::gzip_decompressor());
    is.push(ifs);

    mymainfunc(is);
}