DeleteAll.hpp
1 #pragma once
2 #include <utility>
3 namespace score
4 {
5 template <typename T>
7 {
8  T container;
9 
10  ~delete_later() noexcept
11  {
12  for(auto* e : container)
13  e->deleteLater();
14  }
15 };
16 
17 template <typename T>
18 void clearAndDeleteLater(T& container, delete_later<T>& storage) noexcept
19 {
20  auto tmp = std::move(container);
21  for(auto ptr : tmp)
22  ptr->setParent(nullptr);
23 
24  container.clear();
25 
26  storage.container.insert(storage.container.end(), tmp.begin(), tmp.end());
27 }
28 
29 template <typename T>
30 [[nodiscard]] auto clearAndDeleteLater(T& container) noexcept
31 {
32  auto tmp = std::move(container);
33  for(auto ptr : tmp)
34  ptr->setParent(nullptr);
35 
36  container.clear();
37 
38  // Note ! this code *requires* RVO, which is guaranteed
39  // starting from C++17 in this specific case.
40  // Don't put later in a variable instead !
41  return delete_later<T>{std::move(tmp)};
42 }
43 
44 }
Base toolkit upon which the software is built.
Definition: Application.cpp:90
Definition: DeleteAll.hpp:7