middle of porting; unstable, don't checkout; refs #1
This commit is contained in:
55
doc/examples/exceptionhandling.cpp
Normal file
55
doc/examples/exceptionhandling.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#include <mrw/exception.hpp>
|
||||
#include <mrw/stacktrace.hpp>
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
|
||||
void unexpectedHandler() {
|
||||
try {
|
||||
throw;
|
||||
} catch (mrw::exception& x) {
|
||||
mrw::StackTrace::createSymtable();
|
||||
std::cerr<<"UNEXPECTED:"<<x.what()<<std::endl
|
||||
<<"---------------------------Stack:"<<std::endl
|
||||
<<x.stacktrace()
|
||||
<<"---------------------------------"<<std::endl;
|
||||
} catch (std::exception& x) {
|
||||
std::cerr<<"UNEXPECTED:"<<x.what()<<std::endl;
|
||||
} catch (...) {
|
||||
std::cerr<<"UNKNOWN UNEXPECTED"<<std::endl;
|
||||
}
|
||||
throw std::bad_exception(); // try to recover
|
||||
}
|
||||
|
||||
void fn2() throw(std::bad_exception) {
|
||||
std::cout<<"enter fn2"<<std::endl;
|
||||
throw mrw::exception(); // that's wrong, no exception excpected
|
||||
std::cout<<"leave fn2"<<std::endl;
|
||||
}
|
||||
|
||||
void fn1() throw(std::bad_exception) {
|
||||
std::cout<<"enter fn1"<<std::endl;
|
||||
fn2();
|
||||
std::cout<<"leave fn1"<<std::endl;
|
||||
}
|
||||
|
||||
void fn0() throw(std::bad_exception) {
|
||||
std::cout<<"enter fn0"<<std::endl;
|
||||
try {
|
||||
fn1();
|
||||
} catch (std::exception& x) {
|
||||
std::cerr<<"EXCEPTION caught in fn0:"<<x.what()<<std::endl;
|
||||
}
|
||||
std::cout<<"leave fn0"<<std::endl;
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::set_unexpected(&unexpectedHandler);
|
||||
try {
|
||||
std::cout<<"call fn0"<<std::endl;
|
||||
fn0();
|
||||
std::cout<<"call of fn0 successful"<<std::endl;
|
||||
} catch (...) {
|
||||
std::cerr<<"OOOPS!!!"<<std::endl;
|
||||
}
|
||||
return 0;
|
||||
}
|
25
doc/examples/smartpointer.cpp
Normal file
25
doc/examples/smartpointer.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <mrw/smartpointer.hpp>
|
||||
|
||||
class A {
|
||||
public: int a;
|
||||
protected: virtual void fn() {} // dynamic_cast requires a virtual function
|
||||
};
|
||||
|
||||
class B: virtual public A {
|
||||
public: int b;
|
||||
};
|
||||
|
||||
int main(int, char**) {
|
||||
utl::SmartPointer<int> i1 = new int;
|
||||
*i1 = 13;
|
||||
utl::SmartPointer<int> i2 = i1;
|
||||
utl::SmartPointer<int> i3;
|
||||
if (!i3) i3 = i2;
|
||||
*i2 = 666; // *i1 is now 666 too
|
||||
utl::SmartPointer<A> b1 = new B;
|
||||
utl::SmartPointer<B> b2 = b1; // b1 and b2 point to the same instance
|
||||
b1->a = 0; // b1->b does not compile
|
||||
b2->a = 1;
|
||||
b2->b = 2;
|
||||
return 0;
|
||||
} // memory is automatically freed
|
Reference in New Issue
Block a user