0
#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;
}
std :: iostream에서 상속 된 클래스 MyIoStream을 impl하려고합니다. 재정의 된 함수 MyBuffer :: xsputn 및 MyBuffer :: _ Xsgetn_s 예외를 throw합니다, 나는 자신을 잡아야하지만 throw 된 예외는 기본 클래스에 의해 catch됩니다.std :: iostream을 비활성화하는 방법 읽기 및 쓰기시 catch 시도
MyBuffer 클래스의 객체는 바깥에서 직접 사용되지 않습니다. 읽을 수 있듯이 외부에서 읽기 쓰기가 호출되면 내부 MyBuffer :: _ Xsgetn_s 및 MyBuffer :: xsputn이 호출됩니다. – sainimu78