Blame view

src/scopeguard.h 994 Bytes
7ba6afb5   Steven de Ridder   Initial commit. d...
1
2
3
4
5
6
7
8
  #ifndef OSDEV_COMPONENTS_SCOPEGUARD_H
  #define OSDEV_COMPONENTS_SCOPEGUARD_H
  
  #include <functional>
  
  namespace osdev {
  namespace components {
  
0288e1ab   Peter M. Groen   Fixed Loggin
9
  using CleanUpFunction = std::function<void()>;
7ba6afb5   Steven de Ridder   Initial commit. d...
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  
  /**
   * @brief Ensures that a cleanup function is called at the end of the current scope.
   */
  class ScopeGuard
  {
  public:
  
      /**
       * @brief   Constructs a RAII instance that will call cleanupFunc in it's destructor.
       * @param   cleanupFunc The cleanup function to call at the end of the current scope.
       *          This cleanup function must not throw exceptions. If it does, the behaviour
       *          is undefined.
       */
      ScopeGuard( const CleanUpFunction& cleanupFunc );
  
      // not copyable
      ScopeGuard( const ScopeGuard& ) = delete;
      ScopeGuard& operator=( const ScopeGuard& ) = delete;
  
      ~ScopeGuard() noexcept;
  
  private:
      CleanUpFunction m_cleanupFunc;
  };
  
  }   /* End namespace components */
  }   /* End namespace osdev */
  
  #endif  /* OSDEV_COMPONENTS_SCOPEGUARD_H */