19 #ifndef INCLUDE_UTIL_THROW_HPP
20 #define INCLUDE_UTIL_THROW_HPP
25 #include <boost/current_function.hpp>
27 #include "VariableArgMacro.hpp"
36 virtual ~I_InvokeThrow() {}
37 virtual void invoke(
const std::string &context,
const std::string & args,
int logName,
int logLevel,
const char * file,
int line,
const char * func) = 0;
41 class InvokeThrow :
public I_InvokeThrow
44 InvokeThrow(
const E &e) : mE(e)
47 void invoke(
const std::string &context,
const std::string & args,
int logName,
int logLevel,
const char * file,
int line,
const char * func)
49 const_cast<E &
>(mE).setContext(context);
51 if (RCF::LogManager::instance().isEnabled(logName, logLevel))
53 RCF::LogEntry entry(logName, logLevel, file, line, func);
56 <<
"Exception thrown. "
75 class InvokeThrow<std::runtime_error> :
public I_InvokeThrow
78 InvokeThrow(
const std::runtime_error &e) : mE(e)
81 void invoke(
const std::string &context,
const std::string & args,
int logName,
int logLevel,
const char * file,
int line,
const char * func)
83 if (RCF::LogManager::instance().isEnabled(logName, logLevel))
85 RCF::LogEntry entry(logName, logLevel, file, line, func);
88 <<
"Exception thrown. "
99 throw std::runtime_error( std::string(mE.what()) +
": " + context);
103 const std::runtime_error &mE;
107 const char *getTypeName(
const T &t)
109 return typeid(t).name();
114 class ThrowFunctor :
public VariableArgMacroFunctor
117 ThrowFunctor() : mThrown(false), mLogName(0), mLogLevel(0)
121 ThrowFunctor(
const E &e,
int logName,
int logLevel) :
122 VariableArgMacroFunctor(),
123 mInvokeThrow(new detail::InvokeThrow<E>(e)),
136 std::string args(mArgs->str(),
static_cast<std::size_t
>(mArgs->tellp()));
138 std::string context = mHeader->str();
141 if (!std::uncaught_exception())
143 mInvokeThrow->invoke(context, args, mLogName, mLogLevel, mFile, mLine, mFunc);
149 std::auto_ptr<detail::I_InvokeThrow> mInvokeThrow;
158 #pragma warning( push )
159 #pragma warning( disable : 4355 ) // warning C4355: 'this' : used in base member initializer list
162 #if defined(__GNUC__) && (__GNUC__ < 3 || (__GNUC__ == 3 && __GNUC_MINOR__ < 4))
163 #define UTIL_THROW_GCC_33_HACK (const RCF::VariableArgMacro<RCF::ThrowFunctor> &)
165 #define UTIL_THROW_GCC_33_HACK
168 DECLARE_VARIABLE_ARG_MACRO( UTIL_THROW, ThrowFunctor );
169 #define UTIL_THROW(e, logName, logLevel) \
171 UTIL_THROW_GCC_33_HACK \
172 RCF::VariableArgMacro<RCF::ThrowFunctor>(e, logName, logLevel) \
178 BOOST_CURRENT_FUNCTION) \
179 .cast( (RCF::VariableArgMacro<RCF::ThrowFunctor> *) NULL) \
184 #define UTIL_THROW_A(x) UTIL_THROW_OP(x, B)
185 #define UTIL_THROW_B(x) UTIL_THROW_OP(x, A)
186 #define UTIL_THROW_OP(x, next) UTIL_THROW_A.notify_((x), #x).UTIL_THROW_ ## next
189 #pragma warning( pop )
192 #define UTIL_VERIFY(cond, e, logName, logLevel) if (cond); else UTIL_THROW(e, logName, logLevel)
196 #endif // ! INCLUDE_UTIL_THROW_HPP