繁体   English   中英

如何移动std :: ostream?

[英]How can an std::ostream be moved?

由于设计无法移动std::ostream因此问题变为:如何移动std::ostream以便它可以写入不同的目的地?

基本目标是让一个工厂函数获取URI并返回一些东西,让我们称之为omstream (输出可移动流),它可以像std::ostream一样使用:

omstream stream_factory(std::string const& uri);
void     process(std::ostream& out);

int main(int ac, char* av[]) {
    omstream destination{ stream_factory(ac == 2? av[1]: "example.txt") };
    process(destination);
}

omstream将负责正确移动对象:

class omstream
    : public std::ostream {
    // suitable members
public:
    omstream(/* suitable constructor arguments */);
    omstream(omstream&& other) // follow recipe of 27.9.1.11 [ofstream.cons] paragraph 4
        : std:ios(std::move(other))
        , std::ostream(std::move(other))
        // move any members {
        this->set_rdbuf(/* get the stream buffer */);
    }
    // other helpful or necessary members
};

问题是实现omstream (或者甚至是相应的类模板basic_omstream )需要什么?

你几乎做对了。 你的例子是两次构建ios基础的移动。 您应该移动直接基类。 假设有成员streambuf ,也移动它:

class omstream
    : public std::ostream {
    // suitable members
public:
    omstream(/* suitable constructor arguments */);
    omstream(omstream&& other) // follow recipe of 27.9.1.11 [ofstream.cons] paragraph 4
        : std: ostream(std::move(other)),
        // move any members {
        this->set_rdbuf(/* install the stream buffer */);
    }
    // other helpful or necessary members
};

我在set_rdbuf注释set_rdbuf “get”更改为“install”。 通常,这会将指向成员streambuf的指针安装到ios基类中。

istream/ostream的移动和交换成员的当前非正统设计被设置为使得派生类(例如ofstreamomstream )的移动和交换成员更直观。 食谱是:

移动基础和成员,并在移动构造函数中设置rdbuf

嵌入式rdbuf是整个层次结构的复杂因素。

霍华德答案中公布的代码是草稿(基于问题中的草案)。 霍华德的答案帮助解决了virtual基类std::ios一个令人困惑的问题:当移动派生流时,基类需要默认构造,因为流的std::ios部分将由std::ostream显式移动使用std::ios::move()移动构造函数。 这个答案只是填补了缺失的部分。

下面的实现维护一个指向流缓冲区的指针,该缓冲区通常预期存在于堆上,并在std::unique_ptr<...>的帮助下在销毁时释放。 由于可能需要返回std::omstream长流的流缓冲区,例如std::coutstd::unique_ptr<...>设置为使用可能无效的删除器如果omstream不拥有流缓冲区。

#include <ostream>
#include <memory>
#include <utility>

template <typename cT, typename Traits = std::char_traits<cT>>
class basic_omstream
    : public std::basic_ostream<cT, Traits>
{
    using deleter = void (*)(std::basic_streambuf<cT, Traits>*);

    static void delete_sbuf(std::basic_streambuf<cT, Traits>* sbuf) {
        delete sbuf;
    }
    static void ignore_sbuf(std::basic_streambuf<cT, Traits>*) {
    }
    std::unique_ptr<std::basic_streambuf<cT, Traits>, deleter> m_sbuf;
public:
    basic_omstream()
        : std::basic_ios<cT, Traits>()
        , std::basic_ostream<cT, Traits>(nullptr)
        , m_sbuf(nullptr, &ignore_sbuf) {
    }
    basic_omstream(std::basic_streambuf<cT, Traits>* sbuf,
                   bool owns_streambuf)
        : std::basic_ios<cT, Traits>()
        , std::basic_ostream<cT, Traits>(sbuf)
        , m_sbuf(sbuf, owns_streambuf? &delete_sbuf: &ignore_sbuf) {
        this->set_rdbuf(this->m_sbuf.get());
    }
    basic_omstream(basic_omstream&& other)
        : std::basic_ios<cT, Traits>() // default construct ios!
        , std::basic_ostream<cT, Traits>(std::move(other))
        , m_sbuf(std::move(other.m_sbuf)) {
        this->set_rdbuf(this->m_sbuf.get());
    }
    basic_omstream& operator=(basic_omstream&& other) {
        this->std::basic_ostream<cT, Traits>::swap(other);
        this->m_sbuf.swap(other.m_sbuf);
        this->set_rdbuf(this->m_sbuf.get());
        return *this;
    }
};

typedef basic_omstream<char>    omstream;
typedef basic_omstream<wchar_t> womstream;

使用std::ofstreamstd::ostringstream来初始化omstream不起作用,除非相应的流超过omstream 通常,将分配相应的流缓冲区。 例如,类omstream可以像下面的代码一样使用,该代码基于给予合适的工厂函数的URI创建流:

#include <iostream>
#include <sstream>
#include <fstream>

omstream make_stream(std::string const& uri) {
    if (uri == "stream://stdout") {
        return omstream(std::cout.rdbuf(), false);
    }
    else if (uri == "stream://stdlog") {
        return omstream(std::clog.rdbuf(), false);
    }
    else if (uri == "stream://stderr") {
        return omstream(std::cerr.rdbuf(), false);
    }
    else if (uri.substr(0, 8) == "file:///") {
        std::unique_ptr<std::filebuf> fbuf(new std::filebuf);
        fbuf->open(uri.substr(8), std::ios_base::out);
        return omstream(fbuf.release(), true);
    }
    else if (uri.substr(0, 9) == "string://") {
        return omstream(new std::stringbuf(uri.substr(9)), true);
    }
    throw std::runtime_error("unknown URI: '" + uri + "'");
}

int main(int ac, char* av[])
{
    omstream out{ make_stream(ac == 2? av[1]: "stream://stdout") };
    out << "hello, world\n";
}

如果有其他可用的流缓冲区可以从URI构造,则可以将这些缓冲区添加到make_stream()函数中。

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM