1 | //===- Any.h - Generic type erased holder of any type -----------*- C++ -*-===// |
2 | // |
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 |
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 { |
41 | return std::make_unique<StorageImpl<T>>(Value); |
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. |
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> |
79 | Any(T &&Value) { |
80 | Storage = |
81 | std::make_unique<StorageImpl<std::decay_t<T>>>(std::forward<T>(Value)); |
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; |
117 | return Value.Storage->id() == &Any::TypeId<remove_cvref_t<T>>::Id; |
118 | } |
119 | |
120 | template <class T> T any_cast(const Any &Value) { |
121 | return static_cast<T>(*any_cast<remove_cvref_t<T>>(&Value)); |
122 | } |
123 | |
124 | template <class T> T any_cast(Any &Value) { |
125 | return static_cast<T>(*any_cast<remove_cvref_t<T>>(&Value)); |
126 | } |
127 | |
128 | template <class T> T any_cast(Any &&Value) { |
129 | return static_cast<T>(std::move(*any_cast<remove_cvref_t<T>>(&Value))); |
130 | } |
131 | |
132 | template <class T> const T *any_cast(const Any *Value) { |
133 | using U = remove_cvref_t<T>; |
134 | assert(Value && any_isa<T>(*Value) && "Bad any cast!" ); |
135 | if (!Value || !any_isa<U>(*Value)) |
136 | return nullptr; |
137 | return &static_cast<Any::StorageImpl<U> &>(*Value->Storage).Value; |
138 | } |
139 | |
140 | template <class T> T *any_cast(Any *Value) { |
141 | using U = std::decay_t<T>; |
142 | assert(Value && any_isa<U>(*Value) && "Bad any cast!" ); |
143 | if (!Value || !any_isa<U>(*Value)) |
144 | return nullptr; |
145 | return &static_cast<Any::StorageImpl<U> &>(*Value->Storage).Value; |
146 | } |
147 | |
148 | } // end namespace llvm |
149 | |
150 | #endif // LLVM_ADT_ANY_H |
151 | |