moved kdeaccessibility kdeaddons kdeadmin kdeartwork kdebindings kdeedu kdegames...
[kdeedu.git] / kig / misc / boost_intrusive_pointer.hpp
blobde6c1f6830c50812dad8550d98ae6832e31c18a4
1 // Copyright (C) 2003 Dominique Devriese <devriese@kde.org>
3 // This program is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU General Public License
5 // as published by the Free Software Foundation; either version 2
6 // of the License, or (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
16 // 02111-1307, USA.
20 // This code comes from the boost::intrusive_ptr. I adapted it to
21 // suit my needs ( no dependencies on other boost libs, change the
22 // namespace to avoid conflicts,
24 #ifndef MYBOOST_INTRUSIVE_PTR_HPP_INCLUDED
25 #define MYBOOST_INTRUSIVE_PTR_HPP_INCLUDED
28 // intrusive_ptr.hpp
30 // Copyright (c) 2001, 2002 Peter Dimov
32 // Permission to copy, use, modify, sell and distribute this software
33 // is granted provided this copyright notice appears in all copies.
34 // This software is provided "as is" without express or implied
35 // warranty, and with no claim as to its suitability for any purpose.
37 // See http://www.boost.org/libs/smart_ptr/intrusive_ptr.html for documentation.
40 #include <functional> // for std::less
41 #include <iosfwd> // for std::basic_ostream
44 namespace myboost
48 // intrusive_ptr
50 // A smart pointer that uses intrusive reference counting.
52 // Relies on unqualified calls to
54 // void intrusive_ptr_add_ref(T * p);
55 // void intrusive_ptr_release(T * p);
57 // (p != 0)
59 // The object is responsible for destroying itself.
62 template<class T> class intrusive_ptr
64 private:
66 typedef intrusive_ptr this_type;
68 public:
70 typedef T element_type;
72 intrusive_ptr(): p_(0)
76 intrusive_ptr(T * p, bool add_ref = true): p_(p)
78 if(p_ != 0 && add_ref) intrusive_ptr_add_ref(p_);
81 #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
83 template<class U> intrusive_ptr(intrusive_ptr<U> const & rhs): p_(rhs.get())
85 if(p_ != 0) intrusive_ptr_add_ref(p_);
88 #endif
90 intrusive_ptr(intrusive_ptr const & rhs): p_(rhs.p_)
92 if(p_ != 0) intrusive_ptr_add_ref(p_);
95 ~intrusive_ptr()
97 if(p_ != 0) intrusive_ptr_release(p_);
100 #if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
102 template<class U> intrusive_ptr & operator=(intrusive_ptr<U> const & rhs)
104 this_type(rhs).swap(*this);
105 return *this;
108 #endif
110 intrusive_ptr & operator=(intrusive_ptr const & rhs)
112 this_type(rhs).swap(*this);
113 return *this;
116 intrusive_ptr & operator=(T * rhs)
118 this_type(rhs).swap(*this);
119 return *this;
122 T * get() const
124 return p_;
127 T & operator*() const
129 return *p_;
132 T * operator->() const
134 return p_;
137 typedef T * (intrusive_ptr::*unspecified_bool_type) () const;
139 operator unspecified_bool_type () const
141 return p_ == 0? 0: &intrusive_ptr::get;
144 // operator! is a Borland-specific workaround
145 bool operator! () const
147 return p_ == 0;
150 void swap(intrusive_ptr & rhs)
152 T * tmp = p_;
153 p_ = rhs.p_;
154 rhs.p_ = tmp;
157 private:
159 T * p_;
162 template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
164 return a.get() == b.get();
167 template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
169 return a.get() != b.get();
172 template<class T> inline bool operator==(intrusive_ptr<T> const & a, T * b)
174 return a.get() == b;
177 template<class T> inline bool operator!=(intrusive_ptr<T> const & a, T * b)
179 return a.get() != b;
182 template<class T> inline bool operator==(T * a, intrusive_ptr<T> const & b)
184 return a == b.get();
187 template<class T> inline bool operator!=(T * a, intrusive_ptr<T> const & b)
189 return a != b.get();
192 #if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
194 // Resolve the ambiguity between our op!= and the one in rel_ops
196 template<class T> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
198 return a.get() != b.get();
201 #endif
203 template<class T> inline bool operator<(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
205 return std::less<T *>()(a.get(), b.get());
208 template<class T> void swap(intrusive_ptr<T> & lhs, intrusive_ptr<T> & rhs)
210 lhs.swap(rhs);
213 // mem_fn support
215 template<class T> T * get_pointer(intrusive_ptr<T> const & p)
217 return p.get();
220 template<class T, class U> intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> const & p)
222 return static_cast<T *>(p.get());
225 template<class T, class U> intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const & p)
227 return dynamic_cast<T *>(p.get());
230 // operator<<
232 #if defined(__GNUC__) && (__GNUC__ < 3)
234 template<class Y> std::ostream & operator<< (std::ostream & os, intrusive_ptr<Y> const & p)
236 os << p.get();
237 return os;
240 #else
242 template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
244 os << p.get();
245 return os;
248 #endif
250 } // namespace myboost
252 #ifdef BOOST_MSVC
253 # pragma warning(pop)
254 #endif
256 #endif // #ifndef MYBOOST_INTRUSIVE_PTR_HPP_INCLUDED