当前位置: 首页>>代码示例>>C++>>正文


C++ Istream::rdstate方法代码示例

本文整理汇总了C++中Istream::rdstate方法的典型用法代码示例。如果您正苦于以下问题:C++ Istream::rdstate方法的具体用法?C++ Istream::rdstate怎么用?C++ Istream::rdstate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Istream的用法示例。


在下文中一共展示了Istream::rdstate方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: if

void
test_extractor (CharT*, Traits*, ArithmeticType*,
                const char     *cname,
                const char     *tname,
                const char     *aname,
                int             line,
                // printf formatting directive for ArithmeticType
                const char *valfmt,
                // character buffer (input sequence)
                const char *cbuf,
                // number of characters in buffer:
                std::size_t cbuf_size,
                // ctype and numpunct data
                const LocaleData &locale_data,
                // stream flags():
                int flags,
                // initial stream rdstate():
                int init_state,
                // unmasked exceptions:
                int exceptions,
                // expected exception:
                int expect_exception,
                // expected stream state after extraction:
                int expect_state,
                // expected number of extracted characters:
                int expect_extract,
                // have streambuf fail (or throw) after so many calls
                // to underflow() (each call extracts a single chracter):
                int fail_when,
                // initial value of the argument to extractor:
                ArithmeticType init_value,
                // expected value of the argument after extraction:
                ArithmeticType expect_value)
{
    _RWSTD_UNUSED (cname);
    _RWSTD_UNUSED (tname);

    typedef std::basic_istream<CharT, Traits> Istream;
    typedef MyStreambuf<CharT, Traits>        Streambuf;

    const char *fail_desc = 0;

    int fail_how = 0;

    if (fail_when < 0) {
        // have the stream buffer object's underflow() fail (by throwing
        // an exception if possible) after `fail_when' characters have
        // been extracted (this object calls underflow() for every char)
        fail_how  = Underflow | Throw;
        fail_when = -fail_when;
        fail_desc = "threw";
    }
    else if (0 < fail_when) {
        // have the stream buffer object's underflow() fail by returning
        // eof after `fail_when' characters have been extracted (this
        // object calls underflow() for every char)
        fail_how  = Underflow;
        fail_desc = "returned EOF";
    }

    // construct a stream buffer object and initialize its read sequence
    // with the character buffer
    Streambuf sb (cbuf, cbuf_size, fail_how, fail_when);

    // construct an istream object and initialize it with the user
    // defined streambuf object
    Istream is (&sb);

    if (-1 == flags) {
        // get the initial stream object's format control flags
        flags = is.flags ();
    }
    else {
        // set the stream object's format control flags
        is.flags (std::ios::fmtflags (flags));
    }

    if (-1 == exceptions) {
        // get the initial stream object's exceptions
        exceptions = is.exceptions ();
    }
    else {
        // unmask the stream objects exceptions (must be done
        // before calling setstate() to prevent the latter from
        // throwing ios::failure)
        is.exceptions (std::ios::iostate (exceptions));
    }

    if (-1 == init_state) {
        // get the initial stream object's state
        init_state = is.rdstate ();
    }
    else {
        // set the stream object's initial state

#ifndef _RWSTD_NO_EXCEPTIONS
        try {
            is.setstate (std::ios::iostate (init_state));
        }
        catch (...) {
//.........这里部分代码省略.........
开发者ID:Flameeyes,项目名称:stdcxx,代码行数:101,代码来源:27.istream.fmat.arith.cpp

示例2: test_readsome


//.........这里部分代码省略.........
    // exceptions()

    rw_assert (!caught || (k % 2), 0, __LINE__,
               "%{$FCALL} unexpectedly propagated an exception");

#else   // if defined (_RWSTD_NO_EXCEPTIONS)

    nread = is.readsome (buf, j);

#endif   // _RWSTD_NO_EXCEPTIONS

    //////////////////////////////////////////////////////////////////
    // verify that the function returned the expected number of
    // extracted characters

    const std::streamsize extracted = sb.pubgptr () - sb.pubeback ();

    rw_assert (extract == extracted, 0, __LINE__,
               "%{$FCALL} expected to extract %d chars, got %u; "
               "initial state = %{Is}, underflow %s at extraction %u",
               extract, extracted, states [i],
               err_type, k);

    //////////////////////////////////////////////////////////////////
    // verify that the expected number of characters have been
    // extracted from the stream

    rw_assert (cbuf + extract == sb.pubgptr (), 0, __LINE__,
               "%{$FCALL} expected to extract %d chars, got %u; "
               "initial state = %{Is}, underflow %s at extraction %u",
               extract, extracted, states [i],
               err_type, k);

    //////////////////////////////////////////////////////////////////
    // verify that the extracted characters match those in the buffer

    rw_assert (0 == std::char_traits<charT>::compare (buf, cbuf, extract),
               0, __LINE__,
               "%{$FCALL} expected to extract the first %d chars, got %{*Ac}",
               extract, int (sizeof *buf), buf);

    //////////////////////////////////////////////////////////////////
    // verify that gcount() correctly reflects the number of
    // characters successfully extracted from the stream

    rw_assert (extract == is.gcount (), 0, __LINE__,
               "%{$FCALL}: gcount() == %d, got %d; initial state = %{Is}, "
               "underflow %s at extraction %u",
               extract, is.gcount (), states [i],
               err_type, k);

    //////////////////////////////////////////////////////////////////
    // verify the state of the stream object after the function call

    // expected stream state after the function call is unchanged
    // (i.e., the initial stream state), except...
    std::ios_base::iostate expect_state = states [i];

    if (!states [i]) {

#ifndef _RWSTD_NO_EXCEPTIONS

        // ...if an extraction is attempted, or even if the first
        // character on the stream is peeked at, and an exception
        // is thrown during input, badbit should be set, otherwise
        // if in_avail() returned -1, eofbit should be set, else
        // the state should be good
        if (-2 == l)
            expect_state = std::ios_base::badbit;
        else if (l < 0)
            expect_state = std::ios_base::eofbit;
        else
            expect_state = std::ios_base::goodbit;

#else   // if defined (_RWSTD_NO_EXCEPTIONS)

        if (l < 0)
            expect_state = std::ios_base::eofbit;
        else
            expect_state = std::ios_base::goodbit;

#endif   // _RWSTD_NO_EXCEPTIONS

    }
    else {

        // ...if the initial stream state is not good, failbit
        // must be set
        expect_state = states [i] | std::ios_base::failbit;
    }

    rw_assert (is.rdstate () == expect_state, 0, __LINE__,
               "%{$FCALL}: rdstate() == %{Is}, got %{Is}; "
               "extracted %u characters; "
               "initial state = %{Is}, underflow %s at extraction %u",
               expect_state, is.rdstate (), extracted,
               states [i], err_type, k);

    ++iter;
}
开发者ID:Flameeyes,项目名称:stdcxx,代码行数:101,代码来源:27.istream.readsome.cpp


注:本文中的Istream::rdstate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。