ZFFramework
Loading...
Searching...
No Matches
Reflection

Reflect class

all ZFObject class or ZFInterface class can be reflected if:

  • any instance of the class have ever been created
  • ClassData have ever been called
  • ZFOBJECT_REGISTER have been declared

if matched any of the condition above, you may reflect the class's meta data:

// find the class meta data, null if not found
const ZFClass *cls = ZFClass::classForName("MyObject");
// you may create the object's instance by the meta data
zfauto obj = cls->newInstance();
// cast to desired type if necessary
MyObject *myObj = obj;
// also, you may access class's info by ZFClass's method
zfLog() << "class name: " << cls->className();
#define zfLog(...)
thread-safe log utility
Definition ZFLog.h:64
ZFObject's class info.
Definition ZFClass.h:66
zfauto newInstance(void) const
make a new instance of ZFObject dynamically, which is described by ZFClass
const zfchar * className(void) const
class name, e.g. "ZFObject"
Definition ZFClass.h:200
static const ZFClass * classForName(const zfchar *classNameOrFullName)
get class info by name
a ZFObject holder which would release content object automatically when destroyed
Definition zfautoFwd.h:34

Reflect method

a method is reflectable if it's declared by ZFMETHOD_XXX_DECLARE_XXX series, you may reflect it like this:

// find the method info by its owner ZFClass's method,
// return null if not found
const ZFMethod *method = cls->methodForName("myFunc");
// invoke the method, note that you must:
// - explicitly specify the return type and each param type
// - ensure the proto type of the method is right
// - ensure obj is the right object that have the method
method->execute<ReturnType, ParamType0, ParamType1>(obj, param0, param1);
const ZFMethod * methodForName(const zfchar *methodName, const zfchar *methodParamTypeId0, 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
get the method by name hierarchically from class inherit tree, or zfnull if not exists
reflectable method for ZFObject
Definition ZFMethod.h:237
T_ReturnType execute(ZFObject *obj) const
see ZFMethod
Definition ZFMethod.h:265

Reflect property

also, a property is reflectable if it's declared by ZFPROPERTY_XXX series, you may reflect it like this:

// find the property info by its owner ZFClass's method,
// return null if not found
const ZFProperty *property = cls->propertyForName("myProperty");
// change the property's value by setter method,
// also, must ensure all the type matches the original type
property->setterMethod()->execute<void, Type const &>(obj, newValue);
const ZFProperty * propertyForName(const zfchar *propertyName) const
get the property by name hierarchically from class inherit tree, or zfnull if not exists
info for a property for ZFObject, see ZFPROPERTY_RETAIN for more info
Definition ZFProperty.h:29
const ZFMethod * setterMethod(void) const
get the getter method
Definition ZFProperty.h:118

Existing class

any existing class can also be reflectable if you are able to supply some extra registration code:

// header file
class YourType {
public:
int yourProp;
};
ZFTYPEID_DECLARE(ZFLIB_APP, YourType, YourType)
// source file
v.yourProp = yourParser(src, srcLen);
return zftrue;
}, {
yourPrinter(s, v);
return zftrue;
})
#define ZFLIB_APP
used to export symbols
Definition ZFCoreEnvDef.h:35
#define zftrue
bool true type
Definition ZFCoreTypeDef_CoreType.h:62
_ZFT_t_zfint zfint
same as int, see zfindex
Definition ZFCoreTypeDef_CoreType.h:121
#define ZFMETHOD_USER_REGISTER_FOR_WRAPPER_VAR(WrapperClass, VarType, VarName)
see ZFMethodUserRegister_0
Definition ZFMethodUserRegister_Wrapper.h:59
#define ZFTYPEID_DECLARE(ZFLIB_, TypeName, Type)
register a type
Definition ZFTypeIdCore.h:98
#define ZFTYPEID_DEFINE_BY_STRING_CONVERTER(TypeName, Type, convertFromStringAction, convertToStringAction)
see ZFTYPEID_DECLARE
Definition ZFTypeIdCore.h:291

once registered:

zfauto obj = ZFClass::classForName("v_YourType")->newInstance();
int yourProp = obj->classData()->propertyGetterForName("yourProp")->execute<zfint const &>(obj);

or even for lua: (see also Automatically lua binding)

local obj = YourType();
local yourProp = obj:yourProp();