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 #ifndef TEST_HAS_NO_EXCEPTIONS
26 CopyThrows() = default;
27 CopyThrows(CopyThrows
const&) { throw 42; }
28 CopyThrows
& operator=(CopyThrows
const&) { throw 42; }
33 MoveThrows() { ++alive
; }
34 MoveThrows(MoveThrows
const&) {++alive
;}
35 MoveThrows(MoveThrows
&&) { throw 42; }
36 MoveThrows
& operator=(MoveThrows
const&) { return *this; }
37 MoveThrows
& operator=(MoveThrows
&&) { throw 42; }
38 ~MoveThrows() { --alive
; }
41 int MoveThrows::alive
= 0;
45 MakeEmptyT() { ++alive
; }
46 MakeEmptyT(MakeEmptyT
const&) {
48 // Don't throw from the copy constructor since variant's assignment
49 // operator performs a copy before committing to the assignment.
51 MakeEmptyT(MakeEmptyT
&&) {
54 MakeEmptyT
& operator=(MakeEmptyT
const&) {
57 MakeEmptyT
& operator=(MakeEmptyT
&&) {
60 ~MakeEmptyT() { --alive
; }
62 static_assert(std::is_swappable_v
<MakeEmptyT
>, ""); // required for test
64 int MakeEmptyT::alive
= 0;
66 template <class Variant
>
67 void makeEmpty(Variant
& v
) {
68 Variant
v2(std::in_place_type
<MakeEmptyT
>);
73 assert(v
.valueless_by_exception());
76 #endif // TEST_HAS_NO_EXCEPTIONS
78 enum CallType
: unsigned {
86 inline constexpr CallType
operator|(CallType LHS
, CallType RHS
) {
87 return static_cast<CallType
>(static_cast<unsigned>(LHS
) |
88 static_cast<unsigned>(RHS
));
91 struct ForwardingCallObject
{
93 template <class... Args
>
94 ForwardingCallObject
& operator()(Args
&&...) & {
95 set_call
<Args
&&...>(CT_NonConst
| CT_LValue
);
99 template <class... Args
>
100 const ForwardingCallObject
& operator()(Args
&&...) const & {
101 set_call
<Args
&&...>(CT_Const
| CT_LValue
);
105 template <class... Args
>
106 ForwardingCallObject
&& operator()(Args
&&...) && {
107 set_call
<Args
&&...>(CT_NonConst
| CT_RValue
);
108 return std::move(*this);
111 template <class... Args
>
112 const ForwardingCallObject
&& operator()(Args
&&...) const && {
113 set_call
<Args
&&...>(CT_Const
| CT_RValue
);
114 return std::move(*this);
117 template <class... Args
> static void set_call(CallType type
) {
118 assert(last_call_type
== CT_None
);
119 assert(last_call_args
== nullptr);
120 last_call_type
= type
;
121 last_call_args
= std::addressof(makeArgumentID
<Args
...>());
124 template <class... Args
> static bool check_call(CallType type
) {
125 bool result
= last_call_type
== type
&& last_call_args
&&
126 *last_call_args
== makeArgumentID
<Args
...>();
127 last_call_type
= CT_None
;
128 last_call_args
= nullptr;
132 // To check explicit return type for visit<R>
133 constexpr operator int() const
138 static CallType last_call_type
;
139 static const TypeID
*last_call_args
;
142 CallType
ForwardingCallObject::last_call_type
= CT_None
;
143 const TypeID
*ForwardingCallObject::last_call_args
= nullptr;
146 template <class... Args
> constexpr int operator()(int f
, Args
&&...) const {
152 template <class... Args
> constexpr int operator()(Args
&&...) const {
153 return sizeof...(Args
);
157 #endif // SUPPORT_VARIANT_TEST_HELPERS_H