diff --git a/include/boost/smart_ptr/owner_less.hpp b/include/boost/smart_ptr/owner_less.hpp new file mode 100644 index 0000000..6899325 --- /dev/null +++ b/include/boost/smart_ptr/owner_less.hpp @@ -0,0 +1,57 @@ +#ifndef BOOST_SMART_PTR_OWNER_LESS_HPP_INCLUDED +#define BOOST_SMART_PTR_OWNER_LESS_HPP_INCLUDED + +// +// owner_less.hpp +// +// Copyright (c) 2008 Frank Mori Hess +// +// 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/smart_ptr.htm for documentation. +// + +#include + +namespace boost +{ + template class shared_ptr; + template class weak_ptr; + + namespace detail + { + template + struct generic_owner_less : public std::binary_function + { + bool operator()(const T &lhs, const T &rhs) const + { + return lhs.owner_before(rhs); + } + bool operator()(const T &lhs, const U &rhs) const + { + return lhs.owner_before(rhs); + } + bool operator()(const U &lhs, const T &rhs) const + { + return lhs.owner_before(rhs); + } + }; + } // namespace detail + + template struct owner_less; + + template + struct owner_less >: + public detail::generic_owner_less, weak_ptr > + {}; + + template + struct owner_less >: + public detail::generic_owner_less, shared_ptr > + {}; + +} // namespace boost + +#endif // #ifndef BOOST_SMART_PTR_OWNER_LESS_HPP_INCLUDED diff --git a/include/boost/smart_ptr/shared_ptr.hpp b/include/boost/smart_ptr/shared_ptr.hpp index 9ead1ba..1bfb75c 100644 --- a/include/boost/smart_ptr/shared_ptr.hpp +++ b/include/boost/smart_ptr/shared_ptr.hpp @@ -449,7 +449,12 @@ public: pn.swap(other.pn); } - template bool _internal_less(shared_ptr const & rhs) const + template bool owner_before( shared_ptr const & rhs ) const + { + return pn < rhs.pn; + } + + template bool owner_before( weak_ptr const & rhs ) const { return pn < rhs.pn; } @@ -505,7 +510,7 @@ template inline bool operator!=(shared_ptr const & a, shared_ptr template inline bool operator<(shared_ptr const & a, shared_ptr const & b) { - return a._internal_less(b); + return a.owner_before( b ); } template inline void swap(shared_ptr & a, shared_ptr & b) diff --git a/include/boost/smart_ptr/weak_ptr.hpp b/include/boost/smart_ptr/weak_ptr.hpp index 34b0d1e..3dfae07 100644 --- a/include/boost/smart_ptr/weak_ptr.hpp +++ b/include/boost/smart_ptr/weak_ptr.hpp @@ -205,7 +205,12 @@ public: pn = pn2; } - template bool _internal_less(weak_ptr const & rhs) const + template bool owner_before( weak_ptr const & rhs ) const + { + return pn < rhs.pn; + } + + template bool owner_before( shared_ptr const & rhs ) const { return pn < rhs.pn; } @@ -229,7 +234,7 @@ private: template inline bool operator<(weak_ptr const & a, weak_ptr const & b) { - return a._internal_less(b); + return a.owner_before( b ); } template void swap(weak_ptr & a, weak_ptr & b) diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index a11b431..c5dfba3 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -67,5 +67,6 @@ import testing ; [ run sp_hash_test.cpp ] [ run get_deleter_array_test.cpp ] [ run ip_hash_test.cpp ] + [ run owner_less_test.cpp ] ; } diff --git a/test/owner_less_test.cpp b/test/owner_less_test.cpp new file mode 100644 index 0000000..572b72b --- /dev/null +++ b/test/owner_less_test.cpp @@ -0,0 +1,67 @@ +// +// owner_less_test.cpp +// +// A regression test for owner_less +// +// Copyright (c) 2008 Frank Mori Hess +// +// 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) +// + +#include +#include +#include +#include + +int main() +{ + boost::owner_less > comp; + { + boost::shared_ptr x; + boost::shared_ptr y; + boost::weak_ptr w; + BOOST_TEST(!(comp(x, w) || comp(w, x))); + } + { + boost::shared_ptr z((int*)0); + boost::weak_ptr w; + BOOST_TEST(comp(z, w) || comp(w, z)); + { + boost::shared_ptr zz(z); + w = boost::weak_ptr(zz); + BOOST_TEST(!(comp(z, zz) || comp(z, zz))); + BOOST_TEST(!(comp(z, w) || comp(z, w))); + } + BOOST_TEST(!(comp(z, w) || comp(w, z))); + } + { + boost::shared_ptr x; + boost::shared_ptr z((int*)0); + BOOST_TEST(comp(x, z) || comp(z, x)); + } + { + boost::shared_ptr a((int*)0); + boost::shared_ptr b((int*)0); + BOOST_TEST(comp(a, b) || comp(b, a)); + boost::weak_ptr w(a); + BOOST_TEST(!(comp(a, w) || comp(w, a))); + BOOST_TEST(comp(b, w) || comp(w, b)); + } + + boost::owner_less > weak_comp; + { + boost::shared_ptr a((int*)0); + boost::weak_ptr wa(a); + boost::shared_ptr b((int*)0); + boost::weak_ptr wb(b); + BOOST_TEST(!(weak_comp(a, wa) || weak_comp(wa, a))); + BOOST_TEST(!(weak_comp(b, wb) || weak_comp(wb, b))); + BOOST_TEST(weak_comp(wa, wb) || weak_comp(wb, wa)); + BOOST_TEST(weak_comp(wa, b) || weak_comp(b, wa)); + } + + return boost::report_errors(); +}