Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/tvm/ffi/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ struct ObjectPtrEqual {
TypeName::_type_child_slots_can_overflow, ParentType::_GetOrAllocRuntimeTypeIndex()); \
return tindex; \
} \
static inline const int32_t _type_index = TypeName::_GetOrAllocRuntimeTypeIndex(); \
static inline const int32_t _type_index = _GetOrAllocRuntimeTypeIndex(); \
TVM_FFI_INLINE static int32_t RuntimeTypeIndex() noexcept { return TypeName::_type_index; }

/*!
Expand Down
26 changes: 26 additions & 0 deletions tests/cpp/test_object.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,23 @@ namespace {
using namespace tvm::ffi;
using namespace tvm::ffi::testing;

template <typename T>
class CRTPObject : public Object {
public:
static constexpr int _type_child_slots [[maybe_unused]] = 0;
static constexpr bool _type_final [[maybe_unused]] = true;
TVM_FFI_DECLARE_OBJECT_INFO_PREDEFINED_TYPE_KEY(T, Object);

private:
friend T;
CRTPObject() = default;
};

class LeafObject : public CRTPObject<LeafObject> {
public:
static constexpr const char* _type_key = "test.CRTPLeaf";
};

TEST(Object, RefCounter) {
ObjectPtr<TIntObj> a = make_object<TIntObj>(11);
ObjectPtr<TIntObj> b = a;
Expand Down Expand Up @@ -60,6 +77,15 @@ TEST(Object, TypeInfo) {
EXPECT_GE(info->type_index, TypeIndex::kTVMFFIDynObjectBegin);
}

TEST(Object, CRTPObjectInfo) {
const TypeInfo* info = TVMFFIGetTypeInfo(LeafObject::RuntimeTypeIndex());
ASSERT_TRUE(info != nullptr);
EXPECT_EQ(info->type_index, LeafObject::RuntimeTypeIndex());
EXPECT_EQ(info->type_depth, 1);
EXPECT_EQ(info->type_ancestors[0]->type_index, Object::RuntimeTypeIndex());
EXPECT_GE(info->type_index, TypeIndex::kTVMFFIDynObjectBegin);
}

TEST(Object, InstanceCheck) {
ObjectPtr<Object> a = make_object<TIntObj>(11);
ObjectPtr<Object> b = make_object<TFloatObj>(11);
Expand Down
Loading