gtsam  4.1.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 #ifdef GTSAM_USE_EIGEN_MKL_OPENMP
38 #include <omp.h>
39 #endif
40 
41 #ifdef __clang__
42 # define CLANG_DIAGNOSTIC_PUSH_IGNORE(diag) \
43  _Pragma("clang diagnostic push") \
44  _Pragma("clang diagnostic ignored \"" diag "\"")
45 #else
46 # define CLANG_DIAGNOSTIC_PUSH_IGNORE(diag)
47 #endif
48 
49 #ifdef __clang__
50 # define CLANG_DIAGNOSTIC_POP() _Pragma("clang diagnostic pop")
51 #else
52 # define CLANG_DIAGNOSTIC_POP()
53 #endif
54 
55 namespace gtsam {
56 
58  std::string GTSAM_EXPORT demangle(const char* name);
59 
61  typedef std::uint64_t Key;
62 
64  typedef std::uint64_t FactorIndex;
65 
67  typedef ptrdiff_t DenseIndex;
68 
69  /* ************************************************************************* */
74  template<typename TEST_TYPE, typename BASIC_TYPE, typename AS_NON_CONST,
75  typename AS_CONST>
76  struct const_selector {
77  };
78 
80  template<typename BASIC_TYPE, typename AS_NON_CONST, typename AS_CONST>
81  struct const_selector<BASIC_TYPE, BASIC_TYPE, AS_NON_CONST, AS_CONST> {
82  typedef AS_NON_CONST type;
83  };
84 
86  template<typename BASIC_TYPE, typename AS_NON_CONST, typename AS_CONST>
87  struct const_selector<const BASIC_TYPE, BASIC_TYPE, AS_NON_CONST, AS_CONST> {
88  typedef AS_CONST type;
89  };
90 
91  /* ************************************************************************* */
97  template<typename T, T defaultValue>
99  T value;
100 
102  ValueWithDefault() : value(defaultValue) {}
103 
105  ValueWithDefault(const T& _value) : value(_value) {}
106 
108  T& operator*() { return value; }
109 
111  const T& operator*() const { return value; }
112 
114  operator T() const { return value; }
115  };
116 
117  /* ************************************************************************* */
120  template<typename T>
122  T element_;
123  public:
124  typedef T value_type;
125  typedef const T* const_iterator;
126  typedef T* iterator;
127  ListOfOneContainer(const T& element) : element_(element) {}
128  const T* begin() const { return &element_; }
129  const T* end() const { return &element_ + 1; }
130  T* begin() { return &element_; }
131  T* end() { return &element_ + 1; }
132  size_t size() const { return 1; }
133  };
134 
135  BOOST_CONCEPT_ASSERT((boost::RandomAccessRangeConcept<ListOfOneContainer<int> >));
136 
138  template<typename T>
139  ListOfOneContainer<T> ListOfOne(const T& element) {
140  return ListOfOneContainer<T>(element);
141  }
142 
143  /* ************************************************************************* */
144 #ifdef __clang__
145 # pragma clang diagnostic push
146 # pragma clang diagnostic ignored "-Wunused-private-field" // Clang complains that previousOpenMPThreads is unused in the #else case below
147 #endif
148 
153  {
154  int previousOpenMPThreads;
155 
156  public:
157 #if defined GTSAM_USE_TBB && defined GTSAM_USE_EIGEN_MKL_OPENMP
159  previousOpenMPThreads(omp_get_num_threads())
160  {
161  omp_set_num_threads(omp_get_num_procs() / 4);
162  }
163 
165  {
166  omp_set_num_threads(previousOpenMPThreads);
167  }
168 #else
169  TbbOpenMPMixedScope() : previousOpenMPThreads(-1) {}
170  ~TbbOpenMPMixedScope() {}
171 #endif
172  };
173 
174 #ifdef __clang__
175 # pragma clang diagnostic pop
176 #endif
177 
178 }
179 
180 /* ************************************************************************* */
183 #ifdef NDEBUG
184 #define assert_throw(CONDITION, EXCEPTION) ((void)0)
185 #else
186 #define assert_throw(CONDITION, EXCEPTION) \
187  if (!(CONDITION)) { \
188  throw (EXCEPTION); \
189  }
190 #endif
191 
192 #ifdef _MSC_VER
193 
194 // Define some common g++ functions and macros we use that MSVC does not have
195 
196 #if (_MSC_VER < 1800)
197 
198 #include <boost/math/special_functions/fpclassify.hpp>
199 namespace std {
200  template<typename T> inline int isfinite(T a) {
201  return (int)boost::math::isfinite(a); }
202  template<typename T> inline int isnan(T a) {
203  return (int)boost::math::isnan(a); }
204  template<typename T> inline int isinf(T a) {
205  return (int)boost::math::isinf(a); }
206 }
207 
208 #endif
209 
210 #include <boost/math/constants/constants.hpp>
211 #ifndef M_PI
212 #define M_PI (boost::math::constants::pi<double>())
213 #endif
214 #ifndef M_PI_2
215 #define M_PI_2 (boost::math::constants::pi<double>() / 2.0)
216 #endif
217 #ifndef M_PI_4
218 #define M_PI_4 (boost::math::constants::pi<double>() / 4.0)
219 #endif
220 
221 #endif
222 
223 #ifdef min
224 #undef min
225 #endif
226 
227 #ifdef max
228 #undef max
229 #endif
230 
231 #ifdef ERROR
232 #undef ERROR
233 #endif
234 
235 namespace gtsam {
236 
238  template<typename ...> using void_t = void;
239 
255  template<typename, typename = void_t<>>
256  struct needs_eigen_aligned_allocator : std::false_type {
257  };
258  template<typename T>
260  };
261 
262 }
263 
269 #define GTSAM_MAKE_ALIGNED_OPERATOR_NEW \
270  EIGEN_MAKE_ALIGNED_OPERATOR_NEW \
271  using _eigen_aligned_allocator_trait = void;
272 
278 #define GTSAM_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) \
279  EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign) \
280  using _eigen_aligned_allocator_trait = void;
gtsam::needs_eigen_aligned_allocator
A SFINAE trait to mark classes that need special alignment.
Definition: types.h:256
gtsam::ValueWithDefault
Helper struct that encapsulates a value with a default, this is just used as a member object so you d...
Definition: types.h:98
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:61
gtsam::ValueWithDefault::ValueWithDefault
ValueWithDefault()
Default constructor, initialize to default value supplied in template argument.
Definition: types.h:102
gtsam::ValueWithDefault::operator*
T & operator*()
Operator to access the value.
Definition: types.h:108
gtsam::TbbOpenMPMixedScope
An object whose scope defines a block where TBB and OpenMP parallelism are mixed.
Definition: types.h:153
gtsam
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
gtsam::const_selector
Helper class that uses templates to select between two types based on whether TEST_TYPE is const or n...
Definition: types.h:76
gtsam::FactorIndex
std::uint64_t FactorIndex
Integer nonlinear factor index type.
Definition: types.h:64
gtsam::demangle
std::string demangle(const char *name)
Pretty print Value type name.
Definition: types.cpp:37
gtsam::DenseIndex
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition: types.h:67
gtsam::ValueWithDefault::ValueWithDefault
ValueWithDefault(const T &_value)
Initialize to the given value.
Definition: types.h:105
gtsam::void_t
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:238
gtsam::ListOfOneContainer
A helper class that behaves as a container with one element, and works with boost::range.
Definition: types.h:121
gtsam::ListOfOne
ListOfOneContainer< T > ListOfOne(const T &element)
Factory function for ListOfOneContainer to enable ListOfOne(e) syntax.
Definition: types.h:139