//===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #ifndef TEST_STD_CONTAINERS_CONTAINER_ADAPTORS_FLAT_HELPERS_H #define TEST_STD_CONTAINERS_CONTAINER_ADAPTORS_FLAT_HELPERS_H #include #include #include "test_macros.h" template struct CopyOnlyVector : std::vector { using std::vector::vector; constexpr CopyOnlyVector(const CopyOnlyVector&) = default; constexpr CopyOnlyVector(CopyOnlyVector&& other) : CopyOnlyVector(other) {} constexpr CopyOnlyVector(CopyOnlyVector&& other, std::vector::allocator_type alloc) : CopyOnlyVector(other, alloc) {} constexpr CopyOnlyVector& operator=(const CopyOnlyVector&) = default; constexpr CopyOnlyVector& operator=(CopyOnlyVector& other) { return this->operator=(other); } }; template struct SillyReserveVector : std::vector { using std::vector::vector; constexpr void reserve(std::size_t) { this->clear(); } }; template struct Transparent { T t; constexpr explicit operator T() const requires ConvertibleToT { return t; } }; template using ConvertibleTransparent = Transparent; template using ExplicitlyConvertibleTransparent = Transparent; template using NonConvertibleTransparent = Transparent; struct TransparentComparator { using is_transparent = void; bool* transparent_used = nullptr; TransparentComparator() = default; constexpr TransparentComparator(bool& used) : transparent_used(&used) {} template constexpr bool operator()(const T& t, const Transparent& transparent) const { if (transparent_used != nullptr) { *transparent_used = true; } return t < transparent.t; } template constexpr bool operator()(const Transparent& transparent, const T& t) const { if (transparent_used != nullptr) { *transparent_used = true; } return transparent.t < t; } template constexpr bool operator()(const T& t1, const T& t2) const { return t1 < t2; } }; struct NonTransparentComparator { template bool operator()(const T&, const Transparent&) const; template bool operator()(const Transparent&, const T&) const; template bool operator()(const T&, const T&) const; }; struct NoDefaultCtr { NoDefaultCtr() = delete; }; class Moveable { int int_; double double_; public: TEST_CONSTEXPR Moveable() : int_(0), double_(0) {} TEST_CONSTEXPR Moveable(int i, double d) : int_(i), double_(d) {} TEST_CONSTEXPR Moveable(Moveable&& x) : int_(x.int_), double_(x.double_) { x.int_ = -1; x.double_ = -1; } TEST_CONSTEXPR Moveable& operator=(Moveable&& x) { int_ = x.int_; x.int_ = -1; double_ = x.double_; x.double_ = -1; return *this; } Moveable(const Moveable&) = delete; Moveable& operator=(const Moveable&) = delete; TEST_CONSTEXPR bool operator==(const Moveable& x) const { return int_ == x.int_ && double_ == x.double_; } TEST_CONSTEXPR bool operator<(const Moveable& x) const { return int_ < x.int_ || (int_ == x.int_ && double_ < x.double_); } TEST_CONSTEXPR int get() const { return int_; } TEST_CONSTEXPR bool moved() const { return int_ == -1; } }; #ifndef TEST_HAS_NO_EXCEPTIONS template struct EmplaceUnsafeContainer : std::vector { using std::vector::vector; template auto emplace(Args&&... args) -> decltype(std::declval>().emplace(std::forward(args)...)) { if (this->size() > 1) { auto it1 = this->begin(); auto it2 = it1 + 1; // messing up the container std::iter_swap(it1, it2); } throw 42; } template auto insert(Args&&... args) -> decltype(std::declval>().insert(std::forward(args)...)) { if (this->size() > 1) { auto it1 = this->begin(); auto it2 = it1 + 1; // messing up the container std::iter_swap(it1, it2); } throw 42; } template auto insert_range(Args&&... args) -> decltype(std::declval>().insert_range(std::forward(args)...)) { if (this->size() > 1) { auto it1 = this->begin(); auto it2 = it1 + 1; // messing up the container std::iter_swap(it1, it2); } throw 42; } }; template struct ThrowOnEraseContainer : std::vector { using std::vector::vector; template auto erase(Args&&... args) -> decltype(std::declval>().erase(std::forward(args)...)) { throw 42; } }; template struct ThrowOnMoveContainer : std::vector { using std::vector::vector; ThrowOnMoveContainer(ThrowOnMoveContainer&&) { throw 42; } ThrowOnMoveContainer& operator=(ThrowOnMoveContainer&&) { throw 42; } }; #endif // TEST_HAS_NO_EXCEPTIONS #endif // TEST_STD_CONTAINERS_CONTAINER_ADAPTORS_FLAT_HELPERS_H