diff options
author | Eric Fiselier <eric@efcs.ca> | 2016-08-11 03:13:11 +0000 |
---|---|---|
committer | Eric Fiselier <eric@efcs.ca> | 2016-08-11 03:13:11 +0000 |
commit | 324506b9f3a414487f8ba8e128f7aac038412e4f (patch) | |
tree | cd8522d7bfd47cce6b722d19e669b86a62ef7deb /libcxx/test/support | |
parent | 25fb5bda0f4c03ce71e6ddbaf09e12af558b5eda (diff) | |
download | bcm5719-llvm-324506b9f3a414487f8ba8e128f7aac038412e4f.tar.gz bcm5719-llvm-324506b9f3a414487f8ba8e128f7aac038412e4f.zip |
[libcxx] Add std::any
Summary:
This patch adds std::any by moving/adapting <experimental/any>.
This patch also implements the std::any parts of p0032r3 (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0032r3.pdf)
and LWG 2509 (http://cplusplus.github.io/LWG/lwg-defects.html#2509).
I plan to push it in a day or two if there are no comments.
Reviewers: mclow.lists, EricWF
Subscribers: cfe-commits
Differential Revision: https://reviews.llvm.org/D22733
llvm-svn: 278310
Diffstat (limited to 'libcxx/test/support')
-rw-r--r-- | libcxx/test/support/any_helpers.h | 215 | ||||
-rw-r--r-- | libcxx/test/support/experimental_any_helpers.h | 326 | ||||
-rw-r--r-- | libcxx/test/support/test_macros.h | 7 |
3 files changed, 519 insertions, 29 deletions
diff --git a/libcxx/test/support/any_helpers.h b/libcxx/test/support/any_helpers.h index bb1ad175c1f..a0ff8dbc1f9 100644 --- a/libcxx/test/support/any_helpers.h +++ b/libcxx/test/support/any_helpers.h @@ -9,12 +9,14 @@ #ifndef ANY_HELPERS_H #define ANY_HELPERS_H -#include <experimental/any> #include <typeinfo> #include <type_traits> #include <cassert> +namespace std { namespace experimental {} } + #include "test_macros.h" +#include "type_id.h" #if !defined(TEST_HAS_NO_RTTI) #define RTTI_ASSERT(X) assert(X) @@ -32,42 +34,55 @@ template <class _Tp> > {}; +template <class T> +bool containsType(std::any const& a) { +#if !defined(TEST_HAS_NO_RTTI) + return a.type() == typeid(T); +#else + return a.has_value() && std::any_cast<T>(&a) != nullptr; +#endif +} // Return 'true' if 'Type' will be considered a small type by 'any' template <class Type> bool isSmallType() { -#if defined(_LIBCPP_VERSION) - return std::experimental::__any_imp::_IsSmallObject<Type>::value; -#else return IsSmallObject<Type>::value; -#endif - } // Assert that an object is empty. If the object used to contain an object // of type 'LastType' check that it can no longer be accessed. template <class LastType = int> -void assertEmpty(std::experimental::any const& a) { - assert(a.empty()); +void assertEmpty(std::any const& a) { + using namespace std; + assert(!a.has_value()); RTTI_ASSERT(a.type() == typeid(void)); - assert(std::experimental::any_cast<LastType const>(&a) == nullptr); + assert(any_cast<LastType const>(&a) == nullptr); } // Assert that an 'any' object stores the specified 'Type' and 'value'. template <class Type> -void assertContains(std::experimental::any const& a, int value = 1) { - assert(!a.empty()); - RTTI_ASSERT(a.type() == typeid(Type)); - assert(std::experimental::any_cast<Type const &>(a).value == value); +void assertContains(std::any const& a, int value = 1) { + assert(a.has_value()); + assert(containsType<Type>(a)); + assert(std::any_cast<Type const &>(a).value == value); +} + +template <> +void assertContains<int>(std::any const& a, int value) { + assert(a.has_value()); + assert(containsType<int>(a)); + assert(std::any_cast<int const &>(a) == value); } // Modify the value of a "test type" stored within an any to the specified // 'value'. template <class Type> -void modifyValue(std::experimental::any& a, int value) { - assert(!a.empty()); - RTTI_ASSERT(a.type() == typeid(Type)); - std::experimental::any_cast<Type&>(a).value = value; +void modifyValue(std::any& a, int value) { + using namespace std; + using namespace std::experimental; + assert(a.has_value()); + assert(containsType<Type>(a)); + any_cast<Type&>(a).value = value; } // A test type that will trigger the small object optimization within 'any'. @@ -89,25 +104,31 @@ struct small_type int value; - explicit small_type(int val) : value(val) { + explicit small_type(int val = 0) : value(val) { + ++count; + } + explicit small_type(int, int val, int) : value(val) { + ++count; + } + small_type(std::initializer_list<int> il) : value(*il.begin()) { ++count; } - small_type(small_type const & other) throw() { + small_type(small_type const & other) noexcept { value = other.value; ++count; ++copied; ++const_copied; } - small_type(small_type& other) throw() { + small_type(small_type& other) noexcept { value = other.value; ++count; ++copied; ++non_const_copied; } - small_type(small_type && other) throw() { + small_type(small_type && other) noexcept { value = other.value; other.value = 0; ++count; @@ -163,11 +184,17 @@ struct large_type int value; - large_type(int val) : value(val) { + large_type(int val = 0) : value(val) { ++count; data[0] = 0; } - + large_type(int, int val, int) : value(val) { + ++count; + data[0] = 0; + } + large_type(std::initializer_list<int> il) : value(*il.begin()) { + ++count; + } large_type(large_type const & other) { value = other.value; ++count; @@ -219,6 +246,67 @@ typedef large_type<> large; typedef large_type<1> large1; typedef large_type<2> large2; + +struct deleted_move +{ + static int count; + static int copied; + static int moved; + static int const_copied; + static int non_const_copied; + + static void reset() { + deleted_move::copied = 0; + deleted_move::moved = 0; + deleted_move::const_copied = 0; + deleted_move::non_const_copied = 0; + } + + int value; + + explicit deleted_move(int val = 0) : value(val) { + ++count; + } + explicit deleted_move(int, int val, int) : value(val) { + ++count; + } + deleted_move(std::initializer_list<int> il) : value(*il.begin()) { + ++count; + } + + deleted_move(deleted_move const & other) noexcept { + value = other.value; + ++count; + ++copied; + ++const_copied; + } + + deleted_move(deleted_move& other) noexcept { + value = other.value; + ++count; + ++copied; + ++non_const_copied; + } + + deleted_move(deleted_move && other) = delete; + + ~deleted_move() { + value = -1; + --count; + } + +private: + deleted_move& operator=(deleted_move const&) = delete; + deleted_move& operator=(deleted_move&&) = delete; +}; + +int deleted_move::count = 0; +int deleted_move::copied = 0; +int deleted_move::moved = 0; +int deleted_move::const_copied = 0; +int deleted_move::non_const_copied = 0; + + // The exception type thrown by 'small_throws_on_copy', 'large_throws_on_copy' // and 'throws_on_move'. struct my_any_exception {}; @@ -236,19 +324,24 @@ void throwMyAnyExpression() { struct small_throws_on_copy { static int count; + static int copied; + static int moved; + static void reset() { count = copied = moved = 0; } int value; explicit small_throws_on_copy(int val = 0) : value(val) { ++count; } - + explicit small_throws_on_copy(int, int val, int) : value(val) { + ++count; + } small_throws_on_copy(small_throws_on_copy const &) { throwMyAnyExpression(); } small_throws_on_copy(small_throws_on_copy && other) throw() { value = other.value; - ++count; + ++count; ++moved; } ~small_throws_on_copy() { @@ -260,26 +353,35 @@ private: }; int small_throws_on_copy::count = 0; +int small_throws_on_copy::copied = 0; +int small_throws_on_copy::moved = 0; + // A test type that will NOT trigger the small object optimization within 'any'. // this type throws if it is copied. struct large_throws_on_copy { static int count; + static int copied; + static int moved; + static void reset() { count = copied = moved = 0; } int value = 0; explicit large_throws_on_copy(int val = 0) : value(val) { data[0] = 0; ++count; } - + explicit large_throws_on_copy(int, int val, int) : value(val) { + data[0] = 0; + ++count; + } large_throws_on_copy(large_throws_on_copy const &) { throwMyAnyExpression(); } large_throws_on_copy(large_throws_on_copy && other) throw() { value = other.value; - ++count; + ++count; ++moved; } ~large_throws_on_copy() { @@ -293,19 +395,24 @@ private: }; int large_throws_on_copy::count = 0; +int large_throws_on_copy::copied = 0; +int large_throws_on_copy::moved = 0; // A test type that throws when it is moved. This object will NOT trigger // the small object optimization in 'any'. struct throws_on_move { static int count; + static int copied; + static int moved; + static void reset() { count = copied = moved = 0; } int value; explicit throws_on_move(int val = 0) : value(val) { ++count; } - + explicit throws_on_move(int, int val, int) : value(val) { ++count; } throws_on_move(throws_on_move const & other) { value = other.value; - ++count; + ++count; ++copied; } throws_on_move(throws_on_move &&) { @@ -321,6 +428,56 @@ private: }; int throws_on_move::count = 0; +int throws_on_move::copied = 0; +int throws_on_move::moved = 0; + +struct small_tracked_t { + small_tracked_t() + : arg_types(&makeArgumentID<>()) {} + small_tracked_t(small_tracked_t const&) noexcept + : arg_types(&makeArgumentID<small_tracked_t const&>()) {} + small_tracked_t(small_tracked_t &&) noexcept + : arg_types(&makeArgumentID<small_tracked_t &&>()) {} + template <class ...Args> + explicit small_tracked_t(Args&&...) + : arg_types(&makeArgumentID<Args...>()) {} + template <class ...Args> + explicit small_tracked_t(std::initializer_list<int>, Args&&...) + : arg_types(&makeArgumentID<std::initializer_list<int>, Args...>()) {} + + TypeID const* arg_types; +}; +static_assert(IsSmallObject<small_tracked_t>::value, "must be small"); + +struct large_tracked_t { + large_tracked_t() + : arg_types(&makeArgumentID<>()) { dummy[0] = 42; } + large_tracked_t(large_tracked_t const&) noexcept + : arg_types(&makeArgumentID<large_tracked_t const&>()) {} + large_tracked_t(large_tracked_t &&) noexcept + : arg_types(&makeArgumentID<large_tracked_t &&>()) {} + template <class ...Args> + explicit large_tracked_t(Args&&...) + : arg_types(&makeArgumentID<Args...>()) {} + template <class ...Args> + explicit large_tracked_t(std::initializer_list<int>, Args&&...) + : arg_types(&makeArgumentID<std::initializer_list<int>, Args...>()) {} + + TypeID const* arg_types; + int dummy[10]; +}; + +static_assert(!IsSmallObject<large_tracked_t>::value, "must be small"); + + +template <class Type, class ...Args> +void assertArgsMatch(std::any const& a) { + using namespace std; + using namespace std::experimental; + assert(a.has_value()); + assert(containsType<Type>(a)); + assert(any_cast<Type const &>(a).arg_types == &makeArgumentID<Args...>()); +}; #endif diff --git a/libcxx/test/support/experimental_any_helpers.h b/libcxx/test/support/experimental_any_helpers.h new file mode 100644 index 00000000000..50bd6d68fba --- /dev/null +++ b/libcxx/test/support/experimental_any_helpers.h @@ -0,0 +1,326 @@ +//===----------------------------------------------------------------------===// +// +// 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. +// +//===----------------------------------------------------------------------===// +#ifndef EXPERIMENTAL_ANY_HELPERS_H +#define EXPERIMENTAL_ANY_HELPERS_H + +#include <experimental/any> +#include <typeinfo> +#include <type_traits> +#include <cassert> + +#include "test_macros.h" + +#if !defined(TEST_HAS_NO_RTTI) +#define RTTI_ASSERT(X) assert(X) +#else +#define RTTI_ASSERT(X) +#endif + +template <class _Tp> + struct IsSmallObject + : public std::integral_constant<bool + , sizeof(_Tp) <= (sizeof(void*)*3) + && std::alignment_of<void*>::value + % std::alignment_of<_Tp>::value == 0 + && std::is_nothrow_move_constructible<_Tp>::value + > + {}; + + +// Return 'true' if 'Type' will be considered a small type by 'any' +template <class Type> +bool isSmallType() { +#if defined(_LIBCPP_VERSION) + return std::experimental::__any_imp::_IsSmallObject<Type>::value; +#else + return IsSmallObject<Type>::value; +#endif + +} + +// Assert that an object is empty. If the object used to contain an object +// of type 'LastType' check that it can no longer be accessed. +template <class LastType = int> +void assertEmpty(std::experimental::any const& a) { + assert(a.empty()); + RTTI_ASSERT(a.type() == typeid(void)); + assert(std::experimental::any_cast<LastType const>(&a) == nullptr); +} + +// Assert that an 'any' object stores the specified 'Type' and 'value'. +template <class Type> +void assertContains(std::experimental::any const& a, int value = 1) { + assert(!a.empty()); + RTTI_ASSERT(a.type() == typeid(Type)); + assert(std::experimental::any_cast<Type const &>(a).value == value); +} + +// Modify the value of a "test type" stored within an any to the specified +// 'value'. +template <class Type> +void modifyValue(std::experimental::any& a, int value) { + assert(!a.empty()); + RTTI_ASSERT(a.type() == typeid(Type)); + std::experimental::any_cast<Type&>(a).value = value; +} + +// A test type that will trigger the small object optimization within 'any'. +template <int Dummy = 0> +struct small_type +{ + static int count; + static int copied; + static int moved; + static int const_copied; + static int non_const_copied; + + static void reset() { + small_type::copied = 0; + small_type::moved = 0; + small_type::const_copied = 0; + small_type::non_const_copied = 0; + } + + int value; + + explicit small_type(int val) : value(val) { + ++count; + } + + small_type(small_type const & other) throw() { + value = other.value; + ++count; + ++copied; + ++const_copied; + } + + small_type(small_type& other) throw() { + value = other.value; + ++count; + ++copied; + ++non_const_copied; + } + + small_type(small_type && other) throw() { + value = other.value; + other.value = 0; + ++count; + ++moved; + } + + ~small_type() { + value = -1; + --count; + } + +private: + small_type& operator=(small_type const&) = delete; + small_type& operator=(small_type&&) = delete; +}; + +template <int Dummy> +int small_type<Dummy>::count = 0; + +template <int Dummy> +int small_type<Dummy>::copied = 0; + +template <int Dummy> +int small_type<Dummy>::moved = 0; + +template <int Dummy> +int small_type<Dummy>::const_copied = 0; + +template <int Dummy> +int small_type<Dummy>::non_const_copied = 0; + +typedef small_type<> small; +typedef small_type<1> small1; +typedef small_type<2> small2; + + +// A test type that will NOT trigger the small object optimization in any. +template <int Dummy = 0> +struct large_type +{ + static int count; + static int copied; + static int moved; + static int const_copied; + static int non_const_copied; + + static void reset() { + large_type::copied = 0; + large_type::moved = 0; + large_type::const_copied = 0; + large_type::non_const_copied = 0; + } + + int value; + + large_type(int val) : value(val) { + ++count; + data[0] = 0; + } + + large_type(large_type const & other) { + value = other.value; + ++count; + ++copied; + ++const_copied; + } + + large_type(large_type & other) { + value = other.value; + ++count; + ++copied; + ++non_const_copied; + } + + large_type(large_type && other) { + value = other.value; + other.value = 0; + ++count; + ++moved; + } + + ~large_type() { + value = 0; + --count; + } + +private: + large_type& operator=(large_type const&) = delete; + large_type& operator=(large_type &&) = delete; + int data[10]; +}; + +template <int Dummy> +int large_type<Dummy>::count = 0; + +template <int Dummy> +int large_type<Dummy>::copied = 0; + +template <int Dummy> +int large_type<Dummy>::moved = 0; + +template <int Dummy> +int large_type<Dummy>::const_copied = 0; + +template <int Dummy> +int large_type<Dummy>::non_const_copied = 0; + +typedef large_type<> large; +typedef large_type<1> large1; +typedef large_type<2> large2; + +// The exception type thrown by 'small_throws_on_copy', 'large_throws_on_copy' +// and 'throws_on_move'. +struct my_any_exception {}; + +void throwMyAnyExpression() { +#if !defined(TEST_HAS_NO_EXCEPTIONS) + throw my_any_exception(); +#else + assert(false && "Exceptions are disabled"); +#endif +} + +// A test type that will trigger the small object optimization within 'any'. +// this type throws if it is copied. +struct small_throws_on_copy +{ + static int count; + int value; + + explicit small_throws_on_copy(int val = 0) : value(val) { + ++count; + } + + small_throws_on_copy(small_throws_on_copy const &) { + throwMyAnyExpression(); + } + + small_throws_on_copy(small_throws_on_copy && other) throw() { + value = other.value; + ++count; + } + + ~small_throws_on_copy() { + --count; + } +private: + small_throws_on_copy& operator=(small_throws_on_copy const&) = delete; + small_throws_on_copy& operator=(small_throws_on_copy &&) = delete; +}; + +int small_throws_on_copy::count = 0; + +// A test type that will NOT trigger the small object optimization within 'any'. +// this type throws if it is copied. +struct large_throws_on_copy +{ + static int count; + int value = 0; + + explicit large_throws_on_copy(int val = 0) : value(val) { + data[0] = 0; + ++count; + } + + large_throws_on_copy(large_throws_on_copy const &) { + throwMyAnyExpression(); + } + + large_throws_on_copy(large_throws_on_copy && other) throw() { + value = other.value; + ++count; + } + + ~large_throws_on_copy() { + --count; + } + +private: + large_throws_on_copy& operator=(large_throws_on_copy const&) = delete; + large_throws_on_copy& operator=(large_throws_on_copy &&) = delete; + int data[10]; +}; + +int large_throws_on_copy::count = 0; + +// A test type that throws when it is moved. This object will NOT trigger +// the small object optimization in 'any'. +struct throws_on_move +{ + static int count; + int value; + + explicit throws_on_move(int val = 0) : value(val) { ++count; } + + throws_on_move(throws_on_move const & other) { + value = other.value; + ++count; + } + + throws_on_move(throws_on_move &&) { + throwMyAnyExpression(); + } + + ~throws_on_move() { + --count; + } +private: + throws_on_move& operator=(throws_on_move const&) = delete; + throws_on_move& operator=(throws_on_move &&) = delete; +}; + +int throws_on_move::count = 0; + + +#endif diff --git a/libcxx/test/support/test_macros.h b/libcxx/test/support/test_macros.h index 3e4400af00e..752bcdaecdd 100644 --- a/libcxx/test/support/test_macros.h +++ b/libcxx/test/support/test_macros.h @@ -33,6 +33,13 @@ #else #define TEST_HAS_BUILTIN(X) 0 #endif +#ifdef __is_identifier +// '__is_identifier' returns '0' if '__x' is a reserved identifier provided by +// the compiler and '1' otherwise. +#define TEST_HAS_BUILTIN_IDENTIFIER(X) !__is_identifier(X) +#else +#define TEST_HAS_BUILTIN_IDENTIFIER(X) 0 +#endif #if defined(__apple_build_version__) #define TEST_APPLE_CLANG_VER (__clang_major__ * 100) + __clang_minor__ |