Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:02:49

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 <catch2/internal/catch_platform.hpp>
0012 #include <catch2/internal/catch_compiler_capabilities.hpp>
0013 
0014 #include <cassert>
0015 
0016 namespace Catch {
0017 
0018     /**
0019      * Wrapper for platform-specific fatal error (signals/SEH) handlers
0020      *
0021      * Tries to be cooperative with other handlers, and not step over
0022      * other handlers. This means that unknown structured exceptions
0023      * are passed on, previous signal handlers are called, and so on.
0024      *
0025      * Can only be instantiated once, and assumes that once a signal
0026      * is caught, the binary will end up terminating. Thus, there
0027      */
0028     class FatalConditionHandler {
0029         bool m_started = false;
0030 
0031         // Install/disengage implementation for specific platform.
0032         // Should be if-defed to work on current platform, can assume
0033         // engage-disengage 1:1 pairing.
0034         void engage_platform();
0035         void disengage_platform() noexcept;
0036     public:
0037         // Should also have platform-specific implementations as needed
0038         FatalConditionHandler();
0039         ~FatalConditionHandler();
0040 
0041         void engage() {
0042             assert(!m_started && "Handler cannot be installed twice.");
0043             m_started = true;
0044             engage_platform();
0045         }
0046 
0047         void disengage() noexcept {
0048             assert(m_started && "Handler cannot be uninstalled without being installed first");
0049             m_started = false;
0050             disengage_platform();
0051         }
0052     };
0053 
0054     //! Simple RAII guard for (dis)engaging the FatalConditionHandler
0055     class FatalConditionHandlerGuard {
0056         FatalConditionHandler* m_handler;
0057     public:
0058         FatalConditionHandlerGuard(FatalConditionHandler* handler):
0059             m_handler(handler) {
0060             m_handler->engage();
0061         }
0062         ~FatalConditionHandlerGuard() {
0063             m_handler->disengage();
0064         }
0065     };
0066 
0067 } // end namespace Catch
0068 
0069 #endif // CATCH_FATAL_CONDITION_HANDLER_HPP_INCLUDED