diff options
| author | Marshall Clow <mclow@qualcomm.com> | 2013-07-01 18:16:03 +0000 |
|---|---|---|
| committer | Marshall Clow <mclow@qualcomm.com> | 2013-07-01 18:16:03 +0000 |
| commit | 28d8ba5f79e986ebfdc29fcb67ecaf3a3c823db0 (patch) | |
| tree | 0b18bc0da611f2c35de7efcd2a1391aecb83cb01 | |
| parent | 7a9fcdf6fb84b4d8db49eb0a8488b81155bffb2a (diff) | |
| download | bcm5719-llvm-28d8ba5f79e986ebfdc29fcb67ecaf3a3c823db0.tar.gz bcm5719-llvm-28d8ba5f79e986ebfdc29fcb67ecaf3a3c823db0.zip | |
Implement n3656 - make_unique. Thanks to Howard for the review and suggestions.
llvm-svn: 185352
7 files changed, 191 insertions, 0 deletions
diff --git a/libcxx/include/memory b/libcxx/include/memory index bcf4d6de754..ad4b5d3d0fd 100644 --- a/libcxx/include/memory +++ b/libcxx/include/memory @@ -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 index 00000000000..b2fb58f529f --- /dev/null +++ b/libcxx/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array.pass.cpp @@ -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 index 00000000000..00987919413 --- /dev/null +++ b/libcxx/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array1.fail.cpp @@ -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 index 00000000000..cc94e9ab3aa --- /dev/null +++ b/libcxx/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array2.fail.cpp @@ -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 index 00000000000..cfdc2e1d886 --- /dev/null +++ b/libcxx/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array3.fail.cpp @@ -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 index 00000000000..26eb59bbfd7 --- /dev/null +++ b/libcxx/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.array4.fail.cpp @@ -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 index 00000000000..7326ed22655 --- /dev/null +++ b/libcxx/test/utilities/memory/unique.ptr/unique.ptr.create/make_unique.single.pass.cpp @@ -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 +} |

