HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
exception.h
Go to the documentation of this file.
1 //
2 // Copyright 2021 Pixar
3 //
4 // Licensed under the terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 
8 #ifndef PXR_BASE_TF_EXCEPTION_H
9 #define PXR_BASE_TF_EXCEPTION_H
10 
11 /// \file tf/exception.h
12 /// \ingroup group_tf_Diagnostic
13 
14 #include "pxr/pxr.h"
15 #include "pxr/base/tf/api.h"
18 
19 #include <cstdint>
20 #include <exception>
21 #include <string>
22 #include <vector>
23 
25 
26 /// This structure is used to indicate that some number of caller frames should
27 /// be skipped when capturing exception stack traces at the throw point.
29 {
30  explicit TfSkipCallerFrames(int n=0) : numToSkip(n) {}
31  int numToSkip;
32 };
33 
34 /// The base class for exceptions supported by the Tf exceptions facilities.
35 /// Typical usage is to publically derive your own exception class from this
36 /// one, and throw using the TF_THROW() macro.
37 ///
38 /// Deriving this base class and throwing via TF_THROW() will record the throw
39 /// point's call context (see GetThrowContext()) and will also capture a portion
40 /// of the throwing thread's call stack (see GetThrowStack()).
41 ///
42 /// Additionally, the Tf library registers an exception translator with
43 /// boost.python to raise a Python exeption wrapping the thrown exception
44 /// object. Similarly utilties that call Python via Tf will re-throw the
45 /// embedded C++ exception if the Python exception unwinds back into C++.
46 class TfBaseException : public std::exception
47 {
48 public:
49  TF_API
50  virtual ~TfBaseException();
51 
52  /// Construct with \p message, reported by this class's what()
53  /// implementation.
54  TF_API
55  explicit TfBaseException(std::string const &message);
56 
57  /// Return the call context from the throw point associated with this
58  /// exception. Note that this context may be invalid if this exception was
59  /// not thrown with TF_THROW().
60  TfCallContext const &GetThrowContext() const {
61  return _callContext;
62  }
63 
64  /// Return the stack frame pointers from the throw point. See
65  /// ArchPrintStackFrames() to turn these into human-readable strings.
66  std::vector<uintptr_t> const &GetThrowStack() const {
67  return _throwStack;
68  }
69 
70  /// Move the stack frame pointers from the throw point to \p out. See
71  /// GetThrowStack() for more details.
72  void MoveThrowStackTo(std::vector<uintptr_t> &out) {
73  out = std::move(_throwStack);
74  _throwStack.clear();
75  }
76 
77  /// Override std::exception::what() to return the message passed during
78  /// construction.
79  TF_API
80  virtual const char *what() const noexcept override;
81 
82  // Friend throw support.
83  template <class Derived, class ... Args>
84  friend void
85  Tf_Throw(TfCallContext const &cc,
86  TfSkipCallerFrames skipFrames,
87  Args && ... args);
88 
89 private:
90  TF_API
91  static void _ThrowImpl(TfCallContext const &cc,
92  TfBaseException &exc,
93  TfFunctionRef<void ()> thrower,
94  int skipNCallerFrames);
95 
96  TfCallContext _callContext;
97  std::vector<uintptr_t> _throwStack;
98  std::string _message;
99 };
100 
101 // TF_THROW() support function.
102 template <class Exception, class ... Args>
103 void
105  TfSkipCallerFrames skipFrames,
106  Args && ... args) {
107  Exception exc(std::forward<Args>(args)...);
108  auto thrower = [&exc]() { throw std::move(exc); };
109  TfBaseException::_ThrowImpl(cc, exc, thrower, skipFrames.numToSkip);
110 }
111 
112 // TF_THROW() support function.
113 template <class Exception, class ... Args>
114 void Tf_Throw(TfCallContext const &cc, Args && ... args) {
115  Tf_Throw<Exception>(cc, TfSkipCallerFrames(), std::forward<Args>(args)...);
116 }
117 
118 #ifdef doxygen
119 
120 /// Construct an instance of Exception (which must derive TfBaseException) with
121 /// Exception-ctor-args and throw it. Also capture a portion of this thread's
122 /// current call stack and the throw point's source filename & line number to
123 /// embed in the exception. If the exception goes unhandled these will be
124 /// reported in the crash report that Tf's terminate handler generates, or in
125 /// the unhandled exception message in the python interpreter.
126 #define TF_THROW(Exception, Exception-ctor-args...)
127 #define TF_THROW(Exception, TfSkipCallerFrames, Exception-ctor-args...)
128 
129 #else
130 
131 #define TF_THROW(Exception, ...) \
132  Tf_Throw<Exception>(TF_CALL_CONTEXT, __VA_ARGS__)
133 
134 #endif
135 
137 
138 #endif // PXR_BASE_TF_EXCEPTION_H
GLuint GLsizei const GLchar * message
Definition: glcorearb.h:2543
#define TF_API
Definition: api.h:23
*get result *(waiting if necessary)*A common idiom is to fire a bunch of sub tasks at the and then *wait for them to all complete We provide a helper class
Definition: thread.h:632
uint128_t uintptr_t
Definition: format.h:479
class OCIOEXPORT Exception
TfCallContext const & GetThrowContext() const
Definition: exception.h:60
GLdouble n
Definition: glcorearb.h:2008
virtual TF_API const char * what() const noexceptoverride
virtual TF_API ~TfBaseException()
std::vector< uintptr_t > const & GetThrowStack() const
Definition: exception.h:66
TF_API TfBaseException(std::string const &message)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1425
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
**If you just want to fire and args
Definition: thread.h:618
friend void Tf_Throw(TfCallContext const &cc, TfSkipCallerFrames skipFrames, Args &&...args)
Definition: exception.h:104
TfSkipCallerFrames(int n=0)
Definition: exception.h:30
void MoveThrowStackTo(std::vector< uintptr_t > &out)
Definition: exception.h:72