[svn] commit: r421 - in /branches/jinmei-dnsmessageapi/src/lib/dns/cpp: Makefile.am exceptions.cc exceptions.h exceptions_unittest.cc

BIND 10 source code commits bind10-changes at lists.isc.org
Thu Dec 31 00:35:41 UTC 2009


Author: jinmei
Date: Thu Dec 31 00:35:41 2009
New Revision: 421

Log:
make isc::dns::Exception inherit from std::exception

Added:
    branches/jinmei-dnsmessageapi/src/lib/dns/cpp/exceptions.cc
Modified:
    branches/jinmei-dnsmessageapi/src/lib/dns/cpp/Makefile.am
    branches/jinmei-dnsmessageapi/src/lib/dns/cpp/exceptions.h
    branches/jinmei-dnsmessageapi/src/lib/dns/cpp/exceptions_unittest.cc

Modified: branches/jinmei-dnsmessageapi/src/lib/dns/cpp/Makefile.am
==============================================================================
--- branches/jinmei-dnsmessageapi/src/lib/dns/cpp/Makefile.am (original)
+++ branches/jinmei-dnsmessageapi/src/lib/dns/cpp/Makefile.am Thu Dec 31 00:35:41 2009
@@ -1,6 +1,6 @@
 lib_LTLIBRARIES = libdns.la
 libdns_la_SOURCES = buffer.h name.cc name.h messagerenderer.h messagerenderer.cc
-libdns_la_SOURCES += exceptions.h
+libdns_la_SOURCES += exceptions.h exceptions.cc
 
 TESTS =
 if HAVE_GTEST

Modified: branches/jinmei-dnsmessageapi/src/lib/dns/cpp/exceptions.h
==============================================================================
--- branches/jinmei-dnsmessageapi/src/lib/dns/cpp/exceptions.h (original)
+++ branches/jinmei-dnsmessageapi/src/lib/dns/cpp/exceptions.h Thu Dec 31 00:35:41 2009
@@ -17,6 +17,7 @@
 #ifndef __EXCEPTIONS_H
 #define __EXCEPTIONS_H 1
 
+#include <stdexcept>
 #include <string>
 
 namespace isc {
@@ -29,7 +30,7 @@
 /// exception such as the file name and line number where the exception is
 /// triggered.
 ///
-class Exception {
+class Exception : public std::exception {
 public:
     ///
     /// \name Constructors and Destructor
@@ -44,7 +45,7 @@
     Exception(const char* file, size_t line, const char* what) :
         file_(file), line_(line), what_(what) {}
     /// The destructor
-    virtual ~Exception() {}
+    virtual ~Exception() throw() {}
     //@}
 private:
     ///
@@ -53,6 +54,19 @@
     void operator=(const Exception& src);
 
 public:
+    ///
+    /// \name Methods Reimplemented against the Standard Exception Class
+    ///
+    //@{
+    /// \brief Returns a C-style character string of the cause of the exception.
+    ///
+    /// Note: we normally don't use exception specifications, but this is an
+    /// "exception" to that policy as it's enforced by the base class.
+    ///
+    /// @return A C-style character string of the exception cause.
+    virtual const char* what() const throw();
+    //@}
+
     ///
     /// \name Getter Methods
     ///
@@ -71,6 +85,7 @@
     ///
     /// @return an integer specifying the line number.
     size_t getLine() const { return (line_); }
+    //@}
 
 private:
     const char* const file_;

Modified: branches/jinmei-dnsmessageapi/src/lib/dns/cpp/exceptions_unittest.cc
==============================================================================
--- branches/jinmei-dnsmessageapi/src/lib/dns/cpp/exceptions_unittest.cc (original)
+++ branches/jinmei-dnsmessageapi/src/lib/dns/cpp/exceptions_unittest.cc Thu Dec 31 00:35:41 2009
@@ -14,21 +14,39 @@
 
 // $Id$
 
+#include <stdexcept>
+#include <string>
+
 #include "exceptions.h"
 
 #include <gtest/gtest.h>
 
+using isc::dns::Exception;
+
 namespace {
 
-using isc::dns::Exception;
+class ExceptionTest : public ::testing::Test {
+protected:
+    ExceptionTest() : teststring("test") {}
+    const char* teststring;
+};
 
-TEST(ExceptionTest, ExceptionTest) {
+TEST_F(ExceptionTest, BasicMethods) {
     try {
-        dns_throw(Exception, "test");
+        dns_throw(Exception, teststring);
     } catch (Exception& ex) {
-        EXPECT_EQ(ex.getMessage(), "test");
+        EXPECT_EQ(ex.getMessage(), std::string(teststring));
         EXPECT_EQ(ex.getFile(), std::string(__FILE__));
         EXPECT_EQ(ex.getLine(), __LINE__ - 4);
     }
 }
+
+// Test to see if it works as a proper derived class of std::exception.
+TEST_F(ExceptionTest, StdInheritance) {
+    try {
+        dns_throw(Exception, teststring);
+    } catch (std::exception& ex) {
+        EXPECT_EQ(std::string(ex.what()), std::string(teststring));
+    }
 }
+}




More information about the bind10-changes mailing list