[libxml++] Remove wrapped_exception



commit 75c0b1c9ed7fd86c99df9c7207c43b122deb9e96
Author: Kjell Ahlstedt <kjell ahlstedt bredband net>
Date:   Tue Sep 22 09:00:42 2015 +0200

    Remove wrapped_exception
    
    * libxml++/exceptions/wrapped_exception.[h|cc]: Remove these files.
    * libxml++/filelist.am: Remove wrapped_exception.h.
    * libxml++/validators/validator.[h|cc]:
    * libxml++/parsers/parser.[h|cc]: Replace handleException(const exception&)
    by handle_exception(). Replace exception* exception_ by
    std::exception_ptr exception_ptr_.
    * libxml++/parsers/saxparser.cc: Call handle_exception() instead of
    handleException(). Bug #754673.

 libxml++/exceptions/wrapped_exception.cc |   42 -----------
 libxml++/exceptions/wrapped_exception.h  |   56 --------------
 libxml++/filelist.am                     |    3 +-
 libxml++/parsers/parser.cc               |   56 ++++++++------
 libxml++/parsers/parser.h                |    8 +-
 libxml++/parsers/saxparser.cc            |  118 ++++++++----------------------
 libxml++/validators/validator.cc         |   61 +++++++++-------
 libxml++/validators/validator.h          |   10 ++-
 8 files changed, 110 insertions(+), 244 deletions(-)
---
diff --git a/libxml++/filelist.am b/libxml++/filelist.am
index 68a119d..b9b1f5b 100644
--- a/libxml++/filelist.am
+++ b/libxml++/filelist.am
@@ -16,8 +16,7 @@ h_exceptions_sources_public = \
   exceptions/exception.h \
   exceptions/parse_error.h \
   exceptions/validity_error.h \
-  exceptions/internal_error.h \
-  exceptions/wrapped_exception.h
+  exceptions/internal_error.h
 h_io_sources_public = \
   io/istreamparserinputbuffer.h \
   io/outputbuffer.h \
diff --git a/libxml++/parsers/parser.cc b/libxml++/parsers/parser.cc
index 70d9eb8..e45a905 100644
--- a/libxml++/parsers/parser.cc
+++ b/libxml++/parsers/parser.cc
@@ -4,7 +4,6 @@
  * included with libxml++ as the file COPYING.
  */
 
-#include "libxml++/exceptions/wrapped_exception.h"
 #include "libxml++/parsers/parser.h"
 
 #include <libxml/parser.h>
@@ -35,14 +34,13 @@ struct Parser::Impl
 };
 
 Parser::Parser()
-: context_(nullptr), exception_(nullptr), pimpl_(new Impl)
+: context_(nullptr), exception_ptr_(nullptr), pimpl_(new Impl)
 {
 }
 
 Parser::~Parser()
 {
   release_underlying();
-  delete exception_;
 }
 
 void Parser::set_validate(bool val)
@@ -195,7 +193,20 @@ void Parser::on_validity_warning(const Glib::ustring& message)
 
 void Parser::check_for_error_and_warning_messages()
 {
-  Glib::ustring msg(exception_ ? exception_->what() : "");
+  Glib::ustring msg;
+  try
+  {
+    if (exception_ptr_)
+      std::rethrow_exception(exception_ptr_);
+  }
+  catch (const std::exception& e)
+  {
+    msg = e.what();
+  }
+  catch (...)
+  {
+    msg = "Unknown exception\n";
+  }
   bool parser_msg = false;
   bool validity_msg = false;
 
@@ -227,13 +238,16 @@ void Parser::check_for_error_and_warning_messages()
     pimpl_->validate_warning_.erase();
   }
 
-  if (parser_msg || validity_msg)
+  try
   {
-    delete exception_;
     if (validity_msg)
-      exception_ = new validity_error(msg);
-    else
-      exception_ = new parse_error(msg);
+      throw validity_error(msg);
+    else if (parser_msg)
+      throw parse_error(msg);
+  }
+  catch (...)
+  {
+    exception_ptr_ = std::current_exception();
   }
 }
   
@@ -318,24 +332,19 @@ void Parser::callback_error_or_warning(MsgType msg_type, void* ctx,
             break;
         }
       }
-      catch(const exception& e)
-      {
-        parser->handleException(e);
-      }
-      catch(...)
+      catch (...)
       {
-        parser->handleException(wrapped_exception(std::current_exception()));
+        parser->handle_exception();
       }
     }
   }
 }
 
-void Parser::handleException(const exception& e)
+void Parser::handle_exception()
 {
-  delete exception_;
-  exception_ = e.Clone();
+  exception_ptr_ = std::current_exception();
 
-  if(context_)
+  if (context_)
     xmlStopParser(context_);
 
   //release_underlying();
@@ -345,13 +354,12 @@ void Parser::check_for_exception()
 {
   check_for_error_and_warning_messages();
   
-  if(exception_)
+  if (exception_ptr_)
   {
-    std::unique_ptr<exception> tmp(exception_);
-    exception_ = nullptr;
-    tmp->Raise();
+    std::exception_ptr tmp(exception_ptr_);
+    exception_ptr_ = nullptr;
+    std::rethrow_exception(tmp);
   }
 }
 
 } // namespace xmlpp
-
diff --git a/libxml++/parsers/parser.h b/libxml++/parsers/parser.h
index 21f8a35..6cf9b58 100644
--- a/libxml++/parsers/parser.h
+++ b/libxml++/parsers/parser.h
@@ -16,8 +16,9 @@
 #include <libxml++/exceptions/internal_error.h>
 
 #include <istream>
-#include <cstdarg> //For va_list.
+#include <cstdarg> // va_list
 #include <memory> // std::unique_ptr
+#include <exception> // std::exception_ptr
 
 #ifndef DOXYGEN_SHOULD_SKIP_THIS
 extern "C" {
@@ -173,7 +174,8 @@ protected:
   virtual void on_validity_error(const Glib::ustring& message);
   virtual void on_validity_warning(const Glib::ustring& message);
 
-  virtual void handleException(const exception& e);
+  /// To be called in an exception handler.
+  virtual void handle_exception();
   virtual void check_for_exception();
 
   virtual void check_for_error_and_warning_messages();
@@ -195,7 +197,7 @@ protected:
                                         const char* msg, va_list var_args);
 
   _xmlParserCtxt* context_;
-  exception* exception_;
+  std::exception_ptr exception_ptr_;
 
 private:
   struct Impl;
diff --git a/libxml++/parsers/saxparser.cc b/libxml++/parsers/saxparser.cc
index ea81762..69ebae0 100644
--- a/libxml++/parsers/saxparser.cc
+++ b/libxml++/parsers/saxparser.cc
@@ -7,7 +7,6 @@
  * 2002/01/21 Valentin Rusu - added CDATA handlers
  */
 
-#include "libxml++/exceptions/wrapped_exception.h"
 #include "libxml++/parsers/saxparser.h"
 #include "libxml++/nodes/element.h"
 #include "libxml++/keepblanks.h"
@@ -245,8 +244,7 @@ void SaxParser::parse_stream(std::istream& in)
   //TODO: Shouldn't we use a Glib::ustring here, and some alternative to std::getline()?
   int firstParseError = XML_ERR_OK;
   std::string line;
-  while( ( ! exception_ )
-      && std::getline(in, line))
+  while (!exception_ptr_ && std::getline(in, line))
   {
     // since getline does not get the line separator, we have to add it since the parser care
     // about layout in certain cases.
@@ -262,7 +260,7 @@ void SaxParser::parse_stream(std::istream& in)
       firstParseError = parseError;
   }
 
-  if( ! exception_ )
+  if (!exception_ptr_)
   {
      //This is called just to terminate parsing.
     const int parseError = xmlParseChunk(context_, 0 /* chunk */, 0 /* size */, 1 /* terminate (1 or 0) */);
@@ -314,7 +312,7 @@ void SaxParser::parse_chunk_raw(const unsigned char* contents, size_type bytes_c
     xmlCtxtResetLastError(context_);
   
   int parseError = XML_ERR_OK;
-  if(!exception_)
+  if (!exception_ptr_)
     parseError = xmlParseChunk(context_, (const char*)contents, bytes_count, 0 /* don't terminate */);
 
   check_for_exception();
@@ -355,7 +353,7 @@ void SaxParser::finish_chunk_parsing()
     xmlCtxtResetLastError(context_);
 
   int parseError = XML_ERR_OK;
-  if(!exception_)
+  if (!exception_ptr_)
     //This is called just to terminate parsing.
     parseError = xmlParseChunk(context_, 0 /* chunk */, 0 /* size */, 1 /* terminate (1 or 0) */);
 
@@ -384,13 +382,9 @@ xmlEntityPtr SaxParserCallback::get_entity(void* context, const xmlChar* name)
   {
     result = parser->on_get_entity((const char*)name);
   }
-  catch(const exception& e)
+  catch (...)
   {
-    parser->handleException(e);
-  }
-  catch(...)
-  {
-    parser->handleException(wrapped_exception(std::current_exception()));
+    parser->handle_exception();
   }
 
   return result;
@@ -410,13 +404,9 @@ void SaxParserCallback::entity_decl(void* context, const xmlChar* name, int type
       ( systemId ? Glib::ustring((const char*)systemId) : ""),
       ( content ? Glib::ustring((const char*)content) : "") );
   }
-  catch(const exception& e)
-  {
-    parser->handleException(e);
-  }
-  catch(...)
+  catch (...)
   {
-    parser->handleException(wrapped_exception(std::current_exception()));
+    parser->handle_exception();
   }
 }
 
@@ -429,13 +419,9 @@ void SaxParserCallback::start_document(void* context)
   {
     parser->on_start_document();
   }
-  catch(const exception& e)
+  catch (...)
   {
-    parser->handleException(e);
-  }
-  catch(...)
-  {
-    parser->handleException(wrapped_exception(std::current_exception()));
+    parser->handle_exception();
   }
 }
 
@@ -444,20 +430,16 @@ void SaxParserCallback::end_document(void* context)
   auto the_context = static_cast<_xmlParserCtxt*>(context);
   auto parser = static_cast<SaxParser*>(the_context->_private);
 
-  if(parser->exception_)
+  if (parser->exception_ptr_)
     return;
 
   try
   {
     parser->on_end_document();
   }
-  catch(const exception& e)
+  catch (...)
   {
-    parser->handleException(e);
-  }
-  catch(...)
-  {
-    parser->handleException(wrapped_exception(std::current_exception()));
+    parser->handle_exception();
   }
 }
 
@@ -479,13 +461,9 @@ void SaxParserCallback::start_element(void* context,
   {
     parser->on_start_element(Glib::ustring((const char*) name), attributes);
   }
-  catch(const exception& e)
-  {
-    parser->handleException(e);
-  }
-  catch(...)
+  catch (...)
   {
-    parser->handleException(wrapped_exception(std::current_exception()));
+    parser->handle_exception();
   }
 }
 
@@ -498,13 +476,9 @@ void SaxParserCallback::end_element(void* context, const xmlChar* name)
   {
     parser->on_end_element(Glib::ustring((const char*) name));
   }
-  catch(const exception& e)
+  catch (...)
   {
-    parser->handleException(e);
-  }
-  catch(...)
-  {
-    parser->handleException(wrapped_exception(std::current_exception()));
+    parser->handle_exception();
   }
 }
 
@@ -523,13 +497,9 @@ void SaxParserCallback::characters(void * context, const xmlChar* ch, int len)
           reinterpret_cast<const char *>(ch),
           reinterpret_cast<const char *>(ch + len) ) );
   }
-  catch(const exception& e)
-  {
-    parser->handleException(e);
-  }
-  catch(...)
+  catch (...)
   {
-    parser->handleException(wrapped_exception(std::current_exception()));
+    parser->handle_exception();
   }
 }
 
@@ -542,13 +512,9 @@ void SaxParserCallback::comment(void* context, const xmlChar* value)
   {
     parser->on_comment(Glib::ustring((const char*) value));
   }
-  catch(const exception& e)
-  {
-    parser->handleException(e);
-  }
-  catch(...)
+  catch (...)
   {
-    parser->handleException(wrapped_exception(std::current_exception()));
+    parser->handle_exception();
   }
 }
 
@@ -568,13 +534,9 @@ void SaxParserCallback::warning(void* context, const char* fmt, ...)
   {
     parser->on_warning(Glib::ustring(buff));
   }
-  catch(const exception& e)
+  catch (...)
   {
-    parser->handleException(e);
-  }
-  catch(...)
-  {
-    parser->handleException(wrapped_exception(std::current_exception()));
+    parser->handle_exception();
   }
 }
 
@@ -586,7 +548,7 @@ void SaxParserCallback::error(void* context, const char* fmt, ...)
   va_list arg;
   char buff[1024]; //TODO: Larger/Shared
 
-  if(parser->exception_)
+  if (parser->exception_ptr_)
     return;
 
   va_start(arg, fmt);
@@ -597,13 +559,9 @@ void SaxParserCallback::error(void* context, const char* fmt, ...)
   {
     parser->on_error(Glib::ustring(buff));
   }
-  catch(const exception& e)
-  {
-    parser->handleException(e);
-  }
-  catch(...)
+  catch (...)
   {
-    parser->handleException(wrapped_exception(std::current_exception()));
+    parser->handle_exception();
   }
 }
 
@@ -623,13 +581,9 @@ void SaxParserCallback::fatal_error(void* context, const char* fmt, ...)
   {
     parser->on_fatal_error(Glib::ustring(buff));
   }
-  catch(const exception& e)
+  catch (...)
   {
-    parser->handleException(e);
-  }
-  catch(...)
-  {
-    parser->handleException(wrapped_exception(std::current_exception()));
+    parser->handle_exception();
   }
 }
 
@@ -647,13 +601,9 @@ void SaxParserCallback::cdata_block(void* context, const xmlChar* value, int len
           reinterpret_cast<const char *>(value),
           reinterpret_cast<const char *>(value + len) ) );
   }
-  catch(const exception& e)
-  {
-    parser->handleException(e);
-  }
-  catch(...)
+  catch (...)
   {
-    parser->handleException(wrapped_exception(std::current_exception()));
+    parser->handle_exception();
   }
 }
 
@@ -670,13 +620,9 @@ void SaxParserCallback::internal_subset(void* context, const xmlChar* name,
 
     parser->on_internal_subset( Glib::ustring((const char*) name), pid, sid);
   }
-  catch(const exception& e)
-  {
-    parser->handleException(e);
-  }
-  catch(...)
+  catch (...)
   {
-    parser->handleException(wrapped_exception(std::current_exception()));
+    parser->handle_exception();
   }
 }
 
diff --git a/libxml++/validators/validator.cc b/libxml++/validators/validator.cc
index 220d459..71e5999 100644
--- a/libxml++/validators/validator.cc
+++ b/libxml++/validators/validator.cc
@@ -5,7 +5,6 @@
  * included with libxml++ as the file COPYING.
  */
 
-#include "libxml++/exceptions/wrapped_exception.h"
 #include "libxml++/validators/validator.h"
 
 #include <libxml/parser.h>
@@ -16,7 +15,7 @@
 namespace xmlpp {
 
 Validator::Validator()
-: valid_(nullptr), exception_(nullptr)
+: valid_(nullptr), exception_ptr_(nullptr)
 {
 }
 
@@ -69,7 +68,20 @@ void Validator::on_validity_warning(const Glib::ustring& message)
 
 void Validator::check_for_validity_messages()
 {
-  Glib::ustring msg(exception_ ? exception_->what() : "");
+  Glib::ustring msg;
+  try
+  {
+    if (exception_ptr_)
+      std::rethrow_exception(exception_ptr_);
+  }
+  catch (const std::exception& e)
+  {
+    msg = e.what();
+  }
+  catch (...)
+  {
+    msg = "Unknown exception\n";
+  }
   bool validity_msg = false;
 
   if (!validate_error_.empty())
@@ -86,10 +98,14 @@ void Validator::check_for_validity_messages()
     validate_warning_.erase();
   }
 
-  if (validity_msg)
+  try
   {
-    delete exception_;
-    exception_ = new validity_error(msg);
+    if (validity_msg)
+      throw validity_error(msg);
+  }
+  catch (...)
+  {
+    exception_ptr_ = std::current_exception();
   }
 }
 
@@ -111,13 +127,9 @@ void Validator::callback_validity_error(void* valid_, const char* msg, ...)
     {
       validator->on_validity_error(Glib::ustring(buff));
     }
-    catch(const exception& e)
-    {
-      validator->handleException(e);
-    }
-    catch(...)
+    catch (...)
     {
-      validator->handleException(wrapped_exception(std::current_exception()));
+      validator->handle_exception();
     }
   }
 }
@@ -140,26 +152,21 @@ void Validator::callback_validity_warning(void* valid_, const char* msg, ...)
     {
       validator->on_validity_warning(Glib::ustring(buff));
     }
-    catch(const exception& e)
+    catch (...)
     {
-      validator->handleException(e);
-    }
-    catch(...)
-    {
-      validator->handleException(wrapped_exception(std::current_exception()));
+      validator->handle_exception();
     }
   }
 }
 
-void Validator::handleException(const exception& e)
+void Validator::handle_exception()
 {
-  delete exception_;
-  exception_ = e.Clone();
+  exception_ptr_ = std::current_exception();
 
   // Don't delete the DTD validation context or schema validation context
   // while validating. It would cause accesses to deallocated memory in libxml2
   // functions after the return from Validator::callback_validity_...().
-  // Parser::handleException() calls xmlStopParser(), but there is no
+  // Parser::handle_exception() calls xmlStopParser(), but there is no
   // xmlStopValidator() or similar function to call here.
   // We don't throw the exception here, since it would have to pass through
   // C functions. That's not guaranteed to work. It might work, but it depends
@@ -171,12 +178,12 @@ void Validator::handleException(const exception& e)
 void Validator::check_for_exception()
 {
   check_for_validity_messages();
-
-  if(exception_)
+  
+  if (exception_ptr_)
   {
-    std::unique_ptr<exception> tmp(exception_);
-    exception_ = nullptr;
-    tmp->Raise();
+    std::exception_ptr tmp(exception_ptr_);
+    exception_ptr_ = nullptr;
+    std::rethrow_exception(tmp);
   }
 }
 
diff --git a/libxml++/validators/validator.h b/libxml++/validators/validator.h
index cdf9f2f..82c94c8 100644
--- a/libxml++/validators/validator.h
+++ b/libxml++/validators/validator.h
@@ -15,6 +15,7 @@
 #include <libxml++/nodes/element.h>
 #include <libxml++/exceptions/validity_error.h>
 #include <libxml++/exceptions/internal_error.h>
+#include <exception> // std::exception_ptr
 
 extern "C" {
   struct _xmlValidCtxt;
@@ -37,7 +38,8 @@ protected:
   virtual void on_validity_error(const Glib::ustring& message);
   virtual void on_validity_warning(const Glib::ustring& message);
 
-  virtual void handleException(const exception& e);
+  /// To be called in an exception handler.
+  virtual void handle_exception();
   virtual void check_for_exception();
   virtual void check_for_validity_messages();
 
@@ -45,12 +47,12 @@ protected:
   static void callback_validity_warning(void* ctx, const char* msg, ...);
 
   _xmlValidCtxt* valid_;
-  exception* exception_;
+  std::exception_ptr exception_ptr_;
+  // Built gradually - used in an exception at the end of validation.
   Glib::ustring validate_error_;
-  Glib::ustring validate_warning_; //Built gradually - used in an exception at the end of parsing.
+  Glib::ustring validate_warning_;
 };
 
 } // namespace xmlpp
 
 #endif //__LIBXMLPP_VALIDATOR_H
-


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]