简体   繁体   中英

How to disable std::iostream try catch when read and write

#undef _HAS_EXCEPTIONS
#define _HAS_EXCEPTIONS 0

#include <stdio.h>
#include <streambuf>
#include <ostream>
#include <iostream>
#include <fstream>

using namespace std;


class MyIoStream : public std::basic_iostream< char, std::char_traits< char > >
{
public:
    class MyBuffer : public std::basic_streambuf<char, std::char_traits<char> >
    {

    public:
        streamsize __CLR_OR_THIS_CALL xsputn(const char *_Ptr,
            streamsize _Count)
        {
            par->clear();
            par->setstate(ios_base::goodbit);
            printf("write\n");
            return _Count;
        }
        streamsize __CLR_OR_THIS_CALL _Xsgetn_s(char * _Ptr,
            size_t _Ptr_size, streamsize _Count)
        {
            par->clear();
            par->setstate(ios_base::goodbit);
            a = a + 1;
            if(a == 2)
                throw "asdf";
            printf("read\n");
            return _Count;
        }
        MyIoStream* par;
        int a;
    };
public:

    MyIoStream() :
    std::basic_iostream< char, std::char_traits< char > >(&buf)
    {
        buf.par = this;
        buf.a = 0;
    }

private:

    MyBuffer buf;

};

void MyRead(istream* pIs, int siz, void* buf)
{
    try
    {
        pIs->read((char*)buf, siz);
    }
    catch(char*)
    {
        printf("my catch\n");
    }
}

void MyWrite(ostream* pOs, int siz, void* buf)
{
    pOs->write((const char*)buf, siz);
}

int main(void)
{
    MyIoStream o;
    char buf[1234];

    MyRead((istream*)&o, 10, buf);
    MyWrite((ostream*)&o, 10, buf);
    MyWrite((ostream*)&o, 10, buf);
    MyRead((istream*)&o, 10, buf);
    MyRead((istream*)&o, 10, buf);
    MyWrite((ostream*)&o, 10, buf);
    MyRead((istream*)&o, 10, buf);
    MyRead((istream*)&o, 10, buf);

    return 0;
}

I'm trying to impl a class MyIoStream inherited from std::iostream. The overrided function MyBuffer::xsputn and MyBuffer::_Xsgetn_s throw exceptions, I need to catch myself, but the exception thrown is catched by base class.

Assuming you want to catch the exception inside of MyBuffer::xsputn or inside of MyBuffer::_Xsgetn_s, you could wrap the code inside of the method in a try-catch block.

In my own custom LogFile class I inherit from std::streambuf, and redirect where the ostream goes, using cout.rdbuff(...). You must return the pointer to the default streambuff after your class is done with it, though. I've included a stripped down version of the class below:

class Logger : public std::streambuf
{
public:
    std::streamsize xsputn (const char* s, std::streamsize n) 
    {
        /* Your Code Here */
    }

    template <typename T>
    friend Logger& operator<<(Logger& logger, const T& v);

    Logger()
    {
        m_pPrevStreamBuffer = cout.rdbuf(this);
    }

    virtual ~Logger();
    {
        cout.rdbuf(m_pPrevStreamBuffer);
    }

private:
    std::streambuf* m_pPrevStreamBuffer;
    String m_CurrentLine;
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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