GTSAM  4.0.2
C++ library for smoothing and mapping (SAM)
types.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
20 #pragma once
21 
22 #include <gtsam/dllexport.h>
23 #ifdef GTSAM_USE_BOOST_FEATURES
24 #include <boost/concept/assert.hpp>
25 #include <boost/range/concepts.hpp>
26 #endif
27 #include <gtsam/config.h> // for GTSAM_USE_TBB
28 
29 #include <cstddef>
30 #include <cstdint>
31 
32 #include <exception>
33 #include <string>
34 
35 #ifdef GTSAM_USE_TBB
36 #include <tbb/scalable_allocator.h>
37 #endif
38 
39 #if defined(__GNUC__) || defined(__clang__)
40 #define GTSAM_DEPRECATED __attribute__((deprecated))
41 #elif defined(_MSC_VER)
42 #define GTSAM_DEPRECATED __declspec(deprecated)
43 #else
44 #define GTSAM_DEPRECATED
45 #endif
46 
47 #ifdef GTSAM_USE_EIGEN_MKL_OPENMP
48 #include <omp.h>
49 #endif
50 
51 /* Define macros for ignoring compiler warnings.
52  * Usage Example:
53  * ```
54  * CLANG_DIAGNOSTIC_PUSH_IGNORE("-Wdeprecated-declarations")
55  * GCC_DIAGNOSTIC_PUSH_IGNORE("-Wdeprecated-declarations")
56  * MSVC_DIAGNOSTIC_PUSH_IGNORE(4996)
57  * // ... code you want to suppress deprecation warnings for ...
58  * DIAGNOSTIC_POP()
59  * ```
60  */
61 #define DO_PRAGMA(x) _Pragma (#x)
62 #ifdef __clang__
63 # define CLANG_DIAGNOSTIC_PUSH_IGNORE(diag) \
64  _Pragma("clang diagnostic push") \
65  DO_PRAGMA(clang diagnostic ignored diag)
66 #else
67 # define CLANG_DIAGNOSTIC_PUSH_IGNORE(diag)
68 #endif
69 
70 #ifdef __GNUC__
71 # define GCC_DIAGNOSTIC_PUSH_IGNORE(diag) \
72  _Pragma("GCC diagnostic push") \
73  DO_PRAGMA(GCC diagnostic ignored diag)
74 #else
75 # define GCC_DIAGNOSTIC_PUSH_IGNORE(diag)
76 #endif
77 
78 #ifdef _MSC_VER
79 # define MSVC_DIAGNOSTIC_PUSH_IGNORE(code) \
80  _Pragma("warning ( push )") \
81  DO_PRAGMA(warning ( disable : code ))
82 #else
83 # define MSVC_DIAGNOSTIC_PUSH_IGNORE(code)
84 #endif
85 
86 #if defined(__clang__)
87 # define DIAGNOSTIC_POP() _Pragma("clang diagnostic pop")
88 #elif defined(__GNUC__)
89 # define DIAGNOSTIC_POP() _Pragma("GCC diagnostic pop")
90 #elif defined(_MSC_VER)
91 # define DIAGNOSTIC_POP() _Pragma("warning ( pop )")
92 #else
93 # define DIAGNOSTIC_POP()
94 #endif
95 
96 namespace gtsam {
97 
99  std::string GTSAM_EXPORT demangle(const char* name);
100 
102  typedef std::uint64_t Key;
103 
105  typedef std::uint64_t FactorIndex;
106 
108  typedef ptrdiff_t DenseIndex;
109 
110  /* ************************************************************************* */
115  template<typename TEST_TYPE, typename BASIC_TYPE, typename AS_NON_CONST,
116  typename AS_CONST>
117  struct const_selector {
118  };
119 
121  template<typename BASIC_TYPE, typename AS_NON_CONST, typename AS_CONST>
122  struct const_selector<BASIC_TYPE, BASIC_TYPE, AS_NON_CONST, AS_CONST> {
123  typedef AS_NON_CONST type;
124  };
125 
127  template<typename BASIC_TYPE, typename AS_NON_CONST, typename AS_CONST>
128  struct const_selector<const BASIC_TYPE, BASIC_TYPE, AS_NON_CONST, AS_CONST> {
129  typedef AS_CONST type;
130  };
131 
132  /* ************************************************************************* */
138  template<typename T, T defaultValue>
140  T value;
141 
143  ValueWithDefault() : value(defaultValue) {}
144 
146  ValueWithDefault(const T& _value) : value(_value) {}
147 
149  T& operator*() { return value; }
150 
152  const T& operator*() const { return value; }
153 
155  operator T() const { return value; }
156  };
157 
158  /* ************************************************************************* */
159 #ifdef __clang__
160 # pragma clang diagnostic push
161 # pragma clang diagnostic ignored "-Wunused-private-field" // Clang complains that previousOpenMPThreads is unused in the #else case below
162 #endif
163 
168  {
169  int previousOpenMPThreads;
170 
171  public:
172 #if defined GTSAM_USE_TBB && defined GTSAM_USE_EIGEN_MKL_OPENMP
174  previousOpenMPThreads(omp_get_num_threads())
175  {
176  omp_set_num_threads(omp_get_num_procs() / 4);
177  }
178 
180  {
181  omp_set_num_threads(previousOpenMPThreads);
182  }
183 #else
184  TbbOpenMPMixedScope() : previousOpenMPThreads(-1) {}
185  ~TbbOpenMPMixedScope() {}
186 #endif
187  };
188 
189 #ifdef __clang__
190 # pragma clang diagnostic pop
191 #endif
192 
193 }
194 
195 /* ************************************************************************* */
198 #ifdef NDEBUG
199 #define assert_throw(CONDITION, EXCEPTION) ((void)0)
200 #else
201 #define assert_throw(CONDITION, EXCEPTION) \
202  if (!(CONDITION)) { \
203  throw (EXCEPTION); \
204  }
205 #endif
206 
207 #ifdef _MSC_VER
208 
209 // Define some common g++ functions and macros we use that MSVC does not have
210 
211 #if (_MSC_VER < 1800)
212 
213 #include <cmath>
214 namespace std {
215  template<typename T> inline int isfinite(T a) {
216  return (int)std::isfinite(a); }
217  template<typename T> inline int isnan(T a) {
218  return (int)std::isnan(a); }
219  template<typename T> inline int isinf(T a) {
220  return (int)std::isinf(a); }
221 }
222 
223 #endif
224 
225 #include <cmath>
226 #ifndef M_PI
227 #define M_PI (3.14159265358979323846)
228 #endif
229 #ifndef M_PI_2
230 #define M_PI_2 (M_PI / 2.0)
231 #endif
232 #ifndef M_PI_4
233 #define M_PI_4 (M_PI / 4.0)
234 #endif
235 
236 #endif
237 
238 #ifdef min
239 #undef min
240 #endif
241 
242 #ifdef max
243 #undef max
244 #endif
245 
246 #ifdef ERROR
247 #undef ERROR
248 #endif
249 
250 namespace gtsam {
251 
253  template<typename ...> using void_t = void;
254 
270  template<typename, typename = void_t<>>
271  struct needs_eigen_aligned_allocator : std::false_type {
272  };
273  template<typename T>
275  };
276 
277 }
278 
284 #define GTSAM_MAKE_ALIGNED_OPERATOR_NEW \
285  EIGEN_MAKE_ALIGNED_OPERATOR_NEW \
286  using _eigen_aligned_allocator_trait = void;
287 
293 #define GTSAM_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) \
294  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) \
295  using _eigen_aligned_allocator_trait = void;
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition: types.h:108
Definition: types.h:167
std::string GTSAM_EXPORT demangle(const char *name)
Function to demangle type name of variable, e.g. demangle(typeid(x).name())
T & operator*()
Definition: types.h:149
Definition: types.h:139
Definition: chartTesting.h:28
ValueWithDefault(const T &_value)
Definition: types.h:146
const T & operator*() const
Definition: types.h:152
ValueWithDefault()
Definition: types.h:143
void void_t
Convenience void_t as we assume C++11, it will not conflict the std one in C++17 as this is in gtsam:...
Definition: types.h:253
std::uint64_t FactorIndex
Integer nonlinear factor index type.
Definition: types.h:105
Definition: types.h:117
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:102