45 lines
1.1 KiB
C++
45 lines
1.1 KiB
C++
#pragma once
|
|
|
|
#include <exception>
|
|
#include <functional>
|
|
#include <vector>
|
|
|
|
namespace ddkit {
|
|
namespace utils {
|
|
namespace exceptions {
|
|
|
|
class GlobalHandler {
|
|
|
|
public:
|
|
template<typename Exception> static void registerHandler(std::function<void(Exception&)> handler){
|
|
instance().m_registeredHandlers.push_back([handler](std::exception_ptr exceptionPtr){
|
|
try {
|
|
std::rethrow_exception(exceptionPtr);
|
|
} catch (Exception& e) {
|
|
handler(e);
|
|
} catch (...){
|
|
// NO OP
|
|
}
|
|
});
|
|
}
|
|
|
|
static void registerHandler(std::function<void(std::exception_ptr)> handler){
|
|
instance().m_registeredHandlers.push_back(handler);
|
|
}
|
|
|
|
private:
|
|
std::terminate_handler m_previousHandler;
|
|
std::vector<std::function<void(std::exception_ptr)>> m_registeredHandlers;
|
|
|
|
GlobalHandler();
|
|
void handleException(const std::exception_ptr&);
|
|
|
|
static GlobalHandler& instance();
|
|
static void terminateHandler();
|
|
|
|
};
|
|
|
|
} // namespace exceptions
|
|
} // namespace utils
|
|
} // namespace ddkit
|