All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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
13
14#include "pxr/pxr.h"
15#include "pxr/base/tf/api.h"
17#include "pxr/base/tf/functionRef.h"
18
19#include <cstdint>
20#include <exception>
21#include <string>
22#include <vector>
23
24PXR_NAMESPACE_OPEN_SCOPE
25
29{
30 explicit TfSkipCallerFrames(int n=0) : numToSkip(n) {}
31 int numToSkip;
32};
33
46class TfBaseException : public std::exception
47{
48public:
49 TF_API
50 virtual ~TfBaseException();
51
54 TF_API
55 explicit TfBaseException(std::string const &message);
56
60 TfCallContext const &GetThrowContext() const {
61 return _callContext;
62 }
63
66 std::vector<uintptr_t> const &GetThrowStack() const {
67 return _throwStack;
68 }
69
72 void MoveThrowStackTo(std::vector<uintptr_t> &out) {
73 out = std::move(_throwStack);
74 _throwStack.clear();
75 }
76
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
89private:
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.
102template <class Exception, class ... Args>
103void
104Tf_Throw(TfCallContext const &cc,
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.
113template <class Exception, class ... Args>
114void Tf_Throw(TfCallContext const &cc, Args && ... args) {
115 Tf_Throw<Exception>(cc, TfSkipCallerFrames(), std::forward<Args>(args)...);
116}
117
118#ifdef doxygen
119
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
136PXR_NAMESPACE_CLOSE_SCOPE
137
138#endif // PXR_BASE_TF_EXCEPTION_H
Functions for recording call locations.
The base class for exceptions supported by the Tf exceptions facilities.
Definition: exception.h:47
void MoveThrowStackTo(std::vector< uintptr_t > &out)
Move the stack frame pointers from the throw point to out.
Definition: exception.h:72
TF_API TfBaseException(std::string const &message)
Construct with message, reported by this class's what() implementation.
virtual TF_API const char * what() const noexcept override
Override std::exception::what() to return the message passed during construction.
TfCallContext const & GetThrowContext() const
Return the call context from the throw point associated with this exception.
Definition: exception.h:60
std::vector< uintptr_t > const & GetThrowStack() const
Return the stack frame pointers from the throw point.
Definition: exception.h:66
This class provides a non-owning reference to a type-erased callable object with a specified signatur...
Definition: functionRef.h:19
STL namespace.
This structure is used to indicate that some number of caller frames should be skipped when capturing...
Definition: exception.h:29