From 92084d06a5c87338cc988b5bc5868e617213e6b9 Mon Sep 17 00:00:00 2001 From: jbzdarkid Date: Sun, 24 Nov 2019 12:28:53 -0800 Subject: Try/catch, and select seed --- Source/MemoryException.h | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 Source/MemoryException.h (limited to 'Source/MemoryException.h') 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 @@ +#pragma once +#include +#include +#include + +#define MEMORY_CATCH(expr) \ +try { \ + (expr); \ +} catch (MemoryException exc) { \ + MemoryException::HandleException(exc); \ +} \ +do {} while (0) + +#define MEMORY_THROW(...) throw MemoryException(__func__, __LINE__, ##__VA_ARGS__); + +class MemoryException : public std::exception { +public: + inline MemoryException(const char* func, int32_t line, const char* message) noexcept + : MemoryException(func, line, message, {}, 0) {} + inline MemoryException(const char* func, int32_t line, const char* message, const std::vector& offsets) noexcept + : MemoryException(func, line, message, offsets, 0) {} + inline MemoryException(const char* func, int32_t line, const char* message, const std::vector& offsets, size_t numItems) noexcept + : _func(func), _line(line), _message(message), _offsets(offsets), _numItems(numItems) {} + + ~MemoryException() = default; + inline const char* what() const noexcept { + return _message; + } + static void HandleException(const MemoryException& exc) noexcept { + std::string msg = "MemoryException thrown in function "; + msg += exc._func; + msg += " on line " + std::to_string(exc._line) + ":\n" + exc._message + "\nOffsets:"; + for (int offset : exc._offsets) { + msg += " " + std::to_string(offset); + } + msg += "\n"; + if (exc._numItems != 0) { + msg += "Num Items: " + std::to_string(exc._numItems) + "\n"; + } + OutputDebugStringA(msg.c_str()); +#ifndef NDEBUG + MessageBoxA(NULL, msg.c_str(), "Memory Exception Thrown", MB_OK); +#endif + } + +private: + const char* _func; + int32_t _line; + const char* _message; + const std::vector _offsets; + size_t _numItems = 0; +}; + -- cgit 1.4.1