qxLib
destruction_callback.h
Go to the documentation of this file.
1 /**
2 
3  @file destruction_callback.h
4  @author Khrapov
5  @date 12.12.2021
6  @copyright © Nick Khrapov, 2021. All right reserved.
7 
8 **/
9 #pragma once
10 
12 
13 namespace qx
14 {
15 
16 /**
17 
18  @class destruction_callback
19  @brief Class for RAII: functor passed in constructor will be called in destructor
20  @author Khrapov
21  @date 12.12.2021
22 
23 **/
24 class [[nodiscard]] destruction_callback
25 {
26 public:
27  using destroyer_type = std::function<void()>;
28 
29 public:
32 
33  destruction_callback() noexcept = default;
34 
35  /**
36  @brief destruction_callback object constructor
37  @tparam destroyer_t - callable type
38  @param destroyer - functor that will be called when object is destroyed
39  **/
40  template<class destroyer_t>
41  destruction_callback(destroyer_t destroyer) : m_Destroyer(std::move(destroyer))
42  {
43  }
44 
46  {
47  if (m_Destroyer)
48  m_Destroyer();
49  }
50 
51 private:
52  destroyer_type m_Destroyer;
53 };
54 
55 } // namespace qx
Class for RAII: functor passed in constructor will be called in destructor.
destruction_callback(destroyer_t destroyer)
destruction_callback object constructor
#define QX_NONCOPYABLE(className)
Define class as non copyable.
#define QX_MOVABLE(className)
Define class as default movable.