[OpenACC] Implement 'collapse' for combined constructs.
[llvm-project.git] / libcxx / test / support / variant_test_helpers.h
blobd1bc36dea671ecafb5f311634dd3fa45a9d762b7
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
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
7 //
8 //===----------------------------------------------------------------------===//
10 #ifndef SUPPORT_VARIANT_TEST_HELPERS_H
11 #define SUPPORT_VARIANT_TEST_HELPERS_H
13 #include <type_traits>
14 #include <utility>
15 #include <cassert>
17 #include "test_macros.h"
18 #include "type_id.h"
20 #if TEST_STD_VER <= 14
21 #error This file requires C++17
22 #endif
24 #ifndef TEST_HAS_NO_EXCEPTIONS
25 struct CopyThrows {
26 CopyThrows() = default;
27 CopyThrows(CopyThrows const&) { throw 42; }
28 CopyThrows& operator=(CopyThrows const&) { throw 42; }
31 struct MoveThrows {
32 static int alive;
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;
43 struct MakeEmptyT {
44 static int alive;
45 MakeEmptyT() { ++alive; }
46 MakeEmptyT(MakeEmptyT const&) {
47 ++alive;
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 &&) {
52 throw 42;
54 MakeEmptyT& operator=(MakeEmptyT const&) {
55 throw 42;
57 MakeEmptyT& operator=(MakeEmptyT&&) {
58 throw 42;
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>);
69 try {
70 v = std::move(v2);
71 assert(false);
72 } catch (...) {
73 assert(v.valueless_by_exception());
76 #endif // TEST_HAS_NO_EXCEPTIONS
78 enum CallType : unsigned {
79 CT_None,
80 CT_NonConst = 1,
81 CT_Const = 2,
82 CT_LValue = 4,
83 CT_RValue = 8
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);
96 return *this;
99 template <class... Args>
100 const ForwardingCallObject& operator()(Args&&...) const & {
101 set_call<Args &&...>(CT_Const | CT_LValue);
102 return *this;
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;
129 return result;
132 // To check explicit return type for visit<R>
133 constexpr operator int() const
135 return 0;
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;
145 struct ReturnFirst {
146 template <class... Args> constexpr int operator()(int f, Args &&...) const {
147 return f;
151 struct ReturnArity {
152 template <class... Args> constexpr int operator()(Args &&...) const {
153 return sizeof...(Args);
157 #endif // SUPPORT_VARIANT_TEST_HELPERS_H