Class ObjectRef#

Inheritance Relationships#

Derived Types#

Class Documentation#

class ObjectRef#

ObjectRef is the base class for named, managed references to TVM-FFI objects.

Each reference holds an ObjectPtr<Object>, which manages the underlying object’s lifetime through reference counting. Use operator-> to access data members and member functions of the underlying object.

A reference class can define its own constructors, member functions, and operators, and inherit from other reference classes to share and specialize its interface. It can also impose additional constraints on the underlying object.

Subclassed by tvm::ffi::Array< tvm::ffi::Any >, tvm::ffi::Array< tvm::ffi::Tuple< int32_t, tvm::ffi::Function > >, tvm::ffi::Dict< tvm::ffi::Any, tvm::ffi::ObjectRef >, tvm::ffi::Dict< tvm::ffi::String, tvm::ffi::Dict< tvm::ffi::ObjectRef, tvm::ffi::Any > >, tvm::ffi::List< tvm::ffi::ObjectRef >, tvm::ffi::Map< tvm::ffi::String, tvm::ffi::Function >, tvm::ffi::Array< T, typename >, tvm::ffi::Dict< K, V, typename >, tvm::ffi::Enum, tvm::ffi::EnumState, tvm::ffi::Error, tvm::ffi::Function, tvm::ffi::List< T, typename >, tvm::ffi::Map< K, V, typename >, tvm::ffi::Module, tvm::ffi::Optional< T, std::enable_if_t< use_object_ref_optional_v< T > > >, tvm::ffi::Shape, tvm::ffi::StructuralKey, tvm::ffi::StructuralMutator, tvm::ffi::StructuralVisitor, tvm::ffi::Tensor, tvm::ffi::Tuple< Types >, tvm::ffi::VisitErrorContext, tvm::ffi::VisitInterrupt, tvm::ffi::reflection::AccessPath, tvm::ffi::reflection::AccessStep

Public Types

using ContainerType = Object#

type indicate the container type.

Public Functions

ObjectRef() = default#

default constructor

~ObjectRef() = default#
ObjectRef(const ObjectRef &other) = default#

copy constructor

inline ObjectRef(ObjectRef &&other) noexcept#

move constructor

inline explicit ObjectRef(ObjectPtr<Object> data)#

Constructor from existing object ptr.

inline explicit ObjectRef(UnsafeInit)#

Constructor from UnsafeInit.

inline bool same_as(const ObjectRef &other) const#

Comparator.

Parameters:

other – Another object ref.

Returns:

the compare result.

inline bool defined() const#
Returns:

whether the object is defined.

inline const Object *get() const#
Returns:

the internal object pointer

inline bool unique() const#
Returns:

whether the reference is unique

inline int use_count() const#
Returns:

The use count of the ptr, for debug purposes

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

Try to downcast the internal Object to a raw pointer of a corresponding type.

The function will return a nullptr if the cast failed.

if (const AddNode *ptr = node_ref.as<AddNode>()) {
  // This is an add node
}

Template Parameters:

ObjectType – the target type, must be a subtype of Object

Returns:

The pointer to the requested type.

template<typename ObjectRefType, typename = std::enable_if_t<std::is_base_of_v<ObjectRef, ObjectRefType>>>
inline std::optional<ObjectRefType> as() const &#

Try to downcast the ObjectRef to Optional<T> of the requested type.

If the cast fails, returns std::nullopt. If this ObjectRef is null, returns a null ref for nullable target refs and std::nullopt for non-nullable targets.

Template Parameters:

ObjectRefType – the target type, must be a subtype of ObjectRef.

Returns:

The optional value of the requested type.

template<typename ObjectRefType, typename = std::enable_if_t<std::is_base_of_v<ObjectRef, ObjectRefType>>>
inline std::optional<ObjectRefType> as() &&#

Try to move-downcast the ObjectRef to Optional<T> of the requested type.

If the cast succeeds, moves the internal object pointer to the returned ObjectRefType. If the cast fails, returns std::nullopt. If this ObjectRef is null, returns a null ref for nullable target refs and std::nullopt for non-nullable targets.

Template Parameters:

ObjectRefType – the target type, must be a subtype of ObjectRef.

Returns:

The optional value of the requested type.

template<typename ObjectRefType, typename = std::enable_if_t<std::is_base_of_v<ObjectRef, ObjectRefType>>>
ObjectRefType as_or_throw() const &#

Downcast the ObjectRef to the requested type or throw.

Template Parameters:

ObjectRefType – the target type, must be a subtype of ObjectRef

Returns:

The requested value.

template<typename ObjectRefType, typename = std::enable_if_t<std::is_base_of_v<ObjectRef, ObjectRefType>>>
ObjectRefType as_or_throw() &&#

Move-downcast the ObjectRef to the requested type or throw.

If the cast succeeds, moves the internal object pointer to the returned ObjectRefType.

Template Parameters:

ObjectRefType – the target type, must be a subtype of ObjectRef

Returns:

The requested value.

inline int32_t type_index() const#

Get the type index of the ObjectRef.

Returns:

The type index of the ObjectRef

inline std::string GetTypeKey() const#

Get the type key of the ObjectRef.

Returns:

The type key of the ObjectRef

Public Static Attributes

static constexpr bool _type_container_is_exact = true#

Whether ContainerType exactly describes the objects this ref can hold.

static constexpr bool _type_is_nullable = true#

Whether the reference can point to nullptr.

Protected Functions

inline Object *get_mutable() const#
Returns:

return a mutable internal ptr, can be used by sub-classes.

Protected Attributes

ObjectPtr<Object> data_#

Internal pointer that backs the reference.

Friends

friend struct ObjectPtrHash