lemon/error.h: boost::shared_ptr helyett std::auto_ptr
authorklao
Sat, 19 Feb 2005 21:11:20 +0000
changeset 11573996d2098090
parent 1156 91f9236dfec9
child 1158 29961fa390a3
lemon/error.h: boost::shared_ptr helyett std::auto_ptr
(Sokkal kenylemetlenebb, es nem teljesen biztos, hogy helyes megoldas)
src/lemon/error.h
src/work/klao/error2_test.cc
     1.1 --- a/src/lemon/error.h	Fri Feb 18 16:40:48 2005 +0000
     1.2 +++ b/src/lemon/error.h	Sat Feb 19 21:11:20 2005 +0000
     1.3 @@ -1,8 +1,10 @@
     1.4  /* -*- C++ -*-
     1.5 + *
     1.6   * src/lemon/error.h - Part of LEMON, a generic C++ optimization library
     1.7   *
     1.8 - * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     1.9 - * (Egervary Combinatorial Optimization Research Group, EGRES).
    1.10 + * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi
    1.11 + * Kutatocsoport (Egervary Combinatorial Optimization Research Group,
    1.12 + * EGRES).
    1.13   *
    1.14   * Permission to use, modify and distribute this software is granted
    1.15   * provided that this copyright notice appears in all copies. For
    1.16 @@ -26,8 +28,7 @@
    1.17  #include <sstream>
    1.18  #include <iostream>
    1.19  #include <cstdlib>
    1.20 -
    1.21 -#include <boost/shared_ptr.hpp>
    1.22 +#include <memory>
    1.23  
    1.24  namespace lemon {
    1.25  
    1.26 @@ -42,7 +43,9 @@
    1.27    class ErrorMessage {
    1.28    protected:
    1.29      ///\e 
    1.30 -    boost::shared_ptr<std::ostringstream> buf;
    1.31 +    ///\todo The good solution is boost:shared_ptr...
    1.32 +    mutable
    1.33 +    std::auto_ptr<std::ostringstream> buf;
    1.34      
    1.35      ///\e 
    1.36      bool init() throw() {
    1.37 @@ -52,7 +55,7 @@
    1.38        catch(...) {
    1.39  	buf.reset();
    1.40        }
    1.41 -      return buf;
    1.42 +      return buf.get();
    1.43      }
    1.44  
    1.45    public:
    1.46 @@ -60,6 +63,8 @@
    1.47      ///\e 
    1.48      ErrorMessage() throw() { init(); }
    1.49  
    1.50 +    ErrorMessage(const ErrorMessage& em) throw() : buf(em.buf) { }
    1.51 +
    1.52      ///\e 
    1.53      ErrorMessage(const char *message) throw() {
    1.54        init();
    1.55 @@ -75,7 +80,7 @@
    1.56      ///\e 
    1.57      template <typename T>
    1.58      ErrorMessage& operator<<(const T &t) throw() {
    1.59 -      if( !buf ) return *this;
    1.60 +      if( ! buf.get() ) return *this;
    1.61  
    1.62        try {
    1.63  	*buf << t;
    1.64 @@ -87,7 +92,7 @@
    1.65  
    1.66      ///\e 
    1.67      const char* message() throw() {
    1.68 -      if( !buf ) return 0;
    1.69 +      if( ! buf.get() ) return 0;
    1.70  
    1.71        const char* mes = 0;
    1.72        try {
    1.73 @@ -187,9 +192,17 @@
    1.74    protected:
    1.75      const char *_message;
    1.76      int _line;
    1.77 -    boost::shared_ptr<std::string> _file;
    1.78 +
    1.79 +    ///\todo Much better solution is boost::shared_ptr
    1.80 +    mutable
    1.81 +    std::auto_ptr<std::string> _file;
    1.82  
    1.83    public:
    1.84 +
    1.85 +    DataFormatError(const DataFormatError &dfe) : 
    1.86 +      IOError(dfe), _message(dfe._message), _line(dfe._line),
    1.87 +      _file(dfe._file) {}
    1.88 +
    1.89      ///\e 
    1.90      explicit DataFormatError(const char *the_message)
    1.91        : _message(the_message), _line(0) {}
    1.92 @@ -222,7 +235,7 @@
    1.93      ///
    1.94      /// Returns \e "(unknown)" if the filename was not specified.
    1.95      const char* file() const {
    1.96 -      if( _file )
    1.97 +      if( _file.get() )
    1.98  	return _file->c_str();
    1.99        else
   1.100  	return "(unknown)";
   1.101 @@ -234,10 +247,10 @@
   1.102        try {
   1.103  	std::ostringstream ostr;
   1.104  	ostr << _message;
   1.105 -	if( _file || _line ) {
   1.106 +	if( _file.get() || _line ) {
   1.107  	  ostr << " (";
   1.108 -	  if( _file ) ostr << "in file '" << *_file << "'";
   1.109 -	  if( _file && _line ) ostr << " ";
   1.110 +	  if( _file.get() ) ostr << "in file '" << *_file << "'";
   1.111 +	  if( _file.get() && _line ) ostr << " ";
   1.112  	  if( _line ) ostr << "at line " << _line;
   1.113  	  ostr << ")";
   1.114  	}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/work/klao/error2_test.cc	Sat Feb 19 21:11:20 2005 +0000
     2.3 @@ -0,0 +1,68 @@
     2.4 +#include <iostream>
     2.5 +#include <memory>
     2.6 +
     2.7 +#include <boost/shared_ptr.hpp>
     2.8 +
     2.9 +using namespace std;
    2.10 +
    2.11 +class Ex : public exception {
    2.12 +
    2.13 +  typedef exception Parent;
    2.14 +
    2.15 +  mutable
    2.16 +  auto_ptr<string> uz;
    2.17 +public:
    2.18 +
    2.19 +  // boost::shared_ptr<string> uz;
    2.20 +
    2.21 +  Ex(const Ex &e) : Parent(e), uz(e.uz) {}
    2.22 +
    2.23 +  explicit
    2.24 +  Ex(const char *msg = 0) {
    2.25 +    if( msg ) {
    2.26 +      try {
    2.27 +	uz.reset(new string);
    2.28 +	*uz = msg;
    2.29 +      }
    2.30 +      catch(...) {
    2.31 +	uz.reset();
    2.32 +      }
    2.33 +    }
    2.34 +  }
    2.35 +
    2.36 +  virtual
    2.37 +  const char* what() const throw() {
    2.38 +    if( uz.get() )
    2.39 +      return uz->c_str();
    2.40 +    return "Kivetel";
    2.41 +  }
    2.42 +
    2.43 +  virtual ~Ex() throw() {}
    2.44 +};
    2.45 +
    2.46 +static void fn1() {
    2.47 +  Ex e("alma");
    2.48 +  throw e;
    2.49 +}
    2.50 +
    2.51 +static
    2.52 +void fn2() {
    2.53 +  throw Ex("korte");
    2.54 +}
    2.55 +
    2.56 +int main() {
    2.57 +  try {
    2.58 +    fn1();
    2.59 +  }
    2.60 +  catch(exception const &e) {
    2.61 +    cerr << "Hiba: " << e.what() << endl;
    2.62 +  }
    2.63 +
    2.64 +  try {
    2.65 +    fn2();
    2.66 +  }
    2.67 +  catch(exception const &e) {
    2.68 +    cerr << "Hiba: " << e.what() << endl;
    2.69 +  }
    2.70 +
    2.71 +}