# HG changeset patch # User klao # Date 1108847480 0 # Node ID 3996d20980907c817be7b0ed2b4604bc57b775c6 # Parent 91f9236dfec908b21a43821bf96c9aa28aa7394a lemon/error.h: boost::shared_ptr helyett std::auto_ptr (Sokkal kenylemetlenebb, es nem teljesen biztos, hogy helyes megoldas) diff -r 91f9236dfec9 -r 3996d2098090 src/lemon/error.h --- a/src/lemon/error.h Fri Feb 18 16:40:48 2005 +0000 +++ b/src/lemon/error.h Sat Feb 19 21:11:20 2005 +0000 @@ -1,8 +1,10 @@ /* -*- C++ -*- + * * src/lemon/error.h - Part of LEMON, a generic C++ optimization library * - * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport - * (Egervary Combinatorial Optimization Research Group, EGRES). + * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi + * Kutatocsoport (Egervary Combinatorial Optimization Research Group, + * EGRES). * * Permission to use, modify and distribute this software is granted * provided that this copyright notice appears in all copies. For @@ -26,8 +28,7 @@ #include #include #include - -#include +#include namespace lemon { @@ -42,7 +43,9 @@ class ErrorMessage { protected: ///\e - boost::shared_ptr buf; + ///\todo The good solution is boost:shared_ptr... + mutable + std::auto_ptr buf; ///\e bool init() throw() { @@ -52,7 +55,7 @@ catch(...) { buf.reset(); } - return buf; + return buf.get(); } public: @@ -60,6 +63,8 @@ ///\e ErrorMessage() throw() { init(); } + ErrorMessage(const ErrorMessage& em) throw() : buf(em.buf) { } + ///\e ErrorMessage(const char *message) throw() { init(); @@ -75,7 +80,7 @@ ///\e template ErrorMessage& operator<<(const T &t) throw() { - if( !buf ) return *this; + if( ! buf.get() ) return *this; try { *buf << t; @@ -87,7 +92,7 @@ ///\e const char* message() throw() { - if( !buf ) return 0; + if( ! buf.get() ) return 0; const char* mes = 0; try { @@ -187,9 +192,17 @@ protected: const char *_message; int _line; - boost::shared_ptr _file; + + ///\todo Much better solution is boost::shared_ptr + mutable + std::auto_ptr _file; public: + + DataFormatError(const DataFormatError &dfe) : + IOError(dfe), _message(dfe._message), _line(dfe._line), + _file(dfe._file) {} + ///\e explicit DataFormatError(const char *the_message) : _message(the_message), _line(0) {} @@ -222,7 +235,7 @@ /// /// Returns \e "(unknown)" if the filename was not specified. const char* file() const { - if( _file ) + if( _file.get() ) return _file->c_str(); else return "(unknown)"; @@ -234,10 +247,10 @@ try { std::ostringstream ostr; ostr << _message; - if( _file || _line ) { + if( _file.get() || _line ) { ostr << " ("; - if( _file ) ostr << "in file '" << *_file << "'"; - if( _file && _line ) ostr << " "; + if( _file.get() ) ostr << "in file '" << *_file << "'"; + if( _file.get() && _line ) ostr << " "; if( _line ) ostr << "at line " << _line; ostr << ")"; } diff -r 91f9236dfec9 -r 3996d2098090 src/work/klao/error2_test.cc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/work/klao/error2_test.cc Sat Feb 19 21:11:20 2005 +0000 @@ -0,0 +1,68 @@ +#include +#include + +#include + +using namespace std; + +class Ex : public exception { + + typedef exception Parent; + + mutable + auto_ptr uz; +public: + + // boost::shared_ptr uz; + + Ex(const Ex &e) : Parent(e), uz(e.uz) {} + + explicit + Ex(const char *msg = 0) { + if( msg ) { + try { + uz.reset(new string); + *uz = msg; + } + catch(...) { + uz.reset(); + } + } + } + + virtual + const char* what() const throw() { + if( uz.get() ) + return uz->c_str(); + return "Kivetel"; + } + + virtual ~Ex() throw() {} +}; + +static void fn1() { + Ex e("alma"); + throw e; +} + +static +void fn2() { + throw Ex("korte"); +} + +int main() { + try { + fn1(); + } + catch(exception const &e) { + cerr << "Hiba: " << e.what() << endl; + } + + try { + fn2(); + } + catch(exception const &e) { + cerr << "Hiba: " << e.what() << endl; + } + +}