2013-02-28 105 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。

相關問題