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:77
ZFObject's class info.
Definition ZFClass.h:67
zfauto newInstance(void) const
make a new instance of ZFObject dynamically, which is described by ZFClass
static const ZFClass * classForName(const zfstring &classNameOrFullName)
get class info by name
const zfstring & className(void) const
class name, e.g. "ZFObject"
Definition ZFClass.h:181
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->executeExact<ReturnType, ParamType0, ParamType1>(obj, param0, param1);
const ZFMethod * methodForName(const zfstring &methodName, const zfchar *paramTypeId0, 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
get the method by name hierarchically from class inherit tree, or zfnull if not exists
reflectable method for ZFObject
Definition ZFMethod.h:252
T_ReturnType executeExact(ZFObject *obj) const
see ZFMethod
Definition ZFMethod.h:278

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()->executeExact<void, Type const &>(obj, newValue);
const ZFProperty * propertyForName(const zfstring &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:28
const ZFMethod * setterMethod(void) const
get the getter method
Definition ZFProperty.h:117

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:107
_ZFT_t_zfint zfint
same as int, see zfindex
Definition ZFCoreTypeDef_CoreType.h:165
#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 for reflection
Definition ZFTypeIdDeclare.h:137
#define ZFTYPEID_DEFINE_BY_STRING_CONVERTER(TypeName, Type, convertFromStringAction, convertToStringAction)
see ZFTYPEID_DECLARE
Definition ZFTypeIdDeclare.h:166

once registered:

int yourProp = obj->classData()->propertyGetterForName("yourProp")->executeExact<zfint const &>(obj);

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

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