moved doc/examples to examples according to new build process
This commit is contained in:
		
							
								
								
									
										95
									
								
								examples/arguments.cxx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										95
									
								
								examples/arguments.cxx
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,95 @@
 | 
			
		||||
/*! @file
 | 
			
		||||
 | 
			
		||||
    @id $Id$
 | 
			
		||||
*/
 | 
			
		||||
//       1         2         3         4         5         6         7         8
 | 
			
		||||
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
 | 
			
		||||
 | 
			
		||||
#include <mrw/args.hxx>
 | 
			
		||||
#ifdef MRW__OLD_PRE11_COMPILER
 | 
			
		||||
#include <mrw/vector.hxx>
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
void test_func() {
 | 
			
		||||
  std::cout<<"test parameter was given on command line"<<std::endl;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main(int argc, char** argv) try {
 | 
			
		||||
 | 
			
		||||
  // option variables
 | 
			
		||||
  bool flag(false);
 | 
			
		||||
  int r(5);
 | 
			
		||||
  std::string txt("Hello World");
 | 
			
		||||
  int x(0), y(0);
 | 
			
		||||
 | 
			
		||||
  std::string description("This is an example for argument processing. If the "
 | 
			
		||||
                          "description is very long, it is automatically "
 | 
			
		||||
                          "splitted over several lines and indented on each "
 | 
			
		||||
                          "new line. Arguments may have several parameters. "
 | 
			
		||||
                          "Help is generated automatically from the argument "
 | 
			
		||||
                          "declaration. The argument declaration contains long "
 | 
			
		||||
                          "and short options and a list of parameters.");
 | 
			
		||||
  std::string returns("Returns a status of 0 on success and 1 on any error.");
 | 
			
		||||
 | 
			
		||||
  // bind and evaluate options
 | 
			
		||||
#ifdef MRW__OLD_PRE11_COMPILER
 | 
			
		||||
  mrw::args::parse(argc, argv, description, mrw::args::defaults()
 | 
			
		||||
                   <<mrw::args::decl("h", "help", "show this help",
 | 
			
		||||
                                     mrw::args::decl::param_list()
 | 
			
		||||
                                     <<mrw::args::help()
 | 
			
		||||
                                     <<mrw::args::exit())
 | 
			
		||||
                   <<mrw::args::decl("r", "repeat", "number of repetitions",
 | 
			
		||||
                                     mrw::args::decl::param_list()
 | 
			
		||||
                                     <<mrw::args::param(r, "number"))
 | 
			
		||||
                   <<mrw::args::decl("f", "flag", "check a flag",
 | 
			
		||||
                                     mrw::args::decl::param_list()
 | 
			
		||||
                                     <<mrw::args::param(flag))
 | 
			
		||||
                   <<mrw::args::decl("t", "text", "pass a text",
 | 
			
		||||
                                     mrw::args::decl::param_list()
 | 
			
		||||
                                     <<mrw::args::param(txt, "text"))
 | 
			
		||||
                   <<mrw::args::decl("c", "coord", "add some 2D coordinates",
 | 
			
		||||
                                     mrw::args::decl::param_list()
 | 
			
		||||
                                     <<mrw::args::param(x, "x")
 | 
			
		||||
                                     <<mrw::args::param(y, "y"))
 | 
			
		||||
                   <<mrw::args::decl("",  "test", "call test_func, no shortcut",
 | 
			
		||||
                                     mrw::args::decl::param_list()
 | 
			
		||||
                                     <<mrw::args::func(test_func)),
 | 
			
		||||
                   returns);
 | 
			
		||||
#else
 | 
			
		||||
  mrw::args::parse(argc, argv,
 | 
			
		||||
                   description,
 | 
			
		||||
                   {
 | 
			
		||||
                     {"h", "help", "show this help",
 | 
			
		||||
                         {mrw::args::help(), mrw::args::exit()}},
 | 
			
		||||
                     {"r", "repeat", "number of repetitions",
 | 
			
		||||
                         {mrw::args::param(r, "number")}},
 | 
			
		||||
                     {"f", "flag",
 | 
			
		||||
                         "check a flag; this is a boolean, so it is false by "
 | 
			
		||||
                         "default and true if the user specifies the flag on "
 | 
			
		||||
                         "the command line", {mrw::args::param(flag)}},
 | 
			
		||||
                     {"t", "text", "pass a text",
 | 
			
		||||
                         {mrw::args::param(txt, "text")}},
 | 
			
		||||
                     {"c", "coord",
 | 
			
		||||
                         "add some 2D coordinates; this is an example on how "
 | 
			
		||||
                         "you can add more than one parameter",
 | 
			
		||||
                           {mrw::args::param(x, "x"),
 | 
			
		||||
                            mrw::args::param(y, "y")}},
 | 
			
		||||
                     {"",  "test", "call test_func, no shortcut",
 | 
			
		||||
                         {mrw::args::func(test_func)}},
 | 
			
		||||
                   },
 | 
			
		||||
                   returns);
 | 
			
		||||
#endif
 | 
			
		||||
  
 | 
			
		||||
  // show the result
 | 
			
		||||
  std::cout<<std::endl
 | 
			
		||||
           <<"commandline is parsed, these are the settings now:"<<std::endl
 | 
			
		||||
           <<"r           = "<<r<<std::endl
 | 
			
		||||
           <<"flag        = "<<(flag?"yes":"no")<<std::endl
 | 
			
		||||
           <<"txt         = "<<txt<<std::endl
 | 
			
		||||
           <<"coordinates = "<<x<<", "<<y<<std::endl;
 | 
			
		||||
  
 | 
			
		||||
  return 0;
 | 
			
		||||
 } catch (const std::exception& x) {
 | 
			
		||||
  std::cerr<<"**** ERROR: "<<x.what()<<std::endl;
 | 
			
		||||
  return 1;
 | 
			
		||||
 }
 | 
			
		||||
							
								
								
									
										55
									
								
								examples/exceptionhandling.cxx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								examples/exceptionhandling.cxx
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,55 @@
 | 
			
		||||
#include <mrw/exception.hxx>
 | 
			
		||||
#include <mrw/stacktrace.hxx>
 | 
			
		||||
#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;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										29
									
								
								examples/makefile.am
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								examples/makefile.am
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,29 @@
 | 
			
		||||
## @id $Id$
 | 
			
		||||
 | 
			
		||||
##       1         2         3         4         5         6         7         8
 | 
			
		||||
## 45678901234567890123456789012345678901234567890123456789012345678901234567890
 | 
			
		||||
 | 
			
		||||
exampledir = ${docdir}/examples
 | 
			
		||||
example_PROGRAMS = smartpointer arguments shared
 | 
			
		||||
if HAVE_STACKTRACE
 | 
			
		||||
example_PROGRAMS += exceptionhandling
 | 
			
		||||
endif
 | 
			
		||||
example_DATA = ${smartpointer_SOURCES} ${exceptionhandling_SOURCES}	\
 | 
			
		||||
               ${arguments_SOURCES}
 | 
			
		||||
 | 
			
		||||
CPPFLAGS = -I${top_srcdir}/src
 | 
			
		||||
LDFLAGS = -L${top_builddir}/src
 | 
			
		||||
 | 
			
		||||
if HAVE_STACKTRACE
 | 
			
		||||
exceptionhandling_SOURCES = exceptionhandling.cxx
 | 
			
		||||
exceptionhandling_LDADD = ${top_builddir}/src/.libs/libmrw.la
 | 
			
		||||
endif
 | 
			
		||||
 | 
			
		||||
smartpointer_SOURCES = smartpointer.cxx
 | 
			
		||||
smartpointer_LDADD = ${top_builddir}/src/.libs/libmrw.la
 | 
			
		||||
 | 
			
		||||
arguments_SOURCES = arguments.cxx
 | 
			
		||||
 | 
			
		||||
shared_SOURCES = shared.cxx
 | 
			
		||||
 | 
			
		||||
MAINTAINERCLEANFILES = makefile.in
 | 
			
		||||
							
								
								
									
										30
									
								
								examples/shared.cxx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								examples/shared.cxx
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,30 @@
 | 
			
		||||
/*! @file
 | 
			
		||||
 | 
			
		||||
    @id $Id$
 | 
			
		||||
*/
 | 
			
		||||
//       1         2         3         4         5         6         7         8
 | 
			
		||||
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
 | 
			
		||||
 | 
			
		||||
#include <mrw/shared.hxx>
 | 
			
		||||
 | 
			
		||||
#include <iostream>
 | 
			
		||||
 | 
			
		||||
class Test {
 | 
			
		||||
  public:
 | 
			
		||||
    Test(int i): _int1(i), _int2(0) {}
 | 
			
		||||
    Test(int i, int j): _int1(i), _int2(j) {}
 | 
			
		||||
    int _int1;
 | 
			
		||||
    int _int2;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
int main(int, char**) {
 | 
			
		||||
  mrw::Shared<Test> test1(15);
 | 
			
		||||
  mrw::Shared<Test> test2(13, 14);
 | 
			
		||||
  mrw::Shared<Test> test3(new Test(13));
 | 
			
		||||
  mrw::Shared<Test> test4(test2);
 | 
			
		||||
  std::cout<<"test1="<<test1->_int1<<", "<<test1->_int2<<std::endl
 | 
			
		||||
           <<"test2="<<test2->_int1<<", "<<test2->_int2<<std::endl
 | 
			
		||||
           <<"test3="<<test3->_int1<<", "<<test3->_int2<<std::endl
 | 
			
		||||
           <<"test4="<<test4->_int1<<", "<<test4->_int2<<std::endl;
 | 
			
		||||
  return 0;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										26
									
								
								examples/smartpointer.cxx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								examples/smartpointer.cxx
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,26 @@
 | 
			
		||||
#include <mrw/smartpointer.hxx>
 | 
			
		||||
 | 
			
		||||
class A {
 | 
			
		||||
  public: int a;
 | 
			
		||||
  public: virtual ~A() {}
 | 
			
		||||
  protected: virtual void fn() {} // dynamic_cast requires a virtual function
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
class B: virtual public A {
 | 
			
		||||
  public: int b;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
int main(int, char**) {
 | 
			
		||||
  mrw::SmartPointer<int> i1 = new int;
 | 
			
		||||
  *i1 = 13;
 | 
			
		||||
  mrw::SmartPointer<int> i2 = i1;
 | 
			
		||||
  mrw::SmartPointer<int> i3;
 | 
			
		||||
  if (!i3) i3 = i2;
 | 
			
		||||
  *i2 = 666;			// *i1 is now 666 too
 | 
			
		||||
  mrw::SmartPointer<A> b1 = new B;
 | 
			
		||||
  mrw::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