1 /**************************************************************************** 2 ** 3 ** DQt - D bindings for the Qt Toolkit 4 ** 5 ** GNU Lesser General Public License Usage 6 ** This file may be used under the terms of the GNU Lesser 7 ** General Public License version 3 as published by the Free Software 8 ** Foundation and appearing in the file LICENSE.LGPL3 included in the 9 ** packaging of this file. Please review the following information to 10 ** ensure the GNU Lesser General Public License version 3 requirements 11 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. 12 ** 13 ****************************************************************************/ 14 module qt.core.refcount; 15 extern(C++): 16 17 import qt.config; 18 import qt.core.basicatomic; 19 import qt.helpers; 20 21 extern(C++, "QtPrivate") 22 { 23 24 25 extern(C++, class) struct RefCount 26 { 27 public: 28 extern(D) pragma(inline, true) bool ref_(string filename = __FILE__, size_t line = __LINE__)/+ noexcept+/ { 29 int count = atomic.loadRelaxed(); 30 version(QT_NO_UNSHARABLE_CONTAINERS){}else 31 { 32 if (count == 0) // !isSharable 33 return false; 34 } 35 36 if (count != -1) // !isStatic 37 atomic.ref_(); 38 return true; 39 } 40 41 extern(D) pragma(inline, true) bool deref(string filename = __FILE__, size_t line = __LINE__)/+ noexcept+/ { 42 int count = atomic.loadRelaxed(); 43 version(QT_NO_UNSHARABLE_CONTAINERS){}else 44 { 45 if (count == 0) // !isSharable 46 return false; 47 } 48 49 if (count == -1) // isStatic 50 return true; 51 return atomic.deref(); 52 } 53 54 version(QT_NO_UNSHARABLE_CONTAINERS){}else 55 { 56 bool setSharable(bool sharable)/+ noexcept+/ 57 { 58 import qt.core.global; 59 60 (mixin(Q_ASSERT(q{!isShared()}))); 61 if (sharable) 62 return atomic.testAndSetRelaxed(0, 1); 63 else 64 return atomic.testAndSetRelaxed(1, 0); 65 } 66 67 bool isSharable() const/+ noexcept+/ 68 { 69 // Sharable === Shared ownership. 70 return atomic.loadRelaxed() != 0; 71 } 72 } 73 74 /+ bool isStatic() const/+ noexcept+/ 75 { 76 // Persistent object, never deleted 77 return atomic.loadRelaxed() == -1; 78 }+/ 79 80 bool isShared() const/+ noexcept+/ 81 { 82 int count = atomic.loadRelaxed(); 83 return (count != 1) && (count != 0); 84 } 85 86 // void initializeOwned()/+ noexcept+/ { atomic.storeRelaxed(1); } 87 // void initializeUnsharable()/+ noexcept+/ { atomic.storeRelaxed(0); } 88 89 QBasicAtomicInt atomic; 90 } 91 92 } 93 94 /+ #define Q_REFCOUNT_INITIALIZE_STATIC { Q_BASIC_ATOMIC_INITIALIZER(-1) } +/ 95 96 97 enum Q_REFCOUNT_INITIALIZE_STATIC = RefCount(QBasicAtomicInt(-1)); 98