1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
//===- Any.h - Generic type erased holder of any type -----------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file provides Any, a non-template class modeled in the spirit of
// std::any. The idea is to provide a type-safe replacement for C's void*.
// It can hold a value of any copy-constructible copy-assignable type
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_ADT_ANY_H
#define LLVM_ADT_ANY_H
#include "llvm/ADT/STLExtras.h"
#include <cassert>
#include <memory>
#include <type_traits>
namespace llvm {
class Any {
template <typename T> struct TypeId { static const char Id; };
struct StorageBase {
virtual ~StorageBase() = default;
virtual std::unique_ptr<StorageBase> clone() const = 0;
virtual const void *id() const = 0;
};
template <typename T> struct StorageImpl : public StorageBase {
explicit StorageImpl(const T &Value) : Value(Value) {}
explicit StorageImpl(T &&Value) : Value(std::move(Value)) {}
std::unique_ptr<StorageBase> clone() const override {
return llvm::make_unique<StorageImpl<T>>(Value);
}
const void *id() const override { return &TypeId<T>::Id; }
T Value;
private:
StorageImpl &operator=(const StorageImpl &Other) = delete;
StorageImpl(const StorageImpl &Other) = delete;
};
public:
Any() = default;
Any(const Any &Other)
: Storage(Other.Storage ? Other.Storage->clone() : nullptr) {}
// When T is Any or T is not copy-constructible we need to explicitly disable
// the forwarding constructor so that the copy constructor gets selected
// instead.
template <
typename T,
typename std::enable_if<
llvm::conjunction<
llvm::negation<std::is_same<typename std::decay<T>::type, Any>>,
std::is_copy_constructible<typename std::decay<T>::type>>::value,
int>::type = 0>
Any(T &&Value) {
using U = typename std::decay<T>::type;
Storage = llvm::make_unique<StorageImpl<U>>(std::forward<T>(Value));
}
Any(Any &&Other) : Storage(std::move(Other.Storage)) {}
Any &swap(Any &Other) {
std::swap(Storage, Other.Storage);
return *this;
}
Any &operator=(Any Other) {
Storage = std::move(Other.Storage);
return *this;
}
bool hasValue() const { return !!Storage; }
void reset() { Storage.reset(); }
private:
template <class T> friend T any_cast(const Any &Value);
template <class T> friend T any_cast(Any &Value);
template <class T> friend T any_cast(Any &&Value);
template <class T> friend const T *any_cast(const Any *Value);
template <class T> friend T *any_cast(Any *Value);
template <typename T> friend bool any_isa(const Any &Value);
std::unique_ptr<StorageBase> Storage;
};
template <typename T> const char Any::TypeId<T>::Id = 0;
template <typename T> bool any_isa(const Any &Value) {
if (!Value.Storage)
return false;
using U =
typename std::remove_cv<typename std::remove_reference<T>::type>::type;
return Value.Storage->id() == &Any::TypeId<U>::Id;
}
template <class T> T any_cast(const Any &Value) {
using U =
typename std::remove_cv<typename std::remove_reference<T>::type>::type;
return static_cast<T>(*any_cast<U>(&Value));
}
template <class T> T any_cast(Any &Value) {
using U =
typename std::remove_cv<typename std::remove_reference<T>::type>::type;
return static_cast<T>(*any_cast<U>(&Value));
}
template <class T> T any_cast(Any &&Value) {
using U =
typename std::remove_cv<typename std::remove_reference<T>::type>::type;
return static_cast<T>(std::move(*any_cast<U>(&Value)));
}
template <class T> const T *any_cast(const Any *Value) {
using U =
typename std::remove_cv<typename std::remove_reference<T>::type>::type;
assert(Value && any_isa<T>(*Value) && "Bad any cast!");
if (!Value || !any_isa<U>(*Value))
return nullptr;
return &static_cast<Any::StorageImpl<U> &>(*Value->Storage).Value;
}
template <class T> T *any_cast(Any *Value) {
using U = typename std::decay<T>::type;
assert(Value && any_isa<U>(*Value) && "Bad any cast!");
if (!Value || !any_isa<U>(*Value))
return nullptr;
return &static_cast<Any::StorageImpl<U> &>(*Value->Storage).Value;
}
} // end namespace llvm
#endif // LLVM_ADT_ANY_H
|