tvm
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Friends | List of all members
tvm::runtime::Object Class Reference

base class of all object containers. More...

#include <object.h>

Inheritance diagram for tvm::runtime::Object:
Collaboration diagram for tvm::runtime::Object:

Public Types

typedef void(* FDeleter) (Object *self)
 Object deleter. More...
 
using RefCounterType = std::atomic< int32_t >
 

Public Member Functions

uint32_t type_index () const
 
std::string GetTypeKey () const
 
size_t GetTypeKeyHash () const
 
template<typename TargetType >
bool IsInstance () const
 
bool unique () const
 
 Object ()
 
 Object (const Object &other)
 
 Object (Object &&other)
 
Objectoperator= (const Object &other)
 
Objectoperator= (Object &&other)
 

Static Public Member Functions

static std::string TypeIndex2Key (uint32_t tindex)
 Get the type key of the corresponding index from runtime. More...
 
static size_t TypeIndex2KeyHash (uint32_t tindex)
 Get the type key hash of the corresponding index from runtime. More...
 
static uint32_t TypeKey2Index (const std::string &key)
 Get the type index of the corresponding key from runtime. More...
 
static uint32_t _GetOrAllocRuntimeTypeIndex ()
 
static uint32_t RuntimeTypeIndex ()
 

Static Public Attributes

static constexpr const char * _type_key = "runtime.Object"
 
static constexpr bool _type_final = false
 
static constexpr uint32_t _type_child_slots = 0
 
static constexpr bool _type_child_slots_can_overflow = true
 
static constexpr bool _type_has_method_visit_attrs = true
 
static constexpr bool _type_has_method_sequal_reduce = false
 
static constexpr bool _type_has_method_shash_reduce = false
 
static constexpr uint32_t _type_index = TypeIndex::kDynamic
 

Protected Member Functions

void IncRef ()
 developer function, increases reference counter. More...
 
void DecRef ()
 developer function, decrease reference counter. More...
 

Static Protected Member Functions

static uint32_t GetOrAllocRuntimeTypeIndex (const std::string &key, uint32_t static_tindex, uint32_t parent_tindex, uint32_t type_child_slots, bool type_child_slots_can_overflow)
 Get the type index using type key. More...
 

Protected Attributes

uint32_t type_index_ {0}
 Type index(tag) that indicates the type of the object. More...
 
RefCounterType ref_counter_ {0}
 The internal reference counter. More...
 
FDeleter deleter_ = nullptr
 deleter of this object to enable customized allocation. If the deleter is nullptr, no deletion will be performed. The creator of the object must always set the deleter field properly. More...
 

Friends

template<typename >
class ObjAllocatorBase
 
template<typename >
class ObjectPtr
 
class TVMRetValue
 
class ObjectInternal
 

Detailed Description

base class of all object containers.

Sub-class of objects should declare the following static constexpr fields:

The following two fields are necessary for base classes that can be sub-classed.

Two macros are used to declare helper functions in the object:

New objects can be created using make_object function. Which will automatically populate the type_index and deleter of the object.

See also
make_object
ObjectPtr
ObjectRef
// Create a base object
class BaseObj : public Object {
public:
// object fields
int field0;
// object properties
static constexpr const uint32_t _type_index = TypeIndex::kDynamic;
static constexpr const char* _type_key = "test.BaseObj";
};
class LeafObj : public BaseObj {
public:
// fields
int child_field0;
// object properties
static constexpr const uint32_t _type_index = TypeIndex::kDynamic;
static constexpr const char* _type_key = "test.LeafObj";
};
// The following code should be put into a cc file.
// Usage example.
void TestObjects() {
// create an object
ObjectRef leaf_ref(make_object<LeafObj>());
// cast to a specific instance
const LeafObj* leaf_ptr = leaf_ref.as<LeafObj>();
ICHECK(leaf_ptr != nullptr);
// can also cast to the base class.
ICHECK(leaf_ref.as<BaseObj>() != nullptr);
}
Object()
Definition: object.h:245
static constexpr const char * _type_key
Definition: object.h:226
static constexpr uint32_t _type_index
Definition: object.h:242
#define TVM_DECLARE_BASE_OBJECT_INFO(TypeName, ParentType)
helper macro to declare a base object type that can be inherited.
Definition: object.h:679
#define TVM_REGISTER_OBJECT_TYPE(TypeName)
Helper macro to register the object type to runtime. Makes sure that the runtime type table is correc...
Definition: object.h:725
@ kDynamic
Type index is allocated during runtime.
Definition: object.h:84

Member Typedef Documentation

◆ FDeleter

typedef void(* tvm::runtime::Object::FDeleter) (Object *self)

Object deleter.

Parameters
selfpointer to the Object.

◆ RefCounterType

using tvm::runtime::Object::RefCounterType = std::atomic<int32_t>

Constructor & Destructor Documentation

◆ Object() [1/3]

tvm::runtime::Object::Object ( )
inline

◆ Object() [2/3]

tvm::runtime::Object::Object ( const Object other)
inline

◆ Object() [3/3]

tvm::runtime::Object::Object ( Object &&  other)
inline

Member Function Documentation

◆ _GetOrAllocRuntimeTypeIndex()

static uint32_t tvm::runtime::Object::_GetOrAllocRuntimeTypeIndex ( )
inlinestatic

◆ DecRef()

void tvm::runtime::Object::DecRef ( )
inlineprotected

developer function, decrease reference counter.

Note
The deleter will be called when ref_counter_ becomes zero.

◆ GetOrAllocRuntimeTypeIndex()

static uint32_t tvm::runtime::Object::GetOrAllocRuntimeTypeIndex ( const std::string &  key,
uint32_t  static_tindex,
uint32_t  parent_tindex,
uint32_t  type_child_slots,
bool  type_child_slots_can_overflow 
)
staticprotected

Get the type index using type key.

When the function is first time called for a type, it will register the type to the type table in the runtime. If the static_tindex is TypeIndex::kDynamic, the function will allocate a runtime type index. Otherwise, we will populate the type table and return the static index.

Parameters
keythe type key.
static_tindexThe current _type_index field. can be TypeIndex::kDynamic.
parent_tindexThe index of the parent.
type_child_slotsNumber of slots reserved for its children.
type_child_slots_can_overflowWhether to allow child to overflow the slots.
Returns
The allocated type index.

◆ GetTypeKey()

std::string tvm::runtime::Object::GetTypeKey ( ) const
inline
Returns
the type key of the object.
Note
this operation is expensive, can be used for error reporting.

◆ GetTypeKeyHash()

size_t tvm::runtime::Object::GetTypeKeyHash ( ) const
inline
Returns
A hash value of the return of GetTypeKey.

◆ IncRef()

void tvm::runtime::Object::IncRef ( )
inlineprotected

developer function, increases reference counter.

◆ IsInstance()

template<typename TargetType >
bool tvm::runtime::Object::IsInstance
inline

Check if the object is an instance of TargetType.

Template Parameters
TargetTypeThe target type to be checked.
Returns
Whether the target type is true.

◆ operator=() [1/2]

Object& tvm::runtime::Object::operator= ( const Object other)
inline

◆ operator=() [2/2]

Object& tvm::runtime::Object::operator= ( Object &&  other)
inline

◆ RuntimeTypeIndex()

static uint32_t tvm::runtime::Object::RuntimeTypeIndex ( )
inlinestatic

◆ type_index()

uint32_t tvm::runtime::Object::type_index ( ) const
inline
Returns
The internal runtime type index of the object.

◆ TypeIndex2Key()

static std::string tvm::runtime::Object::TypeIndex2Key ( uint32_t  tindex)
static

Get the type key of the corresponding index from runtime.

Parameters
tindexThe type index.
Returns
the result.

◆ TypeIndex2KeyHash()

static size_t tvm::runtime::Object::TypeIndex2KeyHash ( uint32_t  tindex)
static

Get the type key hash of the corresponding index from runtime.

Parameters
tindexThe type index.
Returns
the related key-hash.

◆ TypeKey2Index()

static uint32_t tvm::runtime::Object::TypeKey2Index ( const std::string &  key)
static

Get the type index of the corresponding key from runtime.

Parameters
keyThe type key.
Returns
the result.

◆ unique()

bool tvm::runtime::Object::unique ( ) const
inline
Returns
Whether the cell has only one reference
Note
We use stl style naming to be consistent with known API in shared_ptr.

Friends And Related Function Documentation

◆ ObjAllocatorBase

template<typename >
friend class ObjAllocatorBase
friend

◆ ObjectInternal

friend class ObjectInternal
friend

◆ ObjectPtr

template<typename >
friend class ObjectPtr
friend

◆ TVMRetValue

friend class TVMRetValue
friend

Member Data Documentation

◆ _type_child_slots

constexpr uint32_t tvm::runtime::Object::_type_child_slots = 0
staticconstexpr

◆ _type_child_slots_can_overflow

constexpr bool tvm::runtime::Object::_type_child_slots_can_overflow = true
staticconstexpr

◆ _type_final

constexpr bool tvm::runtime::Object::_type_final = false
staticconstexpr

◆ _type_has_method_sequal_reduce

constexpr bool tvm::runtime::Object::_type_has_method_sequal_reduce = false
staticconstexpr

◆ _type_has_method_shash_reduce

constexpr bool tvm::runtime::Object::_type_has_method_shash_reduce = false
staticconstexpr

◆ _type_has_method_visit_attrs

constexpr bool tvm::runtime::Object::_type_has_method_visit_attrs = true
staticconstexpr

◆ _type_index

constexpr uint32_t tvm::runtime::Object::_type_index = TypeIndex::kDynamic
staticconstexpr

◆ _type_key

constexpr const char* tvm::runtime::Object::_type_key = "runtime.Object"
staticconstexpr

◆ deleter_

FDeleter tvm::runtime::Object::deleter_ = nullptr
protected

deleter of this object to enable customized allocation. If the deleter is nullptr, no deletion will be performed. The creator of the object must always set the deleter field properly.

◆ ref_counter_

RefCounterType tvm::runtime::Object::ref_counter_ {0}
protected

The internal reference counter.

◆ type_index_

uint32_t tvm::runtime::Object::type_index_ {0}
protected

Type index(tag) that indicates the type of the object.


The documentation for this class was generated from the following file: