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 /// \file
10 /// This file provides Any, a non-template class modeled in the spirit of
11 /// std::any. The idea is to provide a type-safe replacement for C's void*.
12 /// It can hold a value of any copy-constructible copy-assignable type
13 ///
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_ADT_ANY_H
17 #define LLVM_ADT_ANY_H
18
19 #include "llvm/ADT/STLForwardCompat.h"
20 #include "llvm/Support/Compiler.h"
21
22 #include <cassert>
23 #include <memory>
24 #include <type_traits>
25
26 namespace llvm {
27
28 class LLVM_EXTERNAL_VISIBILITY Any {
29
30 // The `Typeid<T>::Id` static data member below is a globally unique
31 // identifier for the type `T`. It is explicitly marked with default
32 // visibility so that when `-fvisibility=hidden` is used, the loader still
33 // merges duplicate definitions across DSO boundaries.
34 template <typename T> struct TypeId { static const char Id; };
35
36 struct StorageBase {
37 virtual ~StorageBase() = default;
38 virtual std::unique_ptr<StorageBase> clone() const = 0;
39 virtual const void *id() const = 0;
40 };
41
42 template <typename T> struct StorageImpl : public StorageBase {
StorageImplStorageImpl43 explicit StorageImpl(const T &Value) : Value(Value) {}
44
StorageImplStorageImpl45 explicit StorageImpl(T &&Value) : Value(std::move(Value)) {}
46
cloneStorageImpl47 std::unique_ptr<StorageBase> clone() const override {
48 return std::make_unique<StorageImpl<T>>(Value);
49 }
50
idStorageImpl51 const void *id() const override { return &TypeId<T>::Id; }
52
53 T Value;
54
55 private:
56 StorageImpl &operator=(const StorageImpl &Other) = delete;
57 StorageImpl(const StorageImpl &Other) = delete;
58 };
59
60 public:
61 Any() = default;
62
Any(const Any & Other)63 Any(const Any &Other)
64 : Storage(Other.Storage ? Other.Storage->clone() : nullptr) {}
65
66 // When T is Any or T is not copy-constructible we need to explicitly disable
67 // the forwarding constructor so that the copy constructor gets selected
68 // instead.
69 template <typename T,
70 std::enable_if_t<
71 llvm::conjunction<
72 llvm::negation<std::is_same<std::decay_t<T>, Any>>,
73 // We also disable this overload when an `Any` object can be
74 // converted to the parameter type because in that case,
75 // this constructor may combine with that conversion during
76 // overload resolution for determining copy
77 // constructibility, and then when we try to determine copy
78 // constructibility below we may infinitely recurse. This is
79 // being evaluated by the standards committee as a potential
80 // DR in `std::any` as well, but we're going ahead and
81 // adopting it to work-around usage of `Any` with types that
82 // need to be implicitly convertible from an `Any`.
83 llvm::negation<std::is_convertible<Any, std::decay_t<T>>>,
84 std::is_copy_constructible<std::decay_t<T>>>::value,
85 int> = 0>
Any(T && Value)86 Any(T &&Value) {
87 Storage =
88 std::make_unique<StorageImpl<std::decay_t<T>>>(std::forward<T>(Value));
89 }
90
Any(Any && Other)91 Any(Any &&Other) : Storage(std::move(Other.Storage)) {}
92
swap(Any & Other)93 Any &swap(Any &Other) {
94 std::swap(Storage, Other.Storage);
95 return *this;
96 }
97
98 Any &operator=(Any Other) {
99 Storage = std::move(Other.Storage);
100 return *this;
101 }
102
hasValue()103 bool hasValue() const { return !!Storage; }
104
reset()105 void reset() { Storage.reset(); }
106
107 private:
108 template <class T> friend T any_cast(const Any &Value);
109 template <class T> friend T any_cast(Any &Value);
110 template <class T> friend T any_cast(Any &&Value);
111 template <class T> friend const T *any_cast(const Any *Value);
112 template <class T> friend T *any_cast(Any *Value);
113 template <typename T> friend bool any_isa(const Any &Value);
114
115 std::unique_ptr<StorageBase> Storage;
116 };
117
118 template <typename T> const char Any::TypeId<T>::Id = 0;
119
120
any_isa(const Any & Value)121 template <typename T> bool any_isa(const Any &Value) {
122 if (!Value.Storage)
123 return false;
124 return Value.Storage->id() == &Any::TypeId<remove_cvref_t<T>>::Id;
125 }
126
any_cast(const Any & Value)127 template <class T> T any_cast(const Any &Value) {
128 return static_cast<T>(*any_cast<remove_cvref_t<T>>(&Value));
129 }
130
any_cast(Any & Value)131 template <class T> T any_cast(Any &Value) {
132 return static_cast<T>(*any_cast<remove_cvref_t<T>>(&Value));
133 }
134
any_cast(Any && Value)135 template <class T> T any_cast(Any &&Value) {
136 return static_cast<T>(std::move(*any_cast<remove_cvref_t<T>>(&Value)));
137 }
138
any_cast(const Any * Value)139 template <class T> const T *any_cast(const Any *Value) {
140 using U = remove_cvref_t<T>;
141 assert(Value && any_isa<T>(*Value) && "Bad any cast!");
142 if (!Value || !any_isa<U>(*Value))
143 return nullptr;
144 return &static_cast<Any::StorageImpl<U> &>(*Value->Storage).Value;
145 }
146
any_cast(Any * Value)147 template <class T> T *any_cast(Any *Value) {
148 using U = std::decay_t<T>;
149 assert(Value && any_isa<U>(*Value) && "Bad any cast!");
150 if (!Value || !any_isa<U>(*Value))
151 return nullptr;
152 return &static_cast<Any::StorageImpl<U> &>(*Value->Storage).Value;
153 }
154
155 } // end namespace llvm
156
157 #endif // LLVM_ADT_ANY_H
158