[Lemon-commits] [lemon_svn] klao: r1558 - in hugo/trunk/src: lemon work/klao
Lemon SVN
svn at lemon.cs.elte.hu
Mon Nov 6 20:46:11 CET 2006
Author: klao
Date: Sat Feb 19 22:11:20 2005
New Revision: 1558
Added:
hugo/trunk/src/work/klao/error2_test.cc
Modified:
hugo/trunk/src/lemon/error.h
Log:
lemon/error.h: boost::shared_ptr helyett std::auto_ptr
(Sokkal kenylemetlenebb, es nem teljesen biztos, hogy helyes megoldas)
Modified: hugo/trunk/src/lemon/error.h
==============================================================================
--- hugo/trunk/src/lemon/error.h (original)
+++ hugo/trunk/src/lemon/error.h Sat Feb 19 22:11:20 2005
@@ -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 <sstream>
#include <iostream>
#include <cstdlib>
-
-#include <boost/shared_ptr.hpp>
+#include <memory>
namespace lemon {
@@ -42,7 +43,9 @@
class ErrorMessage {
protected:
///\e
- boost::shared_ptr<std::ostringstream> buf;
+ ///\todo The good solution is boost:shared_ptr...
+ mutable
+ std::auto_ptr<std::ostringstream> 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 <typename T>
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<std::string> _file;
+
+ ///\todo Much better solution is boost::shared_ptr
+ mutable
+ std::auto_ptr<std::string> _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 << ")";
}
Added: hugo/trunk/src/work/klao/error2_test.cc
==============================================================================
--- (empty file)
+++ hugo/trunk/src/work/klao/error2_test.cc Sat Feb 19 22:11:20 2005
@@ -0,0 +1,68 @@
+#include <iostream>
+#include <memory>
+
+#include <boost/shared_ptr.hpp>
+
+using namespace std;
+
+class Ex : public exception {
+
+ typedef exception Parent;
+
+ mutable
+ auto_ptr<string> uz;
+public:
+
+ // boost::shared_ptr<string> 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;
+ }
+
+}
More information about the Lemon-commits
mailing list