LLVM  16.0.0git
Any.h
Go to the documentation of this file.
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 
20 #include "llvm/Support/Compiler.h"
21 
22 #include <cassert>
23 #include <memory>
24 #include <type_traits>
25 
26 namespace llvm {
27 
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 {
35  static const char Id;
36  };
37 
38  struct StorageBase {
39  virtual ~StorageBase() = default;
40  virtual std::unique_ptr<StorageBase> clone() const = 0;
41  virtual const void *id() const = 0;
42  };
43 
44  template <typename T> struct StorageImpl : public StorageBase {
45  explicit StorageImpl(const T &Value) : Value(Value) {}
46 
47  explicit StorageImpl(T &&Value) : Value(std::move(Value)) {}
48 
49  std::unique_ptr<StorageBase> clone() const override {
50  return std::make_unique<StorageImpl<T>>(Value);
51  }
52 
53  const void *id() const override { return &TypeId<T>::Id; }
54 
55  T Value;
56 
57  private:
58  StorageImpl &operator=(const StorageImpl &Other) = delete;
59  StorageImpl(const StorageImpl &Other) = delete;
60  };
61 
62 public:
63  Any() = default;
64 
65  Any(const Any &Other)
66  : Storage(Other.Storage ? Other.Storage->clone() : nullptr) {}
67 
68  // When T is Any or T is not copy-constructible we need to explicitly disable
69  // the forwarding constructor so that the copy constructor gets selected
70  // instead.
71  template <typename T,
72  std::enable_if_t<
73  std::conjunction<
74  std::negation<std::is_same<std::decay_t<T>, Any>>,
75  // We also disable this overload when an `Any` object can be
76  // converted to the parameter type because in that case,
77  // this constructor may combine with that conversion during
78  // overload resolution for determining copy
79  // constructibility, and then when we try to determine copy
80  // constructibility below we may infinitely recurse. This is
81  // being evaluated by the standards committee as a potential
82  // DR in `std::any` as well, but we're going ahead and
83  // adopting it to work-around usage of `Any` with types that
84  // need to be implicitly convertible from an `Any`.
85  std::negation<std::is_convertible<Any, std::decay_t<T>>>,
86  std::is_copy_constructible<std::decay_t<T>>>::value,
87  int> = 0>
88  Any(T &&Value) {
89  Storage =
90  std::make_unique<StorageImpl<std::decay_t<T>>>(std::forward<T>(Value));
91  }
92 
93  Any(Any &&Other) : Storage(std::move(Other.Storage)) {}
94 
95  Any &swap(Any &Other) {
96  std::swap(Storage, Other.Storage);
97  return *this;
98  }
99 
100  Any &operator=(Any Other) {
101  Storage = std::move(Other.Storage);
102  return *this;
103  }
104 
105  bool has_value() const { return !!Storage; }
106 
107  void reset() { Storage.reset(); }
108 
109 private:
110  template <class T> friend T any_cast(const Any &Value);
111  template <class T> friend T any_cast(Any &Value);
112  template <class T> friend T any_cast(Any &&Value);
113  template <class T> friend const T *any_cast(const Any *Value);
114  template <class T> friend T *any_cast(Any *Value);
115  template <typename T> friend bool any_isa(const Any &Value);
116 
117  std::unique_ptr<StorageBase> Storage;
118 };
119 
120 template <typename T> const char Any::TypeId<T>::Id = 0;
121 
122 template <typename T> bool any_isa(const Any &Value) {
123  if (!Value.Storage)
124  return false;
125  return Value.Storage->id() == &Any::TypeId<remove_cvref_t<T>>::Id;
126 }
127 
128 template <class T> T any_cast(const Any &Value) {
129  return static_cast<T>(*any_cast<remove_cvref_t<T>>(&Value));
130 }
131 
132 template <class T> T any_cast(Any &Value) {
133  return static_cast<T>(*any_cast<remove_cvref_t<T>>(&Value));
134 }
135 
136 template <class T> T any_cast(Any &&Value) {
137  return static_cast<T>(std::move(*any_cast<remove_cvref_t<T>>(&Value)));
138 }
139 
140 template <class T> const T *any_cast(const Any *Value) {
141  using U = remove_cvref_t<T>;
142  assert(Value && any_isa<T>(*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 template <class T> T *any_cast(Any *Value) {
149  using U = std::decay_t<T>;
150  assert(Value && any_isa<U>(*Value) && "Bad any cast!");
151  if (!Value || !any_isa<U>(*Value))
152  return nullptr;
153  return &static_cast<Any::StorageImpl<U> &>(*Value->Storage).Value;
154 }
155 
156 } // end namespace llvm
157 
158 #endif // LLVM_ADT_ANY_H
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::Any::Any
Any(T &&Value)
Definition: Any.h:88
llvm::Any::Any
Any(Any &&Other)
Definition: Any.h:93
T
#define T
Definition: Mips16ISelLowering.cpp:341
STLForwardCompat.h
llvm::Any::Any
Any(const Any &Other)
Definition: Any.h:65
LLVM_EXTERNAL_VISIBILITY
#define LLVM_EXTERNAL_VISIBILITY
Definition: Compiler.h:127
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::Any::operator=
Any & operator=(Any Other)
Definition: Any.h:100
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1861
std::swap
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:853
llvm::any_isa
bool any_isa(const Any &Value)
Definition: Any.h:122
remove_cvref_t
llvm::Any::reset
void reset()
Definition: Any.h:107
Compiler.h
llvm::Any
Definition: Any.h:28
std
Definition: BitVector.h:851
llvm::any_cast
T any_cast(const Any &Value)
Definition: Any.h:128
llvm::TargetStackID::Value
Value
Definition: TargetFrameLowering.h:27
llvm::Any::swap
Any & swap(Any &Other)
Definition: Any.h:95
llvm::Any::has_value
bool has_value() const
Definition: Any.h:105
llvm::AMDGPU::VGPRIndexMode::Id
Id
Definition: SIDefines.h:241
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
Other
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1251