modernize: Pass arguments by value and move in constructors
[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 "common-export.h"
24
25 #include <memory>
26 #include <type_traits>
27
28 #include <QObject>
29
30 namespace detail {
31
32 /**
33  * Deleter for storing QObjects in STL containers and smart pointers
34  *
35  * QObject should always be deleted by calling deleteLater() on them, so the event loop can
36  * perform necessary cleanups.
37  */
38 struct DeferredDeleter {
39     /// Deletes the given QObject
40     void operator()(QObject *object) const {
41         if (object)
42             object->deleteLater();
43     }
44 };
45
46 } // detail
47
48 /**
49  * Unique pointer for QObjects with deferred deletion
50  *
51  * @tparam T Type derived from QObject
52  */
53 template<typename T>
54 using DeferredUniquePtr = std::unique_ptr<T, detail::DeferredDeleter>;
55
56
57 /**
58  * Helper function for creating a DeferredUniquePtr
59  *
60  * An instance of T is created and returned in a DeferredUniquePtr, such that it will be deleted via
61  * QObject::deleteLater().
62  *
63  * @tparam T       The type to create
64  * @tparam Args    Constructor argument types
65  * @param[in] args Constructor arguments
66  * @returns A DeferredUniquePtr holding a new instance of T
67  */
68 template<typename T, typename ...Args>
69 DeferredUniquePtr<T> makeDeferredUnique(Args... args)
70 {
71     static_assert(std::is_base_of<QObject, T>::value, "Type must inherit from QObject");
72     return DeferredUniquePtr<T>(new T(std::forward<Args>(args)...));
73 }
74
75
76 /**
77  * Shared pointer for QObjects with deferred deletion
78  *
79  * @tparam T Type derived from QObject
80  */
81 template<typename T>
82 using DeferredSharedPtr = std::shared_ptr<T>;
83
84
85 /**
86  * Helper function for creating a DeferredSharedPtr
87  *
88  * An instance of T is created and returned in a DeferredSharedPtr, such that it will be deleted via
89  * QObject::deleteLater().
90  *
91  * @tparam T       The type to create
92  * @tparam Args    Constructor argument types
93  * @param[in] args Constructor arguments
94  * @returns A DeferredSharedPtr holding a new instance of T
95  */
96 template<typename T, typename ...Args>
97 DeferredSharedPtr<T> makeDeferredShared(Args... args)
98 {
99     static_assert(std::is_base_of<QObject, T>::value, "Type must inherit from QObject");
100     return DeferredSharedPtr<T>(new T(std::forward<Args>(args)...), detail::DeferredDeleter{});
101 }