53 #ifndef ABSL_TYPES_ANY_H_ 54 #define ABSL_TYPES_ANY_H_ 59 #ifdef ABSL_HAVE_STD_ANY 66 using std::bad_any_cast;
70 #else // ABSL_HAVE_STD_ANY 74 #include <initializer_list> 77 #include <type_traits> 87 #ifdef ABSL_ANY_DETAIL_HAS_RTTI 88 #error ABSL_ANY_DETAIL_HAS_RTTI cannot be directly set 89 #elif !defined(__GNUC__) || defined(__GXX_RTTI) 90 #define ABSL_ANY_DETAIL_HAS_RTTI 1 91 #endif // !defined(__GNUC__) || defined(__GXX_RTTI) 95 namespace any_internal {
97 template <
typename Type>
102 template <
typename Type>
108 template<
typename Type>
110 return &TypeTag<Type>::dummy_var;
126 template <
typename T,
typename... Args>
131 template <
typename T,
typename U,
typename... Args>
132 any make_any(std::initializer_list<U> il, Args&&... args);
147 template <
typename ValueType>
154 template <
typename ValueType>
160 template <
typename ValueType>
166 template <
typename ValueType>
167 const ValueType*
any_cast(
const any* operand) noexcept;
172 template <
typename ValueType>
216 template <
typename T>
224 constexpr
any() noexcept;
230 : obj_(other.has_value() ? other.obj_->
Clone()
236 any(
any&& other) noexcept =
default;
254 std::is_copy_constructible<VT>,
255 std::is_constructible<VT, Args...>>::value>* =
nullptr>
257 : obj_(new
Obj<VT>(in_place,
std::
forward<Args>(args)...)) {}
267 std::is_constructible<VT, std::initializer_list<U>&,
268 Args...>>::value>* =
nullptr>
271 : obj_(new
Obj<VT>(in_place, ilist,
std::
forward<Args>(args)...)) {}
290 template <
typename T,
typename VT = absl::decay_t<T>,
291 absl::enable_if_t<absl::conjunction<
292 absl::negation<std::is_same<VT, any>>,
293 std::is_copy_constructible<VT>>::value>* =
nullptr>
315 std::is_constructible<VT, Args...>::value>* =
nullptr>
319 new Obj<VT>(in_place, std::forward<Args>(args)...);
320 obj_ = std::unique_ptr<ObjInterface>(object_ptr);
321 return object_ptr->
value;
338 std::is_constructible<VT, std::initializer_list<U>&,
339 Args...>::value>* =
nullptr>
340 VT&
emplace(std::initializer_list<U> ilist, Args&&... args) {
343 new Obj<VT>(in_place, ilist, std::forward<Args>(args)...);
344 obj_ = std::unique_ptr<ObjInterface>(object_ptr);
345 return object_ptr->
value;
352 void reset() noexcept { obj_ =
nullptr; }
357 void swap(
any& other) noexcept { obj_.swap(other.obj_); }
365 bool has_value() const noexcept {
return obj_ !=
nullptr; }
367 #if ABSL_ANY_DETAIL_HAS_RTTI 370 const std::type_info&
type() const noexcept {
377 #endif // ABSL_ANY_DETAIL_HAS_RTTI 384 virtual std::unique_ptr<ObjInterface>
Clone()
const = 0;
385 virtual const void* ObjTypeId()
const noexcept = 0;
386 #if ABSL_ANY_DETAIL_HAS_RTTI 387 virtual const std::type_info& Type()
const noexcept = 0;
388 #endif // ABSL_ANY_DETAIL_HAS_RTTI 392 template <
typename T>
395 template <
typename... Args>
399 std::unique_ptr<ObjInterface>
Clone() const final {
400 return std::unique_ptr<ObjInterface>(
new Obj(in_place,
value));
403 const void*
ObjTypeId() const noexcept final {
return IdForType<T>(); }
405 #if ABSL_ANY_DETAIL_HAS_RTTI 406 const std::type_info&
Type() const noexcept final {
return typeid(T); }
407 #endif // ABSL_ANY_DETAIL_HAS_RTTI 413 if (!obj_)
return nullptr;
414 return obj_->Clone();
417 template <
typename T>
420 using NormalizedType =
421 typename std::remove_cv<typename std::remove_reference<T>::type>::type;
423 return any_internal::FastTypeId<NormalizedType>();
427 return obj_ ? obj_->ObjTypeId() : any_internal::FastTypeId<void>();
433 template <
typename ValueType>
437 template <
typename ValueType>
441 template <
typename T>
442 friend const T*
any_cast(
const any* operand) noexcept;
445 template <
typename T>
448 std::unique_ptr<ObjInterface>
obj_;
455 constexpr
any::any() noexcept = default;
457 template <typename T>
460 template <
typename T>
466 template <
typename T,
typename... Args>
472 template <
typename T,
typename U,
typename... Args>
478 template <
typename ValueType>
480 using U =
typename std::remove_cv<
481 typename std::remove_reference<ValueType>::type>::type;
483 "Invalid ValueType");
484 auto*
const result = (any_cast<U>)(&operand);
485 if (result ==
nullptr) {
488 return static_cast<ValueType
>(*result);
492 template <
typename ValueType>
494 using U =
typename std::remove_cv<
495 typename std::remove_reference<ValueType>::type>::type;
497 "Invalid ValueType");
498 auto* result = (any_cast<U>)(&operand);
499 if (result ==
nullptr) {
502 return static_cast<ValueType
>(*result);
506 template <
typename ValueType>
508 using U =
typename std::remove_cv<
509 typename std::remove_reference<ValueType>::type>::type;
511 "Invalid ValueType");
512 return static_cast<ValueType
>(
std::move((any_cast<U&>)(operand)));
516 template <
typename T>
518 return operand && operand->
GetObjTypeId() == any::IdForType<T>()
525 template <
typename T>
527 return operand && operand->
GetObjTypeId() == any::IdForType<T>()
535 #undef ABSL_ANY_DETAIL_HAS_RTTI 537 #endif // ABSL_HAVE_STD_ANY 539 #endif // ABSL_TYPES_ANY_H_
const void * GetObjTypeId() const
static constexpr const void * IdForType()
Obj(in_place_t, Args &&... args)
bool has_value() const noexcept
any make_any(std::initializer_list< U > il, Args &&... args)
constexpr T && forward(absl::remove_reference_t< T > &t) noexcept
any make_any(Args &&... args)
void swap(any &other) noexcept
typename std::decay< T >::type decay_t
const void * ObjTypeId() const noexcept final
void * Clone(FlagOpFn op, const void *obj)
any(in_place_type_t< T >, std::initializer_list< U > ilist, Args &&... args)
const std::type_info & Type() const noexcept final
void(*)(utility_internal::InPlaceTypeTag< T >) in_place_type_t
static constexpr char dummy_var
typename std::enable_if< B, T >::type enable_if_t
std::unique_ptr< ObjInterface > Clone() const final
std::unique_ptr< ObjInterface > CloneObj() const
std::unique_ptr< ObjInterface > obj_
void swap(absl::InlinedVector< T, N, A > &a, absl::InlinedVector< T, N, A > &b) noexcept(noexcept(a.swap(b)))
ValueType any_cast(const any &operand)
const std::type_info & type() const noexcept
any & operator=(any &&rhs) noexcept
constexpr const void * FastTypeId()
VT & emplace(Args &&... args)
any & operator=(const any &rhs)
any(in_place_type_t< T >, Args &&... args)
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
T * any_cast(any *operand) noexcept
VT & emplace(std::initializer_list< U > ilist, Args &&... args)