about summary refs log tree commit diff stats
path: root/Source/MemoryException.h
diff options
context:
space:
mode:
Diffstat (limited to 'Source/MemoryException.h')
-rw-r--r--Source/MemoryException.h53
1 files changed, 53 insertions, 0 deletions
diff --git a/Source/MemoryException.h b/Source/MemoryException.h new file mode 100644 index 0000000..ad2824d --- /dev/null +++ b/Source/MemoryException.h
@@ -0,0 +1,53 @@
1#pragma once
2#include <exception>
3#include <string>
4#include <vector>
5
6#define MEMORY_CATCH(expr) \
7try { \
8 (expr); \
9} catch (MemoryException exc) { \
10 MemoryException::HandleException(exc); \
11} \
12do {} while (0)
13
14#define MEMORY_THROW(...) throw MemoryException(__func__, __LINE__, ##__VA_ARGS__);
15
16class MemoryException : public std::exception {
17public:
18 inline MemoryException(const char* func, int32_t line, const char* message) noexcept
19 : MemoryException(func, line, message, {}, 0) {}
20 inline MemoryException(const char* func, int32_t line, const char* message, const std::vector<int>& offsets) noexcept
21 : MemoryException(func, line, message, offsets, 0) {}
22 inline MemoryException(const char* func, int32_t line, const char* message, const std::vector<int>& offsets, size_t numItems) noexcept
23 : _func(func), _line(line), _message(message), _offsets(offsets), _numItems(numItems) {}
24
25 ~MemoryException() = default;
26 inline const char* what() const noexcept {
27 return _message;
28 }
29 static void HandleException(const MemoryException& exc) noexcept {
30 std::string msg = "MemoryException thrown in function ";
31 msg += exc._func;
32 msg += " on line " + std::to_string(exc._line) + ":\n" + exc._message + "\nOffsets:";
33 for (int offset : exc._offsets) {
34 msg += " " + std::to_string(offset);
35 }
36 msg += "\n";
37 if (exc._numItems != 0) {
38 msg += "Num Items: " + std::to_string(exc._numItems) + "\n";
39 }
40 OutputDebugStringA(msg.c_str());
41#ifndef NDEBUG
42 MessageBoxA(NULL, msg.c_str(), "Memory Exception Thrown", MB_OK);
43#endif
44 }
45
46private:
47 const char* _func;
48 int32_t _line;
49 const char* _message;
50 const std::vector<int> _offsets;
51 size_t _numItems = 0;
52};
53