PGROUTING  3.2
pgr_assert.h File Reference

An assert functionality that uses C++ throw(). More...

#include <string>
#include <exception>
Include dependency graph for pgr_assert.h:

Go to the source code of this file.

Classes

class  AssertFailedException
 Extends std::exception and is the exception that we throw if an assert fails. More...
 

Macros

#define __STRING(x)   #x
 
#define __TOSTRING(x)   __STRING(x)
 
#define pgassert(expr)
 Uses the standard assert syntax. More...
 
#define pgassertwm(expr, msg)
 Adds a message to the assertion. More...
 

Functions

std::string get_backtrace ()
 returns the execution path of the trace More...
 
std::string get_backtrace (const std::string &)
 

Detailed Description

An assert functionality that uses C++ throw().

Assertions Handling

This file provides an alternative to assert functionality that will convert all pgassert() into C++ throw using an AssertFailedException class.

This allows catching errors and do appropriate clean up re-throw if needed to catch errors in the postgresql environment

Do not crash the backend server.

Definition in file pgr_assert.h.

Macro Definition Documentation

◆ __STRING

#define __STRING (   x)    #x

Definition at line 55 of file pgr_assert.h.

◆ __TOSTRING

#define __TOSTRING (   x)    __STRING(x)

Definition at line 58 of file pgr_assert.h.

◆ pgassert

#define pgassert (   expr)
Value:
((expr) \
? static_cast<void>(0) \
"AssertFailedException: " __STRING(expr) \
" at " __FILE__ ":" __TOSTRING(__LINE__) + get_backtrace() ) )

Uses the standard assert syntax.

When an assertion fails it will throw AssertFailedException and what() will return a string like "AssertFailedException(2+2 == 5) at t.cpp:11"

Example:

#include <iostream>
#include "pgr_assert.h"
int main() {
try {
pgassert(2+2 == 4);
pgassert(2+2 == 5);
}
catch (AssertFailedException &e) {
std::cout << e.what() << "\n";
}
catch (std::exception& e) {
std::cout << e.what() << "\n";
}
catch(...) {
std::cout << "Caught unknown exception!\n";
}
return 0;
}

Definition at line 94 of file pgr_assert.h.

◆ pgassertwm

#define pgassertwm (   expr,
  msg 
)
Value:
((expr) \
? static_cast<void>(0) \
"AssertFailedException: " __STRING(expr) \
" at " __FILE__ ":" __TOSTRING(__LINE__) + get_backtrace(msg) ) )

Adds a message to the assertion.

Example:

pgassert(2+2 == 5, "Expected a 4 as result");
std::ostringstream log;
log << "Expected a 4 as result"
pgassert(2+2 == 5, log.str());

Definition at line 117 of file pgr_assert.h.

Function Documentation

◆ get_backtrace() [1/2]

std::string get_backtrace ( )

returns the execution path of the trace

In case of a failed exception the backtrace can be is shown in the error message.

Does not work for windows, please read: http://stackoverflow.com/questions/27639931/can-not-find-execinfo-h-when-setup-malpack

Definition at line 40 of file pgr_assert.cpp.

40  {
41 #ifdef __GLIBC__
42  void *trace[16];
43  int i, trace_size = 0;
44 
45  trace_size = backtrace(trace, 16);
46  char** funcNames = backtrace_symbols(trace, trace_size);
47 
48 
49  std::string message = "\n*** Execution path***\n";
50  for (i = 0; i < trace_size; ++i) {
51  message += "[bt]" + static_cast<std::string>(funcNames[i]) + "\n";
52  }
53 
54  free(funcNames);
55  return message;
56 #else
57  return "";
58 #endif
59 }

Referenced by get_backtrace().

◆ get_backtrace() [2/2]

std::string get_backtrace ( const std::string &  )

Definition at line 61 of file pgr_assert.cpp.

61  {
62  return std::string("\n") + msg + "\n" + get_backtrace();
63 }

References get_backtrace().

__STRING
#define __STRING(x)
Definition: pgr_assert.h:55
AssertFailedException::what
virtual const char * what() const
Definition: pgr_assert.cpp:67
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:94
get_backtrace
std::string get_backtrace()
returns the execution path of the trace
Definition: pgr_assert.cpp:40
pgr_assert.h
An assert functionality that uses C++ throw().
get_backtrace
std::string get_backtrace()
returns the execution path of the trace
Definition: pgr_assert.cpp:40
__TOSTRING
#define __TOSTRING(x)
Definition: pgr_assert.h:58
AssertFailedException
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:139