Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #ifndef ADUMP_COMMON_CONTEXT_GUARD_H
12 : #define ADUMP_COMMON_CONTEXT_GUARD_H
13 : #include <functional>
14 :
15 : namespace Adx {
16 : using OnExitFn = std::function<void()>;
17 :
18 : class ContextGuard {
19 : public:
20 182 : explicit ContextGuard(const OnExitFn &onExit) : onExit_(onExit) {}
21 182 : ~ContextGuard()
22 : {
23 182 : if (onExit_ != nullptr) {
24 : try {
25 182 : onExit_();
26 0 : } catch (std::bad_function_call &) {
27 : // just catch exception
28 0 : } catch (...) {
29 : // just catch exception
30 0 : }
31 : }
32 182 : }
33 :
34 : private:
35 : OnExitFn onExit_;
36 : };
37 :
38 : #define MAKE_CONTEXT_GUARD(var, cb) const ::Adx::ContextGuard ctxGuard_##var __attribute__((unused))(cb)
39 : } // namespace Adx
40 : #endif // ADUMP_COMMON_CONTEXT_GUARD_H
|