[svn] commit: r1185 - /trunk/src/lib/exceptions/exceptions.h

BIND 10 source code commits bind10-changes at lists.isc.org
Sun Mar 7 22:46:21 UTC 2010


Author: jinmei
Date: Sun Mar  7 22:46:21 2010
New Revision: 1185

Log:
allow isc_throw to have a partial statement using ostream + operator<<.
added notes about design choices and possible future changes.

Modified:
    trunk/src/lib/exceptions/exceptions.h

Modified: trunk/src/lib/exceptions/exceptions.h
==============================================================================
--- trunk/src/lib/exceptions/exceptions.h (original)
+++ trunk/src/lib/exceptions/exceptions.h Sun Mar  7 22:46:21 2010
@@ -19,6 +19,7 @@
 
 #include <stdexcept>
 #include <string>
+#include <sstream>
 
 namespace isc {
 
@@ -125,8 +126,30 @@
 ///
 /// A shortcut macro to insert known values into exception arguments.
 ///
-#define isc_throw(type, args...) throw type(__FILE__, __LINE__, args)
-
+/// It allows the \c stream argument to be part of a statement using an
+/// \c ostream object and its \c operator<<.  For example,
+/// \code int x = 10;
+/// isc_throw(SomeException, "Error happened, parameter: " << x);
+/// \endcode
+/// will throw an exception of class \c SomeException whose \c what string
+/// will be <code>"Error happened, parameter: 10"</code>.
+///
+/// Note: the stream related operations or creation of the exception object
+/// may itself throw an exception (specifically \c std::bad_alloc).
+/// Even though it should be very rare, we may have to address this issue later.
+///
+/// Note: in general we hate macros and avoid using it in the code.  This is
+/// one of few exceptions to that policy.  inline functions cannot be used
+/// for embedding \c __FILE__ and \c __LINE__.  This is the main reason why
+/// this is defined as a macro.  The convenience for the ostream is a secondary
+/// purpose (if that were the only possible reason we should rather avoid
+/// using a macro).
+#define isc_throw(type, stream) \
+    do { \
+        std::ostringstream oss__; \
+        oss__ << stream; \
+        throw type(__FILE__, __LINE__, oss__.str().c_str()); \
+    } while (1)
 }
 #endif // __EXCEPTIONS_H
 




More information about the bind10-changes mailing list