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
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
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
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);
59 // The object is responsible for destroying itself.
62 template<class T
> class intrusive_ptr
66 typedef intrusive_ptr this_type
;
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_
);
90 intrusive_ptr(intrusive_ptr
const & rhs
): p_(rhs
.p_
)
92 if(p_
!= 0) intrusive_ptr_add_ref(p_
);
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);
110 intrusive_ptr
& operator=(intrusive_ptr
const & rhs
)
112 this_type(rhs
).swap(*this);
116 intrusive_ptr
& operator=(T
* rhs
)
118 this_type(rhs
).swap(*this);
127 T
& operator*() const
132 T
* operator->() const
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
150 void swap(intrusive_ptr
& rhs
)
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
)
177 template<class T
> inline bool operator!=(intrusive_ptr
<T
> const & a
, T
* b
)
182 template<class T
> inline bool operator==(T
* a
, intrusive_ptr
<T
> const & b
)
187 template<class T
> inline bool operator!=(T
* a
, intrusive_ptr
<T
> const & b
)
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();
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
)
215 template<class T
> T
* get_pointer(intrusive_ptr
<T
> const & p
)
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());
232 #if defined(__GNUC__) && (__GNUC__ < 3)
234 template<class Y
> std::ostream
& operator<< (std::ostream
& os
, intrusive_ptr
<Y
> const & p
)
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
)
250 } // namespace myboost
253 # pragma warning(pop)
256 #endif // #ifndef MYBOOST_INTRUSIVE_PTR_HPP_INCLUDED