lemon/error.h
changeset 210 81cfc04531e8
parent 108 889d0c289d19
child 212 1ae84dea7d09
equal deleted inserted replaced
1:02eefc7d01a5 2:cc70dedce4c1
     1 /* -*- C++ -*-
     1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
     2  *
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     3  * This file is a part of LEMON, a generic C++ optimization library.
     4  *
     4  *
     5  * Copyright (C) 2003-2008
     5  * Copyright (C) 2003-2008
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     8  *
    43   public:
    43   public:
    44     typedef _Type Type;
    44     typedef _Type Type;
    45 
    45 
    46     ExceptionMember() throw() {
    46     ExceptionMember() throw() {
    47       try {
    47       try {
    48 	ptr.reset(new Type());
    48         ptr.reset(new Type());
    49       } catch (...) {}
    49       } catch (...) {}
    50     }
    50     }
    51 
    51 
    52     ExceptionMember(const Type& type) throw() {
    52     ExceptionMember(const Type& type) throw() {
    53       try {
    53       try {
    54 	ptr.reset(new Type());
    54         ptr.reset(new Type());
    55 	if (ptr.get() == 0) return;
    55         if (ptr.get() == 0) return;
    56 	*ptr = type;
    56         *ptr = type;
    57       } catch (...) {}
    57       } catch (...) {}
    58     }
    58     }
    59 
    59 
    60     ExceptionMember(const ExceptionMember& copy) throw() {
    60     ExceptionMember(const ExceptionMember& copy) throw() {
    61       try {
    61       try {
    62 	if (!copy.valid()) return;
    62         if (!copy.valid()) return;
    63 	ptr.reset(new Type());
    63         ptr.reset(new Type());
    64 	if (ptr.get() == 0) return;
    64         if (ptr.get() == 0) return;
    65 	*ptr = copy.get();
    65         *ptr = copy.get();
    66       } catch (...) {}
    66       } catch (...) {}
    67     }
    67     }
    68 
    68 
    69     ExceptionMember& operator=(const ExceptionMember& copy) throw() {
    69     ExceptionMember& operator=(const ExceptionMember& copy) throw() {
    70       if (ptr.get() == 0) return;
    70       if (ptr.get() == 0) return;
    71       try {
    71       try {
    72 	if (!copy.valid()) return;
    72         if (!copy.valid()) return;
    73  	*ptr = copy.get();
    73          *ptr = copy.get();
    74       } catch (...) {}
    74       } catch (...) {}
    75     }
    75     }
    76 
    76 
    77     void set(const Type& type) throw() {
    77     void set(const Type& type) throw() {
    78       if (ptr.get() == 0) return;
    78       if (ptr.get() == 0) return;
    79       try {
    79       try {
    80 	*ptr = type;
    80         *ptr = type;
    81       } catch (...) {}
    81       } catch (...) {}
    82     }
    82     }
    83 
    83 
    84     const Type& get() const {
    84     const Type& get() const {
    85       return *ptr;
    85       return *ptr;
   107     mutable std::auto_ptr<std::ostringstream> buf;
   107     mutable std::auto_ptr<std::ostringstream> buf;
   108 
   108 
   109     ///\e
   109     ///\e
   110     bool init() throw() {
   110     bool init() throw() {
   111       try {
   111       try {
   112 	buf.reset(new std::ostringstream);
   112         buf.reset(new std::ostringstream);
   113       }
   113       }
   114       catch(...) {
   114       catch(...) {
   115 	buf.reset();
   115         buf.reset();
   116       }
   116       }
   117       return buf.get();
   117       return buf.get();
   118     }
   118     }
   119 
   119 
   120   public:
   120   public:
   140     template <typename T>
   140     template <typename T>
   141     ErrorMessage& operator<<(const T &t) throw() {
   141     ErrorMessage& operator<<(const T &t) throw() {
   142       if( ! buf.get() ) return *this;
   142       if( ! buf.get() ) return *this;
   143 
   143 
   144       try {
   144       try {
   145 	*buf << t;
   145         *buf << t;
   146       }
   146       }
   147       catch(...) {
   147       catch(...) {
   148 	buf.reset();
   148         buf.reset();
   149       }
   149       }
   150       return *this;
   150       return *this;
   151     }
   151     }
   152 
   152 
   153     ///\e
   153     ///\e
   154     const char* message() throw() {
   154     const char* message() throw() {
   155       if( ! buf.get() ) return 0;
   155       if( ! buf.get() ) return 0;
   156 
   156 
   157       const char* mes = 0;
   157       const char* mes = 0;
   158       try {
   158       try {
   159 	mes = buf->str().c_str();
   159         mes = buf->str().c_str();
   160       }
   160       }
   161       catch(...) {}
   161       catch(...) {}
   162       return mes;
   162       return mes;
   163     }
   163     }
   164 
   164 
   252     explicit DataFormatError(const char *the_message)
   252     explicit DataFormatError(const char *the_message)
   253       : _message(the_message), _line(0) {}
   253       : _message(the_message), _line(0) {}
   254 
   254 
   255     ///\e
   255     ///\e
   256     DataFormatError(const std::string &file_name, int line_num,
   256     DataFormatError(const std::string &file_name, int line_num,
   257 		    const char *the_message)
   257                     const char *the_message)
   258       : _message(the_message), _line(line_num) { file(file_name); }
   258       : _message(the_message), _line(line_num) { file(file_name); }
   259 
   259 
   260     ///\e
   260     ///\e
   261     void line(int ln) { _line = ln; }
   261     void line(int ln) { _line = ln; }
   262     ///\e
   262     ///\e
   267     ///\e
   267     ///\e
   268     int line() const { return _line; }
   268     int line() const { return _line; }
   269     ///\e
   269     ///\e
   270     const char* message() const {
   270     const char* message() const {
   271       if (_message.valid() && !_message.get().empty()) {
   271       if (_message.valid() && !_message.get().empty()) {
   272 	return _message.get().c_str();
   272         return _message.get().c_str();
   273       } else {
   273       } else {
   274 	return 0;
   274         return 0;
   275       }
   275       }
   276     }
   276     }
   277 
   277 
   278     /// \brief Returns the filename.
   278     /// \brief Returns the filename.
   279     ///
   279     ///
   280     /// Returns \e null if the filename was not specified.
   280     /// Returns \e null if the filename was not specified.
   281     const char* file() const {
   281     const char* file() const {
   282       if (_file.valid() && !_file.get().empty()) {
   282       if (_file.valid() && !_file.get().empty()) {
   283 	return _file.get().c_str();
   283         return _file.get().c_str();
   284       } else {
   284       } else {
   285 	return 0;
   285         return 0;
   286       }
   286       }
   287     }
   287     }
   288 
   288 
   289     ///\e
   289     ///\e
   290     virtual const char* what() const throw() {
   290     virtual const char* what() const throw() {
   291       try {
   291       try {
   292 	std::ostringstream ostr;
   292         std::ostringstream ostr;
   293 	ostr << "lemon:DataFormatError" << ": ";
   293         ostr << "lemon:DataFormatError" << ": ";
   294 	if (message()) ostr << message();
   294         if (message()) ostr << message();
   295 	if( file() || line() != 0 ) {
   295         if( file() || line() != 0 ) {
   296 	  ostr << " (";
   296           ostr << " (";
   297 	  if( file() ) ostr << "in file '" << file() << "'";
   297           if( file() ) ostr << "in file '" << file() << "'";
   298 	  if( file() && line() != 0 ) ostr << " ";
   298           if( file() && line() != 0 ) ostr << " ";
   299 	  if( line() != 0 ) ostr << "at line " << line();
   299           if( line() != 0 ) ostr << "at line " << line();
   300 	  ostr << ")";
   300           ostr << ")";
   301 	}
   301         }
   302 	_message_holder.set(ostr.str());
   302         _message_holder.set(ostr.str());
   303       }
   303       }
   304       catch (...) {}
   304       catch (...) {}
   305       if( _message_holder.valid()) return _message_holder.get().c_str();
   305       if( _message_holder.valid()) return _message_holder.get().c_str();
   306       return "lemon:DataFormatError";
   306       return "lemon:DataFormatError";
   307     }
   307     }
   331     /// \brief Returns the filename.
   331     /// \brief Returns the filename.
   332     ///
   332     ///
   333     /// Returns \e null if the filename was not specified.
   333     /// Returns \e null if the filename was not specified.
   334     const char* file() const {
   334     const char* file() const {
   335       if (_file.valid() && !_file.get().empty()) {
   335       if (_file.valid() && !_file.get().empty()) {
   336 	return _file.get().c_str();
   336         return _file.get().c_str();
   337       } else {
   337       } else {
   338 	return 0;
   338         return 0;
   339       }
   339       }
   340     }
   340     }
   341 
   341 
   342     ///\e
   342     ///\e
   343     virtual const char* what() const throw() {
   343     virtual const char* what() const throw() {
   344       try {
   344       try {
   345 	std::ostringstream ostr;
   345         std::ostringstream ostr;
   346 	ostr << "lemon::FileOpenError" << ": ";
   346         ostr << "lemon::FileOpenError" << ": ";
   347 	ostr << "Cannot open file - " << file();
   347         ostr << "Cannot open file - " << file();
   348 	_message_holder.set(ostr.str());
   348         _message_holder.set(ostr.str());
   349       }
   349       }
   350       catch (...) {}
   350       catch (...) {}
   351       if( _message_holder.valid()) return _message_holder.get().c_str();
   351       if( _message_holder.valid()) return _message_holder.get().c_str();
   352       return "lemon::FileOpenError";
   352       return "lemon::FileOpenError";
   353     }
   353     }
   379     void file(const std::string &fl) { _file.set(fl); }
   379     void file(const std::string &fl) { _file.set(fl); }
   380 
   380 
   381      ///\e
   381      ///\e
   382     const char* message() const {
   382     const char* message() const {
   383       if (_message.valid()) {
   383       if (_message.valid()) {
   384 	return _message.get().c_str();
   384         return _message.get().c_str();
   385       } else {
   385       } else {
   386 	return 0;
   386         return 0;
   387       }
   387       }
   388     }
   388     }
   389 
   389 
   390     /// \brief Returns the filename.
   390     /// \brief Returns the filename.
   391     ///
   391     ///
   392     /// Returns \c 0 if the filename was not specified.
   392     /// Returns \c 0 if the filename was not specified.
   393     const char* file() const {
   393     const char* file() const {
   394       if (_file.valid()) {
   394       if (_file.valid()) {
   395 	return _file.get().c_str();
   395         return _file.get().c_str();
   396       } else {
   396       } else {
   397 	return 0;
   397         return 0;
   398       }
   398       }
   399     }
   399     }
   400 
   400 
   401     ///\e
   401     ///\e
   402     virtual const char* what() const throw() {
   402     virtual const char* what() const throw() {
   403       try {
   403       try {
   404 	std::ostringstream ostr;
   404         std::ostringstream ostr;
   405 	if (message()) ostr << message();
   405         if (message()) ostr << message();
   406 	if (file()) ostr << "(when reading file '" << file() << "')";
   406         if (file()) ostr << "(when reading file '" << file() << "')";
   407 	_message_holder.set(ostr.str());
   407         _message_holder.set(ostr.str());
   408       }
   408       }
   409       catch (...) {}
   409       catch (...) {}
   410       if( _message_holder.valid() ) return _message_holder.get().c_str();
   410       if( _message_holder.valid() ) return _message_holder.get().c_str();
   411       return "lemon:IoParameterError";
   411       return "lemon:IoParameterError";
   412     }
   412     }