gtsam  3.2.1
gtsam
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 <gtsam/config.h>
24 
25 #include <cstddef>
26 #include <string>
27 #include <iostream>
28 #include <boost/function/function1.hpp>
29 #include <boost/range/concepts.hpp>
30 #include <boost/optional.hpp>
31 
32 #ifdef GTSAM_USE_TBB
33 #include <tbb/task_scheduler_init.h>
34 #include <tbb/tbb_exception.h>
35 #include <tbb/scalable_allocator.h>
36 #endif
37 
38 #ifdef GTSAM_USE_EIGEN_MKL_OPENMP
39 #include <omp.h>
40 #endif
41 
42 #ifdef __clang__
43 # define CLANG_DIAGNOSTIC_PUSH_IGNORE(diag) \
44  _Pragma("clang diagnostic push") \
45  _Pragma("clang diagnostic ignored \"" diag "\"")
46 #else
47 # define CLANG_DIAGNOSTIC_PUSH_IGNORE(diag)
48 #endif
49 
50 #ifdef __clang__
51 # define CLANG_DIAGNOSTIC_POP() _Pragma("clang diagnostic pop")
52 #else
53 # define CLANG_DIAGNOSTIC_POP()
54 #endif
55 
56 namespace gtsam {
57 
59  typedef size_t Key;
60 
62  typedef boost::function<std::string(Key)> KeyFormatter;
63 
64  // Helper function for DefaultKeyFormatter
65  GTSAM_EXPORT std::string _defaultKeyFormatter(Key key);
66 
70  static const KeyFormatter DefaultKeyFormatter = &_defaultKeyFormatter;
71 
72 
74  typedef ptrdiff_t DenseIndex;
75 
76 
77  /* ************************************************************************* */
82  template<typename TEST_TYPE, typename BASIC_TYPE, typename AS_NON_CONST,
83  typename AS_CONST>
84  struct const_selector {
85  };
86 
88  template<typename BASIC_TYPE, typename AS_NON_CONST, typename AS_CONST>
89  struct const_selector<BASIC_TYPE, BASIC_TYPE, AS_NON_CONST, AS_CONST> {
90  typedef AS_NON_CONST type;
91  };
92 
94  template<typename BASIC_TYPE, typename AS_NON_CONST, typename AS_CONST>
95  struct const_selector<const BASIC_TYPE, BASIC_TYPE, AS_NON_CONST, AS_CONST> {
96  typedef AS_CONST type;
97  };
98 
99  /* ************************************************************************* */
105  template<typename T, T defaultValue>
107  T value;
108 
110  ValueWithDefault() : value(defaultValue) {}
111 
113  ValueWithDefault(const T& _value) : value(_value) {}
114 
116  T& operator*() { return value; }
117 
119  const T& operator*() const { return value; }
120 
122  operator T() const { return value; }
123  };
124 
125  /* ************************************************************************* */
128  template<typename T>
130  T element_;
131  public:
132  typedef T value_type;
133  typedef const T* const_iterator;
134  typedef T* iterator;
135  ListOfOneContainer(const T& element) : element_(element) {}
136  const T* begin() const { return &element_; }
137  const T* end() const { return &element_ + 1; }
138  T* begin() { return &element_; }
139  T* end() { return &element_ + 1; }
140  size_t size() const { return 1; }
141  };
142 
143  BOOST_CONCEPT_ASSERT((boost::RandomAccessRangeConcept<ListOfOneContainer<int> >));
144 
146  template<typename T>
147  ListOfOneContainer<T> ListOfOne(const T& element) {
148  return ListOfOneContainer<T>(element);
149  }
150 
151  /* ************************************************************************* */
153  template<class DERIVED>
155 #ifdef GTSAM_USE_TBB
156  public tbb::tbb_exception
157 #else
158  public std::exception
159 #endif
160  {
161 #ifdef GTSAM_USE_TBB
162  private:
163  typedef tbb::tbb_exception Base;
164  protected:
165  typedef std::basic_string<char, std::char_traits<char>, tbb::tbb_allocator<char> > String;
166 #else
167  private:
168  typedef std::exception Base;
169  protected:
170  typedef std::string String;
171 #endif
172 
173  protected:
174  bool dynamic_;
175  mutable boost::optional<String> description_;
176 
179 
181  ThreadsafeException(const ThreadsafeException& other) : Base(other), dynamic_(false) {}
182 
184  ThreadsafeException(const std::string& description) : dynamic_(false), description_(String(description.begin(), description.end())) {}
185 
187  virtual ~ThreadsafeException() throw () {}
188 
189  public:
190  // Implement functions for tbb_exception
191 #ifdef GTSAM_USE_TBB
192  virtual tbb::tbb_exception* move() throw () {
193  void* cloneMemory = scalable_malloc(sizeof(DERIVED));
194  if (!cloneMemory) {
195  std::cerr << "Failed allocating memory to copy thrown exception, exiting now." << std::endl;
196  exit(-1);
197  }
198  DERIVED* clone = ::new(cloneMemory) DERIVED(static_cast<DERIVED&>(*this));
199  clone->dynamic_ = true;
200  return clone;
201  }
202 
203  virtual void destroy() throw() {
204  if (dynamic_) {
205  DERIVED* derivedPtr = static_cast<DERIVED*>(this);
206  derivedPtr->~DERIVED();
207  scalable_free(derivedPtr);
208  }
209  }
210 
211  virtual void throw_self() {
212  throw *static_cast<DERIVED*>(this); }
213 
214  virtual const char* name() const throw() {
215  return typeid(DERIVED).name(); }
216 #endif
217 
218  virtual const char* what() const throw() {
219  return description_ ? description_->c_str() : ""; }
220  };
221 
222  /* ************************************************************************* */
224  class RuntimeErrorThreadsafe : public ThreadsafeException<RuntimeErrorThreadsafe>
225  {
226  public:
228  RuntimeErrorThreadsafe(const std::string& description) : ThreadsafeException<RuntimeErrorThreadsafe>(description) {}
229  };
230 
231  /* ************************************************************************* */
233  class OutOfRangeThreadsafe : public ThreadsafeException<OutOfRangeThreadsafe>
234  {
235  public:
237  OutOfRangeThreadsafe(const std::string& description) : ThreadsafeException<OutOfRangeThreadsafe>(description) {}
238  };
239 
240  /* ************************************************************************* */
242  class InvalidArgumentThreadsafe : public ThreadsafeException<InvalidArgumentThreadsafe>
243  {
244  public:
246  InvalidArgumentThreadsafe(const std::string& description) : ThreadsafeException<InvalidArgumentThreadsafe>(description) {}
247  };
248 
249  /* ************************************************************************* */
250 #ifdef __clang__
251 # pragma clang diagnostic push
252 # pragma clang diagnostic ignored "-Wunused-private-field" // Clang complains that previousOpenMPThreads is unused in the #else case below
253 #endif
254 
259  {
260  int previousOpenMPThreads;
261 
262  public:
263 #if defined GTSAM_USE_TBB && defined GTSAM_USE_EIGEN_MKL_OPENMP
265  previousOpenMPThreads(omp_get_num_threads())
266  {
267  omp_set_num_threads(omp_get_num_procs() / 4);
268  }
269 
271  {
272  omp_set_num_threads(previousOpenMPThreads);
273  }
274 #else
275  TbbOpenMPMixedScope() : previousOpenMPThreads(-1) {}
276  ~TbbOpenMPMixedScope() {}
277 #endif
278  };
279 
280 #ifdef __clang__
281 # pragma clang diagnostic pop
282 #endif
283 
284 }
285 
286 /* ************************************************************************* */
289 #ifdef NDEBUG
290 #define assert_throw(CONDITION, EXCEPTION) ((void)0)
291 #else
292 #define assert_throw(CONDITION, EXCEPTION) \
293  if (!(CONDITION)) { \
294  throw (EXCEPTION); \
295  }
296 #endif
297 
298 #ifdef _MSC_VER
299 
300 // Define some common g++ functions and macros we use that MSVC does not have
301 
302 #if (_MSC_VER < 1800)
303 
304 #include <boost/math/special_functions/fpclassify.hpp>
305 namespace std {
306  template<typename T> inline int isfinite(T a) {
307  return (int)boost::math::isfinite(a); }
308  template<typename T> inline int isnan(T a) {
309  return (int)boost::math::isnan(a); }
310  template<typename T> inline int isinf(T a) {
311  return (int)boost::math::isinf(a); }
312 }
313 
314 #endif
315 
316 #include <boost/math/constants/constants.hpp>
317 #ifndef M_PI
318 #define M_PI (boost::math::constants::pi<double>())
319 #endif
320 #ifndef M_PI_2
321 #define M_PI_2 (boost::math::constants::pi<double>() / 2.0)
322 #endif
323 #ifndef M_PI_4
324 #define M_PI_4 (boost::math::constants::pi<double>() / 4.0)
325 #endif
326 
327 #endif
328 
329 #ifdef min
330 #undef min
331 #endif
332 
333 #ifdef max
334 #undef max
335 #endif
336 
337 #ifdef ERROR
338 #undef ERROR
339 #endif
Threadsafe runtime error exception.
Definition: types.h:233
ThreadsafeException(const ThreadsafeException &other)
Copy constructor is protected - may only be created from derived classes.
Definition: types.h:181
RuntimeErrorThreadsafe(const std::string &description)
Construct with a string describing the exception.
Definition: types.h:228
A helper class that behaves as a container with one element, and works with boost::range.
Definition: types.h:129
Base exception type that uses tbb_exception if GTSAM is compiled with TBB.
Definition: types.h:154
ThreadsafeException(const std::string &description)
Construct with description string.
Definition: types.h:184
Helper class that uses templates to select between two types based on whether TEST_TYPE is const or n...
Definition: types.h:84
Threadsafe invalid argument exception.
Definition: types.h:242
const T & operator*() const
Operator to access the value.
Definition: types.h:119
Threadsafe runtime error exception.
Definition: types.h:224
size_t Key
Integer nonlinear key type.
Definition: types.h:59
T & operator*()
Operator to access the value.
Definition: types.h:116
ValueWithDefault()
Default constructor, initialize to default value supplied in template argument.
Definition: types.h:110
ptrdiff_t DenseIndex
The index type for Eigen objects.
Definition: types.h:74
bool dynamic_
Whether this object was moved.
Definition: types.h:174
InvalidArgumentThreadsafe(const std::string &description)
Construct with a string describing the exception.
Definition: types.h:246
boost::optional< String > description_
Optional description.
Definition: types.h:175
Helper struct that encapsulates a value with a default, this is just used as a member object so you d...
Definition: types.h:106
virtual ~ThreadsafeException()
Default destructor doesn't have the throw()
Definition: types.h:187
ValueWithDefault(const T &_value)
Initialize to the given value.
Definition: types.h:113
OutOfRangeThreadsafe(const std::string &description)
Construct with a string describing the exception.
Definition: types.h:237
ThreadsafeException()
Default constructor is protected - may only be created from derived classes.
Definition: types.h:178
boost::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: types.h:62
ListOfOneContainer< T > ListOfOne(const T &element)
Factory function for ListOfOneContainer to enable ListOfOne(e) syntax.
Definition: types.h:147
An object whose scope defines a block where TBB and OpenMP parallelism are mixed. ...
Definition: types.h:258