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

WTF

OwnPtr.h

Go to the documentation of this file.
00001 // -*- mode: c++; c-basic-offset: 4 -*-
00002 /*
00003  *  Copyright (C) 2006 Apple Computer, Inc.
00004  *
00005  *  This library is free software; you can redistribute it and/or
00006  *  modify it under the terms of the GNU Library General Public
00007  *  License as published by the Free Software Foundation; either
00008  *  version 2 of the License, or (at your option) any later version.
00009  *
00010  *  This library is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  *  Library General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU Library General Public License
00016  *  along with this library; see the file COPYING.LIB.  If not, write to
00017  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  *  Boston, MA 02110-1301, USA.
00019  *
00020  */
00021 
00022 #ifndef WTF_OwnPtr_h
00023 #define WTF_OwnPtr_h
00024 
00025 #include <algorithm>
00026 #include "Assertions.h"
00027 #include "Noncopyable.h"
00028 
00029 namespace WTF {
00030 
00031     template <typename T> class OwnPtr : Noncopyable {
00032     public:
00033         explicit OwnPtr(T* ptr = 0) : m_ptr(ptr) { }
00034         ~OwnPtr() { safeDelete(); }
00035 
00036         T* get() const { return m_ptr; }
00037         T* release() { T* ptr = m_ptr; m_ptr = 0; return ptr; }
00038 
00039         void set(T* ptr) { ASSERT(m_ptr != ptr); safeDelete(); m_ptr = ptr; }
00040         void clear() { safeDelete(); m_ptr = 0; }
00041 
00042         T& operator*() const { ASSERT(m_ptr); return *m_ptr; }
00043         T* operator->() const { ASSERT(m_ptr); return m_ptr; }
00044 
00045         bool operator!() const { return !m_ptr; }
00046 
00047         // This conversion operator allows implicit conversion to bool but not to other integer types.
00048         typedef T* (OwnPtr::*UnspecifiedBoolType)() const;
00049         operator UnspecifiedBoolType() const { return m_ptr ? &OwnPtr::get : 0; }
00050 
00051         void swap(OwnPtr& o) { std::swap(m_ptr, o.m_ptr); }
00052 
00053     private:
00054         void safeDelete() { typedef char known[sizeof(T) ? 1 : -1]; if (sizeof(known)) delete m_ptr; }
00055 
00056         T* m_ptr;
00057     };
00058 
00059     template <typename T> inline void swap(OwnPtr<T>& a, OwnPtr<T>& b) { a.swap(b); }
00060 
00061 } // namespace WTF
00062 
00063 using WTF::OwnPtr;
00064 
00065 #endif // WTF_OwnPtr_h

WTF

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

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal