1 /*
2  *  Copyright (c) 2016 Dmitry Kazakov <dimula73@gmail.com>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18 
19 #ifndef KIS_PINNED_SHARED_PTR_H
20 #define KIS_PINNED_SHARED_PTR_H
21 
22 /**
23  * A special type of KisSharedPtr that doesn't support conversion
24  * into raw pointer. You cannot convert it into raw pointer and cannot
25  * accidentally delete it. It is done with a hiding KisSharedPtr's
26  * conversion routine and substituting is with a custom one.
27  */
28 template <typename T>
29 class KisPinnedSharedPtr : public KisSharedPtr<T>
30 {
31     typedef KisSharedPtr<T> BaseClass;
32     class NotConvertibleToT {~NotConvertibleToT() = delete;};
33     typedef NotConvertibleToT* RestrictedBool;
34 public:
KisPinnedSharedPtr()35     KisPinnedSharedPtr()
36     {
37     }
38 
KisPinnedSharedPtr(T * other)39     inline KisPinnedSharedPtr(T *other)
40         : BaseClass(other)
41     {
42     }
43 
44     template <typename X>
KisPinnedSharedPtr(const KisWeakSharedPtr<X> & other)45     inline KisPinnedSharedPtr(const KisWeakSharedPtr<X>& other)
46         : BaseClass(other)
47     {
48     }
49 
50 
51     template <typename X>
KisPinnedSharedPtr(const KisSharedPtr<X> & other)52     inline KisPinnedSharedPtr(const KisSharedPtr<X> &other)
53         : BaseClass(other)
54     {
55     }
56 
57 
RestrictedBool()58     inline operator RestrictedBool() const
59     {
60         return this->isNull() ? 0 : reinterpret_cast<NotConvertibleToT*>(1);
61     }
62 
63     bool operator!() const
64     {
65         return this->isNull();
66     }
67 private:
68     explicit operator const T*() const;
69 };
70 
71 #include <kis_debug.h>
72 
73 template <typename T>
74 inline QDebug operator<<(QDebug dbg, const KisPinnedSharedPtr<T> &ptr)
75 {
76     dbg.nospace() << ptr.data();
77     return dbg;
78 }
79 
80 #endif // KIS_PINNED_SHARED_PTR_H
81 
82