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:
31 
32  destruction_callback() noexcept = default;
33 
34  /**
35  @brief destruction_callback object constructor
36  @tparam destroyer_t - callable type
37  @param destroyer - functor that will be called when object is destroyed
38  **/
39  template<class destroyer_t>
40  destruction_callback(destroyer_t destroyer);
41 
43 
44  destruction_callback& operator=(destruction_callback&& other) noexcept;
45 
47 
48 private:
49  destroyer_type m_Destroyer;
50 };
51 
52 inline destruction_callback::destruction_callback(destruction_callback&& other) noexcept
53 {
54  std::swap(m_Destroyer, other.m_Destroyer);
55 }
56 
57 inline destruction_callback& destruction_callback::operator=(destruction_callback&& other) noexcept
58 {
59  std::swap(m_Destroyer, other.m_Destroyer);
60  return *this;
61 }
62 
63 inline destruction_callback::~destruction_callback()
64 {
65  if (m_Destroyer)
66  m_Destroyer();
67 }
68 
69 template<class destroyer_t>
70 destruction_callback::destruction_callback(destroyer_t destroyer) : m_Destroyer(std::move(destroyer))
71 {
72 }
73 
74 } // namespace qx
Class for RAII: functor passed in constructor will be called in destructor.
#define QX_NONCOPYABLE(className)
Define class as non copyable.