Jafar
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
jafarMacro.hpp
Go to the documentation of this file.
00001 /* $Id$ */
00002 
00009 #ifndef KERNEL_JAFAR_MACRO
00010 #define KERNEL_JAFAR_MACRO
00011 
00012 // Define _JFR_MODULE_ when using a jafar library outside the jafar build system
00013 #ifndef _JFR_MODULE_
00014 #define _JFR_MODULE_ "jafar"
00015 #endif
00016 
00017 #include <string>
00018 #include <sstream>
00019 
00020 namespace jafar {
00021   namespace kernel {
00022     namespace details {
00023 
00024       struct ForeachInfoBase {};
00025   
00026       template<typename T>
00027       struct ForeachInfo : public ForeachInfoBase {
00028         inline ForeachInfo( const T& t ) : it( t.begin() ), end( t.end() ), count(0)
00029         {
00030         }
00031         inline bool finished() const
00032         {
00033           return it == end;
00034         }
00035         inline void next() const
00036         {
00037           count = 0;
00038           ++it;
00039         }
00040         mutable typename T::const_iterator it, end;
00041         mutable int count;
00042       };
00043       template <typename T>
00044       inline ForeachInfo<T> CreateForeachInfo(const T& t)
00045       {
00046         return ForeachInfo<T>( t );
00047       }
00048   
00049       template <typename T>
00050       inline const ForeachInfo<T> *getForeachInfo(const ForeachInfoBase *base, const T *)
00051       {
00052         return static_cast<const ForeachInfo<T> *>(base); 
00053       }
00054       template <typename T>
00055       inline T* getForeachType(const T &) { return 0; }
00056     }
00057   }
00058 }
00059 
00075 #define JFR_FOREACH(var, cont ) \
00076   for( const jafar::kernel::details::ForeachInfoBase&  contInfo = jafar::kernel::details::CreateForeachInfo( cont ); \
00077        not jafar::kernel::details::getForeachInfo( &contInfo, true ? 0 : jafar::kernel::details::getForeachType(cont) )->finished(); \
00078        jafar::kernel::details::getForeachInfo( &contInfo, true ? 0 : jafar::kernel::details::getForeachType(cont) )->next() ) \
00079     for( var = *jafar::kernel::details::getForeachInfo( &contInfo, true ? 0 : jafar::kernel::details::getForeachType(cont) )->it; \
00080          jafar::kernel::details::getForeachInfo( &contInfo, true ? 0 : jafar::kernel::details::getForeachType(cont) )->count < 1; \
00081          ++jafar::kernel::details::getForeachInfo( &contInfo, true ? 0 : jafar::kernel::details::getForeachType(cont) )->count )
00082 
00083 
00094 #define JFR_DEFINE_PARAM_VAR(type, name) \
00095   private: \
00096       type m_##name;
00097 
00105 #define JFR_DEFINE_PARAM_GETTER(type, name) \
00106   public: \
00107      inline type name() const { return m_##name; }
00108 
00118 #define JFR_DEFINE_PARAM_SETTER(type, name, settername ) \
00119   public: \
00120      inline void settername(type v) { m_##name = v; }
00121 
00140 #define JFR_DEFINE_PARAM_RW( type, name, settername ) \
00141   JFR_DEFINE_PARAM_GETTER(type, name) \
00142   JFR_DEFINE_PARAM_SETTER(type, name, settername ) \
00143   JFR_DEFINE_PARAM_VAR(type,name)
00144 
00179 #define JFR_DEFINE_PARAM_RO( type, name) \
00180   JFR_DEFINE_PARAM_GETTER(type, name) \
00181   JFR_DEFINE_PARAM_VAR(type,name)
00182 
00200 #define JFR_PARAM_WO( type, name, settername ) \
00201   JFR_DEFINE_PARAM_SETTER(type, name, settername ) \
00202   JFR_DEFINE_PARAM_VAR(type,name)
00203 
00228 #ifndef JAFAR_DEPRECATED
00229 #if __GNUC__ - 0 > 3 || (__GNUC__ - 0 == 3 && __GNUC_MINOR__ - 0 >= 2)
00230   /* gcc >= 3.2 */
00231 # define JAFAR_DEPRECATED __attribute__ ((deprecated))
00232 #elif defined(_MSC_VER) && (_MSC_VER >= 1300)
00233   /* msvc >= 7 */
00234 # define JAFAR_DEPRECATED __declspec(deprecated)
00235 #else
00236 # define JAFAR_DEPRECATED
00237 #endif
00238 #endif
00239 
00240 // Must be include after the declaration of the JAFAR_DEPRECATED
00241 #include "kernel/jafarException.hpp"
00242 
00253 #define JFR_PP_VAR( var ) #var << " = " << var << " "
00254 
00255 
00263 #define JFR_ERROR(ExceptionName, id, message)     \
00264   {               \
00265     std::ostringstream s;         \
00266     s << message;           \
00267     throw ExceptionName(id, s.str(), __FILE__, __LINE__); \
00268   }
00269 
00275 #ifdef JFR_NDEBUG
00276   #define JFR_GLOBAL_TRY try{
00277   #define JFR_GLOBAL_CATCH } catch (kernel::Exception &e) { std::cout << e.what(); throw e; }
00278 #else
00279   #define JFR_GLOBAL_TRY
00280   #define JFR_GLOBAL_CATCH
00281 #endif
00282 
00290 #define JFR_PRED_ERROR(predicate, ExceptionName, id, message)   \
00291 { \
00292   if (!(predicate)) {             \
00293     std::ostringstream s;           \
00294     s << message;             \
00295     s << " (" << #predicate << ")";         \
00296     throw ExceptionName(id, s.str(), __FILE__, __LINE__);   \
00297   } \
00298 } 
00299 
00302 #define JFR_RUN_TIME(message)           \
00303   {                 \
00304     using jafar::kernel::JafarException;        \
00305     std::ostringstream s;           \
00306     s << message;             \
00307     throw JafarException(JafarException::RUN_TIME,      \
00308        s.str(),_JFR_MODULE_, __FILE__, __LINE__); \
00309   }
00310 
00314 #define JFR_PRED_RUN_TIME(predicate, message)       \
00315 { \
00316   if (!(predicate)) {             \
00317     using jafar::kernel::JafarException;        \
00318     std::ostringstream s;           \
00319     s << message;             \
00320     s << " (" << #predicate << ")";         \
00321     throw JafarException(JafarException::RUN_TIME,      \
00322        s.str(),_JFR_MODULE_, __FILE__, __LINE__); \
00323   } \
00324 } 
00325 
00342 #define JFR_IO_STREAM(predicate, message)       \
00343 { \
00344   if (!(predicate))             \
00345     {                 \
00346       using jafar::kernel::JafarException;        \
00347       std::ostringstream s;           \
00348       s << message;             \
00349       throw JafarException(JafarException::IO_STREAM,     \
00350          s.str(), _JFR_MODULE_, __FILE__, __LINE__);  \
00351   } \
00352 } 
00353 
00362 #define JFR_NUMERIC(predicate, message)         \
00363 { \
00364   if (!(predicate))             \
00365     {                 \
00366       using jafar::kernel::JafarException;        \
00367       std::ostringstream s;           \
00368       s << message;             \
00369       s << " (" << #predicate << ")";         \
00370       throw JafarException(JafarException::NUMERIC,     \
00371          s.str(),         \
00372          _JFR_MODULE_, __FILE__, __LINE__);   \
00373   } \
00374 } 
00375 
00378 #define JFR_CHECK_PARAM(predicate, param)       \
00379 { \
00380   if (!(predicate)) {             \
00381     std::ostringstream s;           \
00382     s << "invalid parameter " << #param << "=" << param;    \
00383     s << " (" << #predicate << ")";         \
00384     jafar::kernel::throwInvalidParamException(param, s.str(),   \
00385                 _JFR_MODULE_,   \
00386                 __FILE__, __LINE__);  \
00387   } \
00388 } 
00389 
00390 #include "kernel/jafarDebug.hpp"
00391 using jafar::debug::DebugStream;
00392 
00393 
00397 #ifndef JFR_NDEBUG
00398 
00402 #  define JFR_ASSERT(predicate, message)        \
00403 { \
00404   if (!(predicate)) {             \
00405     using jafar::kernel::JafarException;        \
00406     std::ostringstream s;           \
00407     s << message;             \
00408     s << " (" << #predicate << ")";         \
00409     throw JafarException(JafarException::ASSERT,      \
00410        s.str(),_JFR_MODULE_, __FILE__, __LINE__); \
00411   } \
00412 } 
00413 
00414 
00418 #  define JFR_PRECOND(predicate, message)       \
00419 { \
00420   if (!(predicate)) {             \
00421     using jafar::kernel::JafarException;        \
00422     std::ostringstream s;           \
00423     s << message;             \
00424     s << " (" << #predicate << ")";         \
00425     throw JafarException(JafarException::PRECONDITION,      \
00426        s.str(),_JFR_MODULE_, __FILE__, __LINE__); \
00427   } \
00428 } 
00429   
00430 
00434 #  define JFR_POSTCOND(predicate, message)        \
00435 { \
00436   if (!(predicate)) {             \
00437     using jafar::kernel::JafarException;        \
00438     std::ostringstream s;           \
00439     s << message;             \
00440     s << " (" << #predicate << ")";         \
00441     throw JafarException(JafarException::POSTCONDITION,     \
00442        s.str(),_JFR_MODULE_, __FILE__, __LINE__); \
00443   } \
00444 } 
00445 
00449 #  define JFR_INVARIANT(predicate, message)       \
00450 { \
00451   if (!(predicate)) {             \
00452     using jafar::kernel::JafarException;        \
00453     std::ostringstream s;           \
00454     s << message;             \
00455     s << " (" << #predicate << ")";         \
00456     throw JafarException(JafarException::INVARIANT,     \
00457        s.str(),_JFR_MODULE_, __FILE__, __LINE__); \
00458   } \
00459 } 
00460 
00473 #define JFR_WARNING(message)            \
00474     {                 \
00475       DebugStream::setup(_JFR_MODULE_, DebugStream::Warning);   \
00476       DebugStream::sendLocation(_JFR_MODULE_, __FILE__, __LINE__);  \
00477       DebugStream::instance() << message        \
00478             << jafar::debug::endl;      \
00479     }
00480 
00481 
00494 #define JFR_DEBUG_BEGIN() \
00495 {                          \
00496     DebugStream::setup(_JFR_MODULE_, DebugStream::Debug); \
00497     DebugStream::sendLocation(_JFR_MODULE_, __FILE__, __LINE__); \
00498 }
00499 
00504 #define JFR_DEBUG_SEND(message) \
00505 {                                \
00506     DebugStream::instance() << message; \
00507 }
00508 
00512 #define JFR_DEBUG_VSEND(message) \
00513 {                                 \
00514     DebugStream::setup(_JFR_MODULE_, DebugStream::VerboseDebug, false); \
00515     DebugStream::instance() << message; \
00516     DebugStream::setup(_JFR_MODULE_, DebugStream::Debug, false); \
00517 }
00518 
00522 #define JFR_DEBUG_VVSEND(message) \
00523 {                                 \
00524     DebugStream::setup(_JFR_MODULE_, DebugStream::VeryVerboseDebug, false); \
00525     DebugStream::instance() << message; \
00526     DebugStream::setup(_JFR_MODULE_, DebugStream::Debug, false); \
00527 }
00528 
00532 #define JFR_DEBUG_END() \
00533 {                        \
00534     DebugStream::instance() << jafar::debug::endl; \
00535 }
00536 
00537 
00538 
00550 #define JFR_DEBUG(message)            \
00551   {                 \
00552     JFR_DEBUG_BEGIN()       \
00553     JFR_DEBUG_SEND(message)     \
00554     JFR_DEBUG_END()     \
00555   }
00556 
00563 #define JFR_DEBUG_COND(test, message) \
00564 { \
00565   if( (test) ) JFR_DEBUG( message); \
00566 }
00567 
00568 
00573 #define JFR_VDEBUG(message)           \
00574   {                 \
00575     DebugStream::setup(_JFR_MODULE_, DebugStream::VerboseDebug);  \
00576     DebugStream::sendLocation(_JFR_MODULE_, __FILE__, __LINE__);  \
00577     DebugStream::instance() << message          \
00578           << jafar::debug::endl;      \
00579   }
00580 
00585 #define JFR_VVDEBUG(message)            \
00586   {                 \
00587     DebugStream::setup(_JFR_MODULE_, DebugStream::VeryVerboseDebug);  \
00588     DebugStream::sendLocation(_JFR_MODULE_, __FILE__, __LINE__);  \
00589     DebugStream::instance() << message          \
00590           << jafar::debug::endl;      \
00591   }
00592 
00612 #  define JFR_TRACE(exception, message)         \
00613   {                 \
00614     std::ostringstream s;           \
00615     s << message;             \
00616     exception.addTrace(_JFR_MODULE_, __FILE__, __LINE__, s.str());  \
00617   }
00618 #if 0
00619 
00623 #  define JFR_TRACE_BEGIN                       \
00624   try { ((void)0)
00625 
00628 #  define JFR_TRACE_END(message)          \
00629   }                 \
00630   catch(jafar::kernel::Exception& e) {                                  \
00631     std::ostringstream m;           \
00632     m << message;             \
00633     e.addTrace(_JFR_MODULE_, __FILE__, __LINE__, m.str());    \
00634     throw;                                                              \
00635   }                                                                     \
00636   catch(std::exception& e) {                                            \
00637     DebugStream::setup(_JFR_MODULE_, DebugStream::Trace);   \
00638     DebugStream::sendLocation(_JFR_MODULE_, __FILE__, __LINE__);  \
00639     DebugStream::instance() << "std::exception" << jafar::debug::endl \
00640           << message << jafar::debug::endl;   \
00641     throw;                                                              \
00642   }                                                                     \
00643   catch(...) {                                                          \
00644     DebugStream::setup(_JFR_MODULE_, DebugStream::Trace);   \
00645     DebugStream::sendLocation(_JFR_MODULE_, __FILE__, __LINE__);  \
00646     DebugStream::instance() << "unknown exception"      \
00647           << jafar::debug::endl     \
00648           << message << jafar::debug::endl;   \
00649     throw;                                                              \
00650   } ((void)0)
00651 #else
00652 #  define JFR_TRACE_BEGIN
00653 
00656 #  define JFR_TRACE_END(message)
00657 
00658 #endif
00659 
00665 #    define JFR_TRACE_POINT(message)          \
00666   JFR_TRACE_END(message);           \
00667   JFR_TRACE_BEGIN
00668 
00669 #else // JFR_NDEBUG
00670 #  define JFR_ASSERT(predicate, message) ((void)0)
00671 #  define JFR_PRECOND(predicate, message) ((void)0)
00672 #  define JFR_POSTCOND(predicate, message) ((void)0)
00673 #  define JFR_INVARIANT(predicate, message) ((void)0)
00674 
00675 #  ifndef JFR_QUIET
00676 #    define JFR_WARNING(message)          \
00677   {                 \
00678     std::cerr << "W: " << _JFR_MODULE_ << "/"       \
00679         << __FILE__ << ":" << __LINE__ << ": "      \
00680         << message << std::endl;          \
00681   }
00682 #  else // JFR_QUIET
00683 #    define JFR_WARNING(message) ((void)0)
00684 #  endif // JFR_QUIET
00685 
00686 #  define JFR_DEBUG_BEGIN()  ((void)0)
00687 #  define JFR_DEBUG_SEND(message)  ((void)0)
00688 #  define JFR_DEBUG_VSEND(message)  ((void)0)
00689 #  define JFR_DEBUG_VVSEND(message)  ((void)0)
00690 #  define JFR_DEBUG_END()  ((void)0)
00691 
00692 #  define JFR_DEBUG(message) ((void)0)
00693 #  define JFR_DEBUG_COND(test, message) ((void)0)
00694 #  define JFR_VDEBUG(message) ((void)0)
00695 #  define JFR_VVDEBUG(message) ((void)0)
00696 #  define JFR_TRACE(exception, message) ((void)0)
00697 #  define JFR_TRACE_BEGIN ((void)0)
00698 #  define JFR_TRACE_POINT ((void)0)
00699 #  define JFR_TRACE_END(message) ((void)0)
00700 #endif // JFR_NDEBUG
00701 
00702 #endif // KERNEL_JAFAR_MACRO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Wed Oct 15 2014 00:37:24 for Jafar by doxygen 1.7.6.1
LAAS-CNRS