AGX Dynamics  2.37.2.0
OnScopeExit.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or
9 having been advised so by Algoryx Simulation AB for a time limited evaluation,
10 or having purchased a valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 
18 #pragma once
19 
20 #include <agx/Logger.h>
21 #include <agx/version.h>
22 
23 namespace 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;
67  OnScopeExit& operator=(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>
87  OnScopeExit<Callback> onScopeExit(Callback&& callback)
88  {
89  return OnScopeExit<Callback>(std::forward<Callback>(callback));
90  }
91 }
#define LOGGER_ENDL()
Definition: Logger.h:27
STL namespace.
OnScopeExit(Callback &&callback)
Definition: OnScopeExit.h:56
OnScopeExit< Callback > onScopeExit(Callback &&callback)
Definition: OnScopeExit.h:87
OnScopeExit(OnScopeExit &&other)
Definition: OnScopeExit.h:61
OnScopeExit & operator=(const OnScopeExit &)=delete
#define LOGGER_ERROR()
Definition: Logger.h:22
The agxUtil namespace contain classes and methods for utility functionality.
Register a callback to be called at the end of the current scope.
Definition: OnScopeExit.h:53