gtsam 4.2.0
gtsam
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#include <boost/concept/assert.hpp>
24#include <boost/range/concepts.hpp>
25#include <gtsam/config.h> // for GTSAM_USE_TBB
26
27#include <cstddef>
28#include <cstdint>
29
30#include <exception>
31#include <string>
32
33#ifdef GTSAM_USE_TBB
34#include <tbb/scalable_allocator.h>
35#endif
36
37#if defined(__GNUC__) || defined(__clang__)
38#define GTSAM_DEPRECATED __attribute__((deprecated))
39#elif defined(_MSC_VER)
40#define GTSAM_DEPRECATED __declspec(deprecated)
41#else
42#define GTSAM_DEPRECATED
43#endif
44
45#ifdef GTSAM_USE_EIGEN_MKL_OPENMP
46#include <omp.h>
47#endif
48
49/* Define macros for ignoring compiler warnings.
50 * Usage Example:
51 * ```
52 * CLANG_DIAGNOSTIC_PUSH_IGNORE("-Wdeprecated-declarations")
53 * GCC_DIAGNOSTIC_PUSH_IGNORE("-Wdeprecated-declarations")
54 * MSVC_DIAGNOSTIC_PUSH_IGNORE(4996)
55 * // ... code you want to suppress deprecation warnings for ...
56 * DIAGNOSTIC_POP()
57 * ```
58 */
59#define DO_PRAGMA(x) _Pragma (#x)
60#ifdef __clang__
61# define CLANG_DIAGNOSTIC_PUSH_IGNORE(diag) \
62 _Pragma("clang diagnostic push") \
63 DO_PRAGMA(clang diagnostic ignored diag)
64#else
65# define CLANG_DIAGNOSTIC_PUSH_IGNORE(diag)
66#endif
67
68#ifdef __GNUC__
69# define GCC_DIAGNOSTIC_PUSH_IGNORE(diag) \
70 _Pragma("GCC diagnostic push") \
71 DO_PRAGMA(GCC diagnostic ignored diag)
72#else
73# define GCC_DIAGNOSTIC_PUSH_IGNORE(diag)
74#endif
75
76#ifdef _MSC_VER
77# define MSVC_DIAGNOSTIC_PUSH_IGNORE(code) \
78 _Pragma("warning ( push )") \
79 DO_PRAGMA(warning ( disable : code ))
80#else
81# define MSVC_DIAGNOSTIC_PUSH_IGNORE(code)
82#endif
83
84#if defined(__clang__)
85# define DIAGNOSTIC_POP() _Pragma("clang diagnostic pop")
86#elif defined(__GNUC__)
87# define DIAGNOSTIC_POP() _Pragma("GCC diagnostic pop")
88#elif defined(_MSC_VER)
89# define DIAGNOSTIC_POP() _Pragma("warning ( pop )")
90#else
91# define DIAGNOSTIC_POP()
92#endif
93
94namespace gtsam {
95
97 std::string GTSAM_EXPORT demangle(const char* name);
98
100 typedef std::uint64_t Key;
101
103 typedef std::uint64_t FactorIndex;
104
106 typedef ptrdiff_t DenseIndex;
107
108 /* ************************************************************************* */
113 template<typename TEST_TYPE, typename BASIC_TYPE, typename AS_NON_CONST,
114 typename AS_CONST>
116 };
117
119 template<typename BASIC_TYPE, typename AS_NON_CONST, typename AS_CONST>
120 struct const_selector<BASIC_TYPE, BASIC_TYPE, AS_NON_CONST, AS_CONST> {
121 typedef AS_NON_CONST type;
122 };
123
125 template<typename BASIC_TYPE, typename AS_NON_CONST, typename AS_CONST>
126 struct const_selector<const BASIC_TYPE, BASIC_TYPE, AS_NON_CONST, AS_CONST> {
127 typedef AS_CONST type;
128 };
129
130 /* ************************************************************************* */
136 template<typename T, T defaultValue>
138 T value;
139
141 ValueWithDefault() : value(defaultValue) {}
142
144 ValueWithDefault(const T& _value) : value(_value) {}
145
147 T& operator*() { return value; }
148
150 const T& operator*() const { return value; }
151
153 operator T() const { return value; }
154 };
155
156 /* ************************************************************************* */
159 template<typename T>
161 T element_;
162 public:
163 typedef T value_type;
164 typedef const T* const_iterator;
165 typedef T* iterator;
166 ListOfOneContainer(const T& element) : element_(element) {}
167 const T* begin() const { return &element_; }
168 const T* end() const { return &element_ + 1; }
169 T* begin() { return &element_; }
170 T* end() { return &element_ + 1; }
171 size_t size() const { return 1; }
172 };
173
174 BOOST_CONCEPT_ASSERT((boost::RandomAccessRangeConcept<ListOfOneContainer<int> >));
175
177 template<typename T>
179 return ListOfOneContainer<T>(element);
180 }
181
182 /* ************************************************************************* */
183#ifdef __clang__
184# pragma clang diagnostic push
185# pragma clang diagnostic ignored "-Wunused-private-field" // Clang complains that previousOpenMPThreads is unused in the #else case below
186#endif
187
192 {
193 int previousOpenMPThreads;
194
195 public:
196#if defined GTSAM_USE_TBB && defined GTSAM_USE_EIGEN_MKL_OPENMP
198 previousOpenMPThreads(omp_get_num_threads())
199 {
200 omp_set_num_threads(omp_get_num_procs() / 4);
201 }
202
204 {
205 omp_set_num_threads(previousOpenMPThreads);
206 }
207#else
208 TbbOpenMPMixedScope() : previousOpenMPThreads(-1) {}
210#endif
211 };
212
213#ifdef __clang__
214# pragma clang diagnostic pop
215#endif
216
217}
218
219/* ************************************************************************* */
222#ifdef NDEBUG
223#define assert_throw(CONDITION, EXCEPTION) ((void)0)
224#else
225#define assert_throw(CONDITION, EXCEPTION) \
226 if (!(CONDITION)) { \
227 throw (EXCEPTION); \
228 }
229#endif
230
231#ifdef _MSC_VER
232
233// Define some common g++ functions and macros we use that MSVC does not have
234
235#if (_MSC_VER < 1800)
236
237#include <boost/math/special_functions/fpclassify.hpp>
238namespace std {
239 template<typename T> inline int isfinite(T a) {
240 return (int)boost::math::isfinite(a); }
241 template<typename T> inline int isnan(T a) {
242 return (int)boost::math::isnan(a); }
243 template<typename T> inline int isinf(T a) {
244 return (int)boost::math::isinf(a); }
245}
246
247#endif
248
249#include <boost/math/constants/constants.hpp>
250#ifndef M_PI
251#define M_PI (boost::math::constants::pi<double>())
252#endif
253#ifndef M_PI_2
254#define M_PI_2 (boost::math::constants::pi<double>() / 2.0)
255#endif
256#ifndef M_PI_4
257#define M_PI_4 (boost::math::constants::pi<double>() / 4.0)
258#endif
259
260#endif
261
262#ifdef min
263#undef min
264#endif
265
266#ifdef max
267#undef max
268#endif
269
270#ifdef ERROR
271#undef ERROR
272#endif
273
274namespace gtsam {
275
277 template<typename ...> using void_t = void;
278
294 template<typename, typename = void_t<>>
295 struct needs_eigen_aligned_allocator : std::false_type {
296 };
297 template<typename T>
299 };
300
301}
302
308#define GTSAM_MAKE_ALIGNED_OPERATOR_NEW \
309 EIGEN_MAKE_ALIGNED_OPERATOR_NEW \
310 using _eigen_aligned_allocator_trait = void;
311
317#define GTSAM_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) \
318 EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) \
319 using _eigen_aligned_allocator_trait = void;
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
ListOfOneContainer< T > ListOfOne(const T &element)
Factory function for ListOfOneContainer to enable ListOfOne(e) syntax.
Definition: types.h:178
std::string demangle(const char *name)
Pretty print Value type name.
Definition: types.cpp:37
std::uint64_t FactorIndex
Integer nonlinear factor index type.
Definition: types.h:103
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition: types.h:106
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:277
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:100
Helper class that uses templates to select between two types based on whether TEST_TYPE is const or n...
Definition: types.h:115
Helper struct that encapsulates a value with a default, this is just used as a member object so you d...
Definition: types.h:137
T & operator*()
Operator to access the value.
Definition: types.h:147
ValueWithDefault()
Default constructor, initialize to default value supplied in template argument.
Definition: types.h:141
ValueWithDefault(const T &_value)
Initialize to the given value.
Definition: types.h:144
A helper class that behaves as a container with one element, and works with boost::range.
Definition: types.h:160
An object whose scope defines a block where TBB and OpenMP parallelism are mixed.
Definition: types.h:192
A SFINAE trait to mark classes that need special alignment.
Definition: types.h:295