Skip to content

Commit

Permalink
dynamic_cast<> with pointers instead of reference and compare against…
Browse files Browse the repository at this point in the history
… NULL.
  • Loading branch information
DavidKeller committed Jul 23, 2013
1 parent 04974b7 commit 153397a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 30 deletions.
22 changes: 6 additions & 16 deletions ext/common/ApplicationPool2/Implementation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,11 @@ using namespace std;
using namespace boost;
using namespace oxt;


template<typename T>
static bool
exceptionIsInstanceOf(const tracable_exception &e) {
try {
(void) dynamic_cast<T>(e);
return true;
} catch (const bad_cast &) {
return false;
}
}

#define TRY_COPY_EXCEPTION(klass) \
do { \
if (exceptionIsInstanceOf<const klass &>(e)) { \
return make_shared<klass>( (const klass &) e ); \
const klass * ep = dynamic_cast<const klass *>(&e); \
if (ep != NULL) { \
return make_shared<klass>(*ep); \
} \
} while (false)

Expand Down Expand Up @@ -96,8 +85,9 @@ copyException(const tracable_exception &e) {

#define TRY_RETHROW_EXCEPTION(klass) \
do { \
if (exceptionIsInstanceOf<const klass &>(*e)) { \
throw klass((const klass &) *e); \
const klass * ep = dynamic_cast<const klass *>(&*e); \
if (ep != NULL) { \
throw klass(*ep); \
} \
} while (false)

Expand Down
24 changes: 10 additions & 14 deletions ext/common/UnionStation.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,15 +269,16 @@ class Logger: public boost::noncopyable {
switch (exceptionHandlingMode) {
case THROW:
throw e;
case PRINT:
try {
const tracable_exception &te =
dynamic_cast<const tracable_exception &>(e);
P_WARN(te.what() << "\n" << te.backtrace());
} catch (const bad_cast &) {
P_WARN(e.what());
case PRINT: {
const tracable_exception *te =
dynamic_cast<const tracable_exception *>(&e);
if (te != NULL) {
P_WARN(te->what() << "\n" << te->backtrace());
} else {
P_WARN(e.what());
}
break;
}
break;
default:
break;
}
Expand Down Expand Up @@ -587,12 +588,7 @@ class LoggerFactory: public enable_shared_from_this<LoggerFactory> {

template<typename T>
static bool instanceof(const std::exception &e) {
try {
(void) dynamic_cast<const T &>(e);
return true;
} catch (const bad_cast &) {
return false;
}
return dynamic_cast<const T *>(&e) != NULL;
}

ConnectionPtr createNewConnection() {
Expand Down

0 comments on commit 153397a

Please sign in to comment.