• Skip to content
  • Skip to link menu
KDE 4.0 API Reference
  • KDE API Reference
  • kdeedu
  • Sitemap
  • Contact Us
 

kig

boost_intrusive_pointer.hpp

Go to the documentation of this file.
00001 // Copyright (C)  2003  Dominique Devriese <devriese@kde.org>
00002 
00003 // This program is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU General Public License
00005 // as published by the Free Software Foundation; either version 2
00006 // of the License, or (at your option) any later version.
00007 
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00016 // 02110-1301, USA.
00017 
00018 
00019 
00020 //  This code comes from the boost::intrusive_ptr.  I adapted it to
00021 //  suit my needs ( no dependencies on other boost libs, change the
00022 //  namespace to avoid conflicts,
00023 
00024 #ifndef MYBOOST_INTRUSIVE_PTR_HPP_INCLUDED
00025 #define MYBOOST_INTRUSIVE_PTR_HPP_INCLUDED
00026 
00027 //
00028 //  intrusive_ptr.hpp
00029 //
00030 //  Copyright (c) 2001, 2002 Peter Dimov
00031 //
00032 //  Permission to copy, use, modify, sell and distribute this software
00033 //  is granted provided this copyright notice appears in all copies.
00034 //  This software is provided "as is" without express or implied
00035 //  warranty, and with no claim as to its suitability for any purpose.
00036 //
00037 //  See http://www.boost.org/libs/smart_ptr/intrusive_ptr.html for documentation.
00038 //
00039 
00040 #include <functional>           // for std::less
00041 #include <iosfwd>               // for std::basic_ostream
00042 
00043 
00044 namespace myboost
00045 {
00046 
00047 //
00048 //  intrusive_ptr
00049 //
00050 //  A smart pointer that uses intrusive reference counting.
00051 //
00052 //  Relies on unqualified calls to
00053 //
00054 //      void intrusive_ptr_add_ref(T * p);
00055 //      void intrusive_ptr_release(T * p);
00056 //
00057 //          (p != 0)
00058 //
00059 //  The object is responsible for destroying itself.
00060 //
00061 
00062 template<class T> class intrusive_ptr
00063 {
00064 private:
00065 
00066     typedef intrusive_ptr this_type;
00067 
00068 public:
00069 
00070     typedef T element_type;
00071 
00072     intrusive_ptr(): p_(0)
00073     {
00074     }
00075 
00076     intrusive_ptr(T * p, bool add_ref = true): p_(p)
00077     {
00078         if(p_ != 0 && add_ref) intrusive_ptr_add_ref(p_);
00079     }
00080 
00081 #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
00082 
00083     template<class U> intrusive_ptr(intrusive_ptr<U> const & rhs): p_(rhs.get())
00084     {
00085         if(p_ != 0) intrusive_ptr_add_ref(p_);
00086     }
00087 
00088 #endif
00089 
00090     intrusive_ptr(intrusive_ptr const & rhs): p_(rhs.p_)
00091     {
00092         if(p_ != 0) intrusive_ptr_add_ref(p_);
00093     }
00094 
00095     ~intrusive_ptr()
00096     {
00097         if(p_ != 0) intrusive_ptr_release(p_);
00098     }
00099 
00100 #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
00101 
00102     template<class U> intrusive_ptr & operator=(intrusive_ptr<U> const & rhs)
00103     {
00104         this_type(rhs).swap(*this);
00105         return *this;
00106     }
00107 
00108 #endif
00109 
00110     intrusive_ptr & operator=(intrusive_ptr const & rhs)
00111     {
00112         this_type(rhs).swap(*this);
00113         return *this;
00114     }
00115 
00116     intrusive_ptr & operator=(T * rhs)
00117     {
00118         this_type(rhs).swap(*this);
00119         return *this;
00120     }
00121 
00122     T * get() const
00123     {
00124         return p_;
00125     }
00126 
00127     T & operator*() const
00128     {
00129         return *p_;
00130     }
00131 
00132     T * operator->() const
00133     {
00134         return p_;
00135     }
00136 
00137     typedef T * (intrusive_ptr::*unspecified_bool_type) () const;
00138 
00139     operator unspecified_bool_type () const
00140     {
00141         return p_ == 0? 0: &intrusive_ptr::get;
00142     }
00143 
00144     // operator! is a Borland-specific workaround
00145     bool operator! () const
00146     {
00147         return p_ == 0;
00148     }
00149 
00150     void swap(intrusive_ptr & rhs)
00151     {
00152         T * tmp = p_;
00153         p_ = rhs.p_;
00154         rhs.p_ = tmp;
00155     }
00156 
00157 private:
00158 
00159     T * p_;
00160 };
00161 
00162 template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
00163 {
00164     return a.get() == b.get();
00165 }
00166 
00167 template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
00168 {
00169     return a.get() != b.get();
00170 }
00171 
00172 template<class T> inline bool operator==(intrusive_ptr<T> const & a, T * b)
00173 {
00174     return a.get() == b;
00175 }
00176 
00177 template<class T> inline bool operator!=(intrusive_ptr<T> const & a, T * b)
00178 {
00179     return a.get() != b;
00180 }
00181 
00182 template<class T> inline bool operator==(T * a, intrusive_ptr<T> const & b)
00183 {
00184     return a == b.get();
00185 }
00186 
00187 template<class T> inline bool operator!=(T * a, intrusive_ptr<T> const & b)
00188 {
00189     return a != b.get();
00190 }
00191 
00192 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
00193 
00194 // Resolve the ambiguity between our op!= and the one in rel_ops
00195 
00196 template<class T> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
00197 {
00198     return a.get() != b.get();
00199 }
00200 
00201 #endif
00202 
00203 template<class T> inline bool operator<(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
00204 {
00205     return std::less<T *>()(a.get(), b.get());
00206 }
00207 
00208 template<class T> void swap(intrusive_ptr<T> & lhs, intrusive_ptr<T> & rhs)
00209 {
00210     lhs.swap(rhs);
00211 }
00212 
00213 // mem_fn support
00214 
00215 template<class T> T * get_pointer(intrusive_ptr<T> const & p)
00216 {
00217     return p.get();
00218 }
00219 
00220 template<class T, class U> intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> const & p)
00221 {
00222     return static_cast<T *>(p.get());
00223 }
00224 
00225 template<class T, class U> intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const & p)
00226 {
00227     return dynamic_cast<T *>(p.get());
00228 }
00229 
00230 // operator<<
00231 
00232 #if defined(__GNUC__) &&  (__GNUC__ < 3)
00233 
00234 template<class Y> std::ostream & operator<< (std::ostream & os, intrusive_ptr<Y> const & p)
00235 {
00236     os << p.get();
00237     return os;
00238 }
00239 
00240 #else
00241 
00242 template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
00243 {
00244     os << p.get();
00245     return os;
00246 }
00247 
00248 #endif
00249 
00250 } // namespace myboost
00251 
00252 #ifdef BOOST_MSVC
00253 # pragma warning(pop)
00254 #endif
00255 
00256 #endif  // #ifndef MYBOOST_INTRUSIVE_PTR_HPP_INCLUDED

kig

Skip menu "kig"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

kdeedu

Skip menu "kdeedu"
  • kalzium
  • kanagram
  • kig
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  •   docs
  •   src
  • parley
Generated for kdeedu by doxygen 1.5.4
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