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

WTF

  • sources
  • kde-4.12
  • kdelibs
  • kjs
  • wtf
SharedPtr.h
Go to the documentation of this file.
1 // -*- mode: c++; c-basic-offset: 4 -*-
2 /*
3  * This file is part of the KDE libraries
4  * Copyright (C) 2005 Apple Computer, Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB. If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #ifndef WTF_SharedPtr_h
24 #define WTF_SharedPtr_h
25 
26 namespace WTF {
27 
28  // FIXME: Change template name to RefPtr?
29  template <class T> class SharedPtr
30  {
31  public:
32  SharedPtr() : m_ptr(0) {}
33  SharedPtr(T *ptr) : m_ptr(ptr) { if (ptr) ptr->ref(); }
34  SharedPtr(const SharedPtr &o) : m_ptr(o.m_ptr) { if (T *ptr = m_ptr) ptr->ref(); }
35  ~SharedPtr() { if (T *ptr = m_ptr) ptr->deref(); }
36 
37  template <class U> SharedPtr(const SharedPtr<U> &o) : m_ptr(o.get()) { if (T *ptr = m_ptr) ptr->ref(); }
38 
39  // FIXME: Deprecate in favor of operators below, then remove?
40  bool isNull() const { return m_ptr == 0; }
41  bool notNull() const { return m_ptr != 0; }
42 
43  // FIXME: Deprecate in favor of operator=, then remove?
44  void reset() { if (T *ptr = m_ptr) ptr->deref(); m_ptr = 0; }
45  void reset(T *o) { if (o) o->ref(); if (T *ptr = m_ptr) ptr->deref(); m_ptr = o; }
46 
47  T *get() const { return m_ptr; }
48 
49  T &operator*() const { return *m_ptr; }
50  T *operator->() const { return m_ptr; }
51 
52  bool operator!() const { return m_ptr == 0; }
53  operator bool() const { return m_ptr != 0; }
54 
55  SharedPtr &operator=(const SharedPtr &);
56  SharedPtr &operator=(T *);
57 
58  private:
59  T *m_ptr;
60 
61  operator int() const; // deliberately not implemented; helps prevent operator bool from converting to int accidentally
62  };
63 
64  template <class T> SharedPtr<T> &SharedPtr<T>::operator=(const SharedPtr<T> &o)
65  {
66  T *optr = o.m_ptr;
67  if (optr)
68  optr->ref();
69  if (T *ptr = m_ptr)
70  ptr->deref();
71  m_ptr = optr;
72  return *this;
73  }
74 
75  template <class T> inline SharedPtr<T> &SharedPtr<T>::operator=(T *optr)
76  {
77  if (optr)
78  optr->ref();
79  if (T *ptr = m_ptr)
80  ptr->deref();
81  m_ptr = optr;
82  return *this;
83  }
84 
85  template <class T> inline bool operator==(const SharedPtr<T> &a, const SharedPtr<T> &b)
86  {
87  return a.get() == b.get();
88  }
89 
90  template <class T> inline bool operator==(const SharedPtr<T> &a, const T *b)
91  {
92  return a.get() == b;
93  }
94 
95  template <class T> inline bool operator==(const T *a, const SharedPtr<T> &b)
96  {
97  return a == b.get();
98  }
99 
100  template <class T> inline bool operator!=(const SharedPtr<T> &a, const SharedPtr<T> &b)
101  {
102  return a.get() != b.get();
103  }
104 
105  template <class T> inline bool operator!=(const SharedPtr<T> &a, const T *b)
106  {
107  return a.get() != b;
108  }
109 
110  template <class T> inline bool operator!=(const T *a, const SharedPtr<T> &b)
111  {
112  return a != b.get();
113  }
114 
115  template <class T, class U> inline SharedPtr<T> static_pointer_cast(const SharedPtr<U> &p)
116  {
117  return SharedPtr<T>(static_cast<T *>(p.get()));
118  }
119 
120  template <class T, class U> inline SharedPtr<T> const_pointer_cast(const SharedPtr<U> &p)
121  {
122  return SharedPtr<T>(const_cast<T *>(p.get()));
123  }
124 
125 } // namespace WTF
126 
127 using WTF::SharedPtr;
128 using WTF::static_pointer_cast;
129 using WTF::const_pointer_cast;
130 
131 #endif // WTF_SharedPtr_h
WTF::SharedPtr::notNull
bool notNull() const
Definition: SharedPtr.h:41
WTF::SharedPtr::SharedPtr
SharedPtr(const SharedPtr &o)
Definition: SharedPtr.h:34
WTF::SharedPtr
Definition: SharedPtr.h:29
WTF::SharedPtr::operator=
SharedPtr & operator=(const SharedPtr &)
Definition: SharedPtr.h:64
WTF::SharedPtr::SharedPtr
SharedPtr(T *ptr)
Definition: SharedPtr.h:33
WTF::SharedPtr::get
T * get() const
Definition: SharedPtr.h:47
WTF::SharedPtr::operator*
T & operator*() const
Definition: SharedPtr.h:49
WTF::SharedPtr::SharedPtr
SharedPtr()
Definition: SharedPtr.h:32
WTF::SharedPtr::operator->
T * operator->() const
Definition: SharedPtr.h:50
WTF::SharedPtr::~SharedPtr
~SharedPtr()
Definition: SharedPtr.h:35
WTF::static_pointer_cast
PassRefPtr< T > static_pointer_cast(const PassRefPtr< U > &p)
Definition: PassRefPtr.h:171
WTF::const_pointer_cast
PassRefPtr< T > const_pointer_cast(const PassRefPtr< U > &p)
Definition: PassRefPtr.h:176
WTF::SharedPtr::reset
void reset()
Definition: SharedPtr.h:44
WTF::operator==
bool operator==(const HashTableConstKeysIterator< T, U, V > &a, const HashTableConstKeysIterator< T, U, V > &b)
Definition: HashIterators.h:167
WTF::SharedPtr::isNull
bool isNull() const
Definition: SharedPtr.h:40
WTF::SharedPtr::reset
void reset(T *o)
Definition: SharedPtr.h:45
WTF::operator!=
bool operator!=(const HashTableConstKeysIterator< T, U, V > &a, const HashTableConstKeysIterator< T, U, V > &b)
Definition: HashIterators.h:173
WTF::SharedPtr::operator!
bool operator!() const
Definition: SharedPtr.h:52
WTF::SharedPtr::SharedPtr
SharedPtr(const SharedPtr< U > &o)
Definition: SharedPtr.h:37
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:49:00 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



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