/**************************************************************************** ** ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). ** Contact: http://www.qt-project.org/legal ** ** This file is part of the Qt Mobility Components. ** ** $QT_BEGIN_LICENSE:LGPL$ ** Commercial License Usage ** Licensees holding valid commercial Qt licenses may use this file in ** accordance with the commercial license agreement provided with the ** Software or, alternatively, in accordance with the terms contained in ** a written agreement between you and Digia. For licensing terms and ** conditions see http://qt.digia.com/licensing. For further information ** use the contact form at http://qt.digia.com/contact-us. ** ** GNU Lesser General Public License Usage ** Alternatively, this file may be used under the terms of the GNU Lesser ** General Public License version 2.1 as published by the Free Software ** Foundation and appearing in the file LICENSE.LGPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU Lesser General Public License version 2.1 requirements ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** ** In addition, as a special exception, Digia gives you certain additional ** rights. These rights are described in the Digia Qt LGPL Exception ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3.0 as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU General Public License version 3.0 requirements will be ** met: http://www.gnu.org/copyleft/gpl.html. ** ** ** $QT_END_LICENSE$ ** ****************************************************************************/ #ifndef QPRIVATEIMPLEMENTATION_H #define QPRIVATEIMPLEMENTATION_H // // W A R N I N G // ------------- // // This file is not part of the Qt Extended API. It exists purely as an // implementation detail. This header file may change from version to // version without notice, or even be removed. // // We mean it. // /* Rationale: QSharedDataPointer has some deficiencies when used for implementation hiding, which are exposed by its use in the messaging library: 1. It cannot easily be used with incomplete classes, requiring client classes to unnecessarily define destructors, copy constructors and assignment operators. 2. It is not polymorphic, and must be reused redundantly where a class with a hidden implementation is derived from another class with a hidden implementation. 3. Type-bridging functions are required to provide a supertype with access to the supertype data allocated within a subtype object. The QPrivateImplementation class stores a pointer to the correct destructor and copy-constructor, given the type of the actual object instantiated. This allows it to be copied and deleted in contexts where the true type of the implementation object is not recorded. The QPrivateImplementationPointer provides QSharedDataPointer semantics, providing the pointee type with necessary derived-type information. The pointee type must derive from QPrivateImplementation. The QPrivatelyImplemented<> template provides correct copy and assignment functions, and allows the shared implementation object to be cast to the different object types in the implementation type hierarchy. */ #include "qmailglobal.h" #include #include class QPrivateImplementationBase { public: template inline QPrivateImplementationBase(Subclass* p) : ref_count(0), self(p), delete_function(&QPrivateImplementationBase::typed_delete), copy_function(&QPrivateImplementationBase::typed_copy_construct) { } inline QPrivateImplementationBase(const QPrivateImplementationBase& other) : ref_count(0), self(other.self), delete_function(other.delete_function), copy_function(other.copy_function) { } inline void ref() { ref_count.ref(); } inline bool deref() { if (ref_count.deref() == 0 && delete_function && self) { (*delete_function)(self); return true; } else { return false; } } inline void* detach() { if (copy_function && self && ref_count != 1) { void* copy = (*copy_function)(self); reinterpret_cast(copy)->self = copy; return copy; } else { return 0; } } private: QAtomicInt ref_count; void *self; void (*delete_function)(void *p); void *(*copy_function)(const void *p); template static inline void typed_delete(void *p) { delete static_cast(p); } template static inline void* typed_copy_construct(const void *p) { return new T(*static_cast(p)); } // using the assignment operator would lead to corruption in the ref-counting QPrivateImplementationBase &operator=(const QPrivateImplementationBase &); }; template class QPrivateImplementationPointer { public: inline T &operator*() { return *detach(); } inline const T &operator*() const { return *d; } inline T *operator->() { return detach(); } inline const T *operator->() const { return d; } inline operator T *() { return detach(); } inline operator const T *() const { return d; } inline T *data() { return detach(); } inline const T *data() const { return d; } inline const T *constData() const { return d; } inline bool operator==(const QPrivateImplementationPointer &other) const { return d == other.d; } inline bool operator!=(const QPrivateImplementationPointer &other) const { return d != other.d; } inline QPrivateImplementationPointer() : d(0) { } inline explicit QPrivateImplementationPointer(T *p) : d(p) { increment(d); } template inline explicit QPrivateImplementationPointer(U *p) : d(static_cast(p)) { increment(d); } inline QPrivateImplementationPointer(const QPrivateImplementationPointer &o) : d(o.d) { increment(d); } inline ~QPrivateImplementationPointer() { decrement(d); } inline QPrivateImplementationPointer &operator=(T *p) { assign_helper(p); return *this; } inline QPrivateImplementationPointer &operator=(const QPrivateImplementationPointer &o) { assign_helper(o.d); return *this; } inline bool operator!() const { return !d; } private: void increment(T*& p); void decrement(T*& p); inline T* assign_helper(T *p) { if (p != d) { increment(p); decrement(d); d = p; } return d; } inline T* detach() { if (!d) return 0; if (T* detached = static_cast(d->detach())) { return assign_helper(detached); } else { return d; } } public: T *d; }; template class QTOPIAMAIL_EXPORT QPrivatelyImplemented { public: QPrivatelyImplemented(ImplementationType* p); QPrivatelyImplemented(const QPrivatelyImplemented& other); template QTOPIAMAIL_EXPORT QPrivatelyImplemented(ImplementationType* p, A1 a1); virtual ~QPrivatelyImplemented(); const QPrivatelyImplemented& operator=(const QPrivatelyImplemented& other); template inline ImplementationSubclass* impl() { return static_cast(static_cast(d)); } template inline typename InterfaceType::ImplementationType* impl(InterfaceType*) { return impl(); } template inline const ImplementationSubclass* impl() const { return static_cast(static_cast(d)); } template inline const typename InterfaceType::ImplementationType* impl(const InterfaceType*) const { return impl(); } protected: QPrivateImplementationPointer d; }; class QPrivateNoncopyableBase { public: template inline QPrivateNoncopyableBase(Subclass* p) : self(p), delete_function(&QPrivateNoncopyableBase::typed_delete) { } inline void delete_self() { if (delete_function && self) { (*delete_function)(self); } } private: void *self; void (*delete_function)(void *p); template static inline void typed_delete(void *p) { delete static_cast(p); } // do not permit copying QPrivateNoncopyableBase(const QPrivateNoncopyableBase &); QPrivateNoncopyableBase &operator=(const QPrivateNoncopyableBase &); }; template class QPrivateNoncopyablePointer { public: inline T &operator*() { return *d; } inline const T &operator*() const { return *d; } inline T *operator->() { return d; } inline const T *operator->() const { return d; } inline operator T *() { return d; } inline operator const T *() const { return d; } inline T *data() { return d; } inline const T *data() const { return d; } inline const T *constData() const { return d; } inline bool operator==(const QPrivateNoncopyablePointer &other) const { return d == other.d; } inline bool operator!=(const QPrivateNoncopyablePointer &other) const { return d != other.d; } inline QPrivateNoncopyablePointer() : d(0) { } inline explicit QPrivateNoncopyablePointer(T *p) : d(p) { } template inline explicit QPrivateNoncopyablePointer(U *p) : d(static_cast(p)) { } ~QPrivateNoncopyablePointer(); inline bool operator!() const { return !d; } private: inline QPrivateNoncopyablePointer &operator=(T *) { return *this; } inline QPrivateNoncopyablePointer &operator=(const QPrivateNoncopyablePointer &) { return *this; } public: T *d; }; template class QTOPIAMAIL_EXPORT QPrivatelyNoncopyable { public: QPrivatelyNoncopyable(ImplementationType* p); template QTOPIAMAIL_EXPORT QPrivatelyNoncopyable(ImplementationType* p, A1 a1); virtual ~QPrivatelyNoncopyable(); template inline ImplementationSubclass* impl() { return static_cast(static_cast(d)); } template inline typename InterfaceType::ImplementationType* impl(InterfaceType*) { return impl(); } template inline const ImplementationSubclass* impl() const { return static_cast(static_cast(d)); } template inline const typename InterfaceType::ImplementationType* impl(const InterfaceType*) const { return impl(); } protected: QPrivateNoncopyablePointer d; }; #endif