[cairo-commit] cairomm/cairomm exception.cc, 1.2, 1.3 exception.h, 1.2, 1.3 private.cc, 1.2, 1.3 private.h, 1.2, 1.3

Murray Cumming commit at pdx.freedesktop.org
Wed Dec 7 07:14:40 PST 2005


Committed by: murrayc

Update of /cvs/cairo/cairomm/cairomm
In directory gabe:/tmp/cvs-serv23199/cairomm

Modified Files:
	exception.cc exception.h private.cc private.h 
Log Message:
2005-12-07  Murray Cumming <murrayc at murrayc.com>

        * cairomm/exception.cc:
        * cairomm/exception.h: Rename to logic_error, because
        the cairo documentation says that most of them are
        programming errors, not runtime errors. Derive from
        std::logic_error because of this.
        * cairomm/private.cc:
        * cairomm/private.h: Throw std::bad_alloc for memory
        errors, and std::io_base::failure for read/write runtime
        errors, as suggested by the cairo language-binding
        documentation.

Index: exception.cc
===================================================================
RCS file: /cvs/cairo/cairomm/cairomm/exception.cc,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- exception.cc	2 Dec 2005 14:05:17 -0000	1.2
+++ exception.cc	7 Dec 2005 15:14:38 -0000	1.3
@@ -20,19 +20,28 @@
 namespace Cairo
 {
 
-exception::exception(Status status)
-: m_status(status)
+inline static const char* string_or_empty(const char* text)
 {
+  return (text ? text : "");
 }
 
-exception::~exception() throw()
+//TODO: Is it wise to assume that the string is ASCII, as expected by std::logic_error?
+logic_error::logic_error(Status status)
+: std::logic_error( string_or_empty(cairo_status_to_string((cairo_status_t)m_status)) ),
+  m_status(status)
+{
+}
+
+logic_error::~logic_error() throw()
 {}
 
-const char* exception::what() const throw()
+/*
+const char* logic_error::what() const throw()
 {
   //Hopefully this is a const char* to a static string.
   return cairo_status_to_string((cairo_status_t)m_status);
 }
+*/
 
 } //namespace xmlpp
 

Index: exception.h
===================================================================
RCS file: /cvs/cairo/cairomm/cairomm/exception.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- exception.h	2 Dec 2005 14:05:17 -0000	1.2
+++ exception.h	7 Dec 2005 15:14:38 -0000	1.3
@@ -19,22 +19,22 @@
 #define __CAIRO_EXCEPTION_H
 
 #include <cairomm/enums.h>
-#include <exception>
+#include <stdexcept>
 
 namespace Cairo
 {
 
 typedef cairo_status_t Status;
 
-/** Base class for all cairo exceptions.
+/** 
  */
-class exception: public std::exception
+class logic_error: public std::logic_error
 {
 public:
-  explicit exception(Status status);
-  virtual ~exception() throw();
+  explicit logic_error(Status status);
+  virtual ~logic_error() throw();
 
-  virtual const char* what() const throw();
+  //virtual const char* what() const throw();
 
 private:
   Status m_status;

Index: private.cc
===================================================================
RCS file: /cvs/cairo/cairomm/cairomm/private.cc,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- private.cc	2 Dec 2005 14:05:17 -0000	1.2
+++ private.cc	7 Dec 2005 15:14:38 -0000	1.3
@@ -17,9 +17,53 @@
 
 #include <cairomm/private.h>
 #include <cairomm/exception.h>
+#include <stdexcept>
+#include <iostream>
 
 namespace Cairo
 {
 
+void throw_exception(Status status)
+{
+  switch(status)
+  {
+    case CAIRO_STATUS_NO_MEMORY:
+      throw std::bad_alloc();
+      break;
+
+    // Programmer error
+    case CAIRO_STATUS_INVALID_RESTORE:
+    case CAIRO_STATUS_INVALID_POP_GROUP:
+    case CAIRO_STATUS_NO_CURRENT_POINT:
+    case CAIRO_STATUS_INVALID_MATRIX:
+    //No longer in API?: case CAIRO_STATUS_NO_TARGET_SURFACE:
+    case CAIRO_STATUS_INVALID_STRING:
+    case CAIRO_STATUS_SURFACE_FINISHED:
+    //No longer in API?: case CAIRO_STATUS_BAD_NESTING:
+      throw Cairo::logic_error(status);
+      break;
+
+    // Language binding implementation:
+    case CAIRO_STATUS_NULL_POINTER:
+    case CAIRO_STATUS_INVALID_PATH_DATA:
+    case CAIRO_STATUS_SURFACE_TYPE_MISMATCH:
+      throw Cairo::logic_error(status);
+      break;
+
+    // Other      
+    case CAIRO_STATUS_READ_ERROR:
+    case CAIRO_STATUS_WRITE_ERROR:
+    {
+      //The Cairo language binding advice suggests that these are stream errors 
+      //that should be mapped to their C++ equivalents.
+      const char* error_message = cairo_status_to_string(status);
+      throw std::ios_base::failure( error_message ? error_message : std::string() );
+    }
+    
+    default:
+      throw Cairo::logic_error(status);
+      break;
+  }
+}
 
 } //namespace Cairo

Index: private.h
===================================================================
RCS file: /cvs/cairo/cairomm/cairomm/private.h,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- private.h	2 Dec 2005 14:05:17 -0000	1.2
+++ private.h	7 Dec 2005 15:14:38 -0000	1.3
@@ -26,11 +26,13 @@
 namespace Cairo
 {
 
+void throw_exception(Status status);
+
 //We inline this because it is called so often.
 inline void check_status_and_throw_exception(Status status)
 {
   if(status != CAIRO_STATUS_SUCCESS)
-    throw exception(status);
+    throw_exception(status); //This part doesn't need to be inline because it would rarely be called.
 }
 
 template<class T>



More information about the cairo-commit mailing list