Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 09:08:51

0001 
0002 //              Copyright Catch2 Authors
0003 // Distributed under the Boost Software License, Version 1.0.
0004 //   (See accompanying file LICENSE.txt or copy at
0005 //        https://www.boost.org/LICENSE_1_0.txt)
0006 
0007 // SPDX-License-Identifier: BSL-1.0
0008 #ifndef CATCH_FATAL_CONDITION_HANDLER_HPP_INCLUDED
0009 #define CATCH_FATAL_CONDITION_HANDLER_HPP_INCLUDED
0010 
0011 #include <cassert>
0012 
0013 namespace Catch {
0014 
0015     /**
0016      * Wrapper for platform-specific fatal error (signals/SEH) handlers
0017      *
0018      * Tries to be cooperative with other handlers, and not step over
0019      * other handlers. This means that unknown structured exceptions
0020      * are passed on, previous signal handlers are called, and so on.
0021      *
0022      * Can only be instantiated once, and assumes that once a signal
0023      * is caught, the binary will end up terminating. Thus, there
0024      */
0025     class FatalConditionHandler {
0026         bool m_started = false;
0027 
0028         // Install/disengage implementation for specific platform.
0029         // Should be if-defed to work on current platform, can assume
0030         // engage-disengage 1:1 pairing.
0031         void engage_platform();
0032         void disengage_platform() noexcept;
0033     public:
0034         // Should also have platform-specific implementations as needed
0035         FatalConditionHandler();
0036         ~FatalConditionHandler();
0037 
0038         void engage() {
0039             assert(!m_started && "Handler cannot be installed twice.");
0040             m_started = true;
0041             engage_platform();
0042         }
0043 
0044         void disengage() noexcept {
0045             assert(m_started && "Handler cannot be uninstalled without being installed first");
0046             m_started = false;
0047             disengage_platform();
0048         }
0049     };
0050 
0051     //! Simple RAII guard for (dis)engaging the FatalConditionHandler
0052     class FatalConditionHandlerGuard {
0053         FatalConditionHandler* m_handler;
0054     public:
0055         FatalConditionHandlerGuard(FatalConditionHandler* handler):
0056             m_handler(handler) {
0057             m_handler->engage();
0058         }
0059         ~FatalConditionHandlerGuard() {
0060             m_handler->disengage();
0061         }
0062     };
0063 
0064 } // end namespace Catch
0065 
0066 #endif // CATCH_FATAL_CONDITION_HANDLER_HPP_INCLUDED