2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef SUPPORT_VARIANT_TEST_HELPERS_H
11 #define SUPPORT_VARIANT_TEST_HELPERS_H
13 #include <type_traits>
17 #include "test_macros.h"
20 #if TEST_STD_VER <= 14
21 #error This file requires C++17
24 // FIXME: Currently the variant<T&> tests are disabled using this macro.
25 #define TEST_VARIANT_HAS_NO_REFERENCES
26 #ifdef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
27 # define TEST_VARIANT_ALLOWS_NARROWING_CONVERSIONS
30 #ifdef TEST_VARIANT_ALLOWS_NARROWING_CONVERSIONS
31 constexpr bool VariantAllowsNarrowingConversions
= true;
33 constexpr bool VariantAllowsNarrowingConversions
= false;
36 #ifndef TEST_HAS_NO_EXCEPTIONS
38 CopyThrows() = default;
39 CopyThrows(CopyThrows
const&) { throw 42; }
40 CopyThrows
& operator=(CopyThrows
const&) { throw 42; }
45 MoveThrows() { ++alive
; }
46 MoveThrows(MoveThrows
const&) {++alive
;}
47 MoveThrows(MoveThrows
&&) { throw 42; }
48 MoveThrows
& operator=(MoveThrows
const&) { return *this; }
49 MoveThrows
& operator=(MoveThrows
&&) { throw 42; }
50 ~MoveThrows() { --alive
; }
53 int MoveThrows::alive
= 0;
57 MakeEmptyT() { ++alive
; }
58 MakeEmptyT(MakeEmptyT
const&) {
60 // Don't throw from the copy constructor since variant's assignment
61 // operator performs a copy before committing to the assignment.
63 MakeEmptyT(MakeEmptyT
&&) {
66 MakeEmptyT
& operator=(MakeEmptyT
const&) {
69 MakeEmptyT
& operator=(MakeEmptyT
&&) {
72 ~MakeEmptyT() { --alive
; }
74 static_assert(std::is_swappable_v
<MakeEmptyT
>, ""); // required for test
76 int MakeEmptyT::alive
= 0;
78 template <class Variant
>
79 void makeEmpty(Variant
& v
) {
80 Variant
v2(std::in_place_type
<MakeEmptyT
>);
85 assert(v
.valueless_by_exception());
88 #endif // TEST_HAS_NO_EXCEPTIONS
90 enum CallType
: unsigned {
98 inline constexpr CallType
operator|(CallType LHS
, CallType RHS
) {
99 return static_cast<CallType
>(static_cast<unsigned>(LHS
) |
100 static_cast<unsigned>(RHS
));
103 struct ForwardingCallObject
{
105 template <class... Args
>
106 ForwardingCallObject
& operator()(Args
&&...) & {
107 set_call
<Args
&&...>(CT_NonConst
| CT_LValue
);
111 template <class... Args
>
112 const ForwardingCallObject
& operator()(Args
&&...) const & {
113 set_call
<Args
&&...>(CT_Const
| CT_LValue
);
117 template <class... Args
>
118 ForwardingCallObject
&& operator()(Args
&&...) && {
119 set_call
<Args
&&...>(CT_NonConst
| CT_RValue
);
120 return std::move(*this);
123 template <class... Args
>
124 const ForwardingCallObject
&& operator()(Args
&&...) const && {
125 set_call
<Args
&&...>(CT_Const
| CT_RValue
);
126 return std::move(*this);
129 template <class... Args
> static void set_call(CallType type
) {
130 assert(last_call_type
== CT_None
);
131 assert(last_call_args
== nullptr);
132 last_call_type
= type
;
133 last_call_args
= std::addressof(makeArgumentID
<Args
...>());
136 template <class... Args
> static bool check_call(CallType type
) {
137 bool result
= last_call_type
== type
&& last_call_args
&&
138 *last_call_args
== makeArgumentID
<Args
...>();
139 last_call_type
= CT_None
;
140 last_call_args
= nullptr;
144 // To check explicit return type for visit<R>
145 constexpr operator int() const
150 static CallType last_call_type
;
151 static const TypeID
*last_call_args
;
154 CallType
ForwardingCallObject::last_call_type
= CT_None
;
155 const TypeID
*ForwardingCallObject::last_call_args
= nullptr;
158 template <class... Args
> constexpr int operator()(int f
, Args
&&...) const {
164 template <class... Args
> constexpr int operator()(Args
&&...) const {
165 return sizeof...(Args
);
169 #endif // SUPPORT_VARIANT_TEST_HELPERS_H