Adaptagrams
assertions.h
1 /*
2  * vim: ts=4 sw=4 et tw=0 wm=0
3  *
4  * libvpsc - A solver for the problem of Variable Placement with
5  * Separation Constraints.
6  *
7  * Copyright (C) 2009 Monash University
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  * See the file LICENSE.LGPL distributed with the library.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18  *
19 */
20 
21 #ifndef VPSC_ASSERTIONS_H
22 #define VPSC_ASSERTIONS_H
23 
24 #ifdef NDEBUG
25 
26  #define COLA_ASSERT(expr) static_cast<void>(0)
27 
28 #else // Not NDEBUG
29 
30  // sstream needs ::strcpy_s under MinGW so include cstring.
31  #include <cstring>
32 
33  #include <sstream>
34  #include <cassert>
35 
36  #if defined(USE_ASSERT_EXCEPTIONS)
37 
38  // String seems to be missing on MinGW's gcc,
39  // so define it here if it is missing.
40  #ifndef __STRING
41  #define __STRING(x) #x
42  #endif
43 
44  #if !defined(__ASSERT_FUNCTION)
45  #define COLA_ASSERT(expr) \
46  if (!(expr)) { \
47  throw vpsc::CriticalFailure(__STRING(expr), __FILE__, __LINE__); \
48  }
49  #else
50  #define COLA_ASSERT(expr) \
51  if (!(expr)) { \
52  throw vpsc::CriticalFailure(__STRING(expr), __FILE__, __LINE__, \
53  __ASSERT_FUNCTION); \
54  }
55  #endif
56 
57  #else
58  #define COLA_ASSERT(expr) assert(expr)
59  #endif
60 
61 namespace vpsc {
62 
63 // Critical failure: either something went wrong, or (more likely) there
64 // was infeasible input.
65 class CriticalFailure
66 {
67  public:
68  CriticalFailure(const char *expr, const char *file, int line,
69  const char *function = nullptr)
70  : expr(expr),
71  file(file),
72  line(line),
73  function(function)
74  {
75  }
76  std::string what() const
77  {
78  std::stringstream s;
79  s << "ERROR: Critical assertion failed.\n";
80  s << " expression: " << expr << "\n";
81  s << " at line " << line << " of " << file << "\n";
82  if (function)
83  {
84  s << " in: " << function << "\n";
85  }
86 
87  return s.str();
88  }
89  private:
90  const char *expr;
91  const char *file;
92  int line;
93  const char *function;
94 };
95 
96 }
97 
98 #endif // NDEBUG
99 
100 
101 #endif // VPSC_ASSERTIONS_H
102 
libvpsc: Variable Placement with Separation Constraints quadratic program solver library.
Definition: assertions.h:61