AGX Dynamics 2.41.1.2
Loading...
Searching...
No Matches
OnScopeExit.h
Go to the documentation of this file.
1/*
2Copyright 2007-2025. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or
9having been advised so by Algoryx Simulation AB for a time limited evaluation,
10or having purchased a valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16
17
18#pragma once
19
20#include <agx/Logger.h>
21#include <agx/version.h>
22
23namespace agxUtil
24{
52 template <typename Callback>
54 {
55 public:
56 OnScopeExit(Callback&& callback)
57 : m_callback(std::move(callback))
58 {
59 }
60
62 : m_callback(std::move(other.m_callback))
63 {
64 }
65
66 OnScopeExit(const OnScopeExit&) = delete;
68
70 {
71 try
72 {
73 m_callback();
74 }
75 catch (...)
76 {
77 // What would be a sane thing to do here? Exceptions should not escape destructors.
78 LOGGER_ERROR() << "Exception thrown from a OnScopeExit callback. This is not allowed." << LOGGER_ENDL();
79 }
80 }
81
82 private:
83 Callback m_callback;
84 };
85
86 template <typename Callback>
88 {
89 return OnScopeExit<Callback>(std::forward<Callback>(callback));
90 }
91}
#define LOGGER_ERROR()
Definition: Logger.h:22
#define LOGGER_ENDL()
Definition: Logger.h:27
Register a callback to be called at the end of the current scope.
Definition: OnScopeExit.h:54
OnScopeExit(OnScopeExit &&other)
Definition: OnScopeExit.h:61
OnScopeExit(Callback &&callback)
Definition: OnScopeExit.h:56
OnScopeExit(const OnScopeExit &)=delete
OnScopeExit & operator=(const OnScopeExit &)=delete
The agxUtil namespace contain classes and methods for utility functionality.
OnScopeExit< Callback > onScopeExit(Callback &&callback)
Definition: OnScopeExit.h:87
STL namespace.