language/duchain
duchainpointer.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef DUCHAINPOINTER_H
00020 #define DUCHAINPOINTER_H
00021
00022 #include <QtCore/QMetaType>
00023 #include <ksharedptr.h>
00024 #include "../languageexport.h"
00025
00026
00027
00028 namespace KDevelop {
00029
00030 class DUContext;
00031 class TopDUContext;
00032 class DUChainBase;
00033 class Declaration;
00034 class AbstractFunctionDeclaration;
00035
00050 class KDEVPLATFORMLANGUAGE_EXPORT DUChainPointerData : public KShared {
00051 public:
00055 DUChainBase* base();
00056
00060 DUChainBase* base() const;
00061
00063 DUChainPointerData();
00064
00065 ~DUChainPointerData();
00066
00067 private:
00069 DUChainPointerData( DUChainBase* base );
00070
00071 friend class DUChainBase;
00072 DUChainBase * m_base;
00073 Q_DISABLE_COPY(DUChainPointerData)
00074 };
00075
00087 template<class Type>
00088 class DUChainPointer {
00089 template<class OtherType>
00090 friend class DUChainPointer;
00091
00092 public:
00093 DUChainPointer() : d(KSharedPtr<DUChainPointerData>(0)) {
00094 }
00095
00096 DUChainPointer(const DUChainPointer& rhs)
00097 : d(rhs.d)
00098 {
00099 }
00100
00102 template<class OtherType>
00103 explicit DUChainPointer( OtherType* rhs ) {
00104 if( dynamic_cast<Type*>(rhs) )
00105 d = rhs->weakPointer();
00106 }
00107
00108 template<class OtherType>
00109 explicit DUChainPointer( DUChainPointer<OtherType> rhs ) {
00110 if( dynamic_cast<Type*>(rhs.data()) )
00111 d = rhs.d;
00112 }
00113
00114 explicit DUChainPointer( KSharedPtr<DUChainPointerData> rhs ) {
00115 if( dynamic_cast<Type*>(rhs->base()) )
00116 d = rhs;
00117 }
00118
00119 explicit DUChainPointer( Type* rhs ) {
00120 if( rhs )
00121 d = rhs->weakPointer();
00122 }
00123
00124 bool operator ==( const DUChainPointer<Type>& rhs ) const {
00125 return d.data() == rhs.d.data();
00126 }
00127
00128 bool operator !=( const DUChainPointer<Type>& rhs ) const {
00129 return d.data() != rhs.d.data();
00130 }
00131
00133 operator bool() const {
00134 return d && d->base();
00135 }
00136
00137 Type& operator* () const {
00138 Q_ASSERT(d);
00139 return *static_cast<Type*>(d->base());
00140 }
00141
00142 Type* operator->() const {
00143 Q_ASSERT(d);
00144 return static_cast<Type*>(d->base());
00145 }
00146
00147 bool operator<(const DUChainPointer<Type>& rhs) const {
00148 return d.data() < rhs.d.data();
00149 }
00150
00151 template<class NewType>
00152 DUChainPointer<NewType> dynamicCast() const {
00153 if( dynamic_cast<NewType*>( d->base() ) )
00154 return DUChainPointer<NewType>( static_cast<NewType*>(d->base()) );
00155 else
00156 return DUChainPointer<NewType>();
00157 }
00158
00159 Type* data() const {
00160 if( !d )
00161 return 0;
00162 return static_cast<Type*>(d->base());
00163 }
00164
00165 DUChainPointer<Type>& operator= ( Type* rhs ) {
00166 if( rhs )
00167 d = rhs->weakPointer();
00168 else
00169 d = 0;
00170
00171 return *this;
00172 }
00173
00174 private:
00175 KSharedPtr<DUChainPointerData> d;
00176 };
00177
00178 typedef DUChainPointer<DUChainBase> DUChainBasePointer;
00179 typedef DUChainPointer<DUContext> DUContextPointer;
00180 typedef DUChainPointer<TopDUContext> TopDUContextPointer;
00181 typedef DUChainPointer<Declaration> DeclarationPointer;
00182 typedef DUChainPointer<AbstractFunctionDeclaration> FunctionDeclarationPointer;
00183 }
00184
00185 Q_DECLARE_METATYPE( KDevelop::DUChainBasePointer )
00186
00187 #endif