gtsam  4.0.0
gtsam
ThreadsafeException.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/config.h> // for GTSAM_USE_TBB
23 
24 #include <boost/optional/optional.hpp>
25 #include <string>
26 #include <typeinfo>
27 
28 #ifdef GTSAM_USE_TBB
29 #include <tbb/tbb_allocator.h>
30 #include <tbb/tbb_exception.h>
31 #include <tbb/scalable_allocator.h>
32 #include <iostream>
33 #endif
34 
35 namespace gtsam {
36 
38 template<class DERIVED>
40 #ifdef GTSAM_USE_TBB
41  public tbb::tbb_exception
42 #else
43 public std::exception
44 #endif
45 {
46 #ifdef GTSAM_USE_TBB
47 private:
48  typedef tbb::tbb_exception Base;
49 protected:
50  typedef std::basic_string<char, std::char_traits<char>,
51  tbb::tbb_allocator<char> > String;
52 #else
53 private:
54  typedef std::exception Base;
55 protected:
56  typedef std::string String;
57 #endif
58 
59 protected:
60  bool dynamic_;
61  mutable boost::optional<String> description_;
62 
65  dynamic_(false) {
66  }
67 
70  Base(other), dynamic_(false) {
71  }
72 
74  ThreadsafeException(const std::string& description) :
75  dynamic_(false), description_(
76  String(description.begin(), description.end())) {
77  }
78 
80  virtual ~ThreadsafeException() throw () {
81  }
82 
83 public:
84  // Implement functions for tbb_exception
85 #ifdef GTSAM_USE_TBB
86  virtual tbb::tbb_exception* move() throw () {
87  void* cloneMemory = scalable_malloc(sizeof(DERIVED));
88  if (!cloneMemory) {
89  std::cerr << "Failed allocating memory to copy thrown exception, exiting now." << std::endl;
90  exit(-1);
91  }
92  DERIVED* clone = ::new(cloneMemory) DERIVED(static_cast<DERIVED&>(*this));
93  clone->dynamic_ = true;
94  return clone;
95  }
96 
97  virtual void destroy() throw () {
98  if (dynamic_) {
99  DERIVED* derivedPtr = static_cast<DERIVED*>(this);
100  derivedPtr->~DERIVED();
101  scalable_free(derivedPtr);
102  }
103  }
104 
105  virtual void throw_self() {
106  throw *static_cast<DERIVED*>(this);
107  }
108 
109  virtual const char* name() const throw () {
110  return typeid(DERIVED).name();
111  }
112 #endif
113 
114  virtual const char* what() const throw () {
115  return description_ ? description_->c_str() : "";
116  }
117 };
118 
120 class RuntimeErrorThreadsafe: public ThreadsafeException<RuntimeErrorThreadsafe> {
121 public:
123  RuntimeErrorThreadsafe(const std::string& description) :
125  }
126 };
127 
129 class OutOfRangeThreadsafe: public ThreadsafeException<OutOfRangeThreadsafe> {
130 public:
132  OutOfRangeThreadsafe(const std::string& description) :
134  }
135 };
136 
139  InvalidArgumentThreadsafe> {
140 public:
142  InvalidArgumentThreadsafe(const std::string& description) :
144  }
145 };
146 
148 class CholeskyFailed : public gtsam::ThreadsafeException<CholeskyFailed>
149 {
150 public:
151  CholeskyFailed() throw() {}
152  virtual ~CholeskyFailed() throw() {}
153 };
154 
155 } // namespace gtsam
ThreadsafeException(const std::string &description)
Construct with description string.
Definition: ThreadsafeException.h:74
Thread-safe invalid argument exception.
Definition: ThreadsafeException.h:138
Base exception type that uses tbb_exception if GTSAM is compiled with TBB.
Definition: ThreadsafeException.h:39
InvalidArgumentThreadsafe(const std::string &description)
Construct with a string describing the exception.
Definition: ThreadsafeException.h:142
OutOfRangeThreadsafe(const std::string &description)
Construct with a string describing the exception.
Definition: ThreadsafeException.h:132
Thread-safe out of range exception.
Definition: ThreadsafeException.h:129
Indicate Cholesky factorization failure.
Definition: ThreadsafeException.h:148
bool dynamic_
Whether this object was moved.
Definition: ThreadsafeException.h:60
ThreadsafeException(const ThreadsafeException &other)
Copy constructor is protected - may only be created from derived classes.
Definition: ThreadsafeException.h:69
virtual ~ThreadsafeException()
Default destructor doesn't have the throw()
Definition: ThreadsafeException.h:80
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
boost::optional< String > description_
Optional description.
Definition: ThreadsafeException.h:61
Thread-safe runtime error exception.
Definition: ThreadsafeException.h:120
ThreadsafeException()
Default constructor is protected - may only be created from derived classes.
Definition: ThreadsafeException.h:64
RuntimeErrorThreadsafe(const std::string &description)
Construct with a string describing the exception.
Definition: ThreadsafeException.h:123