ZFFramework
 
Loading...
Searching...
No Matches
ZFMethod Class Reference

reflectable method for ZFObject More...

#include <ZFMethod.h>

Public Member Functions

template<typename T_ReturnType>
T_ReturnType executeExact (ZFObject *obj) const
 see ZFMethod
 
template<typename T_ReturnType, typename Type0>
T_ReturnType executeExact (ZFObject *obj, Type0 param0) const
 see ZFMethod
 
template<typename T_ReturnType, typename Type0, typename Type1>
T_ReturnType executeExact (ZFObject *obj, Type0 param0, Type1 param1) const
 see ZFMethod
 
template<typename T_ReturnType, typename Type0, typename Type1, typename Type2>
T_ReturnType executeExact (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 executeExact (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 executeExact (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 executeExact (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 executeExact (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 executeExact (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 ZFMethodaliasFrom (void) const
 see ZFMethodAlias
 
const ZFCoreArray< const ZFMethod * > & aliasTo (void) const
 see ZFMethodAlias
 
zfidentity methodId (void) const
 internal method id, for debug use only
 
zfbool isUserRegister (void) const
 whether the method is registered by ZFMethodUserRegister_0
 
zfany userRegisterUserData (void) const
 store impl state for ZFMethodUserRegister_0
 
void userRegisterUserData (ZFObject *userData) const
 store impl state for ZFMethodUserRegister_0
 
zfbool isDynamicRegister (void) const
 whether the method is registered by ZFMethodDynamicRegister
 
zfany dynamicRegisterUserData (void) const
 see ZFMethodDynamicRegister
 
const zfstringmethodName (void) const
 get the method's name
 
const zfstringreturnTypeId (void) const
 get the method's return value's type id
 
zfindex paramCount (void) const
 get the method's param num
 
zfindex paramCountMin (void) const
 get the method's minimum param num, same as paramCount if no default param
 
zfbool paramTypeIdIsMatch (const zfchar *paramTypeId0=zft_zfnull, const zfchar *paramTypeId1=zft_zfnull, const zfchar *paramTypeId2=zft_zfnull, const zfchar *paramTypeId3=zft_zfnull, const zfchar *paramTypeId4=zft_zfnull, const zfchar *paramTypeId5=zft_zfnull, const zfchar *paramTypeId6=zft_zfnull, const zfchar *paramTypeId7=zft_zfnull) const
 check whether method param type id matches
 
zfbool paramTypeIdIsMatch (const ZFMethod *method) const
 see paramTypeIdIsMatch
 
const zfstringparamTypeIdAt (zfindex index) const
 get the method's param type id at index
 
const zfstringparamNameAt (zfindex index) const
 get the method's param name at index, usually for debug use
 
const ZFListenerparamDefaultValueCallbackAt (zfindex index) const
 get the method param's default value access callback
 
zfauto paramDefaultValueAt (zfindex index) const
 get the method's param default value at index, null if no default param
 
zfindex paramDefaultBeginIndex (void) const
 return the first default param's index, or zfindexMax if no default param
 
void paramInfoT (zfstring &ret) const
 util to return param info
 
zfstring paramInfo (void) const
 see paramInfoT
 
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=zft_zfnull) const
 util method to invoke ZFMethodGenericInvoker
 
zfauto methodInvoke (ZFObject *ownerObjOrNull, ZFObject *param0, ZFObject *param1=((ZFObject *const &) _ZFP_ZFMP_DEF), ZFObject *param2=((ZFObject *const &) _ZFP_ZFMP_DEF), ZFObject *param3=((ZFObject *const &) _ZFP_ZFMP_DEF), ZFObject *param4=((ZFObject *const &) _ZFP_ZFMP_DEF), ZFObject *param5=((ZFObject *const &) _ZFP_ZFMP_DEF), ZFObject *param6=((ZFObject *const &) _ZFP_ZFMP_DEF), ZFObject *param7=((ZFObject *const &) _ZFP_ZFMP_DEF)) const
 see methodInvoke
 
zfbool methodInvokeT (zfauto &ret, zfstring *errorHint, ZFObject *ownerObjOrNull, ZFObject *param0=((ZFObject *const &) _ZFP_ZFMP_DEF), ZFObject *param1=((ZFObject *const &) _ZFP_ZFMP_DEF), ZFObject *param2=((ZFObject *const &) _ZFP_ZFMP_DEF), ZFObject *param3=((ZFObject *const &) _ZFP_ZFMP_DEF), ZFObject *param4=((ZFObject *const &) _ZFP_ZFMP_DEF), ZFObject *param5=((ZFObject *const &) _ZFP_ZFMP_DEF), ZFObject *param6=((ZFObject *const &) _ZFP_ZFMP_DEF), ZFObject *param7=((ZFObject *const &) _ZFP_ZFMP_DEF)) const
 see methodInvoke
 
zfauto methodInvokeDetail (ZFObject *ownerObjOrNull, const ZFCoreArray< zfauto > &params, zfbool *success=zft_zfnull, zfstring *errorHint=zft_zfnull) const
 see methodInvoke
 
zfbool methodInvokeCheck (ZFObject *ownerObjOrNull, zfstring *errorHint=zft_zfnull) const
 check whether this method can be invoke with specified object
 
ZFMethodGenericInvoker methodGenericInvokerOrig (void) const
 see methodGenericInvoker
 
void methodGenericInvoker (ZFMethodGenericInvoker methodGenericInvoker) const
 change default impl for methodGenericInvoker
 
const ZFClassownerClass (void) const
 method's owner class, null for function type method
 
const ZFPropertyownerProperty (void) const
 method's owner proeprty, null if none
 
ZFMethodAccessType methodAccessType (void) const
 get the method's access type
 
zfbool isPublic (void) const
 util to check whether methodAccessType is ZFMethodAccessTypePublic
 
zfbool isProtected (void) const
 util to check whether methodAccessType is ZFMethodAccessTypeProtected
 
zfbool isPrivate (void) const
 util to check whether methodAccessType is ZFMethodAccessTypePrivate
 
ZFMethodType methodType (void) const
 method type
 
zfbool isNormal (void) const
 util to check whether methodType is ZFMethodTypeNormal
 
zfbool isStatic (void) const
 util to check whether methodType is ZFMethodTypeStatic
 
zfbool isVirtual (void) const
 util to check whether methodType is ZFMethodTypeVirtual
 
zfbool isFuncType (void) const
 true if this method is function type (declared by ZFMETHOD_FUNC_DECLARE_XXX series)
 
const zfstringmethodNamespace (void) const
 get the method namespace, for func type only, null or empty string for global scope (ZF_NAMESPACE_GLOBAL)
 
zfbool isInternal (void) const
 whether the method is internal method
 
zfbool isInternalPrivate (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_,
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_, ReturnType, MethodName, ZFMP_0, ZFMP_1)
see ZFMethod
Definition ZFMethodDeclare.h:1076
  • PublicOrProtectedOrPrivate is the access type for the method, could be: public, protected, private
  • ZFMethodType_ indicates the method's type, could be: ZFMethodTypeStatic, ZFMethodTypeVirtual, ZFMethodTypeNormal
  • RegSig used to differ the method from overloaded method, you may use ZF_CALLER_LINE for most case
  • ReturnType is the method's return value's type, could be any types that are registerd by ZFTYPEID_DECLARE
  • MethodName is the method's name
  • ParamTypeN is the type of param at index N, could be any types that are registerd by ZFTYPEID_DECLARE
  • paramN is the name of param at index N
  • DefaultValueN is the default value for the param

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

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->executeExact<YourReturnType, ParamType...>(obj, someParam...);
// or, you may use generic version:
zfauto result2 = method->methodInvoke(obj, someParam...);
ZFObject's class info.
Definition ZFClass.h:67
zfauto methodInvoke(ZFObject *ownerObjOrNull=zft_zfnull) const
util method to invoke ZFMethodGenericInvoker
T_ReturnType executeExact(ZFObject *obj) const
see ZFMethod
Definition ZFMethod.h:278
a ZFObject holder which would release content object automatically when destroyed
Definition zfautoFwd.h:34
Warning
when using executeExact, 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());
static const zfstring & TypeId(void)
get type id, or return ZFTypeId_none if not registered
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

◆ paramInfoT()

void ZFMethod::paramInfoT ( zfstring & ret) const

util to return param info

sample output: P0 p0, P1 p1 = def, or output nothing if no param

◆ 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:446
util method to cast ZFObject types freely
Definition zfany.h:35
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->executeExact<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:

local obj = MyZFObjectType();
local objParam = SomeType();
local result = obj:myFunc(zfint(1), zfstring("2"), objParam)
_ZFT_t_zfint zfint
same as int, see zfindex
Definition ZFCoreTypeDef_CoreType.h:165
zft_zfstring< zfchar > zfstring
see zft_zfstring
Definition ZFCoreTypeDef_StringType.h:15

◆ methodInvoke()

zfauto ZFMethod::methodInvoke ( ZFObject * ownerObjOrNull = zft_zfnull) 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

◆ isInternal()

zfbool ZFMethod::isInternal ( void ) const
inline

whether the method is internal method

methods would be treated as internal if:

  • method name starts with '_'

see also isInternalPrivate

◆ isInternalPrivate()

zfbool ZFMethod::isInternalPrivate ( void ) const
inline

whether the method is internal private method

methods would be treated as internal private if:

  • method name starts with 'ZFP'

internal methods would be ignored from reflection, see also isInternal


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