Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 1 | //===- Any.h - Generic type erased holder of any type -----------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file provides Any, a non-template class modeled in the spirit of |
| 10 | // std::any. The idea is to provide a type-safe replacement for C's void*. |
| 11 | // It can hold a value of any copy-constructible copy-assignable type |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_ADT_ANY_H |
| 16 | #define LLVM_ADT_ANY_H |
| 17 | |
| 18 | #include "llvm/ADT/STLExtras.h" |
| 19 | |
| 20 | #include <cassert> |
| 21 | #include <memory> |
| 22 | #include <type_traits> |
| 23 | |
| 24 | namespace llvm { |
| 25 | |
| 26 | class Any { |
| 27 | template <typename T> struct TypeId { static const char Id; }; |
| 28 | |
| 29 | struct StorageBase { |
| 30 | virtual ~StorageBase() = default; |
| 31 | virtual std::unique_ptr<StorageBase> clone() const = 0; |
| 32 | virtual const void *id() const = 0; |
| 33 | }; |
| 34 | |
| 35 | template <typename T> struct StorageImpl : public StorageBase { |
| 36 | explicit StorageImpl(const T &Value) : Value(Value) {} |
| 37 | |
| 38 | explicit StorageImpl(T &&Value) : Value(std::move(Value)) {} |
| 39 | |
| 40 | std::unique_ptr<StorageBase> clone() const override { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 41 | return std::make_unique<StorageImpl<T>>(Value); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | const void *id() const override { return &TypeId<T>::Id; } |
| 45 | |
| 46 | T Value; |
| 47 | |
| 48 | private: |
| 49 | StorageImpl &operator=(const StorageImpl &Other) = delete; |
| 50 | StorageImpl(const StorageImpl &Other) = delete; |
| 51 | }; |
| 52 | |
| 53 | public: |
| 54 | Any() = default; |
| 55 | |
| 56 | Any(const Any &Other) |
| 57 | : Storage(Other.Storage ? Other.Storage->clone() : nullptr) {} |
| 58 | |
| 59 | // When T is Any or T is not copy-constructible we need to explicitly disable |
| 60 | // the forwarding constructor so that the copy constructor gets selected |
| 61 | // instead. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 62 | template <typename T, |
| 63 | std::enable_if_t< |
| 64 | llvm::conjunction< |
| 65 | llvm::negation<std::is_same<std::decay_t<T>, Any>>, |
| 66 | // We also disable this overload when an `Any` object can be |
| 67 | // converted to the parameter type because in that case, |
| 68 | // this constructor may combine with that conversion during |
| 69 | // overload resolution for determining copy |
| 70 | // constructibility, and then when we try to determine copy |
| 71 | // constructibility below we may infinitely recurse. This is |
| 72 | // being evaluated by the standards committee as a potential |
| 73 | // DR in `std::any` as well, but we're going ahead and |
| 74 | // adopting it to work-around usage of `Any` with types that |
| 75 | // need to be implicitly convertible from an `Any`. |
| 76 | llvm::negation<std::is_convertible<Any, std::decay_t<T>>>, |
| 77 | std::is_copy_constructible<std::decay_t<T>>>::value, |
| 78 | int> = 0> |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 79 | Any(T &&Value) { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 80 | Storage = |
| 81 | std::make_unique<StorageImpl<std::decay_t<T>>>(std::forward<T>(Value)); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | Any(Any &&Other) : Storage(std::move(Other.Storage)) {} |
| 85 | |
| 86 | Any &swap(Any &Other) { |
| 87 | std::swap(Storage, Other.Storage); |
| 88 | return *this; |
| 89 | } |
| 90 | |
| 91 | Any &operator=(Any Other) { |
| 92 | Storage = std::move(Other.Storage); |
| 93 | return *this; |
| 94 | } |
| 95 | |
| 96 | bool hasValue() const { return !!Storage; } |
| 97 | |
| 98 | void reset() { Storage.reset(); } |
| 99 | |
| 100 | private: |
| 101 | template <class T> friend T any_cast(const Any &Value); |
| 102 | template <class T> friend T any_cast(Any &Value); |
| 103 | template <class T> friend T any_cast(Any &&Value); |
| 104 | template <class T> friend const T *any_cast(const Any *Value); |
| 105 | template <class T> friend T *any_cast(Any *Value); |
| 106 | template <typename T> friend bool any_isa(const Any &Value); |
| 107 | |
| 108 | std::unique_ptr<StorageBase> Storage; |
| 109 | }; |
| 110 | |
| 111 | template <typename T> const char Any::TypeId<T>::Id = 0; |
| 112 | |
| 113 | |
| 114 | template <typename T> bool any_isa(const Any &Value) { |
| 115 | if (!Value.Storage) |
| 116 | return false; |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 117 | return Value.Storage->id() == |
| 118 | &Any::TypeId<std::remove_cv_t<std::remove_reference_t<T>>>::Id; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | template <class T> T any_cast(const Any &Value) { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 122 | return static_cast<T>( |
| 123 | *any_cast<std::remove_cv_t<std::remove_reference_t<T>>>(&Value)); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | template <class T> T any_cast(Any &Value) { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 127 | return static_cast<T>( |
| 128 | *any_cast<std::remove_cv_t<std::remove_reference_t<T>>>(&Value)); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | template <class T> T any_cast(Any &&Value) { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 132 | return static_cast<T>(std::move( |
| 133 | *any_cast<std::remove_cv_t<std::remove_reference_t<T>>>(&Value))); |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | template <class T> const T *any_cast(const Any *Value) { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 137 | using U = std::remove_cv_t<std::remove_reference_t<T>>; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 138 | assert(Value && any_isa<T>(*Value) && "Bad any cast!"); |
| 139 | if (!Value || !any_isa<U>(*Value)) |
| 140 | return nullptr; |
| 141 | return &static_cast<Any::StorageImpl<U> &>(*Value->Storage).Value; |
| 142 | } |
| 143 | |
| 144 | template <class T> T *any_cast(Any *Value) { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 145 | using U = std::decay_t<T>; |
Andrew Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 146 | assert(Value && any_isa<U>(*Value) && "Bad any cast!"); |
| 147 | if (!Value || !any_isa<U>(*Value)) |
| 148 | return nullptr; |
| 149 | return &static_cast<Any::StorageImpl<U> &>(*Value->Storage).Value; |
| 150 | } |
| 151 | |
| 152 | } // end namespace llvm |
| 153 | |
| 154 | #endif // LLVM_ADT_ANY_H |