1 /***************************************************************************
2 * Copyright (C) 2005-2019 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
23 #include "common-export.h"
26 #include <type_traits>
33 * Deleter for storing QObjects in STL containers and smart pointers
35 * QObject should always be deleted by calling deleteLater() on them, so the event loop can
36 * perform necessary cleanups.
38 struct DeferredDeleter
40 /// Deletes the given QObject
41 void operator()(QObject* object) const
44 object->deleteLater();
51 * Unique pointer for QObjects with deferred deletion
53 * @tparam T Type derived from QObject
56 using DeferredUniquePtr = std::unique_ptr<T, detail::DeferredDeleter>;
59 * Helper function for creating a DeferredUniquePtr
61 * An instance of T is created and returned in a DeferredUniquePtr, such that it will be deleted via
62 * QObject::deleteLater().
64 * @tparam T The type to create
65 * @tparam Args Constructor argument types
66 * @param[in] args Constructor arguments
67 * @returns A DeferredUniquePtr holding a new instance of T
69 template<typename T, typename... Args>
70 DeferredUniquePtr<T> makeDeferredUnique(Args... args)
72 static_assert(std::is_base_of<QObject, T>::value, "Type must inherit from QObject");
73 return DeferredUniquePtr<T>(new T(std::forward<Args>(args)...));
77 * Shared pointer for QObjects with deferred deletion
79 * @tparam T Type derived from QObject
82 using DeferredSharedPtr = std::shared_ptr<T>;
85 * Helper function for creating a DeferredSharedPtr
87 * An instance of T is created and returned in a DeferredSharedPtr, such that it will be deleted via
88 * QObject::deleteLater().
90 * @tparam T The type to create
91 * @tparam Args Constructor argument types
92 * @param[in] args Constructor arguments
93 * @returns A DeferredSharedPtr holding a new instance of T
95 template<typename T, typename... Args>
96 DeferredSharedPtr<T> makeDeferredShared(Args... args)
98 static_assert(std::is_base_of<QObject, T>::value, "Type must inherit from QObject");
99 return DeferredSharedPtr<T>(new T(std::forward<Args>(args)...), detail::DeferredDeleter{});