Class Any#

Class Documentation#

class Any#

Managed Any that takes strong reference to a value.

Note

Develooper invariance: the TVMFFIAny data_ in the Any can be safely used in AnyView.

Public Functions

inline void reset()#

Reset any to None.

inline void swap(Any &other) noexcept#

Swap this Any with another Any.

Parameters:

other – The other Any

inline int32_t type_index() const noexcept#
Returns:

the internal type index

inline Any()#

Default constructor.

inline ~Any()#

Destructor.

inline Any(const Any &other)#

Constructor from another Any.

Parameters:

other – The other Any

inline Any(Any &&other)#

Move constructor from another Any.

Parameters:

other – The other Any

inline Any(const AnyView &other)#

Constructor from another AnyView.

Parameters:

other – The other AnyView

template<typename T, typename = std::enable_if_t<TypeTraits<T>::convert_enabled>>
inline Any(T other)#

Constructor from a general type.

Template Parameters:

T – The value type of the other

template<typename T, typename = std::enable_if_t<TypeTraits<T>::storage_enabled || std::is_same_v<T, Any>>>
inline std::optional<T> as() &&#

Try to reinterpret the Any as a type T, return std::nullopt if it is not possible.

Note

This function won’t try to run type conversion (use try_cast for that purpose).

Template Parameters:

T – The type to cast to.

Returns:

The casted value, or std::nullopt if the cast is not possible.

template<typename T, typename = std::enable_if_t<TypeTraits<T>::convert_enabled || std::is_same_v<T, Any>>>
inline std::optional<T> as() const &#

Try to reinterpret the Any as a type T, return std::nullopt if it is not possible.

Note

This function won’t try to run type conversion (use try_cast for that purpose).

Template Parameters:

T – The type to cast to.

Returns:

The casted value, or std::nullopt if the cast is not possible.

template<typename T, typename = std::enable_if_t<std::is_base_of_v<Object, T>>>
inline const T *as() const &#

Shortcut of as Object to cast to a const pointer when T is an Object.

Template Parameters:

T – The object type.

Returns:

The requested pointer, returns nullptr if type mismatches.

template<typename T, typename = std::enable_if_t<TypeTraits<T>::convert_enabled>>
inline T cast() const &#

Cast to a type T, throw an exception if the cast is not possible.

Template Parameters:

T – The type to cast to.

template<typename T, typename = std::enable_if_t<TypeTraits<T>::storage_enabled>>
inline T cast() &&#

Cast to a type T, throw an exception if the cast is not possible.

Template Parameters:

T – The type to cast to.

template<typename T, typename = std::enable_if_t<TypeTraits<T>::convert_enabled || std::is_same_v<T, Any>>>
inline std::optional<T> try_cast() const#

Try to cast to a type T.

Note

use STL name since it to be more consistent with cast API.

Template Parameters:

T – The type to cast to.

Returns:

The casted value, or std::nullopt if the cast is not possible.

inline bool same_as(const Any &other) const noexcept#

Check if the two Any are same type and value in shallow comparison.

Parameters:

other – The other Any

Returns:

True if the two Any are same type and value, false otherwise.

inline bool same_as(const ObjectRef &other) const noexcept#

Check if any and ObjectRef are same type and value in shallow comparison.

Parameters:

other – The other ObjectRef

Returns:

True if the two Any are same type and value, false otherwise.

inline std::string GetTypeKey() const#

Get the type key of the Any.

Returns:

The type key of the Any

Protected Attributes

TVMFFIAny data_#

The underlying backing data of the any object.

Friends

friend struct AnyHash
friend struct AnyEqual