LLVM: include/llvm/ADT/Any.h Source File (original) (raw)

29

30

31

32

33

34

35

36 template struct TypeId { static char Id; };

37

38 struct StorageBase {

39 virtual ~StorageBase() = default;

40 virtual std::unique_ptr clone() const = 0;

41 virtual const void *id() const = 0;

42 };

43

44 template struct StorageImpl : public StorageBase {

46

48

49 std::unique_ptr clone() const override {

50 return std::make_unique<StorageImpl>(Value);

51 }

52

53 const void *id() const override { return &TypeId::Id; }

54

56

57 private:

58 StorageImpl &operator=(const StorageImpl &Other) = delete;

59 StorageImpl(const StorageImpl &Other) = delete;

60 };

61

62public:

64

66 : Storage(Other.Storage ? Other.Storage->clone() : nullptr) {}

67

68

69

70

71 template <typename T,

72 std::enable_if_t<

73 std::conjunction<

74 std::negation<std::is_same<std::decay_t, Any>>,

75

76

77

78

79

80

81

82

83

84

85 std::negation<std::is_convertible<Any, std::decay_t>>,

86 std::is_copy_constructible<std::decay_t>>::value,

87 int> = 0>

89 Storage =

90 std::make_unique<StorageImpl<std::decay_t>>(std::forward(Value));

91 }

92

94

99

101 Storage = std::move(Other.Storage);

102 return *this;

103 }

104

106

107 void reset() { Storage.reset(); }

108

109private:

110

111 template bool isa() const {

112 if (!Storage)

113 return false;

114 return Storage->id() == &Any::TypeId<remove_cvref_t>::Id;

115 }

116

117 template friend T any_cast(const Any &Value);

118 template friend T any_cast(Any &Value);

119 template friend T any_cast(Any &&Value);

120 template friend const T *any_cast(const Any *Value);

121 template friend T *any_cast(Any *Value);

122

123 std::unique_ptr Storage;

124};

155 return nullptr;

156 return &static_cast<Any::StorageImpl &>(*Value->Storage).Value;

157}

160 using U = std::decay_t;

162 return nullptr;

163 return &static_cast<Any::StorageImpl &>(*Value->Storage).Value;

164}