PGROUTING  2.6
pgr_assert.h File Reference

Assertions Handling. 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

Assertions Handling.

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

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

#define __STRING (   x)    #x

Definition at line 42 of file pgr_assert.h.

#define __TOSTRING (   x)    __STRING(x)

Definition at line 45 of file pgr_assert.h.

#define pgassert (   expr)
Value:
((expr) \
? static_cast<void>(0) \
"AssertFailedException: " __STRING(expr) \
" at " __FILE__ ":" __TOSTRING(__LINE__) + get_backtrace() ) )
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:126
std::string get_backtrace()
returns the execution path of the trace
Definition: pgr_assert.cpp:26
#define __TOSTRING(x)
Definition: pgr_assert.h:45
#define __STRING(x)
Definition: pgr_assert.h:42

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:

1 #include <iostream>
2 #include "pgr_assert.h"
3 
4 int main() {
5 
6  try {
7  pgassert(2+2 == 4);
8  pgassert(2+2 == 5);
9  }
10  catch (AssertFailedException &e) {
11  std::cout << e.what() << "\n";
12  }
13  catch (std::exception& e) {
14  std::cout << e.what() << "\n";
15  }
16  catch(...) {
17  std::cout << "Caught unknown exception!\n";
18  }
19  return 0;
20 }

Definition at line 81 of file pgr_assert.h.

Referenced by pgrouting::graph::Pgr_lineGraph< G, T_V, T_E >::add_one_vertex(), pgrouting::contraction::Pgr_linear< G >::add_shortcut(), pgrouting::graph::Pgr_contractionGraph< G, T_V, T_E >::add_shortcut(), pgrouting::vrp::Fleet::add_vehicle(), pgrouting::graph::Pgr_base_graph< G, T_V, T_E >::add_vertices(), pgrouting::graph::Pgr_base_graph< G, T_V, T_E >::adjacent(), pgrouting::tsp::TSP< MATRIX >::annealing(), Path::append(), pgrouting::vrp::Fleet::build_fleet(), pgrouting::trsp::Pgr_trspHandler::construct_path(), pgrouting::Pg_points_graph::create_new_edges(), pgrouting::vrp::Optimize::decrease_truck(), Pgr_dijkstra< G >::dijkstra_1_to_distance_no_init(), Pgr_dijkstra< G >::dijkstra_distance_visitor::dijkstra_distance_visitor(), Pgr_dijkstra< G >::dijkstra_distance_visitor_no_init::dijkstra_distance_visitor_no_init(), pgrouting::trsp::Pgr_trspHandler::dijkstra_exploration(), do_pgr_articulationPoints(), do_pgr_astarManyToMany(), do_pgr_bdAstar(), do_pgr_bdDijkstra(), do_pgr_biconnectedComponents(), do_pgr_bridges(), do_pgr_connectedComponents(), do_pgr_contractGraph(), do_pgr_dijkstraTRSP(), do_pgr_dijkstraVia(), do_pgr_driving_many_to_dist(), do_pgr_eucledianTSP(), do_pgr_floydWarshall(), do_pgr_johnson(), do_pgr_ksp(), do_pgr_lineGraph(), do_pgr_lineGraphFull(), do_pgr_many_to_many_dijkstra(), do_pgr_many_withPointsDD(), do_pgr_max_flow(), do_pgr_pickDeliver(), do_pgr_pickDeliverEuclidean(), do_pgr_strongComponents(), do_pgr_tsp(), do_pgr_withPoints(), do_pgr_withPointsKsp(), do_trsp(), pgrouting::vrp::Vehicle_pickDeliver::do_while_feasable(), pgrouting::vrp::Initial_solution::do_while_foo(), pgrouting::contraction::Pgr_linear< G >::doContraction(), pgrouting::trsp::EdgeInfo::endNode(), pgrouting::vrp::Vehicle_pickDeliver::erase(), pgrouting::vrp::Vehicle::erase(), pgrouting::vrp::Vehicle::evaluate(), Pgr_dijkstra< G >::execute_drivingDistance_no_init(), pgrouting::vrp::PD_Orders::find_best_I(), pgrouting::vrp::PD_Orders::find_best_J(), pgrouting::tsp::TSP< MATRIX >::find_closest_city(), pgrouting::trsp::EdgeInfo::get_cost(), Pgr_dijkstra< G >::get_drivingDistance_with_equicost_paths(), pgrouting::vrp::Vehicle_pickDeliver::get_first_order(), pgrouting::tsp::eucledianDmatrix::get_row(), pgrouting::vrp::Fleet::get_truck(), pgrouting::graph::Pgr_base_graph< G, T_V, T_E >::get_V(), pgrouting::vrp::Vehicle_pickDeliver::get_worse_order(), pgrouting::trsp::Pgr_trspHandler::getRestrictionCost(), Path::getSubpath(), pgrouting::graph::Pgr_componentsGraph< G, T_V, T_E >::graph_add_edge(), pgrouting::graph::Pgr_lineGraphFull< G, T_V, T_E >::graph_add_edge(), pgrouting::graph::Pgr_base_graph< G, T_V, T_E >::graph_add_edge(), pgrouting::graph::Pgr_base_graph< G, T_V, T_E >::graph_add_edge_no_create_vertex(), pgrouting::tsp::TSP< MATRIX >::greedyInitial(), pgrouting::vrp::Initial_solution::Initial_solution(), pgrouting::vrp::Vehicle::insert(), pgrouting::vrp::Vehicle_pickDeliver::insert(), pgrouting::graph::Pgr_base_graph< G, T_V, T_E >::insert_edges(), pgrouting::vrp::Initial_solution::invariant(), pgrouting::vrp::Vehicle::invariant(), pgrouting::tsp::TSP< MATRIX >::invariant(), pgrouting::contraction::Pgr_deadend< G >::is_dead_end(), pgrouting::vrp::Vehicle::is_ok(), pgrouting::vrp::PD_Orders::is_valid(), pgrouting::trsp::Pgr_trspHandler::Predecessor::isIllegal(), pgrouting::vrp::Optimize::move_order(), pgrouting::vrp::Optimize::move_reduce_cost(), Pgr_ksp< G >::compPaths::operator()(), pgrouting::vrp::PD_Orders::operator[](), pgrouting::vrp::Fleet::operator[](), pgrouting::vrp::Optimize::Optimize(), pgrouting::vrp::Pgr_pickDeliver::optimize(), pgrouting::graph::Pgr_base_graph< G, T_V, T_E >::Pgr_base_graph(), pgrouting::vrp::Pgr_pickDeliver::Pgr_pickDeliver(), pgrouting::vrp::Vehicle_pickDeliver::pop_back(), pgrouting::vrp::Vehicle::pop_back(), pgrouting::vrp::Vehicle_pickDeliver::pop_front(), pgrouting::vrp::Vehicle::pop_front(), pgrouting::trsp::Pgr_trspHandler::process_trsp(), pgrouting::vrp::Vehicle_pickDeliver::push_back(), pgrouting::vrp::Vehicle_pickDeliver::push_front(), rand(), pgrouting::tsp::Tour::reverse(), pgrouting::tsp::Tour::rotate(), pgrouting::tsp::Tour::slide(), pgrouting::vrp::Pgr_pickDeliver::solve(), pgrouting::trsp::EdgeInfo::startNode(), succ(), pgrouting::tsp::Tour::swap(), pgrouting::vrp::Vehicle::swap(), pgrouting::vrp::Optimize::swap_order(), pgrouting::vrp::Optimize::swap_worse(), pgrouting::tsp::TSP< MATRIX >::swapClimb(), pgrouting::vrp::Vehicle::tau(), pgrouting::tsp::Dmatrix::tourCost(), pgrouting::vrp::Tw_node::travel_time_to(), pgrouting::tsp::TSP< MATRIX >::TSP(), and pgrouting::vrp::Vehicle::Vehicle().

#define pgassertwm (   expr,
  msg 
)
Value:
((expr) \
? static_cast<void>(0) \
"AssertFailedException: " __STRING(expr) \
" at " __FILE__ ":" __TOSTRING(__LINE__) + get_backtrace(msg) ) )
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:126
std::string get_backtrace()
returns the execution path of the trace
Definition: pgr_assert.cpp:26
#define __TOSTRING(x)
Definition: pgr_assert.h:45
#define __STRING(x)
Definition: pgr_assert.h:42

Adds a message to the assertion.

Example:

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

Definition at line 104 of file pgr_assert.h.

Referenced by pgrouting::vrp::Fleet::build_fleet(), Pgr_dijkstra< G >::dijkstra_1_to_distance_no_init(), pgrouting::vrp::Initial_solution::do_while_foo(), pgrouting::tsp::TSP< MATRIX >::find_closest_city(), pgrouting::vrp::Fleet::get_truck(), pgrouting::tsp::TSP< MATRIX >::getDeltaReverse(), pgrouting::tsp::TSP< MATRIX >::getDeltaSlide(), pgrouting::tsp::TSP< MATRIX >::getDeltaSwap(), pgrouting::graph::Pgr_base_graph< G, T_V, T_E >::graph_add_edge_no_create_vertex(), pgrouting::tsp::TSP< MATRIX >::greedyInitial(), pgrouting::vrp::Vehicle_pickDeliver::insert(), pgrouting::vrp::Fleet::is_fleet_ok(), pgrouting::tsp::Dmatrix::is_symmetric(), pgrouting::vrp::Pgr_pickDeliver::nodesOK(), and pgrouting::tsp::eucledianDmatrix::set_ids().

Function Documentation

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 26 of file pgr_assert.cpp.

Referenced by get_backtrace().

26  {
27 #ifdef __GLIBC__
28  void *trace[16];
29  int i, trace_size = 0;
30 
31  trace_size = backtrace(trace, 16);
32  char** funcNames = backtrace_symbols(trace, trace_size);
33 
34 
35  std::string message = "\n*** Execution path***\n";
36  for (i = 0; i < trace_size; ++i) {
37  message += "[bt]" + static_cast<std::string>(funcNames[i]) + "\n";
38  }
39 
40  free(funcNames);
41  return message;
42 #else
43  return "";
44 #endif
45 }

Here is the caller graph for this function:

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

Definition at line 47 of file pgr_assert.cpp.

References get_backtrace().

47  {
48  return std::string("\n") + msg + "\n" + get_backtrace();
49 }
std::string get_backtrace()
returns the execution path of the trace
Definition: pgr_assert.cpp:26

Here is the call graph for this function: