[cfe-commits] [libcxx] r151084 - in /libcxx/trunk: include/__functional_base include/functional include/memory test/utilities/memory/unique.ptr/unique.ptr.special/cmp_nullptr.pass.cpp test/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/cmp_nullptr.pass.cpp

Howard Hinnant hhinnant at apple.com
Tue Feb 21 13:02:58 PST 2012


Author: hhinnant
Date: Tue Feb 21 15:02:58 2012
New Revision: 151084

URL: http://llvm.org/viewvc/llvm-project?rev=151084&view=rev
Log:
Modernize relational operators for shared_ptr and unique_ptr.  This includes adding support for nullptr, and using less<T*>.  Fixes http://llvm.org/bugs/show_bug.cgi?id=12056.

Added:
    libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.special/cmp_nullptr.pass.cpp
    libcxx/trunk/test/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/cmp_nullptr.pass.cpp
Modified:
    libcxx/trunk/include/__functional_base
    libcxx/trunk/include/functional
    libcxx/trunk/include/memory

Modified: libcxx/trunk/include/__functional_base
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/__functional_base?rev=151084&r1=151083&r2=151084&view=diff
==============================================================================
--- libcxx/trunk/include/__functional_base (original)
+++ libcxx/trunk/include/__functional_base Tue Feb 21 15:02:58 2012
@@ -50,6 +50,13 @@
     static const bool value = sizeof(__test<_Tp>(0)) == 1;
 };
 
+template <class _Tp>
+struct _LIBCPP_VISIBLE less : binary_function<_Tp, _Tp, bool>
+{
+    _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const
+        {return __x < __y;}
+};
+
 #ifdef _LIBCPP_HAS_NO_VARIADICS
 
 #include <__functional_base_03>

Modified: libcxx/trunk/include/functional
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/functional?rev=151084&r1=151083&r2=151084&view=diff
==============================================================================
--- libcxx/trunk/include/functional (original)
+++ libcxx/trunk/include/functional Tue Feb 21 15:02:58 2012
@@ -536,12 +536,7 @@
         {return __x > __y;}
 };
 
-template <class _Tp>
-struct _LIBCPP_VISIBLE less : binary_function<_Tp, _Tp, bool>
-{
-    _LIBCPP_INLINE_VISIBILITY bool operator()(const _Tp& __x, const _Tp& __y) const
-        {return __x < __y;}
-};
+// less in <__functional_base>
 
 template <class _Tp>
 struct _LIBCPP_VISIBLE greater_equal : binary_function<_Tp, _Tp, bool>

Modified: libcxx/trunk/include/memory
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/memory?rev=151084&r1=151083&r2=151084&view=diff
==============================================================================
--- libcxx/trunk/include/memory (original)
+++ libcxx/trunk/include/memory Tue Feb 21 15:02:58 2012
@@ -2964,7 +2964,13 @@
 template <class _T1, class _D1, class _T2, class _D2>
 inline _LIBCPP_INLINE_VISIBILITY
 bool
-operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() < __y.get();}
+operator< (const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y)
+{
+    typedef typename unique_ptr<_T1, _D1>::pointer _P1;
+    typedef typename unique_ptr<_T2, _D2>::pointer _P2;
+    typedef typename common_type<_P1, _P2>::type _V;
+    return less<_V>()(__x.get(), __y.get());
+}
 
 template <class _T1, class _D1, class _T2, class _D2>
 inline _LIBCPP_INLINE_VISIBILITY
@@ -2981,6 +2987,104 @@
 bool
 operator>=(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return !(__x < __y);}
 
+template <class _T1, class _D1>
+inline _LIBCPP_INLINE_VISIBILITY
+bool
+operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t)
+{
+    return !__x;
+}
+
+template <class _T1, class _D1>
+inline _LIBCPP_INLINE_VISIBILITY
+bool
+operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x)
+{
+    return !__x;
+}
+
+template <class _T1, class _D1>
+inline _LIBCPP_INLINE_VISIBILITY
+bool
+operator!=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
+{
+    return static_cast<bool>(__x);
+}
+
+template <class _T1, class _D1>
+inline _LIBCPP_INLINE_VISIBILITY
+bool
+operator!=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
+{
+    return static_cast<bool>(__x);
+}
+
+template <class _T1, class _D1>
+inline _LIBCPP_INLINE_VISIBILITY
+bool
+operator<(const unique_ptr<_T1, _D1>& __x, nullptr_t)
+{
+    typedef typename unique_ptr<_T1, _D1>::pointer _P1;
+    return less<_P1>()(__x.get(), nullptr);
+}
+
+template <class _T1, class _D1>
+inline _LIBCPP_INLINE_VISIBILITY
+bool
+operator<(nullptr_t, const unique_ptr<_T1, _D1>& __x)
+{
+    typedef typename unique_ptr<_T1, _D1>::pointer _P1;
+    return less<_P1>()(nullptr, __x.get());
+}
+
+template <class _T1, class _D1>
+inline _LIBCPP_INLINE_VISIBILITY
+bool
+operator>(const unique_ptr<_T1, _D1>& __x, nullptr_t)
+{
+    return nullptr < __x;
+}
+
+template <class _T1, class _D1>
+inline _LIBCPP_INLINE_VISIBILITY
+bool
+operator>(nullptr_t, const unique_ptr<_T1, _D1>& __x)
+{
+    return __x < nullptr;
+}
+
+template <class _T1, class _D1>
+inline _LIBCPP_INLINE_VISIBILITY
+bool
+operator<=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
+{
+    return !(nullptr < __x);
+}
+
+template <class _T1, class _D1>
+inline _LIBCPP_INLINE_VISIBILITY
+bool
+operator<=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
+{
+    return !(__x < nullptr);
+}
+
+template <class _T1, class _D1>
+inline _LIBCPP_INLINE_VISIBILITY
+bool
+operator>=(const unique_ptr<_T1, _D1>& __x, nullptr_t)
+{
+    return !(__x < nullptr);
+}
+
+template <class _T1, class _D1>
+inline _LIBCPP_INLINE_VISIBILITY
+bool
+operator>=(nullptr_t, const unique_ptr<_T1, _D1>& __x)
+{
+    return !(nullptr < __x);
+}
+
 template <class _Tp> struct hash;
 
 // We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
@@ -4620,7 +4724,128 @@
 bool
 operator<(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
 {
-    return __x.get() < __y.get();
+    typedef typename common_type<_Tp*, _Up*>::type _V;
+    return less<_V>()(__x.get(), __y.get());
+}
+
+template<class _Tp, class _Up>
+inline _LIBCPP_INLINE_VISIBILITY
+bool
+operator>(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
+{
+    return __y < __x;
+}
+
+template<class _Tp, class _Up>
+inline _LIBCPP_INLINE_VISIBILITY
+bool
+operator<=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
+{
+    return !(__y < __x);
+}
+
+template<class _Tp, class _Up>
+inline _LIBCPP_INLINE_VISIBILITY
+bool
+operator>=(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
+{
+    return !(__x < __y);
+}
+
+template<class _Tp>
+inline _LIBCPP_INLINE_VISIBILITY
+bool
+operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
+{
+    return !__x;
+}
+
+template<class _Tp>
+inline _LIBCPP_INLINE_VISIBILITY
+bool
+operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
+{
+    return !__x;
+}
+
+template<class _Tp>
+inline _LIBCPP_INLINE_VISIBILITY
+bool
+operator!=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
+{
+    return static_cast<bool>(__x);
+}
+
+template<class _Tp>
+inline _LIBCPP_INLINE_VISIBILITY
+bool
+operator!=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
+{
+    return static_cast<bool>(__x);
+}
+
+template<class _Tp>
+inline _LIBCPP_INLINE_VISIBILITY
+bool
+operator<(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
+{
+    return less<_Tp*>()(__x.get(), nullptr);
+}
+
+template<class _Tp>
+inline _LIBCPP_INLINE_VISIBILITY
+bool
+operator<(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
+{
+    return less<_Tp*>()(nullptr, __x.get());
+}
+
+template<class _Tp>
+inline _LIBCPP_INLINE_VISIBILITY
+bool
+operator>(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
+{
+    return nullptr < __x;
+}
+
+template<class _Tp>
+inline _LIBCPP_INLINE_VISIBILITY
+bool
+operator>(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
+{
+    return __x < nullptr;
+}
+
+template<class _Tp>
+inline _LIBCPP_INLINE_VISIBILITY
+bool
+operator<=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
+{
+    return !(nullptr < __x);
+}
+
+template<class _Tp>
+inline _LIBCPP_INLINE_VISIBILITY
+bool
+operator<=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
+{
+    return !(__x < nullptr);
+}
+
+template<class _Tp>
+inline _LIBCPP_INLINE_VISIBILITY
+bool
+operator>=(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
+{
+    return !(__x < nullptr);
+}
+
+template<class _Tp>
+inline _LIBCPP_INLINE_VISIBILITY
+bool
+operator>=(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
+{
+    return !(nullptr < __x);
 }
 
 template<class _Tp>

Added: libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.special/cmp_nullptr.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.special/cmp_nullptr.pass.cpp?rev=151084&view=auto
==============================================================================
--- libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.special/cmp_nullptr.pass.cpp (added)
+++ libcxx/trunk/test/utilities/memory/unique.ptr/unique.ptr.special/cmp_nullptr.pass.cpp Tue Feb 21 15:02:58 2012
@@ -0,0 +1,71 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <memory>
+
+// shared_ptr
+
+// template <class T, class D>
+//     bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept;
+// template <class T, class D>
+//     bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept;
+// template <class T, class D>
+//     bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
+// template <class T, class D>
+//     bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
+// template <class T, class D>
+//     bool operator<(const unique_ptr<T, D>& x, nullptr_t) noexcept;
+// template <class T, class D>
+//     bool operator<(nullptr_t, const unique_ptr<T, D>& y) noexcept;
+// template <class T, class D>
+//     bool operator<=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
+// template <class T, class D>
+//     bool operator<=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
+// template <class T, class D>
+//     bool operator>(const unique_ptr<T, D>& x, nullptr_t) noexcept;
+// template <class T, class D>
+//     bool operator>(nullptr_t, const unique_ptr<T, D>& y) noexcept;
+// template <class T, class D>
+//     bool operator>=(const unique_ptr<T, D>& x, nullptr_t) noexcept;
+// template <class T, class D>
+//     bool operator>=(nullptr_t, const unique_ptr<T, D>& y) noexcept;
+
+#include <memory>
+#include <cassert>
+
+void do_nothing(int*) {}
+
+int main()
+{
+    int* ptr1(new int);
+    int* ptr2(new int);
+    const std::unique_ptr<int> p1(new int(1));
+    assert(!(p1 == nullptr));
+    assert(!(nullptr == p1));
+    assert(!(p1 < nullptr));
+    assert( (nullptr < p1));
+    assert(!(p1 <= nullptr));
+    assert( (nullptr <= p1));
+    assert( (p1 > nullptr));
+    assert(!(nullptr > p1));
+    assert( (p1 >= nullptr));
+    assert(!(nullptr >= p1));
+
+    const std::unique_ptr<int> p2;
+    assert( (p2 == nullptr));
+    assert( (nullptr == p2));
+    assert(!(p2 < nullptr));
+    assert(!(nullptr < p2));
+    assert( (p2 <= nullptr));
+    assert( (nullptr <= p2));
+    assert(!(p2 > nullptr));
+    assert(!(nullptr > p2));
+    assert( (p2 >= nullptr));
+    assert( (nullptr >= p2));
+}

Added: libcxx/trunk/test/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/cmp_nullptr.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/cmp_nullptr.pass.cpp?rev=151084&view=auto
==============================================================================
--- libcxx/trunk/test/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/cmp_nullptr.pass.cpp (added)
+++ libcxx/trunk/test/utilities/memory/util.smartptr/util.smartptr.shared/util.smartptr.shared.cmp/cmp_nullptr.pass.cpp Tue Feb 21 15:02:58 2012
@@ -0,0 +1,71 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <memory>
+
+// shared_ptr
+
+// template <class T>
+//     bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept;
+// template <class T>
+//     bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept;
+// template <class T>
+//     bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept;
+// template <class T>
+//     bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept;
+// template <class T>
+//     bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept;
+// template <class T>
+//     bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept;
+// template <class T>
+//     bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept;
+// template <class T>
+//     bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept;
+// template <class T>
+//     bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept;
+// template <class T>
+//     bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept;
+// template <class T>
+//     bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept;
+// template <class T>
+//     bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept;
+
+#include <memory>
+#include <cassert>
+
+void do_nothing(int*) {}
+
+int main()
+{
+    int* ptr1(new int);
+    int* ptr2(new int);
+    const std::shared_ptr<int> p1(new int(1));
+    assert(!(p1 == nullptr));
+    assert(!(nullptr == p1));
+    assert(!(p1 < nullptr));
+    assert( (nullptr < p1));
+    assert(!(p1 <= nullptr));
+    assert( (nullptr <= p1));
+    assert( (p1 > nullptr));
+    assert(!(nullptr > p1));
+    assert( (p1 >= nullptr));
+    assert(!(nullptr >= p1));
+
+    const std::shared_ptr<int> p2;
+    assert( (p2 == nullptr));
+    assert( (nullptr == p2));
+    assert(!(p2 < nullptr));
+    assert(!(nullptr < p2));
+    assert( (p2 <= nullptr));
+    assert( (nullptr <= p2));
+    assert(!(p2 > nullptr));
+    assert(!(nullptr > p2));
+    assert( (p2 >= nullptr));
+    assert( (nullptr >= p2));
+}





More information about the cfe-commits mailing list