mxnet
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
mxnet::runtime::Object Class Reference

base class of all object containers. More...

#include <object.h>

Inheritance diagram for mxnet::runtime::Object:
Inheritance graph
Collaboration diagram for mxnet::runtime::Object:
Collaboration graph

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
 
 Object ()
 
 Object (const Object &other)
 
 Object (Object &&other)
 
Objectoperator= (const Object &other)
 
Objectoperator= (Object &&other)
 

Static Public Member Functions

static MXNET_DLL std::string TypeIndex2Key (uint32_t tindex)
 Get the type key of the corresponding index from runtime. More...
 
static MXNET_DLL size_t TypeIndex2KeyHash (uint32_t tindex)
 Get the type key hash of the corresponding index from runtime. More...
 
static MXNET_DLL 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 = "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 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 MXNET_DLL 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
 we always used ObjectPtr for a reference pointer to the node, so this alias can be changed in case. More...
 
class MXNetRetValue
 
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 ObjLeaf : public ObjBase {
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>();
CHECK(leaf_ptr != nullptr);
// can also cast to the base class.
CHECK(leaf_ref.as<BaseObj>() != nullptr);
}

Member Typedef Documentation

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

Object deleter.

Parameters
selfpointer to the Object.
using mxnet::runtime::Object::RefCounterType = std::atomic<int32_t>

Constructor & Destructor Documentation

mxnet::runtime::Object::Object ( )
inline
mxnet::runtime::Object::Object ( const Object other)
inline
mxnet::runtime::Object::Object ( Object &&  other)
inline

Member Function Documentation

static uint32_t mxnet::runtime::Object::_GetOrAllocRuntimeTypeIndex ( )
inlinestatic
void mxnet::runtime::Object::DecRef ( )
inlineprotected

developer function, decrease reference counter.

Note
The deleter will be called when ref_counter_ becomes zero.
static MXNET_DLL uint32_t mxnet::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.
std::string mxnet::runtime::Object::GetTypeKey ( ) const
inline
Returns
the type key of the object.
Note
this operation is expensive, can be used for error reporting.
size_t mxnet::runtime::Object::GetTypeKeyHash ( ) const
inline
Returns
A hash value of the return of GetTypeKey.
void mxnet::runtime::Object::IncRef ( )
inlineprotected

developer function, increases reference counter.

template<typename TargetType >
bool mxnet::runtime::Object::IsInstance ( ) const
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.
Object& mxnet::runtime::Object::operator= ( const Object other)
inline
Object& mxnet::runtime::Object::operator= ( Object &&  other)
inline
static uint32_t mxnet::runtime::Object::RuntimeTypeIndex ( )
inlinestatic
uint32_t mxnet::runtime::Object::type_index ( ) const
inline
Returns
The internal runtime type index of the object.
static MXNET_DLL std::string mxnet::runtime::Object::TypeIndex2Key ( uint32_t  tindex)
static

Get the type key of the corresponding index from runtime.

Parameters
tindexThe type index.
Returns
the result.
static MXNET_DLL size_t mxnet::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.
static MXNET_DLL uint32_t mxnet::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.

Friends And Related Function Documentation

friend class MXNetRetValue
friend
template<typename >
friend class ObjAllocatorBase
friend
friend class ObjectInternal
friend
template<typename >
friend class ObjectPtr
friend

we always used ObjectPtr for a reference pointer to the node, so this alias can be changed in case.

By default, ObjectPtr is a std::shared_ptr of node

Member Data Documentation

constexpr uint32_t mxnet::runtime::Object::_type_child_slots = 0
static
constexpr bool mxnet::runtime::Object::_type_child_slots_can_overflow = true
static
constexpr bool mxnet::runtime::Object::_type_final = false
static
constexpr uint32_t mxnet::runtime::Object::_type_index = TypeIndex::kDynamic
static
constexpr const char* mxnet::runtime::Object::_type_key = "Object"
static
FDeleter mxnet::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.

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

The internal reference counter.

uint32_t mxnet::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: