CuteHMI - CuteHMI (CuteHMI.2)
Initializer.hpp
1 #ifndef H_EXTENSIONS_CUTEHMI_2_INCLUDE_CUTEHMI_INITIALIZER_HPP
2 #define H_EXTENSIONS_CUTEHMI_2_INCLUDE_CUTEHMI_INITIALIZER_HPP
3 
4 #include "internal/common.hpp"
5 #include "NonCopyable.hpp"
6 
7 #include <functional>
8 
9 namespace cutehmi {
10 
30 template <class DERIVED>
32  public NonCopyable
33 {
34  public:
40  Initializer(std::function<void()> init, std::function<void()> deinit = nullptr);
41 
42  protected:
43  ~Initializer();
44 
45  private:
46  struct Members
47  {
48  std::function<void()> init;
49  std::function<void()> deinit;
50  };
51 
52  MPtr<Members> m;
53 
54  static QAtomicInt M_RefCtr;
55 };
56 
57 template <class DERIVED>
59 
60 template <class DERIVED>
62  m(new Members{init, deinit})
63 {
64  M_RefCtr.ref();
65  if (M_RefCtr == 1)
66  m->init();
67 }
68 
69 template <class DERIVED>
71 {
72  if (!M_RefCtr.deref()) {
73  if (m->deinit)
74  m->deinit();
75  }
76 }
77 
78 }
79 
80 #endif
81 
82 //(c)C: Copyright © 2019, Michał Policht <michal@policht.pl>. All rights reserved.
83 //(c)C: This file is a part of CuteHMI.
84 //(c)C: CuteHMI is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
85 //(c)C: CuteHMI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
86 //(c)C: You should have received a copy of the GNU Lesser General Public License along with CuteHMI. If not, see <https://www.gnu.org/licenses/>.
std::function
cutehmi::MPtr< Members >
cutehmi
Definition: constants.hpp:6
cutehmi::Initializer::Initializer
Initializer(std::function< void()> init, std::function< void()> deinit=nullptr)
Constructor.
Definition: Initializer.hpp:61
cutehmi::NonCopyable
Non-copyable object.
Definition: NonCopyable.hpp:9
QAtomicInt
cutehmi::Initializer::~Initializer
~Initializer()
Definition: Initializer.hpp:70
cutehmi::Initializer
Initializer template.
Definition: Initializer.hpp:31