mxnet
Public Types | Public Member Functions | Friends | List of all members
mxnet::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 mxnet::runtime::TypedPackedFunc< R(Args...)>:
Collaboration graph

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 MXNetRetValue &value)
 constructor from MXNetRetValue More...
 
 TypedPackedFunc (const MXNetArgValue &value)
 constructor from MXNetArgValue 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...
 
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)
 

Friends

class MXNetRetValue
 

Detailed Description

template<typename R, typename... Args>
class mxnet::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;
Template Parameters
RThe return value of the function.
ArgsThe argument signature of the function.

Member Typedef Documentation

◆ TSelf

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

short hand for this function type

Constructor & Destructor Documentation

◆ TypedPackedFunc() [1/6]

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

default constructor

◆ TypedPackedFunc() [2/6]

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

constructor from null

◆ TypedPackedFunc() [3/6]

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

construct by wrap a PackedFunc

Example usage:

PackedFunc packed([](MXNetArgs args, MXNetRetValue *rv) {
int x = args[0];
*rv = x + 1;
});
// construct from packed function
TypedPackedFunc<int(int)> ftyped(packed);
// call the typed version.
CHECK_EQ(ftyped(1), 2);
Parameters
packedThe packed function

◆ TypedPackedFunc() [4/6]

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

constructor from MXNetRetValue

Parameters
valueThe MXNetRetValue

◆ TypedPackedFunc() [5/6]

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

constructor from MXNetArgValue

Parameters
valueThe MXNetArgValue

◆ TypedPackedFunc() [6/6]

template<typename R , typename... Args>
template<typename FLambda , typename = typename std::enable_if< std::is_convertible<FLambda, std::function<R(Args...)>>::value>::type>
mxnet::runtime::TypedPackedFunc< R(Args...)>::TypedPackedFunc ( const FLambda &  typed_lambda)
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);
// call the typed version.
CHECK_EQ(ftyped(1), 2);
Parameters
typed_lambdatyped lambda function.
Template Parameters
FLambdathe type of the lambda function.

Member Function Documentation

◆ AssignTypedLambda()

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

◆ operator PackedFunc()

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

convert to PackedFunc

Returns
the internal PackedFunc

◆ operator!=()

template<typename R , typename... Args>
bool mxnet::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>
R mxnet::runtime::TypedPackedFunc< R(Args...)>::operator() ( Args...  args) const
inline

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& mxnet::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.
CHECK_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& mxnet::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 mxnet::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& mxnet::runtime::TypedPackedFunc< R(Args...)>::packed ( ) const
inline
Returns
reference the internal PackedFunc

Friends And Related Function Documentation

◆ MXNetRetValue

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

The documentation for this class was generated from the following file:
mxnet::runtime::TypedPackedFunc< R(Args...)>::TypedPackedFunc
TypedPackedFunc()
default constructor
Definition: packed_func.h:192
mxnet::runtime::TypedPackedFunc< R(Args...)>::packed
const PackedFunc & packed() const
Definition: packed_func.h:294
mxnet::runtime::TypedPackedFunc< R(Args...)>::MXNetRetValue
friend class MXNetRetValue
Definition: packed_func.h:307