X-Git-Url: https://git.quassel-irc.org/?a=blobdiff_plain;f=src%2Fcommon%2Fdeferredptr.h;fp=src%2Fcommon%2Fdeferredptr.h;h=1ea1f6b41552da594b2541e280c38bdae8734e12;hb=46f20e19a343ad0fa17dde6c32f17e60fc682c9b;hp=0000000000000000000000000000000000000000;hpb=423e088804d243368074ab218b2bda2fff3303c9;p=quassel.git diff --git a/src/common/deferredptr.h b/src/common/deferredptr.h new file mode 100644 index 00000000..1ea1f6b4 --- /dev/null +++ b/src/common/deferredptr.h @@ -0,0 +1,99 @@ +/*************************************************************************** + * Copyright (C) 2005-2016 by the Quassel Project * + * devel@quassel-irc.org * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) version 3. * + * * + * This program 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 General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#pragma once + +#include +#include + +#include + +namespace detail { + +/** + * Deleter for storing QObjects in STL containers and smart pointers + * + * QObject should always be deleted by calling deleteLater() on them, so the event loop can + * perform necessary cleanups. + */ +struct DeferredDeleter { + /// Deletes the given QObject + void operator()(QObject *object) const { + if (object) + object->deleteLater(); + } +}; + +} // detail + +/** + * Unique pointer for QObjects with deferred deletion + * + * @tparam T Type derived from QObject + */ +template +using DeferredUniquePtr = std::unique_ptr; + + +/** + * Helper function for creating a DeferredUniquePtr + * + * An instance of T is created and returned in a DeferredUniquePtr, such that it will be deleted via + * QObject::deleteLater(). + * + * @tparam T The type to create + * @tparam Args Constructor argument types + * @param[in] args Constructor arguments + * @returns A DeferredUniquePtr holding a new instance of T + */ +template +DeferredUniquePtr makeDeferredUnique(Args... args) +{ + static_assert(std::is_base_of::value, "Type must inherit from QObject"); + return DeferredUniquePtr(new T(std::forward(args)...)); +} + + +/** + * Shared pointer for QObjects with deferred deletion + * + * @tparam T Type derived from QObject + */ +template +using DeferredSharedPtr = std::shared_ptr; + + +/** + * Helper function for creating a DeferredSharedPtr + * + * An instance of T is created and returned in a DeferredSharedPtr, such that it will be deleted via + * QObject::deleteLater(). + * + * @tparam T The type to create + * @tparam Args Constructor argument types + * @param[in] args Constructor arguments + * @returns A DeferredSharedPtr holding a new instance of T + */ +template +DeferredSharedPtr makeDeferredShared(Args... args) +{ + static_assert(std::is_base_of::value, "Type must inherit from QObject"); + return DeferredSharedPtr(new T(std::forward(args)...), detail::DeferredDeleter{}); +}