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
27 // TODO(LLVM-19): Remove TEST_VARIANT_ALLOWS_NARROWING_CONVERSIONS
28 #ifdef _LIBCPP_ENABLE_NARROWING_CONVERSIONS_IN_VARIANT
29 # define TEST_VARIANT_ALLOWS_NARROWING_CONVERSIONS
31 #ifdef TEST_VARIANT_ALLOWS_NARROWING_CONVERSIONS
32 constexpr bool VariantAllowsNarrowingConversions
= true;
34 constexpr bool VariantAllowsNarrowingConversions
= false;
37 #ifndef TEST_HAS_NO_EXCEPTIONS
39 CopyThrows() = default;
40 CopyThrows(CopyThrows
const&) { throw 42; }
41 CopyThrows
& operator=(CopyThrows
const&) { throw 42; }
46 MoveThrows() { ++alive
; }
47 MoveThrows(MoveThrows
const&) {++alive
;}
48 MoveThrows(MoveThrows
&&) { throw 42; }
49 MoveThrows
& operator=(MoveThrows
const&) { return *this; }
50 MoveThrows
& operator=(MoveThrows
&&) { throw 42; }
51 ~MoveThrows() { --alive
; }
54 int MoveThrows::alive
= 0;
58 MakeEmptyT() { ++alive
; }
59 MakeEmptyT(MakeEmptyT
const&) {
61 // Don't throw from the copy constructor since variant's assignment
62 // operator performs a copy before committing to the assignment.
64 MakeEmptyT(MakeEmptyT
&&) {
67 MakeEmptyT
& operator=(MakeEmptyT
const&) {
70 MakeEmptyT
& operator=(MakeEmptyT
&&) {
73 ~MakeEmptyT() { --alive
; }
75 static_assert(std::is_swappable_v
<MakeEmptyT
>, ""); // required for test
77 int MakeEmptyT::alive
= 0;
79 template <class Variant
>
80 void makeEmpty(Variant
& v
) {
81 Variant
v2(std::in_place_type
<MakeEmptyT
>);
86 assert(v
.valueless_by_exception());
89 #endif // TEST_HAS_NO_EXCEPTIONS
91 enum CallType
: unsigned {
99 inline constexpr CallType
operator|(CallType LHS
, CallType RHS
) {
100 return static_cast<CallType
>(static_cast<unsigned>(LHS
) |
101 static_cast<unsigned>(RHS
));
104 struct ForwardingCallObject
{
106 template <class... Args
>
107 ForwardingCallObject
& operator()(Args
&&...) & {
108 set_call
<Args
&&...>(CT_NonConst
| CT_LValue
);
112 template <class... Args
>
113 const ForwardingCallObject
& operator()(Args
&&...) const & {
114 set_call
<Args
&&...>(CT_Const
| CT_LValue
);
118 template <class... Args
>
119 ForwardingCallObject
&& operator()(Args
&&...) && {
120 set_call
<Args
&&...>(CT_NonConst
| CT_RValue
);
121 return std::move(*this);
124 template <class... Args
>
125 const ForwardingCallObject
&& operator()(Args
&&...) const && {
126 set_call
<Args
&&...>(CT_Const
| CT_RValue
);
127 return std::move(*this);
130 template <class... Args
> static void set_call(CallType type
) {
131 assert(last_call_type
== CT_None
);
132 assert(last_call_args
== nullptr);
133 last_call_type
= type
;
134 last_call_args
= std::addressof(makeArgumentID
<Args
...>());
137 template <class... Args
> static bool check_call(CallType type
) {
138 bool result
= last_call_type
== type
&& last_call_args
&&
139 *last_call_args
== makeArgumentID
<Args
...>();
140 last_call_type
= CT_None
;
141 last_call_args
= nullptr;
145 // To check explicit return type for visit<R>
146 constexpr operator int() const
151 static CallType last_call_type
;
152 static const TypeID
*last_call_args
;
155 CallType
ForwardingCallObject::last_call_type
= CT_None
;
156 const TypeID
*ForwardingCallObject::last_call_args
= nullptr;
159 template <class... Args
> constexpr int operator()(int f
, Args
&&...) const {
165 template <class... Args
> constexpr int operator()(Args
&&...) const {
166 return sizeof...(Args
);
170 #endif // SUPPORT_VARIANT_TEST_HELPERS_H