Implement n3656 - make_unique. Thanks to Howard for the review and suggestions.
authorMarshall Clow <mclow@qualcomm.com>
Mon, 1 Jul 2013 18:16:03 +0000 (18:16 +0000)
committerMarshall Clow <mclow@qualcomm.com>
Mon, 1 Jul 2013 18:16:03 +0000 (18:16 +0000)
llvm-svn: 185352

libcxx/include/memory
libcxx/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array.pass.cpp [new file with mode: 0644]
libcxx/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array1.fail.cpp [new file with mode: 0644]
libcxx/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array2.fail.cpp [new file with mode: 0644]
libcxx/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array3.fail.cpp [new file with mode: 0644]
libcxx/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array4.fail.cpp [new file with mode: 0644]
libcxx/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.single.pass.cpp [new file with mode: 0644]

index bcf4d6d..ad4b5d3 100644 (file)
@@ -350,6 +350,10 @@ class bad_weak_ptr
     bad_weak_ptr() noexcept;
 };
 
+template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args);     // C++14
+template<class T>                unique_ptr<T> make_unique(size_t n);           // C++14
+template<class T, class... Args> unspecified   make_unique(Args&&...) = delete; // C++14, T == U[N]
+
 template<class T>
 class shared_ptr
 {
@@ -3079,6 +3083,47 @@ move(unique_ptr<_Tp, _Dp>& __t)
 
 #endif
 
+#if _LIBCPP_STD_VER > 11
+
+template<class _Tp>
+struct __unique_if
+{
+    typedef unique_ptr<_Tp> __unique_single;
+};
+
+template<class _Tp>
+struct __unique_if<_Tp[]>
+{
+    typedef unique_ptr<_Tp[]> __unique_array_unknown_bound;
+};
+
+template<class _Tp, size_t _Np>
+struct __unique_if<_Tp[_Np]>
+{
+    typedef void __unique_array_known_bound;
+};
+
+template<class _Tp, class... _Args>
+typename __unique_if<_Tp>::__unique_single
+make_unique(_Args&&... __args)
+{
+    return unique_ptr<_Tp>(new _Tp(_VSTD::forward<_Args>(__args)...));
+}
+
+template<class _Tp>
+typename __unique_if<_Tp>::__unique_array_unknown_bound
+make_unique(size_t __n)
+{
+    typedef typename remove_extent<_Tp>::type _Up;
+    return unique_ptr<_Tp>(new _Up[__n]());
+}
+
+template<class _Tp, class... _Args>
+    typename __unique_if<_Tp>::__unique_array_known_bound
+    make_unique(_Args&&...) = delete;
+
+#endif  // _LIBCPP_STD_VER > 11
+
 template <class _Tp> struct hash;
 
 // We use murmur2 when size_t is 32 bits, and cityhash64 when size_t
diff --git a/libcxx/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array.pass.cpp b/libcxx/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array.pass.cpp
new file mode 100644 (file)
index 0000000..b2fb58f
--- /dev/null
@@ -0,0 +1,45 @@
+//===----------------------------------------------------------------------===//
+//
+//                     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.
+//
+//===----------------------------------------------------------------------===//
+
+#include <memory>
+#include <string>
+#include <cassert>
+
+//    The only way to create an unique_ptr<T[]> is to default construct them.
+
+class foo {
+public:
+    foo () : val_(3) {}
+    int get () const { return val_; }
+private:
+    int val_;
+    };
+
+int main()
+{
+#if _LIBCPP_STD_VER > 11
+    {
+    auto p1 = std::make_unique<int[]>(5);
+    for ( int i = 0; i < 5; ++i )
+        assert ( p1[i] == 0 );
+    }
+    
+    {
+    auto p2 = std::make_unique<std::string[]>(5);
+    for ( int i = 0; i < 5; ++i )
+        assert ( p2[i].size () == 0 );
+    }
+    
+    {
+    auto p3 = std::make_unique<foo[]>(7);
+    for ( int i = 0; i < 7; ++i )
+        assert ( p3[i].get () == 3 );
+    }
+#endif  // _LIBCPP_STD_VER > 11
+}
diff --git a/libcxx/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array1.fail.cpp b/libcxx/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array1.fail.cpp
new file mode 100644 (file)
index 0000000..0098791
--- /dev/null
@@ -0,0 +1,17 @@
+//===----------------------------------------------------------------------===//
+//
+//                     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.
+//
+//===----------------------------------------------------------------------===//
+
+#include <memory>
+#include <string>
+#include <cassert>
+
+int main()
+{
+    auto up1 = std::make_unique<std::string[]>("error"); // doesn't compile - no bound
+}
diff --git a/libcxx/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array2.fail.cpp b/libcxx/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array2.fail.cpp
new file mode 100644 (file)
index 0000000..cc94e9a
--- /dev/null
@@ -0,0 +1,17 @@
+//===----------------------------------------------------------------------===//
+//
+//                     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.
+//
+//===----------------------------------------------------------------------===//
+
+#include <memory>
+#include <string>
+#include <cassert>
+
+int main()
+{
+    auto up2 = std::make_unique<int[]>(10, 20, 30, 40);
+}
diff --git a/libcxx/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array3.fail.cpp b/libcxx/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array3.fail.cpp
new file mode 100644 (file)
index 0000000..cfdc2e1
--- /dev/null
@@ -0,0 +1,17 @@
+//===----------------------------------------------------------------------===//
+//
+//                     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.
+//
+//===----------------------------------------------------------------------===//
+
+#include <memory>
+#include <string>
+#include <cassert>
+
+int main()
+{
+    auto up3 = std::make_unique<int[5]>();    // this is deleted
+}
diff --git a/libcxx/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array4.fail.cpp b/libcxx/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array4.fail.cpp
new file mode 100644 (file)
index 0000000..26eb59b
--- /dev/null
@@ -0,0 +1,17 @@
+//===----------------------------------------------------------------------===//
+//
+//                     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.
+//
+//===----------------------------------------------------------------------===//
+
+#include <memory>
+#include <string>
+#include <cassert>
+
+int main()
+{
+    auto up4 = std::make_unique<int[5]>(11, 22, 33, 44, 55); // deleted 
+}
diff --git a/libcxx/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.single.pass.cpp b/libcxx/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.single.pass.cpp
new file mode 100644 (file)
index 0000000..7326ed2
--- /dev/null
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+//                     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.
+//
+//===----------------------------------------------------------------------===//
+
+#include <memory>
+#include <string>
+#include <cassert>
+
+int main()
+{
+#if _LIBCPP_STD_VER > 11
+    {
+    std::unique_ptr<int> p1 = std::make_unique<int>(1);
+    assert ( *p1 == 1 );
+    p1 = std::make_unique<int> ();
+    assert ( *p1 == 0 );
+    }
+    
+    {
+    std::unique_ptr<std::string> p2 = std::make_unique<std::string> ( "Meow!" );
+    assert ( *p2 == "Meow!" );
+    p2 = std::make_unique<std::string> ();
+    assert ( *p2 == "" );
+    p2 = std::make_unique<std::string> ( 6, 'z' );
+    assert ( *p2 == "zzzzzz" );
+    }
+#endif  // _LIBCPP_STD_VER > 11
+}