XRTraits C++ OpenXR Utilities
BaseExceptions.h
Go to the documentation of this file.
1 // Copyright 2018-2019, Collabora, Ltd.
2 // SPDX-License-Identifier: BSL-1.0
3 /*!
4  * @file
5  * @brief Header
6  * @author Ryan Pavlik <ryan.pavlik@collabora.com>
7  */
8 
9 #pragma once
10 
11 #ifdef XRTRAITS_USE_EXCEPTIONS
12 
13 // Internal Includes
14 // - none
15 
16 // Library Includes
17 #include <openxr/openxr.h>
18 
19 // Standard Includes
20 #include <exception> // IWYU pragma: export
21 #include <string>
22 
23 namespace xrtraits {
24 namespace exceptions {
25 
26  /*!
27  * Base class for all exceptions explicitly thrown by this
28  * implementation's code.
29  *
30  * One constructor permits association of a suggested XrResult code,
31  * used in XRTRAITS_CATCH_FALLBACK.
32  *
33  * @note Do not instantiate directly - use exceptions::xr_logic_error or
34  * exceptions::xr_runtime_error, or a more specific exception if one
35  * exists.
36  *
37  * @ingroup exceptions
38  */
39  class xr_exception : std::exception
40  {
41  public:
42  /*!
43  * A human-readable string describing the exception.
44  */
45  const char* what() const noexcept override;
46 
47  /*!
48  * @return true if an XrResult code was passed when creating
49  * this exception.
50  */
51  bool hasResult() const noexcept;
52 
53  /*!
54  * Access the suggested/associated XrResult.
55  * If none was supplied at creation, it returns the default,
56  * XR_ERROR_RUNTIME_FAILURE.
57  */
58  XrResult result() const noexcept;
59 
60  protected:
61  /*!
62  * Protected constructor (without a result code) for use by
63  * derived classes.
64  */
65  xr_exception(std::string&& what);
66 
67  /*!
68  * Protected constructor (with a result code) for use by derived
69  * classes.
70  */
71  xr_exception(std::string const& what, XrResult err);
72 
73  /*!
74  * Protected constructor (with a result code) for use by derived
75  * classes.
76  */
77  xr_exception(std::string&& what, XrResult err);
78 
79  private:
80  std::string what_;
81  XrResult result_ = XR_ERROR_RUNTIME_FAILURE;
82  bool hasResult_ = false;
83  };
84 
85  /*!
86  * Base class for OpenXR-related exceptions explicitly thrown by this
87  * library's code.
88  *
89  * Conceptually related to std::runtime_error, though not deriving from
90  * it. May specify an associated/recommended XrResult code.
91  *
92  * @note Prefer instantiating derived classes, if a suitable one exists,
93  * instead of instantiating this class directly.
94  *
95  * @ingroup exceptions
96  */
98  {
99  public:
100  /*!
101  * Constructor taking a "what" string as well as an XrResult.
102  */
103  xr_runtime_error(std::string&& what, XrResult err);
104 
105  /*!
106  * Constructor taking a "what" string as well as an XrResult.
107  */
108  xr_runtime_error(std::string const& what, XrResult err);
109 
110  /*!
111  * Constructor taking a "what" string
112  */
113  xr_runtime_error(std::string&& what);
114 
115  /*!
116  * Constructor taking an XrResult (which will be included in the
117  * "what" string), a condition, and a message.
118  *
119  * Primarily used by throwIfNotSucceeded() and
120  * throwIfNotUnqualifiedSuccess()
121  */
122  xr_runtime_error(XrResult err, const char* condition,
123  const char* msg);
124  };
125 
126  /*!
127  * Throw an xr_runtime_error with the given message if the result fails
128  * XR_SUCCEEDED.
129  *
130  * Particularly useful for application-side utilities to allow
131  * non-XrResult return values in the usual case.
132  *
133  * @see throwIfNotUnqualifiedSuccess
134  */
135  void throwIfNotSucceeded(XrResult result, const char* errorMessage);
136 
137  /*!
138  * Throw an xr_runtime_error with the given message if the result fails
139  * XR_UNQUALIFIED_SUCCESS.
140  *
141  * That is, if result is not exactly XR_SUCCESS.
142  *
143  * Particularly useful for application-side utilities to allow
144  * non-XrResult return values in the usual case.
145  *
146  * @see throwIfNotUnqualifiedSuccess
147  */
148  void throwIfNotUnqualifiedSuccess(XrResult result,
149  const char* errorMessage);
150 
151  /*!
152  * Base class for "logic" exceptions explicitly thrown by this library's
153  * code.
154  *
155  * Conceptually related to std::logic_error, though not deriving from
156  * it. May specify an associated/recommended XrResult code.
157  *
158  * @note Prefer instantiating derived classes, instead of instantiating
159  * this class directly.
160  *
161  * @ingroup exceptions
162  */
164  {
165  public:
166  /*!
167  * Constructor taking a "what" string as well as an XrResult.
168  */
169  xr_logic_error(std::string&& what, XrResult err);
170 
171  /*!
172  * Constructor taking a "what" string as well as an XrResult.
173  */
174  xr_logic_error(std::string const& what, XrResult err);
175 
176  /*!
177  * Constructor taking a "what" string.
178  */
179  xr_logic_error(std::string&& what);
180  };
181 
182 } // namespace exceptions
183 } // namespace xrtraits
184 
185 #endif // XRTRAITS_USE_EXCEPTIONS
Main namespace for these C++ OpenXR utilities.
Definition: GetChained.h:26
Definition: BaseExceptions.h:97
void throwIfNotUnqualifiedSuccess(XrResult result, const char *errorMessage)
Definition: BaseExceptions.h:163
XrResult result() const noexcept
xr_exception(std::string &&what)
const char * what() const noexcept override
Definition: BaseExceptions.h:39
bool hasResult() const noexcept
void throwIfNotSucceeded(XrResult result, const char *errorMessage)