2013-02-28 104 views
0

考虑一个简单的计数滤波器:in boost iostream filtering_ostream,sync(),strict_sync()和flush()之间有什么区别?

class CountableOstreamFilter : public boost::iostreams::multichar_output_filter { 
public: 
    CountableOstreamFilter(): m_written(0) { 
    } 

    template<typename Sink> 
    std::streamsize write(Sink& dest, const char* s, std::streamsize n) 
    { 
      auto result = boost::iostreams::write(dest, s, n); 
      assert(n == result); 
      m_written += result; 
      return result; 
    } 

    inline std::streamsize writtenBytes() const { 
     return m_written; 
    } 

private: 
    std::streamsize m_written; 
}; 

并且因此使用它:

boost::iostreams::filtering_ostream counted_cout; 
counted_cout.push(CountableOstreamFilter()); 
counted_cout.push(std::cout); 
counted_cout << "hello world"; 

会是什么调用sync()之间的差,strict_sync()或冲洗()? counting_cout.sync(); //与此调用有什么不同 counts_cout.strict_sync(); //致电 counting_cout.flush(); //给这个电话?

我使用升压1.50.0

回答

3

strict_sync关键的区别之间sync,和flush是它们的返回值。他们全部3人。他们都在filtering_stream的任何过滤器或设备上调用flush方法来满足Flushable概念。任何不支持Flushable概念的过滤器/设备都会被略过。

sync返回true,除非其中一个Flushable Filters/Devices返回false。这意味着如果有不可冲刷的滤镜/设备属于filtering_stream,数据可能会卡在其中,但sync将返回true,因为它们不是可冲刷的。

strict_sync是类似的,除非遇到非Flushable过滤器/设备。在这种情况下,即使所有Flushable Filters/Devices都返回true,strict_sync也会返回false。原因是因为strict_sync的调用者知道如果它返回true,所有数据都被成功刷新。

成员flush只是返回对流的引用,有效地丢弃flush是否成功。非会员flushhas it's own rules for what it returns depending on the input value

在你的情况下,CountableOstreamFilter不是Flushable(它不能转换为必要的flushable_tag)。因此,只要底层数据流的刷新成功,对sync的调用将返回true。但是,strict_sync应返回false。

相关问题