• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

WTF

  • kjs
  • wtf
RefPtr.h
Go to the documentation of this file.
1// -*- mode: c++; c-basic-offset: 4 -*-
2/*
3 * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB. If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22#ifndef WTF_RefPtr_h
23#define WTF_RefPtr_h
24
25#include <algorithm>
26#include "AlwaysInline.h"
27#include "PassRefPtr.h"
28
29namespace WTF {
30
31 enum PlacementNewAdoptType { PlacementNewAdopt };
32
33 template <typename T> class PassRefPtr;
34
35 enum HashTableDeletedValueType { HashTableDeletedValue };
36
37 template <typename T> class RefPtr {
38 public:
39 RefPtr() : m_ptr(0) { }
40 RefPtr(T* ptr) : m_ptr(ptr) { if (ptr) ptr->ref(); }
41 RefPtr(const RefPtr& o) : m_ptr(o.m_ptr) { if (T* ptr = m_ptr) ptr->ref(); }
42 // see comment in PassRefPtr.h for why this takes const reference
43 template <typename U> RefPtr(const PassRefPtr<U>&);
44
45 // Special constructor for cases where we overwrite an object in place.
46 RefPtr(PlacementNewAdoptType) { }
47
48 // Hash table deleted values, which are only constructed and never copied or destroyed.
49 RefPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) { }
50 bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedValue(); }
51
52 ~RefPtr() { if (T* ptr = m_ptr) ptr->deref(); }
53
54 template <typename U> RefPtr(const RefPtr<U>& o) : m_ptr(o.get()) { if (T* ptr = m_ptr) ptr->ref(); }
55
56 T* get() const { return m_ptr; }
57
58 void clear() { if (T* ptr = m_ptr) ptr->deref(); m_ptr = 0; }
59 PassRefPtr<T> release() { PassRefPtr<T> tmp = adoptRef(m_ptr); m_ptr = 0; return tmp; }
60
61 T& operator*() const { return *m_ptr; }
62 ALWAYS_INLINE T* operator->() const { return m_ptr; }
63
64 bool operator!() const { return !m_ptr; }
65
66 // This conversion operator allows implicit conversion to bool but not to other integer types.
67 typedef T* RefPtr::*UnspecifiedBoolType;
68 operator UnspecifiedBoolType() const { return m_ptr ? &RefPtr::m_ptr : 0; }
69
70 RefPtr& operator=(const RefPtr&);
71 RefPtr& operator=(T*);
72 RefPtr& operator=(const PassRefPtr<T>&);
73 template <typename U> RefPtr& operator=(const RefPtr<U>&);
74 template <typename U> RefPtr& operator=(const PassRefPtr<U>&);
75
76 void swap(RefPtr&);
77
78 private:
79 static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); }
80
81 T* m_ptr;
82 };
83
84 template <typename T> template <typename U> inline RefPtr<T>::RefPtr(const PassRefPtr<U>& o)
85 : m_ptr(o.releaseRef())
86 {
87 }
88
89 template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(const RefPtr<T>& o)
90 {
91 T* optr = o.get();
92 if (optr)
93 optr->ref();
94 T* ptr = m_ptr;
95 m_ptr = optr;
96 if (ptr)
97 ptr->deref();
98 return *this;
99 }
100
101 template <typename T> template <typename U> inline RefPtr<T>& RefPtr<T>::operator=(const RefPtr<U>& o)
102 {
103 T* optr = o.get();
104 if (optr)
105 optr->ref();
106 T* ptr = m_ptr;
107 m_ptr = optr;
108 if (ptr)
109 ptr->deref();
110 return *this;
111 }
112
113 template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(T* optr)
114 {
115 if (optr)
116 optr->ref();
117 T* ptr = m_ptr;
118 m_ptr = optr;
119 if (ptr)
120 ptr->deref();
121 return *this;
122 }
123
124 template <typename T> inline RefPtr<T>& RefPtr<T>::operator=(const PassRefPtr<T>& o)
125 {
126 T* ptr = m_ptr;
127 m_ptr = o.releaseRef();
128 if (ptr)
129 ptr->deref();
130 return *this;
131 }
132
133 template <typename T> template <typename U> inline RefPtr<T>& RefPtr<T>::operator=(const PassRefPtr<U>& o)
134 {
135 T* ptr = m_ptr;
136 m_ptr = o.releaseRef();
137 if (ptr)
138 ptr->deref();
139 return *this;
140 }
141
142 template <class T> inline void RefPtr<T>::swap(RefPtr<T>& o)
143 {
144 std::swap(m_ptr, o.m_ptr);
145 }
146
147 template <class T> inline void swap(RefPtr<T>& a, RefPtr<T>& b)
148 {
149 a.swap(b);
150 }
151
152 template <typename T, typename U> inline bool operator==(const RefPtr<T>& a, const RefPtr<U>& b)
153 {
154 return a.get() == b.get();
155 }
156
157 template <typename T, typename U> inline bool operator==(const RefPtr<T>& a, U* b)
158 {
159 return a.get() == b;
160 }
161
162 template <typename T, typename U> inline bool operator==(T* a, const RefPtr<U>& b)
163 {
164 return a == b.get();
165 }
166
167 template <typename T, typename U> inline bool operator!=(const RefPtr<T>& a, const RefPtr<U>& b)
168 {
169 return a.get() != b.get();
170 }
171
172 template <typename T, typename U> inline bool operator!=(const RefPtr<T>& a, U* b)
173 {
174 return a.get() != b;
175 }
176
177 template <typename T, typename U> inline bool operator!=(T* a, const RefPtr<U>& b)
178 {
179 return a != b.get();
180 }
181
182 template <typename T, typename U> inline RefPtr<T> static_pointer_cast(const RefPtr<U>& p)
183 {
184 return RefPtr<T>(static_cast<T*>(p.get()));
185 }
186
187 template <typename T, typename U> inline RefPtr<T> const_pointer_cast(const RefPtr<U>& p)
188 {
189 return RefPtr<T>(const_cast<T*>(p.get()));
190 }
191
192 template <typename T> inline T* getPtr(const RefPtr<T>& p)
193 {
194 return p.get();
195 }
196
197} // namespace WTF
198
199using WTF::RefPtr;
200using WTF::static_pointer_cast;
201using WTF::const_pointer_cast;
202
203#endif // WTF_RefPtr_h
AlwaysInline.h
ALWAYS_INLINE
#define ALWAYS_INLINE
Definition: AlwaysInline.h:30
PassRefPtr.h
WTF::PassRefPtr
Definition: PassRefPtr.h:33
WTF::PassRefPtr::releaseRef
T * releaseRef() const
Definition: PassRefPtr.h:52
WTF::RefPtr
Definition: RefPtr.h:37
WTF::RefPtr::RefPtr
RefPtr(const RefPtr &o)
Definition: RefPtr.h:41
WTF::RefPtr::RefPtr
RefPtr(PlacementNewAdoptType)
Definition: RefPtr.h:46
WTF::RefPtr::operator*
T & operator*() const
Definition: RefPtr.h:61
WTF::RefPtr::release
PassRefPtr< T > release()
Definition: RefPtr.h:59
WTF::RefPtr::RefPtr
RefPtr(HashTableDeletedValueType)
Definition: RefPtr.h:49
WTF::RefPtr::~RefPtr
~RefPtr()
Definition: RefPtr.h:52
WTF::RefPtr::get
T * get() const
Definition: RefPtr.h:56
WTF::RefPtr::clear
void clear()
Definition: RefPtr.h:58
WTF::RefPtr::operator=
RefPtr & operator=(const RefPtr< U > &)
WTF::RefPtr::RefPtr
RefPtr(const RefPtr< U > &o)
Definition: RefPtr.h:54
WTF::RefPtr::RefPtr
RefPtr()
Definition: RefPtr.h:39
WTF::RefPtr::RefPtr
RefPtr(T *ptr)
Definition: RefPtr.h:40
WTF::RefPtr::operator=
RefPtr & operator=(const PassRefPtr< U > &)
WTF::RefPtr::isHashTableDeletedValue
bool isHashTableDeletedValue() const
Definition: RefPtr.h:50
WTF::RefPtr::UnspecifiedBoolType
T *RefPtr::* UnspecifiedBoolType
Definition: RefPtr.h:67
WTF::RefPtr::operator->
ALWAYS_INLINE T * operator->() const
Definition: RefPtr.h:62
WTF::RefPtr::operator=
RefPtr & operator=(const RefPtr &)
Definition: RefPtr.h:89
WTF::RefPtr::operator!
bool operator!() const
Definition: RefPtr.h:64
WTF::RefPtr::swap
void swap(RefPtr &)
Definition: RefPtr.h:142
WTF
Definition: ASCIICType.h:45
WTF::HashTableDeletedValueType
HashTableDeletedValueType
Definition: RefPtr.h:35
WTF::HashTableDeletedValue
@ HashTableDeletedValue
Definition: RefPtr.h:35
WTF::swap
void swap(OwnArrayPtr< T > &a, OwnArrayPtr< T > &b)
Definition: OwnArrayPtr.h:61
WTF::PlacementNewAdoptType
PlacementNewAdoptType
Definition: RefPtr.h:31
WTF::PlacementNewAdopt
@ PlacementNewAdopt
Definition: RefPtr.h:31
WTF::getPtr
T * getPtr(T *p)
Definition: GetPtr.h:26
WTF::static_pointer_cast
PassRefPtr< T > static_pointer_cast(const PassRefPtr< U > &p)
Definition: PassRefPtr.h:171
WTF::adoptRef
PassRefPtr< T > adoptRef(T *)
Definition: PassRefPtr.h:166
WTF::operator!=
bool operator!=(const HashTableConstKeysIterator< T, U, V > &a, const HashTableConstKeysIterator< T, U, V > &b)
Definition: HashIterators.h:173
WTF::const_pointer_cast
PassRefPtr< T > const_pointer_cast(const PassRefPtr< U > &p)
Definition: PassRefPtr.h:176
WTF::operator==
bool operator==(const HashTableConstKeysIterator< T, U, V > &a, const HashTableConstKeysIterator< T, U, V > &b)
Definition: HashIterators.h:167
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

WTF

Skip menu "WTF"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal