Debian Codegraph @

src/mpt/out_of_memory/out_of_memory.hpp

raw
1 /* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
2
3 #ifndef MPT_OUT_OF_MEMORY_OUT_OF_MEMORY_HPP
4 #define MPT_OUT_OF_MEMORY_OUT_OF_MEMORY_HPP
5
6
7
8 #include "mpt/base/detect.hpp"
9 #include "mpt/base/macros.hpp"
10 #include "mpt/base/namespace.hpp"
11 #include "mpt/detect/mfc.hpp"
12
13 #include <exception>
14 #if !MPT_DETECTED_MFC
15 #include <new>
16 #endif // !MPT_DETECTED_MFC
17
18 #if MPT_DETECTED_MFC
19 // cppcheck-suppress missingInclude
20 #include <afx.h>
21 #endif // MPT_DETECTED_MFC
22
23
24
25 namespace mpt {
26 inline namespace MPT_INLINE_NS {
27
28
29 // Exception handling helpers, because MFC requires explicit deletion of the exception object,
30 // Thus, always call exactly one of mpt::rethrow_out_of_memory(e) or mpt::delete_out_of_memory(e).
31
32 #if MPT_DETECTED_MFC
33
34 using out_of_memory = CMemoryException *;
35
36 [[noreturn]] inline void throw_out_of_memory() {
37 AfxThrowMemoryException();
38 }
39
40 [[noreturn]] inline void rethrow_out_of_memory(out_of_memory e) {
41 MPT_UNUSED(e);
42 // cppcheck false-positive
43 // cppcheck-suppress rethrowNoCurrentException
44 throw;
45 }
46
47 inline void delete_out_of_memory(out_of_memory & e) {
48 if (e) {
49 e->Delete();
50 e = nullptr;
51 }
52 }
53
54 #else // !MPT_DETECTED_MFC
55
56 using out_of_memory = const std::bad_alloc &;
57
58 [[noreturn]] inline void throw_out_of_memory() {
59 throw std::bad_alloc();
60 }
61
62 [[noreturn]] inline void rethrow_out_of_memory(out_of_memory e) {
63 MPT_UNUSED(e);
64 // cppcheck false-positive
65 // cppcheck-suppress rethrowNoCurrentException
66 throw;
67 }
68
69 inline void delete_out_of_memory(out_of_memory e) {
70 MPT_UNUSED(e);
71 }
72
73 #endif // MPT_DETECTED_MFC
74
75
76 } // namespace MPT_INLINE_NS
77 } // namespace mpt
78
79
80
81 #endif // MPT_OUT_OF_MEMORY_OUT_OF_MEMORY_HPP