CPN
Computational Process Networks
Classes | Macros | Functions
ThrowingAssert.h File Reference
#include <cpn/common.h>
#include <cpn/utils/Exception.h>
+ Include dependency graph for ThrowingAssert.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  AssertException
 The exception thrown by the ASSERT macro. More...
 

Macros

#define ASSERT(exp,...)   (exp ? true : __ASSERT(#exp, __FILE__, __LINE__, __PRETTY_FUNCTION__, false, ## __VA_ARGS__))
 
#define ASSERT_ABORT(exp,...)   (exp ? true : __ASSERT(#exp, __FILE__, __LINE__, __PRETTY_FUNCTION__, true, ## __VA_ARGS__))
 
#define ENSURE(exp,...)   (exp ? true : __ASSERT(#exp, __FILE__, __LINE__, __PRETTY_FUNCTION__, false, ## __VA_ARGS__))
 
#define ENSURE_ABORT(exp,...)   (exp ? true : __ASSERT(#exp, __FILE__, __LINE__, __PRETTY_FUNCTION__, true, ## __VA_ARGS__))
 

Functions

bool __ASSERT (const char *exp, const char *file, int line, const char *func, bool die) __attribute__((noreturn))
 
bool __ASSERT (const char *exp, const char *file, int line, const char *func, bool die, const std::string &msg) __attribute__((noreturn))
 
bool __ASSERT (const char *exp, const char *file, int line, const char *func, bool die, const char *fmt,...) __attribute__((noreturn))
 

Detailed Description

An implementation of an assert like micro that throws an exception. This is usefull when using a unit test as the test will then fail and the application will continue.

Note that this file uses some GCC only features like exp ?: see http://gcc.gnu.org/onlinedocs/gcc-4.4.1/gcc/Conditionals.html

Use ASSERT when you want the test to be compiled out when NDEBUG is defined and use ENSURE when you want the expression to remain when NDEBUG is defined.

Also these macros evaluate there arguments only ONCE.

Author
John Bridgman

Definition in file ThrowingAssert.h.

Macro Definition Documentation

#define ASSERT (   exp,
  ... 
)    (exp ? true : __ASSERT(#exp, __FILE__, __LINE__, __PRETTY_FUNCTION__, false, ## __VA_ARGS__))

Definition at line 62 of file ThrowingAssert.h.

Referenced by SocketHandle::Connect(), SocketHandle::CreatePair(), CPN::Kernel::CreateQueue(), CPN::PseudoNode::CreateReader(), CPN::Kernel::CreateReaderEndpoint(), CPN::PseudoNode::CreateWriter(), CPN::Kernel::CreateWriterEndpoint(), CPN::RemoteQueue::D4RTagPacket(), CPN::RemoteQueue::DequeuePacket(), CPN::RemoteContextClient::DispatchMessage(), CPN::RemoteContextServer::DispatchMessage(), CPN::RemoteQueue::EndOfReadPacket(), CPN::RemoteQueue::EndOfWritePacket(), CPN::RemoteQueue::EnqueuePacket(), CPN::PacketHandler::FirePacket(), CPN::RemoteContextServer::GetCreateEndpointKey(), CPN::PacketDecoder::GetDecoderBytes(), CPN::RemoteContextClient::GetEndpointInfo(), CPN::RemoteContextServer::GetEndpointInfo(), CPN::NodeLoader::GetFactory(), CPN::LocalContext::GetKernelConnectionInfo(), CPN::RemoteContextServer::GetKernelInfo(), CPN::RemoteContextServer::GetNodeInfo(), CPN::QueueBase::GetRawDequeuePtr(), CPN::QueueBase::GetRawEnqueuePtr(), SocketAddress::GetType(), CPN::ThresholdQueue::TQImpl::Grow(), CPN::RemoteQueue::IDReaderPacket(), CPN::RemoteQueue::IDWriterPacket(), CPN::RemoteQueue::InternalCheckStatus(), CPN::Kernel::InternalCreateNode(), CPN::RemoteQueue::InternalDequeue(), CPN::ThresholdQueue::InternalEnqueue(), CPN::RemoteQueue::InternalEnqueue(), CPN::RemoteQueue::InternalFlush(), CPN::ThresholdQueue::InternalGetRawDequeuePtr(), CPN::ThresholdQueue::InternalGetRawEnqueuePtr(), CPN::RemoteQueue::InternalReset(), CPN::QueueBase::InternalReset(), Loader(), Logger::Logger(), CPN::Kernel::NodeTerminated(), CPN::KernelBase::NotifyTerminate(), CPN::QueueBase::RawDequeue(), CPN::RemoteQueue::ReadBlockPacket(), CPN::PacketDecoder::ReleaseDecoderBytes(), CPN::KernelBase::RemoteCreateNode(), CPN::Kernel::RemoteCreateNode(), CPN::KernelBase::RemoteCreateQueue(), CPN::Kernel::RemoteCreateQueue(), CPN::KernelBase::RemoteCreateReader(), CPN::Kernel::RemoteCreateReader(), CPN::KernelBase::RemoteCreateWriter(), CPN::Kernel::RemoteCreateWriter(), CPN::LocalContext::SendCreateNode(), CPN::LocalContext::SendCreateQueue(), CPN::LocalContext::SendCreateReader(), CPN::LocalContext::SendCreateWriter(), CPN::RemoteQueue::SendEndOfReadPacket(), CPN::RemoteQueue::SendEndOfWritePacket(), CPN::PacketEncoder::SendEnqueue(), CPN::RemoteQueue::SendEnqueuePacket(), CPN::RemoteContextDaemon::SendMessage(), CPN::PacketEncoder::SendPacket(), CPN::ConnectionServer::PendingConnection::Set(), CPN::RemoteContextClient::SetupKernel(), CPN::RemoteContextServer::SignalKernelEnd(), CPN::RemoteContextServer::SignalKernelStart(), CPN::RemoteContextServer::SignalNodeEnd(), CPN::RemoteContextServer::SignalNodeStart(), CPN::ThresholdQueue::UnlockedGrow(), CPN::RemoteQueue::UnlockedSignalReaderTagChanged(), CPN::RemoteQueue::UnlockedSignalWriterTagChanged(), CPN::RemoteContextClient::WaitForAllNodeEnd(), CPN::RemoteQueue::WaitForData(), CPN::RemoteQueue::WaitForFreespace(), CPN::LocalContext::WaitForNodeEnd(), CPN::RemoteQueue::WriteBlockPacket(), and CPN::RemoteQueue::WriteBytes().

#define ASSERT_ABORT (   exp,
  ... 
)    (exp ? true : __ASSERT(#exp, __FILE__, __LINE__, __PRETTY_FUNCTION__, true, ## __VA_ARGS__))

Definition at line 63 of file ThrowingAssert.h.

Referenced by CPN::RemoteQueue::~RemoteQueue().

#define ENSURE (   exp,
  ... 
)    (exp ? true : __ASSERT(#exp, __FILE__, __LINE__, __PRETTY_FUNCTION__, false, ## __VA_ARGS__))
#define ENSURE_ABORT (   exp,
  ... 
)    (exp ? true : __ASSERT(#exp, __FILE__, __LINE__, __PRETTY_FUNCTION__, true, ## __VA_ARGS__))

Function Documentation

bool __ASSERT ( const char *  exp,
const char *  file,
int  line,
const char *  func,
bool  die 
)

Definition at line 59 of file ThrowingAssert.cc.

References CreateMessage(), and GetStack().

59  {
60  if (die) {
61  fprintf(stderr, "%s\nBacktrace:\n%s\n",
62  CreateMessage(exp, file, line, func, "").c_str(), GetStack(2).c_str());
63  abort();
64  } else {
65  throw AssertException(CreateMessage(exp, file, line, func, ""));
66  }
67 }
std::string CreateMessage(const char *exp, const char *file, int line, const char *func, const char *msg)
The exception thrown by the ASSERT macro.
std::string GetStack(unsigned ignore=0)
Definition: StackTrace.cc:39

+ Here is the call graph for this function:

bool __ASSERT ( const char *  exp,
const char *  file,
int  line,
const char *  func,
bool  die,
const std::string &  msg 
)

Definition at line 69 of file ThrowingAssert.cc.

References CreateMessage(), and GetStack().

69  {
70  if (die) {
71  fprintf(stderr, "%s\nBacktrace:\n%s\n",
72  CreateMessage(exp, file, line, func, "").c_str(), GetStack(2).c_str());
73  abort();
74  } else {
75  throw AssertException(CreateMessage(exp, file, line, func, msg.c_str()));
76  }
77 }
std::string CreateMessage(const char *exp, const char *file, int line, const char *func, const char *msg)
The exception thrown by the ASSERT macro.
std::string GetStack(unsigned ignore=0)
Definition: StackTrace.cc:39

+ Here is the call graph for this function:

bool __ASSERT ( const char *  exp,
const char *  file,
int  line,
const char *  func,
bool  die,
const char *  fmt,
  ... 
)

Definition at line 79 of file ThrowingAssert.cc.

References CreateMessage(), and GetStack().

79  {
80  std::vector<char> buff(128, '\0');
81  // Note this is based on the exmaple in the man page for vsnprintf.
82  while (1) {
83  /* Try to print in the allocated space. */
84  va_list ap;
85  va_start(ap, fmt);
86  int n = vsnprintf(&buff[0], buff.size(), fmt, ap);
87  va_end(ap);
88  /* If that worked, return the string. */
89  if (n > -1 && unsigned(n) < buff.size()) {
90  break;
91  }
92  /* Else try again with more space. */
93  if (n > -1) { /* glibc 2.1 */ /* precisely what is needed */
94  buff.resize(n+1, '\0');
95  }
96  else { /* glibc 2.0 */ /* twice the old size */
97  buff.resize(buff.size()*2, '\0');
98  }
99  }
100  std::string msg = CreateMessage(exp, file, line, func, &buff[0]);
101  if (die) {
102  fprintf(stderr, "%s\nBacktrace:\n%s\n", msg.c_str(), GetStack(2).c_str());
103  abort();
104  } else {
105  throw AssertException(msg);
106  }
107 }
std::string CreateMessage(const char *exp, const char *file, int line, const char *func, const char *msg)
The exception thrown by the ASSERT macro.
std::string GetStack(unsigned ignore=0)
Definition: StackTrace.cc:39

+ Here is the call graph for this function: