Any.h 5.2 KB

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