如何移动std :: ostream?

时间:2013-12-25 17:06:01

标签: c++ c++11 iostream

由于设计std::ostream can't be moved问题变为: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)?

2 个答案:

答案 0 :(得分:7)

你几乎做对了。您的示例是移动构建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评论中将“get”更改为“install”。通常,这会将指向成员streambuf的指针安装到ios基类中。

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

  

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

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

答案 1 :(得分:5)

霍华德回答中的代码是草稿(基于问题中的草稿)。 Howard的回答帮助解决了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()函数中。