spot  2.11.6
common.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2013-2022 Laboratoire de Recherche et Développement
3 // de l'Epita (LRDE).
4 //
5 // This file is part of Spot, a model checking library.
6 //
7 // Spot is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // Spot is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 
20 #include <cstdlib>
21 #include <stdexcept>
22 #include <cassert>
23 
24 #pragma once
25 
26 #ifdef __GNUC__
27 #define SPOT_LIKELY(expr) __builtin_expect(!!(expr), 1)
28 #define SPOT_UNLIKELY(expr) __builtin_expect(!!(expr), 0)
29 #else
30 #define SPOT_LIKELY(expr) (expr)
31 #define SPOT_UNLIKELY(expr) (expr)
32 #endif
33 
34 #ifdef SWIG
35  #define SPOT_DEPRECATED(msg)
36  #define SPOT_FALLTHROUGH
37 #else
38  #if __cplusplus < 201703L
39  #error C++17 compiler required
40  #endif
41  #define SPOT_DEPRECATED(msg) [[deprecated(msg)]]
42  #define SPOT_FALLTHROUGH [[fallthrough]]
43 #endif
44 
45 #if defined _WIN32 || defined __CYGWIN__
46  #define SPOT_HELPER_DLL_IMPORT __declspec(dllimport)
47  #define SPOT_HELPER_DLL_EXPORT __declspec(dllexport)
48  #define SPOT_HELPER_DLL_LOCAL
49 #else
50  #if __GNUC__ >= 4
51  #define SPOT_HELPER_DLL_IMPORT __attribute__ ((visibility ("default")))
52  #define SPOT_HELPER_DLL_EXPORT __attribute__ ((visibility ("default")))
53  #define SPOT_HELPER_DLL_LOCAL __attribute__ ((visibility ("hidden")))
54  #else
55  #define SPOT_HELPER_DLL_IMPORT
56  #define SPOT_HELPER_DLL_EXPORT
57  #define SPOT_HELPER_DLL_LOCAL
58  #endif
59 #endif
60 
61 #ifdef SPOT_BUILD
62  #define SPOT_DLL
63 #endif
64 
65 
66 // We should not call assert() in headers. For the rare cases where
67 // we do really want to call assert(), use spot_assert__ instead.
68 // Else use SPOT_ASSERT so the assert() are removed from user's
69 // builds.
70 #define spot_assert__ assert
71 #if defined(SPOT_BUILD) or defined(SPOT_DEBUG)
72  #define SPOT_ASSERT(x) spot_assert__(x)
73 #else
74  // Do not replace by SPOT_ASSUME(x), as x can have some costly
75  // side-effects we do not want to pay outside of debug mode.
76  #define SPOT_ASSERT(x) while (0)
77 #endif
78 
79 // SPOT_API is used for the public API symbols. It either DLL imports
80 // or DLL exports (or does nothing for static build) SPOT_LOCAL is
81 // used for non-api symbols that may occur in header files.
82 #ifdef SPOT_DLL
83  #ifdef SPOT_BUILD
84  #define SPOT_API SPOT_HELPER_DLL_EXPORT
85  #else
86  #define SPOT_API SPOT_HELPER_DLL_IMPORT
87  #endif
88  #define SPOT_LOCAL SPOT_HELPER_DLL_LOCAL
89 #else
90  #define SPOT_API
91  #define SPOT_LOCAL
92 #endif
93 #define SPOT_API_VAR extern SPOT_API
94 
95 
96 // Swig 3.0.2 does not understand 'final' when used
97 // at class definition.
98 #ifdef SWIG
99  #define final
100 #endif
101 
102 
103 // Do not use those in code, prefer SPOT_UNREACHABLE() instead.
104 #if defined __clang__ || defined __GNUC__
105 # define SPOT_UNREACHABLE_BUILTIN() __builtin_unreachable()
106 # elif defined _MSC_VER
107 # define SPOT_UNREACHABLE_BUILTIN() __assume(0)
108 # else
109 # define SPOT_UNREACHABLE_BUILTIN() abort()
110 #endif
111 
112 // The extra parentheses in assert() is so that this
113 // pattern is not caught by the style checker.
114 #define SPOT_UNREACHABLE() do { \
115  SPOT_ASSERT(!("unreachable code reached")); \
116  SPOT_UNREACHABLE_BUILTIN(); \
117  } while (0)
118 
119 #define SPOT_UNIMPLEMENTED() throw std::runtime_error("unimplemented");
120 
121 
122 #if SPOT_DEBUG && !defined(NDEBUG)
123 #define SPOT_ASSUME(cond) assert(cond)
124 #else
125 #define SPOT_ASSUME(cond) \
126  do \
127  { \
128  if (!(cond)) \
129  SPOT_UNREACHABLE_BUILTIN(); \
130  } \
131  while (0)
132 #endif
133 
134 
135 // Useful when forwarding methods such as:
136 // auto func(int param) SPOT_RETURN(implem_.func(param));
137 #define SPOT_RETURN(code) -> decltype(code) { return code; }
138 
139 namespace spot
140 {
141  struct SPOT_API parse_error: public std::runtime_error
142  {
143  parse_error(const std::string& s)
144  : std::runtime_error(s)
145  {
146  }
147  };
148 
155  class SPOT_API parallel_policy
156  {
157  unsigned nthreads_;
158  public:
159  parallel_policy(unsigned nthreads = 1) : nthreads_(nthreads)
160  {
161  }
162 
163  unsigned nthreads() const
164  {
165  return nthreads_;
166  }
167  };
168 
169 }
170 
171 // This is a workaround for the issue described in GNU GCC bug 89303.
172 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=89303
173 //
174 // In brief: with some version of gcc distributed by Debian unstable
175 // and that correspond to something a bit newer than 8.2.1 (Debian is
176 // tracking the gcc-8-branch instead of following releases), mixing
177 // make_shared with enable_shared_from_this produces memory leaks or
178 // bad_weak_ptr exceptions.
179 //
180 // Our workaround is simply to avoid calling make_shared in those
181 // cases.
182 //
183 // The use of "enabled" in the macro name is just here to remember
184 // that we only need this macro for classes that inherit from
185 // enable_shared_from_this.
186 #if __GNUC__ == 8 && __GNUC_MINOR__ == 2
187 # define SPOT_make_shared_enabled__(TYPE, ...) \
188  std::shared_ptr<TYPE>(new TYPE(__VA_ARGS__))
189 #else
190 # define SPOT_make_shared_enabled__(TYPE, ...) \
191  std::make_shared<TYPE>(__VA_ARGS__)
192 #endif
This class is used to tell parallel algorithms what resources they may use.
Definition: common.hh:156
Definition: automata.hh:27
Definition: common.hh:142

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Fri Feb 27 2015 10:00:07 for spot by doxygen 1.9.1