Template Class ObjectFunctor

Template Class ObjectFunctor#

Class Documentation#

template<typename FType>
class ObjectFunctor#

A dynamically dispatched functor on the type of the first argument.

Dispatch is based on the runtime type of the first object argument.

ObjectFunctor<std::string(const ffi::ObjectRef& n, std::string prefix)> tostr;
tostr.SetDispatch<prim::AddObj>([](const ffi::ObjectRef& op, std::string prefix) {
return prefix + "Add";
});
tostr.SetDispatch<IntImmObj>([](const ffi::ObjectRef& op, std::string prefix) {
return prefix + "IntImm";
});

PrimVar x("x");
PrimExpr y = x + 1;
// dispatch to IntImm, outputs "MyIntImm"
LOG(INFO) << tostr(IntImm::Int32(1), "My");
// dispatch to Add, outputs "MyAdd"
LOG(INFO) << tostr(y, "My");

A shared table can register dispatch functions during static initialization:

class Printer {
 public:
using FType = ObjectFunctor<std::string(const ffi::ObjectRef&)>;
static FType& vtable();
};

Printer::FType& Printer::vtable() {
static FType inst;
return inst;
}

TVM_FFI_STATIC_INIT_BLOCK() {
  Printer::vtable()
      .SetDispatch<prim::AddObj>([](const ffi::ObjectRef&) { return std::string("Add"); })
      .SetDispatch<IntImmObj>([](const ffi::ObjectRef&) { return std::string("IntImm"); });
}
Template Parameters:

FType – Function signature, with const ffi::ObjectRef& or const ffi::Object* as its first argument.