ZFFramework
Loading...
Searching...
No Matches
Public Member Functions | List of all members
ZFMethod Class Reference

reflectable method for ZFObject More...

#include <ZFMethod.h>

Public Member Functions

template<typename T_ReturnType >
T_ReturnType execute (ZFObject *obj) const
 see ZFMethod
 
template<typename T_ReturnType , typename Type0 >
T_ReturnType execute (ZFObject *obj, Type0 param0) const
 see ZFMethod
 
template<typename T_ReturnType , typename Type0 , typename Type1 >
T_ReturnType execute (ZFObject *obj, Type0 param0, Type1 param1) const
 see ZFMethod
 
template<typename T_ReturnType , typename Type0 , typename Type1 , typename Type2 >
T_ReturnType execute (ZFObject *obj, Type0 param0, Type1 param1, Type2 param2) const
 see ZFMethod
 
template<typename T_ReturnType , typename Type0 , typename Type1 , typename Type2 , typename Type3 >
T_ReturnType execute (ZFObject *obj, Type0 param0, Type1 param1, Type2 param2, Type3 param3) const
 see ZFMethod
 
template<typename T_ReturnType , typename Type0 , typename Type1 , typename Type2 , typename Type3 , typename Type4 >
T_ReturnType execute (ZFObject *obj, Type0 param0, Type1 param1, Type2 param2, Type3 param3, Type4 param4) const
 see ZFMethod
 
template<typename T_ReturnType , typename Type0 , typename Type1 , typename Type2 , typename Type3 , typename Type4 , typename Type5 >
T_ReturnType execute (ZFObject *obj, Type0 param0, Type1 param1, Type2 param2, Type3 param3, Type4 param4, Type5 param5) const
 see ZFMethod
 
template<typename T_ReturnType , typename Type0 , typename Type1 , typename Type2 , typename Type3 , typename Type4 , typename Type5 , typename Type6 >
T_ReturnType execute (ZFObject *obj, Type0 param0, Type1 param1, Type2 param2, Type3 param3, Type4 param4, Type5 param5, Type6 param6) const
 see ZFMethod
 
template<typename T_ReturnType , typename Type0 , typename Type1 , typename Type2 , typename Type3 , typename Type4 , typename Type5 , typename Type6 , typename Type7 >
T_ReturnType execute (ZFObject *obj, Type0 param0, Type1 param1, Type2 param2, Type3 param3, Type4 param4, Type5 param5, Type6 param6, Type7 param7) const
 see ZFMethod
 
void objectInfoT (zfstring &ret) const
 see objectInfo
 
zfstring objectInfo (void) const
 return object info
 
const ZFMethodmethodAliasFrom (void) const
 see ZFMethodAlias
 
const ZFCoreArray< const ZFMethod * > & methodAliasTo (void) const
 see ZFMethodAlias
 
const zfcharmethodInternalId (void) const
 internal method id, for debug use only
 
zfbool methodIsUserRegister (void) const
 whether the method is registered by ZFMethodUserRegister_0
 
zfany methodUserRegisterUserData (void) const
 store impl state for ZFMethodUserRegister_0
 
void methodUserRegisterUserData (ZFObject *userData) const
 store impl state for ZFMethodUserRegister_0
 
zfbool methodIsDynamicRegister (void) const
 whether the method is registered by ZFMethodDynamicRegister
 
zfany methodDynamicRegisterUserData (void) const
 see ZFMethodDynamicRegister
 
const zfcharmethodName (void) const
 get the method's name
 
const zfcharmethodReturnTypeId (void) const
 get the method's return value's type id
 
const zfcharmethodReturnTypeName (void) const
 get the method's return value's type name
 
zfindex methodParamCount (void) const
 get the method's param num
 
zfindex methodParamCountMin (void) const
 get the method's minimum param num, same as methodParamCount if no default param
 
zfbool methodParamTypeIdIsMatch (const zfchar *methodParamTypeId0=0, const zfchar *methodParamTypeId1=0, const zfchar *methodParamTypeId2=0, const zfchar *methodParamTypeId3=0, const zfchar *methodParamTypeId4=0, const zfchar *methodParamTypeId5=0, const zfchar *methodParamTypeId6=0, const zfchar *methodParamTypeId7=0) const
 check whether method param type id matches
 
zfbool methodParamTypeIdIsMatch (const ZFMethod *method) const
 see methodParamTypeIdIsMatch
 
const zfcharmethodParamTypeIdAt (zfindex index) const
 get the method's param type id at index
 
const zfcharmethodParamTypeNameAt (zfindex index) const
 get the method's param type name at index, usually for debug use
 
const zfcharmethodParamNameAt (zfindex index) const
 get the method's param name at index, usually for debug use
 
ZFMethodParamDefaultValueCallback methodParamDefaultValueCallbackAt (zfindex index) const
 get the method param's default value access callback
 
zfauto methodParamDefaultValueAt (zfindex index) const
 get the method's param default value at index, null if no default param
 
zfindex methodParamDefaultBeginIndex (void) const
 return the first default param's index, or zfindexMax if no default param
 
ZFFuncAddrType methodInvoker (void) const
 invoker function address for both class member type and function type
 
void methodInvoker (ZFFuncAddrType methodInvoker) const
 override original methodInvoker
 
ZFFuncAddrType methodInvokerOrig (void) const
 see methodInvoker
 
ZFMethodGenericInvoker methodGenericInvoker (void) const
 generic invoker for advanced reflection
 
zfauto methodInvoke (ZFObject *ownerObjOrNull=0) const
 util method to invoke ZFMethodGenericInvoker
 
zfauto methodInvoke (ZFObject *ownerObjOrNull, ZFObject *param0, ZFObject *param1=((ZFObject *const &) _ZFP_ZFMethodGenericInvokerDefaultParamRef), ZFObject *param2=((ZFObject *const &) _ZFP_ZFMethodGenericInvokerDefaultParamRef), ZFObject *param3=((ZFObject *const &) _ZFP_ZFMethodGenericInvokerDefaultParamRef), ZFObject *param4=((ZFObject *const &) _ZFP_ZFMethodGenericInvokerDefaultParamRef), ZFObject *param5=((ZFObject *const &) _ZFP_ZFMethodGenericInvokerDefaultParamRef), ZFObject *param6=((ZFObject *const &) _ZFP_ZFMethodGenericInvokerDefaultParamRef), ZFObject *param7=((ZFObject *const &) _ZFP_ZFMethodGenericInvokerDefaultParamRef)) const
 see methodInvoke
 
zfauto methodInvokeDetail (ZFObject *ownerObjOrNull, const ZFCoreArray< zfauto > &params, zfbool *success=0, zfstring *errorHint=0) const
 see methodInvoke
 
ZFMethodGenericInvoker methodGenericInvokerOrig (void) const
 see methodGenericInvoker
 
void methodGenericInvoker (ZFMethodGenericInvoker methodGenericInvoker) const
 change default impl for methodGenericInvoker
 
const ZFClassmethodOwnerClass (void) const
 method's owner class, null for function type method
 
const ZFPropertymethodOwnerProperty (void) const
 method's owner proeprty, null if none
 
ZFMethodPrivilegeType methodPrivilegeType (void) const
 get the method's access type
 
zfbool methodIsPublic (void) const
 util to check whether methodPrivilegeType is ZFMethodPrivilegeTypePublic
 
zfbool methodIsProtected (void) const
 util to check whether methodPrivilegeType is ZFMethodPrivilegeTypeProtected
 
zfbool methodIsPrivate (void) const
 util to check whether methodPrivilegeType is ZFMethodPrivilegeTypePrivate
 
ZFMethodType methodType (void) const
 method type
 
zfbool methodIsNormal (void) const
 util to check whether methodType is ZFMethodTypeNormal
 
zfbool methodIsStatic (void) const
 util to check whether methodType is ZFMethodTypeStatic
 
zfbool methodIsVirtual (void) const
 util to check whether methodType is ZFMethodTypeVirtual
 
zfbool methodIsFunctionType (void) const
 true if this method is function type (declared by ZFMETHOD_FUNC_DECLARE_XXX series)
 
const zfcharmethodNamespace (void) const
 get the method namespace, for func type only, null or empty string for global scope (ZF_NAMESPACE_GLOBAL)
 
zfbool methodIsInternal (void) const
 whether the method is internal method
 
zfbool methodIsInternalPrivate (void) const
 whether the method is internal private method
 

Detailed Description

reflectable method for ZFObject

(for function type of ZFMethod, please refer to ZFMETHOD_FUNC_DECLARE_0)

to use ZFMethod, you must first declare it in your class:

zfclass YourClass : zfextend ZFObject {
/ **
* you can add doxygen docs here
* /
ZFMETHOD_INLINE_0(void, yourMethod) {
// method code here
}
};
#define zfextend
dummy macro shows class inherit from another
Definition ZFCoreTypeDef_ClassType.h:53
#define ZFMETHOD_INLINE_0(ReturnType, MethodName)
see ZFMethod
Definition ZFMethodDeclare.h:755
#define zfclass
same as class, shows that this class is a ZFObject type
Definition ZFObjectClassTypeFwd.h:38
#define ZFOBJECT_DECLARE(ChildClass, SuperClass,...)
necessary for every class inherit from ZFObject
Definition ZFObjectDeclare.h:126
base class of all objects
Definition ZFObjectCore.h:209

or you may want to declare in header and define in cpp file:

// YourClass.h
zfclass YourClass : zfextend ZFObject {
ZFMETHOD_DECLARE_0(void, yourMethod) // declare only
};
// YourClass.cpp
ZFMETHOD_DEFINE_0(YourClass, void, yourMethod) {
// method code here
}
#define ZFMETHOD_DECLARE_0(ReturnType, MethodName)
see ZFMethod
Definition ZFMethodDeclare.h:693
#define ZFMETHOD_DEFINE_0(OwnerClass, ReturnType, MethodName)
see ZFMethod
Definition ZFMethodDeclare.h:821

param and return type must registered by ZFTYPEID_DECLARE series, and the param's num is limited to ZFMETHOD_MAX_PARAM

the ZFMETHOD_XXX macro takes many params, to walk through it, let's take a example:

PublicOrProtectedOrPrivate, ZFMethodType_, RegSig,
ReturnType, MethodName
, ZFMP_IN(ParamType0, param0)
, ZFMP_IN_OPT(ParamType1, param1, DefaultValue1)
)
#define ZFMP_IN_OPT(ParamType, paramName, DefaultValue)
see ZFMP_IN
Definition ZFMethod.h:108
#define ZFMP_IN(ParamType, paramName)
macro to wrap param types for ZFMETHOD_INLINE_0 series
Definition ZFMethod.h:105
#define ZFMETHOD_DECLARE_DETAIL_2(PublicOrProtectedOrPrivate, ZFMethodType_, RegSig, ReturnType, MethodName, ZFMP_0, ZFMP_1)
see ZFMethod
Definition ZFMethodDeclare.h:1076

to override a method declared in parent, simply override like normal virtual functions

once declared, the method would be automatically registered to it's owner class when it's owner class registered, see also ZFOBJECT_REGISTER

to access the method, use ZFMethodAccess/ZFMethodAccessDetail_0 is recommended:

const ZFMethod *method = ZFMethodAccess(YourClassType, yourMethodName);
#define ZFMethodAccess(OwnerClass, MethodName)
see ZFMethod
Definition ZFMethodDeclare.h:16
reflectable method for ZFObject
Definition ZFMethod.h:237

also, you may use ZFClass's member function to reflect from string, such as ZFClass::methodForName

once you have successfully get the ZFMethod data, you can execute it without knowing the owner's class type:

const ZFClass *cls = ...; // we may not know the actual class type
const ZFMethod *method = cls->methodForName("someMethod");
zfauto obj = cls->newInstance();
// execute
YourReturnType result = method->execute<YourReturnType, ParamType...>(obj, someParam...);
// or, you may use generic version:
zfauto result2 = method->methodInvoke(obj, someParam...);
ZFObject's class info.
Definition ZFClass.h:66
T_ReturnType execute(ZFObject *obj) const
see ZFMethod
Definition ZFMethod.h:265
zfauto methodInvoke(ZFObject *ownerObjOrNull=0) const
util method to invoke ZFMethodGenericInvoker
a ZFObject holder which would release content object automatically when destroyed
Definition zfautoFwd.h:34
Warning
you take the full responsibility to make sure the ReturnType and ParamType exactly match the method, if not, no compile error would occurred, but app may (or may not) crash at runtime


if you want to reflect overloaded method, use both methodName and param's type id

cls->methodForName("methodName", ZFTypeId<Param0>::TypeId());
type data traits for ZFTYPEID_DECLARE
Definition ZFTypeIdFwd.h:126
Warning
in subclass, you may declare a method with the same name of parent's one, but the behavior may be unexpected in some situations, so it's highly deprecated to do so
Note
it's thread-safe to execute ZFMethod, but whether the actual run code (the code in the method body) is thread-safe, is depends on you

Member Function Documentation

◆ methodParamTypeIdIsMatch()

zfbool ZFMethod::methodParamTypeIdIsMatch ( const zfchar * methodParamTypeId0 = 0,
const zfchar * methodParamTypeId1 = 0,
const zfchar * methodParamTypeId2 = 0,
const zfchar * methodParamTypeId3 = 0,
const zfchar * methodParamTypeId4 = 0,
const zfchar * methodParamTypeId5 = 0,
const zfchar * methodParamTypeId6 = 0,
const zfchar * methodParamTypeId7 = 0 ) const

check whether method param type id matches

null type id means not specified (any type matches), use ZFTypeId_void to explicitly limit param count

◆ methodInvoker() [1/2]

ZFFuncAddrType ZFMethod::methodInvoker ( void ) const
inline

invoker function address for both class member type and function type

proto type:

ReturnType methodInvoker(
ZF_IN const ZFMethod *invokerMethod
, ZF_IN zfany const &invokerObject
, ZF_IN ParamType0 param0
, ZF_IN ParamType1 param1
, / * ... * /);
#define ZF_IN
dummy macro that shows the param used as required input
Definition ZFCoreTypeDef_ClassType.h:180
ZFFuncAddrType methodInvoker(void) const
invoker function address for both class member type and function type
Definition ZFMethod.h:450
util method to cast ZFObject types freely
Definition zfany.h:60
Note
the methodInvoker is ensured not null for normal method and user registered method, and may be null for dynamic registered method (ZFMethodDynamicRegister), you may change it by methodInvoker

◆ methodInvoker() [2/2]

void ZFMethod::methodInvoker ( ZFFuncAddrType methodInvoker) const

override original methodInvoker

set null to remove the overrided value, the original invoker can be accessed by methodInvokerOrig

Note
no safe check for the method's proto type, you must ensure it's valid and safe to be called
assume the original method's proto type is:
ReturnType method(ParamType0, ParamType1, ...);
then the invoker's proto type must be:
ReturnType method(
const ZFMethod *invokerMethod
, zfany const &invokerObject
, ParamType0 param0
, ParamType1 param1
, ...
);
overriding the invoker only affect methods when they are reflected, calling it directly won't be affected
obj->myMethod(); // won't be affected
method->execute<void>(obj); // would be affected

◆ methodGenericInvoker() [1/2]

ZFMethodGenericInvoker ZFMethod::methodGenericInvoker ( void ) const
inline

generic invoker for advanced reflection

as a static language, C++'s function invoking require explicit type declaration, which is a pain when binding with script languages
since ZFFramework supply reflection (though limited), we are trying to solve the dynamic script binding, how it works:


typical steps for users:

  1. have all methods you need to bind been declared by ZFMETHOD_INLINE_0 series
  2. supply wrapper class to hold the type
  3. ensure all params can be converted to ZFObject types, by declaring them by ZFTYPEID_DECLARE
  4. all done, all binding works should be done by impl


typical steps for impl:

  1. supply type convert methods to bind ZFTypeId::Value types to script languages
  2. using reflection of ZFClass and ZFMethod, to bind all class and methods to script languages


the final script that calling ZFFFramework, should look like:

var obj = MyZFObjectType::newInstance();
var objParam = SomeType::newInstance();
var result = obj.myFunc(v_zfnumber(1), v_zfstring("2"), objParam)
type wrapper for ZFTypeId::Value
Definition ZFTypeId_CoreType.h:171

◆ methodInvoke()

zfauto ZFMethod::methodInvoke ( ZFObject * ownerObjOrNull = 0) const

util method to invoke ZFMethodGenericInvoker

note all params must be exactly same type, use ZFDI_invoke if you want auto param conversion

◆ methodGenericInvoker() [2/2]

void ZFMethod::methodGenericInvoker ( ZFMethodGenericInvoker methodGenericInvoker) const

change default impl for methodGenericInvoker

the original invoker can be accessed by methodGenericInvokerOrig

◆ methodIsInternal()

zfbool ZFMethod::methodIsInternal ( void ) const
inline

whether the method is internal method

all method that have "_ZFP_" as prefix would be treated as internal method
internal method should not be used publicly, however, you may still apply reflection, see methodIsInternalPrivate

◆ methodIsInternalPrivate()

zfbool ZFMethod::methodIsInternalPrivate ( void ) const
inline

whether the method is internal private method

all method that have "_ZFP_I_" as prefix would be treated as internal private method
internal private method should not be used publicly, also, it would be ignored from reflection, see methodIsInternal


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