tvm
Public Types | Public Member Functions | Friends | List of all members
tvm::runtime::TypedPackedFunc< R(Args...)> Class Template Reference

A PackedFunc wrapper to provide typed function signature. It is backed by a PackedFunc internally. More...

#include <packed_func.h>

Collaboration diagram for tvm::runtime::TypedPackedFunc< R(Args...)>:

Public Types

using TSelf = TypedPackedFunc< R(Args...)>
 short hand for this function type More...
 

Public Member Functions

 TypedPackedFunc ()
 default constructor More...
 
 TypedPackedFunc (std::nullptr_t null)
 constructor from null More...
 
 TypedPackedFunc (PackedFunc packed)
 construct by wrap a PackedFunc More...
 
 TypedPackedFunc (const TVMRetValue &value)
 constructor from TVMRetValue More...
 
 TypedPackedFunc (const TVMArgValue &value)
 constructor from TVMArgValue More...
 
 TypedPackedFunc (TVMMovableArgValueWithContext_ &&value)
 constructor from TVMMovableArgValue_ More...
 
template<typename FLambda , typename = typename std::enable_if<std::is_convertible< FLambda, std::function<R(Args...)>>::value>::type>
 TypedPackedFunc (const FLambda &typed_lambda, std::string name)
 construct from a lambda function with the same signature. More...
 
template<typename FLambda , typename = typename std::enable_if<std::is_convertible< FLambda, std::function<R(Args...)>>::value>::type>
 TypedPackedFunc (const FLambda &typed_lambda)
 construct from a lambda function with the same signature. More...
 
template<typename FLambda , typename = typename std::enable_if< std::is_convertible<FLambda, std::function<R(Args...)>>::value>::type>
TSelfoperator= (FLambda typed_lambda)
 copy assignment operator from typed lambda More...
 
TSelfoperator= (PackedFunc packed)
 copy assignment operator from PackedFunc. More...
 
TVM_ALWAYS_INLINE R operator() (Args... args) const
 Invoke the operator. More...
 
 operator PackedFunc () const
 convert to PackedFunc More...
 
const PackedFuncpacked () const
 
bool operator== (std::nullptr_t null) const
 
bool operator!= (std::nullptr_t null) const
 
template<typename FType >
void AssignTypedLambda (FType flambda, std::string name)
 
template<typename FType >
void AssignTypedLambda (FType flambda)
 

Friends

class TVMRetValue
 

Detailed Description

template<typename R, typename... Args>
class tvm::runtime::TypedPackedFunc< R(Args...)>

A PackedFunc wrapper to provide typed function signature. It is backed by a PackedFunc internally.

TypedPackedFunc enables compile time type checking. TypedPackedFunc works with the runtime system:

Developers should prefer TypedPackedFunc over PackedFunc in C++ code as it enables compile time checking. We can construct a TypedPackedFunc from a lambda function with the same signature.

// user defined lambda function.
auto addone = [](int x)->int {
return x + 1;
};
// We can directly convert
// lambda function to TypedPackedFunc
TypedPackedFunc<int(int)> ftyped(addone);
// invoke the function.
int y = ftyped(1);
// Can be directly converted to PackedFunc
PackedFunc packed = ftype;
const PackedFunc & packed() const
Definition: packed_func.h:359
TypedPackedFunc()
default constructor
Definition: packed_func.h:233
Template Parameters
RThe return value of the function.
ArgsThe argument signature of the function.

Member Typedef Documentation

◆ TSelf

template<typename R , typename... Args>
using tvm::runtime::TypedPackedFunc< R(Args...)>::TSelf = TypedPackedFunc<R(Args...)>

short hand for this function type

Constructor & Destructor Documentation

◆ TypedPackedFunc() [1/8]

template<typename R , typename... Args>
tvm::runtime::TypedPackedFunc< R(Args...)>::TypedPackedFunc ( )
inline

default constructor

◆ TypedPackedFunc() [2/8]

template<typename R , typename... Args>
tvm::runtime::TypedPackedFunc< R(Args...)>::TypedPackedFunc ( std::nullptr_t  null)
inline

constructor from null

◆ TypedPackedFunc() [3/8]

template<typename R , typename... Args>
tvm::runtime::TypedPackedFunc< R(Args...)>::TypedPackedFunc ( PackedFunc  packed)
inline

construct by wrap a PackedFunc

Example usage:

PackedFunc packed([](TVMArgs args, TVMRetValue *rv) {
int x = args[0];
*rv = x + 1;
});
// construct from packed function
TypedPackedFunc<int(int)> ftyped(packed);
// call the typed version.
ICHECK_EQ(ftyped(1), 2);
friend class TVMRetValue
Definition: packed_func.h:366
Definition: packed_func.h:38
Parameters
packedThe packed function

◆ TypedPackedFunc() [4/8]

template<typename R , typename... Args>
tvm::runtime::TypedPackedFunc< R(Args...)>::TypedPackedFunc ( const TVMRetValue value)
inline

constructor from TVMRetValue

Parameters
valueThe TVMRetValue

◆ TypedPackedFunc() [5/8]

template<typename R , typename... Args>
tvm::runtime::TypedPackedFunc< R(Args...)>::TypedPackedFunc ( const TVMArgValue value)
inline

constructor from TVMArgValue

Parameters
valueThe TVMArgValue

◆ TypedPackedFunc() [6/8]

template<typename R , typename... Args>
tvm::runtime::TypedPackedFunc< R(Args...)>::TypedPackedFunc ( TVMMovableArgValueWithContext_ &&  value)
inline

constructor from TVMMovableArgValue_

Parameters
valueThe TVMMovableArgValue_

◆ TypedPackedFunc() [7/8]

template<typename R , typename... Args>
template<typename FLambda , typename = typename std::enable_if<std::is_convertible< FLambda, std::function<R(Args...)>>::value>::type>
tvm::runtime::TypedPackedFunc< R(Args...)>::TypedPackedFunc ( const FLambda &  typed_lambda,
std::string  name 
)
inline

construct from a lambda function with the same signature.

Example usage:

auto typed_lambda = [](int x)->int { return x + 1; }
// construct from packed function
TypedPackedFunc<int(int)> ftyped(typed_lambda, "add_one");
// call the typed version.
ICHECK_EQ(ftyped(1), 2);
Parameters
typed_lambdatyped lambda function.
namethe name of the lambda function.
Template Parameters
FLambdathe type of the lambda function.

◆ TypedPackedFunc() [8/8]

template<typename R , typename... Args>
template<typename FLambda , typename = typename std::enable_if<std::is_convertible< FLambda, std::function<R(Args...)>>::value>::type>
tvm::runtime::TypedPackedFunc< R(Args...)>::TypedPackedFunc ( const FLambda &  typed_lambda)
inline

construct from a lambda function with the same signature.

This version does not take a name. It is highly recommend you use the version that takes a name for the lambda.

Example usage:

auto typed_lambda = [](int x)->int { return x + 1; }
// construct from packed function
TypedPackedFunc<int(int)> ftyped(typed_lambda);
// call the typed version.
ICHECK_EQ(ftyped(1), 2);
Parameters
typed_lambdatyped lambda function.
Template Parameters
FLambdathe type of the lambda function.

Member Function Documentation

◆ AssignTypedLambda() [1/2]

template<typename R , typename... Args>
template<typename FType >
void tvm::runtime::TypedPackedFunc< R(Args...)>::AssignTypedLambda ( FType  flambda)
inline

◆ AssignTypedLambda() [2/2]

template<typename R , typename... Args>
template<typename FType >
void tvm::runtime::TypedPackedFunc< R(Args...)>::AssignTypedLambda ( FType  flambda,
std::string  name 
)
inline

◆ operator PackedFunc()

template<typename R , typename... Args>
tvm::runtime::TypedPackedFunc< R(Args...)>::operator PackedFunc ( ) const
inline

convert to PackedFunc

Returns
the internal PackedFunc

◆ operator!=()

template<typename R , typename... Args>
bool tvm::runtime::TypedPackedFunc< R(Args...)>::operator!= ( std::nullptr_t  null) const
inline
Returns
Whether the packed function is not nullptr

◆ operator()()

template<typename R , typename... Args>
TVM_ALWAYS_INLINE R tvm::runtime::TypedPackedFunc< R(Args...)>::operator() ( Args...  args) const

Invoke the operator.

Parameters
argsThe arguments
Returns
The return value.

◆ operator=() [1/2]

template<typename R , typename... Args>
template<typename FLambda , typename = typename std::enable_if< std::is_convertible<FLambda, std::function<R(Args...)>>::value>::type>
TSelf& tvm::runtime::TypedPackedFunc< R(Args...)>::operator= ( FLambda  typed_lambda)
inline

copy assignment operator from typed lambda

Example usage:

// construct from packed function
TypedPackedFunc<int(int)> ftyped;
ftyped = [](int x) { return x + 1; }
// call the typed version.
ICHECK_EQ(ftyped(1), 2);
Parameters
typed_lambdatyped lambda function.
Template Parameters
FLambdathe type of the lambda function.
Returns
reference to self.

◆ operator=() [2/2]

template<typename R , typename... Args>
TSelf& tvm::runtime::TypedPackedFunc< R(Args...)>::operator= ( PackedFunc  packed)
inline

copy assignment operator from PackedFunc.

Parameters
packedThe packed function.
Returns
reference to self.

◆ operator==()

template<typename R , typename... Args>
bool tvm::runtime::TypedPackedFunc< R(Args...)>::operator== ( std::nullptr_t  null) const
inline
Returns
Whether the packed function is nullptr

◆ packed()

template<typename R , typename... Args>
const PackedFunc& tvm::runtime::TypedPackedFunc< R(Args...)>::packed ( ) const
inline
Returns
reference the internal PackedFunc

Friends And Related Function Documentation

◆ TVMRetValue

template<typename R , typename... Args>
friend class TVMRetValue
friend

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