19 #ifndef INCLUDE_UTIL_THROW_HPP
20 #define INCLUDE_UTIL_THROW_HPP
25 #include <boost/current_function.hpp>
27 #include "VariableArgMacro.hpp"
33 inline bool uncaught_exception()
35 return std::uncaught_exception();
41 virtual ~I_InvokeThrow() {}
42 virtual void invoke(
const std::string &context,
const std::string & args,
int logName,
int logLevel,
const char * file,
int line,
const char * func) = 0;
46 class InvokeThrow :
public I_InvokeThrow
49 InvokeThrow(
const E &e) : mE(e)
52 void invoke(
const std::string &context,
const std::string & args,
int logName,
int logLevel,
const char * file,
int line,
const char * func)
54 const_cast<E &
>(mE).setContext(context);
56 if (RCF::LogManager::instance().isEnabled(logName, logLevel))
58 RCF::LogEntry entry(logName, logLevel, file, line, func);
61 <<
"Exception thrown. "
80 class InvokeThrow<std::runtime_error> :
public I_InvokeThrow
83 InvokeThrow(
const std::runtime_error &e) : mE(e)
86 void invoke(
const std::string &context,
const std::string & args,
int logName,
int logLevel,
const char * file,
int line,
const char * func)
88 if (RCF::LogManager::instance().isEnabled(logName, logLevel))
90 RCF::LogEntry entry(logName, logLevel, file, line, func);
93 <<
"Exception thrown. "
104 throw std::runtime_error( std::string(mE.what()) +
": " + context);
108 const std::runtime_error &mE;
112 const char *getTypeName(
const T &t)
114 return typeid(t).name();
119 class ThrowFunctor :
public VariableArgMacroFunctor
122 ThrowFunctor() : mThrown(false), mLogName(0), mLogLevel(0)
126 ThrowFunctor(
const E &e,
int logName,
int logLevel) :
127 VariableArgMacroFunctor(),
128 mInvokeThrow(new detail::InvokeThrow<E>(e)),
141 std::string args(mArgs->str(),
static_cast<std::size_t
>(mArgs->tellp()));
143 std::string context = mHeader->str();
146 if (!util::detail::uncaught_exception())
148 mInvokeThrow->invoke(context, args, mLogName, mLogLevel, mFile, mLine, mFunc);
154 std::auto_ptr<detail::I_InvokeThrow> mInvokeThrow;
163 #pragma warning( push )
164 #pragma warning( disable : 4355 ) // warning C4355: 'this' : used in base member initializer list
167 #if defined(__GNUC__) && (__GNUC__ < 3 || (__GNUC__ == 3 && __GNUC_MINOR__ < 4))
168 #define UTIL_THROW_GCC_33_HACK (const util::VariableArgMacro<util::ThrowFunctor> &)
170 #define UTIL_THROW_GCC_33_HACK
173 DECLARE_VARIABLE_ARG_MACRO( UTIL_THROW, ThrowFunctor );
174 #define UTIL_THROW(e, logName, logLevel) \
176 UTIL_THROW_GCC_33_HACK \
177 util::VariableArgMacro<util::ThrowFunctor>(e, logName, logLevel) \
183 BOOST_CURRENT_FUNCTION) \
184 .cast( (util::VariableArgMacro<util::ThrowFunctor> *) NULL) \
189 #define UTIL_THROW_A(x) UTIL_THROW_OP(x, B)
190 #define UTIL_THROW_B(x) UTIL_THROW_OP(x, A)
191 #define UTIL_THROW_OP(x, next) UTIL_THROW_A.notify_((x), #x).UTIL_THROW_ ## next
194 #pragma warning( pop )
197 #define UTIL_VERIFY(cond, e, logName, logLevel) if (cond); else UTIL_THROW(e, logName, logLevel)
201 #endif // ! INCLUDE_UTIL_THROW_HPP