Line data Source code
1 : //===- Any.h - Generic type erased holder of any type -----------*- C++ -*-===//
2 : //
3 : // The LLVM Compiler Infrastructure
4 : //
5 : // This file is distributed under the University of Illinois Open Source
6 : // License. See LICENSE.TXT for details.
7 : //
8 : //===----------------------------------------------------------------------===//
9 : //
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/STLExtras.h"
20 :
21 : #include <cassert>
22 : #include <memory>
23 : #include <type_traits>
24 :
25 : namespace llvm {
26 :
27 10 : class Any {
28 : template <typename T> struct TypeId { static const char Id; };
29 :
30 : struct StorageBase {
31 0 : virtual ~StorageBase() = default;
32 : virtual std::unique_ptr<StorageBase> clone() const = 0;
33 : virtual const void *id() const = 0;
34 : };
35 :
36 0 : template <typename T> struct StorageImpl : public StorageBase {
37 2374 : explicit StorageImpl(const T &Value) : Value(Value) {}
38 :
39 1063 : explicit StorageImpl(T &&Value) : Value(std::move(Value)) {}
40 :
41 2374 : std::unique_ptr<StorageBase> clone() const override {
42 2374 : return llvm::make_unique<StorageImpl<T>>(Value);
43 : }
44 815 :
45 847 : const void *id() const override { return &TypeId<T>::Id; }
46 :
47 1444 : T Value;
48 1444 :
49 : private:
50 99 : StorageImpl &operator=(const StorageImpl &Other) = delete;
51 1619 : StorageImpl(const StorageImpl &Other) = delete;
52 : };
53 0 :
54 178 : public:
55 : Any() = default;
56 0 :
57 0 : Any(const Any &Other)
58 100 : : Storage(Other.Storage ? Other.Storage->clone() : nullptr) {}
59 :
60 36 : // When T is Any or T is not copy-constructible we need to explicitly disable
61 : // the forwarding constructor so that the copy constructor gets selected
62 : // instead.
63 : template <
64 : typename T,
65 : typename std::enable_if<
66 : llvm::conjunction<
67 : llvm::negation<std::is_same<typename std::decay<T>::type, Any>>,
68 : // We also disable this overload when an `Any` object can be
69 : // converted to the parameter type because in that case, this
70 2724 : // constructor may combine with that conversion during overload
71 : // resolution for determining copy constructibility, and then
72 : // when we try to determine copy constructibility below we may
73 8 : // infinitely recurse. This is being evaluated by the standards
74 : // committee as a potential DR in `std::any` as well, but we're
75 : // going ahead and adopting it to work-around usage of `Any` with
76 : // types that need to be implicitly convertible from an `Any`.
77 : llvm::negation<std::is_convertible<Any, typename std::decay<T>::type>>,
78 : std::is_copy_constructible<typename std::decay<T>::type>>::value,
79 : int>::type = 0>
80 26 : Any(T &&Value) {
81 : using U = typename std::decay<T>::type;
82 : Storage = llvm::make_unique<StorageImpl<U>>(std::forward<T>(Value));
83 26 : }
84 :
85 : Any(Any &&Other) : Storage(std::move(Other.Storage)) {}
86 888 :
87 : Any &swap(Any &Other) {
88 : std::swap(Storage, Other.Storage);
89 920 : return *this;
90 595 : }
91 :
92 136 : Any &operator=(Any Other) {
93 595 : Storage = std::move(Other.Storage);
94 248 : return *this;
95 117 : }
96 16 :
97 248 : bool hasValue() const { return !!Storage; }
98 13 :
99 18 : void reset() { Storage.reset(); }
100 38 :
101 32 : private:
102 2 : template <class T> friend T any_cast(const Any &Value);
103 40 : template <class T> friend T any_cast(Any &Value);
104 82 : template <class T> friend T any_cast(Any &&Value);
105 : template <class T> friend const T *any_cast(const Any *Value);
106 2 : template <class T> friend T *any_cast(Any *Value);
107 51 : template <typename T> friend bool any_isa(const Any &Value);
108 0 :
109 : std::unique_ptr<StorageBase> Storage;
110 1 : };
111 3 :
112 : template <typename T> const char Any::TypeId<T>::Id = 0;
113 :
114 3 :
115 5 : template <typename T> bool any_isa(const Any &Value) {
116 150 : if (!Value.Storage)
117 : return false;
118 5 : using U =
119 : typename std::remove_cv<typename std::remove_reference<T>::type>::type;
120 150 : return Value.Storage->id() == &Any::TypeId<U>::Id;
121 : }
122 :
123 : template <class T> T any_cast(const Any &Value) {
124 : using U =
125 : typename std::remove_cv<typename std::remove_reference<T>::type>::type;
126 : return static_cast<T>(*any_cast<U>(&Value));
127 : }
128 :
129 : template <class T> T any_cast(Any &Value) {
130 : using U =
131 : typename std::remove_cv<typename std::remove_reference<T>::type>::type;
132 50 : return static_cast<T>(*any_cast<U>(&Value));
133 : }
134 :
135 : template <class T> T any_cast(Any &&Value) {
136 : using U =
137 : typename std::remove_cv<typename std::remove_reference<T>::type>::type;
138 : return static_cast<T>(std::move(*any_cast<U>(&Value)));
139 : }
140 :
141 : template <class T> const T *any_cast(const Any *Value) {
142 : using U =
143 : typename std::remove_cv<typename std::remove_reference<T>::type>::type;
144 1580 : assert(Value && any_isa<T>(*Value) && "Bad any cast!");
145 : if (!Value || !any_isa<U>(*Value))
146 : return nullptr;
147 : return &static_cast<Any::StorageImpl<U> &>(*Value->Storage).Value;
148 1580 : }
149 :
150 : template <class T> T *any_cast(Any *Value) {
151 37 : using U = typename std::decay<T>::type;
152 : assert(Value && any_isa<U>(*Value) && "Bad any cast!");
153 50 : if (!Value || !any_isa<U>(*Value))
154 608 : return nullptr;
155 86 : return &static_cast<Any::StorageImpl<U> &>(*Value->Storage).Value;
156 : }
157 :
158 : } // end namespace llvm
159 :
160 : #endif // LLVM_ADT_ANY_H
|