qa: Remove lots of superfluous semicolons
[quassel.git] / src / common / deferredptr.h
1 /***************************************************************************
2  *   Copyright (C) 2005-2018 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #pragma once
22
23 #include <memory>
24 #include <type_traits>
25
26 #include <QObject>
27
28 namespace detail {
29
30 /**
31  * Deleter for storing QObjects in STL containers and smart pointers
32  *
33  * QObject should always be deleted by calling deleteLater() on them, so the event loop can
34  * perform necessary cleanups.
35  */
36 struct DeferredDeleter {
37     /// Deletes the given QObject
38     void operator()(QObject *object) const {
39         if (object)
40             object->deleteLater();
41     }
42 };
43
44 } // detail
45
46 /**
47  * Unique pointer for QObjects with deferred deletion
48  *
49  * @tparam T Type derived from QObject
50  */
51 template<typename T>
52 using DeferredUniquePtr = std::unique_ptr<T, detail::DeferredDeleter>;
53
54
55 /**
56  * Helper function for creating a DeferredUniquePtr
57  *
58  * An instance of T is created and returned in a DeferredUniquePtr, such that it will be deleted via
59  * QObject::deleteLater().
60  *
61  * @tparam T       The type to create
62  * @tparam Args    Constructor argument types
63  * @param[in] args Constructor arguments
64  * @returns A DeferredUniquePtr holding a new instance of T
65  */
66 template<typename T, typename ...Args>
67 DeferredUniquePtr<T> makeDeferredUnique(Args... args)
68 {
69     static_assert(std::is_base_of<QObject, T>::value, "Type must inherit from QObject");
70     return DeferredUniquePtr<T>(new T(std::forward<Args>(args)...));
71 }
72
73
74 /**
75  * Shared pointer for QObjects with deferred deletion
76  *
77  * @tparam T Type derived from QObject
78  */
79 template<typename T>
80 using DeferredSharedPtr = std::shared_ptr<T>;
81
82
83 /**
84  * Helper function for creating a DeferredSharedPtr
85  *
86  * An instance of T is created and returned in a DeferredSharedPtr, such that it will be deleted via
87  * QObject::deleteLater().
88  *
89  * @tparam T       The type to create
90  * @tparam Args    Constructor argument types
91  * @param[in] args Constructor arguments
92  * @returns A DeferredSharedPtr holding a new instance of T
93  */
94 template<typename T, typename ...Args>
95 DeferredSharedPtr<T> makeDeferredShared(Args... args)
96 {
97     static_assert(std::is_base_of<QObject, T>::value, "Type must inherit from QObject");
98     return DeferredSharedPtr<T>(new T(std::forward<Args>(args)...), detail::DeferredDeleter{});
99 }