Any.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- Any.h - Generic type erased holder of any type -----------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// This file provides Any, a non-template class modeled in the spirit of
  16. /// std::any. The idea is to provide a type-safe replacement for C's void*.
  17. /// It can hold a value of any copy-constructible copy-assignable type
  18. ///
  19. //===----------------------------------------------------------------------===//
  20. #ifndef LLVM_ADT_ANY_H
  21. #define LLVM_ADT_ANY_H
  22. #include "llvm/ADT/STLForwardCompat.h"
  23. #include "llvm/Support/Compiler.h"
  24. #include <cassert>
  25. #include <memory>
  26. #include <type_traits>
  27. namespace llvm {
  28. class LLVM_EXTERNAL_VISIBILITY Any {
  29. // The `Typeid<T>::Id` static data member below is a globally unique
  30. // identifier for the type `T`. It is explicitly marked with default
  31. // visibility so that when `-fvisibility=hidden` is used, the loader still
  32. // merges duplicate definitions across DSO boundaries.
  33. template <typename T> struct TypeId { static const char Id; };
  34. struct StorageBase {
  35. virtual ~StorageBase() = default;
  36. virtual std::unique_ptr<StorageBase> clone() const = 0;
  37. virtual const void *id() const = 0;
  38. };
  39. template <typename T> struct StorageImpl : public StorageBase {
  40. explicit StorageImpl(const T &Value) : Value(Value) {}
  41. explicit StorageImpl(T &&Value) : Value(std::move(Value)) {}
  42. std::unique_ptr<StorageBase> clone() const override {
  43. return std::make_unique<StorageImpl<T>>(Value);
  44. }
  45. const void *id() const override { return &TypeId<T>::Id; }
  46. T Value;
  47. private:
  48. StorageImpl &operator=(const StorageImpl &Other) = delete;
  49. StorageImpl(const StorageImpl &Other) = delete;
  50. };
  51. public:
  52. Any() = default;
  53. Any(const Any &Other)
  54. : Storage(Other.Storage ? Other.Storage->clone() : nullptr) {}
  55. // When T is Any or T is not copy-constructible we need to explicitly disable
  56. // the forwarding constructor so that the copy constructor gets selected
  57. // instead.
  58. template <typename T,
  59. std::enable_if_t<
  60. llvm::conjunction<
  61. llvm::negation<std::is_same<std::decay_t<T>, Any>>,
  62. // We also disable this overload when an `Any` object can be
  63. // converted to the parameter type because in that case,
  64. // this constructor may combine with that conversion during
  65. // overload resolution for determining copy
  66. // constructibility, and then when we try to determine copy
  67. // constructibility below we may infinitely recurse. This is
  68. // being evaluated by the standards committee as a potential
  69. // DR in `std::any` as well, but we're going ahead and
  70. // adopting it to work-around usage of `Any` with types that
  71. // need to be implicitly convertible from an `Any`.
  72. llvm::negation<std::is_convertible<Any, std::decay_t<T>>>,
  73. std::is_copy_constructible<std::decay_t<T>>>::value,
  74. int> = 0>
  75. Any(T &&Value) {
  76. Storage =
  77. std::make_unique<StorageImpl<std::decay_t<T>>>(std::forward<T>(Value));
  78. }
  79. Any(Any &&Other) : Storage(std::move(Other.Storage)) {}
  80. Any &swap(Any &Other) {
  81. std::swap(Storage, Other.Storage);
  82. return *this;
  83. }
  84. Any &operator=(Any Other) {
  85. Storage = std::move(Other.Storage);
  86. return *this;
  87. }
  88. bool hasValue() const { return !!Storage; }
  89. void reset() { Storage.reset(); }
  90. private:
  91. template <class T> friend T any_cast(const Any &Value);
  92. template <class T> friend T any_cast(Any &Value);
  93. template <class T> friend T any_cast(Any &&Value);
  94. template <class T> friend const T *any_cast(const Any *Value);
  95. template <class T> friend T *any_cast(Any *Value);
  96. template <typename T> friend bool any_isa(const Any &Value);
  97. std::unique_ptr<StorageBase> Storage;
  98. };
  99. template <typename T> const char Any::TypeId<T>::Id = 0;
  100. template <typename T> bool any_isa(const Any &Value) {
  101. if (!Value.Storage)
  102. return false;
  103. return Value.Storage->id() == &Any::TypeId<remove_cvref_t<T>>::Id;
  104. }
  105. template <class T> T any_cast(const Any &Value) {
  106. return static_cast<T>(*any_cast<remove_cvref_t<T>>(&Value));
  107. }
  108. template <class T> T any_cast(Any &Value) {
  109. return static_cast<T>(*any_cast<remove_cvref_t<T>>(&Value));
  110. }
  111. template <class T> T any_cast(Any &&Value) {
  112. return static_cast<T>(std::move(*any_cast<remove_cvref_t<T>>(&Value)));
  113. }
  114. template <class T> const T *any_cast(const Any *Value) {
  115. using U = remove_cvref_t<T>;
  116. assert(Value && any_isa<T>(*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. template <class T> T *any_cast(Any *Value) {
  122. using U = std::decay_t<T>;
  123. assert(Value && any_isa<U>(*Value) && "Bad any cast!");
  124. if (!Value || !any_isa<U>(*Value))
  125. return nullptr;
  126. return &static_cast<Any::StorageImpl<U> &>(*Value->Storage).Value;
  127. }
  128. } // end namespace llvm
  129. #endif // LLVM_ADT_ANY_H
  130. #ifdef __GNUC__
  131. #pragma GCC diagnostic pop
  132. #endif