//
// Copyright (c) 2001, 2002 Peter Dimov
//
-// Permission to copy, use, modify, sell and distribute this software
-// is granted provided this copyright notice appears in all copies.
-// This software is provided "as is" without express or implied
-// warranty, and with no claim as to its suitability for any purpose.
+// Distributed under the Boost Software License, Version 1.0. (See
+// accompanying file LICENSE_1_0.txt or copy at
+// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/smart_ptr/intrusive_ptr.html for documentation.
//
+#include <boost/config.hpp>
+
#ifdef BOOST_MSVC // moved here to work around VC++ compiler crash
# pragma warning(push)
# pragma warning(disable:4284) // odd return type for operator->
#endif
-#include <functional> // std::less
+#include <boost/assert.hpp>
+#include <boost/detail/workaround.hpp>
+
+#include <functional> // for std::less
+#include <iosfwd> // for std::basic_ostream
+
namespace boost
{
public:
- intrusive_ptr(): p_(0)
- {
- }
+ typedef T element_type;
- intrusive_ptr(T * p): p_(p)
+ intrusive_ptr(): p_(0)
{
- if(p_ != 0) intrusive_ptr_add_ref(p_);
}
- ~intrusive_ptr()
+ intrusive_ptr(T * p, bool add_ref = true): p_(p)
{
- if(p_ != 0) intrusive_ptr_release(p_);
+ if(p_ != 0 && add_ref) intrusive_ptr_add_ref(p_);
}
-#ifdef BOOST_MSVC6_MEMBER_TEMPLATES
+#if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
template<class U> intrusive_ptr(intrusive_ptr<U> const & rhs): p_(rhs.get())
{
if(p_ != 0) intrusive_ptr_add_ref(p_);
}
-#ifdef BOOST_MSVC6_MEMBER_TEMPLATES
+ ~intrusive_ptr()
+ {
+ if(p_ != 0) intrusive_ptr_release(p_);
+ }
+
+#if !defined(BOOST_NO_MEMBER_TEMPLATES) || defined(BOOST_MSVC6_MEMBER_TEMPLATES)
template<class U> intrusive_ptr & operator=(intrusive_ptr<U> const & rhs)
{
return *this;
}
- void swap(intrusive_ptr & rhs)
- {
- T * tmp = p_;
- p_ = rhs.p_;
- rhs.p_ = tmp;
- }
-
T * get() const
{
return p_;
return p_;
}
- bool empty() const
+#if defined(__SUNPRO_CC) && BOOST_WORKAROUND(__SUNPRO_CC, <= 0x530)
+
+ operator bool () const
+ {
+ return p_ != 0;
+ }
+
+#elif defined(__MWERKS__) && BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3003))
+ typedef T * (this_type::*unspecified_bool_type)() const;
+
+ operator unspecified_bool_type() const // never throws
{
- return p_ == 0;
+ return p_ == 0? 0: &this_type::get;
+ }
+
+#else
+
+ typedef T * this_type::*unspecified_bool_type;
+
+ operator unspecified_bool_type () const
+ {
+ return p_ == 0? 0: &this_type::p_;
}
- typedef bool (intrusive_ptr::*bool_type) () const;
+#endif
- operator bool_type () const
+ // operator! is a Borland-specific workaround
+ bool operator! () const
{
- return p_ == 0? 0: &intrusive_ptr::empty;
+ return p_ == 0;
+ }
+
+ void swap(intrusive_ptr & rhs)
+ {
+ T * tmp = p_;
+ p_ = rhs.p_;
+ rhs.p_ = tmp;
}
private:
T * p_;
};
-template<class T> void swap(intrusive_ptr<T> & lhs, intrusive_ptr<T> & rhs)
+template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
{
- lhs.swap(rhs);
+ return a.get() == b.get();
+}
+
+template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
+{
+ return a.get() != b.get();
}
-template<class T, class U> intrusive_ptr<T> shared_dynamic_cast(intrusive_ptr<U> const & p)
+template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, U * b)
{
- return dynamic_cast<T *>(p.get());
+ return a.get() == b;
}
-template<class T, class U> intrusive_ptr<T> shared_static_cast(intrusive_ptr<U> const & p)
+template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, U * b)
{
- return static_cast<T *>(p.get());
+ return a.get() != b;
}
-template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
+template<class T, class U> inline bool operator==(T * a, intrusive_ptr<U> const & b)
{
- return a.get() == b.get();
+ return a == b.get();
}
-template<class T, class U> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<U> const & b)
+template<class T, class U> inline bool operator!=(T * a, intrusive_ptr<U> const & b)
+{
+ return a != b.get();
+}
+
+#if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
+
+// Resolve the ambiguity between our op!= and the one in rel_ops
+
+template<class T> inline bool operator!=(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
{
return a.get() != b.get();
}
+#endif
+
template<class T> inline bool operator<(intrusive_ptr<T> const & a, intrusive_ptr<T> const & b)
{
- return std::less<T *>(a.get(), b.get());
+ return std::less<T *>()(a.get(), b.get());
}
-template<class T> inline bool operator==(intrusive_ptr<T> const & a, T * b)
+template<class T> void swap(intrusive_ptr<T> & lhs, intrusive_ptr<T> & rhs)
{
- return a.get() == b;
+ lhs.swap(rhs);
}
-template<class T> inline bool operator!=(intrusive_ptr<T> const & a, T * b)
+// mem_fn support
+
+template<class T> T * get_pointer(intrusive_ptr<T> const & p)
{
- return a.get() != b;
+ return p.get();
}
-template<class T> inline bool operator==(T * a, intrusive_ptr<T> const & b)
+template<class T, class U> intrusive_ptr<T> static_pointer_cast(intrusive_ptr<U> const & p)
{
- return a == b.get();
+ return static_cast<T *>(p.get());
}
-template<class T> inline bool operator!=(T * a, intrusive_ptr<T> const & b)
+template<class T, class U> intrusive_ptr<T> const_pointer_cast(intrusive_ptr<U> const & p)
{
- return a != b.get();
+ return const_cast<T *>(p.get());
}
-// mem_fn support
+template<class T, class U> intrusive_ptr<T> dynamic_pointer_cast(intrusive_ptr<U> const & p)
+{
+ return dynamic_cast<T *>(p.get());
+}
-template<class T> T * get_pointer(intrusive_ptr<T> const & p)
+// operator<<
+
+#if defined(__GNUC__) && (__GNUC__ < 3)
+
+template<class Y> std::ostream & operator<< (std::ostream & os, intrusive_ptr<Y> const & p)
{
- return p.get();
+ os << p.get();
+ return os;
}
+#else
+
+// in STLport's no-iostreams mode no iostream symbols can be used
+#ifndef _STLP_NO_IOSTREAMS
+
+# if defined(BOOST_MSVC) && BOOST_WORKAROUND(BOOST_MSVC, < 1300 && __SGI_STL_PORT)
+// MSVC6 has problems finding std::basic_ostream through the using declaration in namespace _STL
+using std::basic_ostream;
+template<class E, class T, class Y> basic_ostream<E, T> & operator<< (basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
+# else
+template<class E, class T, class Y> std::basic_ostream<E, T> & operator<< (std::basic_ostream<E, T> & os, intrusive_ptr<Y> const & p)
+# endif
+{
+ os << p.get();
+ return os;
+}
+
+#endif // _STLP_NO_IOSTREAMS
+
+#endif // __GNUC__ < 3
+
} // namespace boost
#ifdef BOOST_MSVC