1 //===- Any.h - Generic type erased holder of any type -----------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_ADT_ANY_H
16 #define LLVM_ADT_ANY_H
18 #include "llvm/ADT/STLExtras.h"
22 #include <type_traits>
27 template <typename T> struct TypeId { static const char Id; };
30 virtual ~StorageBase() = default;
31 virtual std::unique_ptr<StorageBase> clone() const = 0;
32 virtual const void *id() const = 0;
35 template <typename T> struct StorageImpl : public StorageBase {
36 explicit StorageImpl(const T &Value) : Value(Value) {}
38 explicit StorageImpl(T &&Value) : Value(std::move(Value)) {}
40 std::unique_ptr<StorageBase> clone() const override {
41 return std::make_unique<StorageImpl<T>>(Value);
44 const void *id() const override { return &TypeId<T>::Id; }
49 StorageImpl &operator=(const StorageImpl &Other) = delete;
50 StorageImpl(const StorageImpl &Other) = delete;
57 : Storage(Other.Storage ? Other.Storage->clone() : nullptr) {}
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
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,
81 std::make_unique<StorageImpl<std::decay_t<T>>>(std::forward<T>(Value));
84 Any(Any &&Other) : Storage(std::move(Other.Storage)) {}
86 Any &swap(Any &Other) {
87 std::swap(Storage, Other.Storage);
91 Any &operator=(Any Other) {
92 Storage = std::move(Other.Storage);
96 bool hasValue() const { return !!Storage; }
98 void reset() { Storage.reset(); }
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);
108 std::unique_ptr<StorageBase> Storage;
111 template <typename T> const char Any::TypeId<T>::Id = 0;
114 template <typename T> bool any_isa(const Any &Value) {
117 return Value.Storage->id() ==
118 &Any::TypeId<std::remove_cv_t<std::remove_reference_t<T>>>::Id;
121 template <class T> T any_cast(const Any &Value) {
122 return static_cast<T>(
123 *any_cast<std::remove_cv_t<std::remove_reference_t<T>>>(&Value));
126 template <class T> T any_cast(Any &Value) {
127 return static_cast<T>(
128 *any_cast<std::remove_cv_t<std::remove_reference_t<T>>>(&Value));
131 template <class T> T any_cast(Any &&Value) {
132 return static_cast<T>(std::move(
133 *any_cast<std::remove_cv_t<std::remove_reference_t<T>>>(&Value)));
136 template <class T> const T *any_cast(const Any *Value) {
137 using U = std::remove_cv_t<std::remove_reference_t<T>>;
138 assert(Value && any_isa<T>(*Value) && "Bad any cast!");
139 if (!Value || !any_isa<U>(*Value))
141 return &static_cast<Any::StorageImpl<U> &>(*Value->Storage).Value;
144 template <class T> T *any_cast(Any *Value) {
145 using U = std::decay_t<T>;
146 assert(Value && any_isa<U>(*Value) && "Bad any cast!");
147 if (!Value || !any_isa<U>(*Value))
149 return &static_cast<Any::StorageImpl<U> &>(*Value->Storage).Value;
152 } // end namespace llvm
154 #endif // LLVM_ADT_ANY_H