ZFFramework
Loading...
Searching...
No Matches
ZFDynamicInvoker.h
Go to the documentation of this file.
1
5
6#ifndef _ZFI_ZFDynamicInvoker_h_
7#define _ZFI_ZFDynamicInvoker_h_
8
9#include "ZFObjectCore.h"
10#include "ZFObjectRetain.h"
12
13// ============================================================
20zfclass ZFLIB_ZFCore ZFDI_Wrapper : zfextend ZFObject {
21 ZFOBJECT_DECLARE(ZFDI_Wrapper, ZFObject)
22
23public:
25 typedef enum {
29 } ValueType;
30private:
31 zfstring _zfvString;
32 ValueType _zfvType;
33 zfflags _zfvValid;
34 zflong _zfvInt;
35 zfdouble _zfvFloat;
36
37public:
39 ValueType valueType(void) {return _zfvType;}
40public:
47 const zfstring &valueString(void) {
48 switch(_zfvType) {
49 case TypeString:
50 return _zfvString;
51 case TypeInt:
52 if(!_zfvString) {
53 zfsFromIntT(_zfvString, _zfvInt);
54 }
55 return _zfvString;
56 case TypeFloat:
57 if(!_zfvString) {
58 zfsFromFloatT(_zfvString, _zfvFloat);
59 }
60 return _zfvString;
61 default:
63 return _zfvString;
64 }
65 }
66
68 switch(_zfvType) {
69 case TypeString:
70 if(!ZFBitTest(_zfvValid, 1 << TypeInt)) {
71 if(!zfsToIntT(_zfvInt, _zfvString, _zfvString.length())) {
72 return zffalse;
73 }
74 ZFBitSet(_zfvValid, 1 << TypeInt);
75 }
76 ret = _zfvInt;
77 return zftrue;
78 case TypeInt:
79 ret = _zfvInt;
80 return zftrue;
81 case TypeFloat:
82 if(!ZFBitTest(_zfvValid, 1 << TypeInt)) {
83 if(!zffloatIsInt(_zfvFloat)) {
84 return zffalse;
85 }
86 _zfvInt = (zflong)_zfvFloat;
87 ZFBitSet(_zfvValid, 1 << TypeInt);
88 }
89 ret = _zfvInt;
90 return zftrue;
91 default:
93 return zffalse;
94 }
95 }
96
98 zflong ret = 0;
99 this->valueIntT(ret);
100 return ret;
101 }
102
104 switch(_zfvType) {
105 case TypeString:
106 if(!ZFBitTest(_zfvValid, 1 << TypeFloat)) {
107 if(!zfsToFloatT(_zfvFloat, _zfvString, _zfvString.length())) {
108 return zffalse;
109 }
110 ZFBitSet(_zfvValid, 1 << TypeFloat);
111 }
112 ret = _zfvFloat;
113 return zftrue;
114 case TypeInt:
115 if(!ZFBitTest(_zfvValid, 1 << TypeFloat)) {
116 _zfvFloat = (zfdouble)_zfvInt;
117 ZFBitSet(_zfvValid, 1 << TypeFloat);
118 }
119 ret = _zfvFloat;
120 return zftrue;
121 case TypeFloat:
122 ret = _zfvFloat;
123 return zftrue;
124 default:
126 return zffalse;
127 }
128 }
129
131 zfdouble ret = 0;
132 this->valueFloatT(ret);
133 return ret;
134 }
135public:
137 void value(ZF_IN const zfstring &v) {
138 _zfvType = TypeString;
139 _zfvString = v;
140 _zfvValid = 0;
141 }
142
143 void value(ZF_IN const zflong &v) {
144 _zfvType = TypeInt;
145 _zfvInt = v;
146 _zfvValid = 0;
147 }
148
149 void value(ZF_IN const zfdouble &v) {
150 _zfvType = TypeFloat;
151 _zfvFloat = v;
152 _zfvValid = 0;
153 }
154
155public:
158 switch(_zfvType) {
159 case TypeString:
160 return zfidentityHash((zfidentity)_zfvType, zfidentityCalcString(_zfvString));
161 case TypeInt:
162 return zfidentityHash((zfidentity)_zfvType, zfidentityCalc(_zfvInt));
163 case TypeFloat:
164 return zfidentityHash((zfidentity)_zfvType, zfidentityCalc(_zfvFloat));
165 default:
167 return zfidentityInvalid();
168 }
169 }
171 virtual ZFCompareResult objectCompareImpl(ZF_IN ZFObject *anotherObj) {
172 zfself *ref = zfcast(zfself *, anotherObj);
173 if(ref != zfnull && _zfvType == ref->_zfvType) {
174 switch(_zfvType) {
175 case TypeString:
176 return _zfvString == ref->_zfvString ? ZFCompareEqual : ZFCompareUncomparable;
177 case TypeInt:
178 return _zfvInt == ref->_zfvInt ? ZFCompareEqual : ZFCompareUncomparable;
179 case TypeFloat:
180 return _zfvFloat == ref->_zfvFloat ? ZFCompareEqual : ZFCompareUncomparable;
181 default:
184 }
185 }
186 else {
188 }
189 }
191 virtual inline zfbool objectIsInternal(void) {return zftrue;}
193 virtual inline zfbool objectIsInternalPrivate(void) {return zftrue;}
195 virtual void objectInfoImpl(ZF_IN_OUT zfstring &ret) {
196 if(ret.capacity() != 0) {
197 ret += this->valueString();
198 }
199 else {
200 ret = this->valueString().sharedCopy();
201 }
202 }
203protected:
205 virtual void objectOnInit(void) {zfsuper::objectOnInit();}
207 virtual void objectOnInit(ZF_IN const zfstring &v) {this->value(v);}
209 virtual void objectOnInit(ZF_IN const zflong &v) {this->value(v);}
211 virtual void objectOnInit(ZF_IN const zfdouble &v) {this->value(v);}
212};
213
214// ============================================================
223
253 zfdouble ret = -1;
254 if(ZFDI_toNumberT(ret, obj)) {
255 return ret;
256 }
257 else {
258 return -1;
259 }
260}
261
270 , ZF_IN const ZFArgs &zfargs
271 );
273inline zfstring ZFDI_paramInfo(ZF_IN const ZFArgs &zfargs) {
274 zfstring ret;
275 ZFDI_paramInfoT(ret, zfargs);
276 return ret;
277}
278
280inline zfindex ZFDI_paramCount(ZF_IN const ZFArgs &zfargs) {
281 zfindex paramCount = 0;
282 while(paramCount < ZFMETHOD_MAX_PARAM && zfargs.paramAt(paramCount) != ZFMP_DEF()) {
283 ++paramCount;
284 }
285 return paramCount;
286}
287
316 ZF_IN_OUT const ZFArgs &zfargs
317 , ZF_IN const zfstring &name
318 , ZF_IN_OPT zfbool convStr = zffalse
319 );
320
328 ZF_IN_OUT const ZFArgs &zfargs
329 , ZF_IN const ZFClass *cls
330 , ZF_IN_OPT zfbool convStr = zffalse
331 );
332
333// ============================================================
338 ZF_OUT zfauto &ret
339 , ZF_IN const ZFClass *cls
340 , ZF_IN const zfchar *src
341 , ZF_IN_OPT zfindex srcLen = zfindexMax()
342 , ZF_OUT_OPT zfstring *errorHint = zfnull
343 );
348 ZF_OUT zfauto &ret
349 , ZF_IN const zfstring &typeId
350 , ZF_IN const zfchar *src
351 , ZF_IN_OPT zfindex srcLen = zfindexMax()
352 , ZF_OUT_OPT zfstring *errorHint = zfnull
353 );
354
355// ============================================================
356/* ZFMETHOD_MAX_PARAM */
374 ZF_IN const zfstring &name
375 );
378 ZF_IN const zfstring &name
379 , ZF_IN ZFObject *param0
380 , ZF_IN_OPT ZFObject *param1 = ZFMP_DEF()
381 , ZF_IN_OPT ZFObject *param2 = ZFMP_DEF()
382 , ZF_IN_OPT ZFObject *param3 = ZFMP_DEF()
383 , ZF_IN_OPT ZFObject *param4 = ZFMP_DEF()
384 , ZF_IN_OPT ZFObject *param5 = ZFMP_DEF()
385 , ZF_IN_OPT ZFObject *param6 = ZFMP_DEF()
386 , ZF_IN_OPT ZFObject *param7 = ZFMP_DEF()
387 );
390 ZF_OUT zfauto &ret
391 , ZF_OUT_OPT zfstring *errorHint
392 , ZF_IN const zfstring &name
393 , ZF_IN_OPT ZFObject *param0 = ZFMP_DEF()
394 , ZF_IN_OPT ZFObject *param1 = ZFMP_DEF()
395 , ZF_IN_OPT ZFObject *param2 = ZFMP_DEF()
396 , ZF_IN_OPT ZFObject *param3 = ZFMP_DEF()
397 , ZF_IN_OPT ZFObject *param4 = ZFMP_DEF()
398 , ZF_IN_OPT ZFObject *param5 = ZFMP_DEF()
399 , ZF_IN_OPT ZFObject *param6 = ZFMP_DEF()
400 , ZF_IN_OPT ZFObject *param7 = ZFMP_DEF()
401 );
404 ZF_IN const zfstring &name
405 , ZF_IN const ZFCoreArray<zfauto> &params
406 , ZF_OUT_OPT zfbool *success = zfnull
407 , ZF_OUT_OPT zfstring *errorHint = zfnull
408 );
409
410// ============================================================
419
421#endif // #ifndef _ZFI_ZFDynamicInvoker_h_
422
#define ZFLIB_ZFCore
used to export symbols
Definition ZFCoreEnvDef.h:30
#define ZFCoreCriticalShouldNotGoHere()
log that likes "[file function (line)] should not go here"
Definition ZFCoreLog_CommonLog.h:131
zfbool zfsToFloatT(T_Float &ret, const zfchar *src, zfindex srcLen=((zfindex) -1))
convert string to float
Definition ZFCoreStringConvert.h:222
zfbool zfsToIntT(T_Int &ret, const zfchar *src, zfindex srcLen=((zfindex) -1), zfindex radix=10, zfbool allowNegative=_ZFT_t_zftrue)
convert string to int, return error position if failed, or null if success
Definition ZFCoreStringConvert.h:97
zfbool zfsFromIntT(zfstring &s, T_Int n, zfindex radix=10, zfbool useUpperCase=_ZFT_t_zftrue)
convert integer number (int, long, etc) to string
Definition ZFCoreStringConvert.h:32
zfbool zfsFromFloatT(zfstring &s, T_Float n)
convert float number (int, long, etc) to string
Definition ZFCoreStringConvert.h:175
_ZFT_t_zfchar zfchar
char wrapper
Definition ZFCoreTypeDef_CharType.h:17
#define ZF_OUT_OPT
dummy macro that shows the param used as optional output
Definition ZFCoreTypeDef_ClassType.h:208
#define zfextend
dummy macro shows class inherit from another
Definition ZFCoreTypeDef_ClassType.h:53
#define zfoverride
dummy macro shows that method override parent's method
Definition ZFCoreTypeDef_ClassType.h:58
#define ZF_IN
dummy macro that shows the param used as required input
Definition ZFCoreTypeDef_ClassType.h:196
#define ZF_IN_OPT
dummy macro that shows the param used as optional input
Definition ZFCoreTypeDef_ClassType.h:200
#define ZF_OUT
dummy macro that shows the param used as required output
Definition ZFCoreTypeDef_ClassType.h:204
#define ZF_IN_OUT
dummy macro that shows the param used as required input and output
Definition ZFCoreTypeDef_ClassType.h:212
#define zfclassFwd
forward declaration of a class type
Definition ZFCoreTypeDef_ClassType.h:31
bool zffloatIsInt(T_zffloat const &v)
whether the float type holds int value
Definition ZFCoreTypeDef_CoreIntType.h:179
_ZFT_t_zfbool zfbool
bool type
Definition ZFCoreTypeDef_CoreType.h:103
_ZFT_t_zfindex zfindex
similar to size_t, used for index and size only
Definition ZFCoreTypeDef_CoreType.h:154
#define zftrue
bool true type
Definition ZFCoreTypeDef_CoreType.h:107
_zft_zfflags zfflags
used to hold flags, ensured 32 bit, ensured unsigned
Definition ZFCoreTypeDef_CoreType.h:211
_zft_zfidentity zfidentity
identity type, ensured at least 32 bit, ensured unsigned
Definition ZFCoreTypeDef_CoreType.h:225
#define zfindexMax()
(zfindex)-1, indicate a max index value, see zfindex
Definition ZFCoreTypeDef_CoreType.h:159
_zft_zfdouble zfdouble
same as double, see zfindex
Definition ZFCoreTypeDef_CoreType.h:188
_zft_zflong zflong
long, see zfindex
Definition ZFCoreTypeDef_CoreType.h:173
#define zffalse
bool false type
Definition ZFCoreTypeDef_CoreType.h:111
#define zfidentityInvalid()
an invalid id value, ensured ((zfidentity)-1)
Definition ZFCoreTypeDef_CoreType.h:229
#define zfnull
same as NULL, defined for future use
Definition ZFCoreTypeDef_CoreType.h:88
ZFCompareResult
compare result of two ZFObjects
Definition ZFCoreTypeDef_OtherType.h:28
@ ZFCompareUncomparable
Definition ZFCoreTypeDef_OtherType.h:29
@ ZFCompareEqual
Definition ZFCoreTypeDef_OtherType.h:31
zft_zfstring< zfchar > zfstring
see zft_zfstring
Definition ZFCoreTypeDef_StringType.h:15
#define zfidentityHash(hash,...)
util method to connect multiple hash value into one hash value
Definition ZFCoreUtilMacro.h:381
#define ZFBitTest(var, bit)
macro to detect whether bit is set, zftrue if any one of test bit has set
Definition ZFCoreUtilMacro.h:16
#define ZFBitSet(var, bit)
macro to set at bit
Definition ZFCoreUtilMacro.h:28
zfbool ZFInvokeT(zfauto &ret, zfstring *errorHint, const zfstring &name, 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))
see ZFInvoke
zfindex ZFDI_paramCount(const ZFArgs &zfargs)
util method to calc param count
Definition ZFDynamicInvoker.h:280
zfbool ZFDI_toNumberT(zfdouble &ret, ZFObject *obj)
util method to convert to number
zfstring ZFDI_paramInfo(const ZFArgs &zfargs)
see ZFDI_paramInfoT
Definition ZFDynamicInvoker.h:273
zfauto ZFInvoke(const zfstring &name)
util to ZFDI_invoke/ZFDI_alloc for static method or object allocation
zfbool ZFDI_objectFromString(zfauto &ret, const ZFClass *cls, const zfchar *src, zfindex srcLen=((zfindex) -1), zfstring *errorHint=zft_zfnull)
util to convert object from string
void ZFDI_alloc(const ZFArgs &zfargs, const ZFClass *cls, zfbool convStr=_ZFT_t_zffalse)
perform advanced dynamic class alloc
ZFCoreArray< ZFOutput > & ZFDI_errorCallbacks(void)
callbacks when any dynamic invoker fails
void ZFDI_paramInfoT(zfstring &ret, const ZFArgs &zfargs)
util to print param info
zfauto ZFInvokeDetail(const zfstring &name, const ZFCoreArray< zfauto > &params, zfbool *success=zft_zfnull, zfstring *errorHint=zft_zfnull)
see ZFInvoke
zfdouble ZFDI_toNumber(ZFObject *obj)
see ZFDI_toNumberT, return -1 if fail
Definition ZFDynamicInvoker.h:252
zfstring ZFDI_toString(ZFObject *obj)
util method to convert to string
void ZFDI_invoke(const ZFArgs &zfargs, const zfstring &name, zfbool convStr=_ZFT_t_zffalse)
perform advanced dynamic invoke
zfidentity zfidentityCalcString(const zfchar *src, zfindex srcLen)
calculate identity from string
zfidentity zfidentityCalc(T_Type const &v)
calculate identity from POD object
Definition ZFIdentityUtil.h:100
#define ZFMP_DEF()
mark the object as default param
Definition ZFMethodGenericInvoker.h:30
#define ZFMETHOD_MAX_PARAM
max param supported by ZFMethod
Definition ZFMethodGenericInvoker.h:16
#define ZF_NAMESPACE_GLOBAL_BEGIN
begin namespace ZFFramework
Definition ZFNamespace.h:97
#define ZF_NAMESPACE_GLOBAL_END
end namespace ZFFramework
Definition ZFNamespace.h:98
#define zfclass
same as class, shows that this class is a ZFObject type
Definition ZFObjectClassTypeFwd.h:38
base class of all objects
#define ZFOBJECT_DECLARE(ChildClass, SuperClass,...)
necessary for every class inherit from ZFObject
Definition ZFObjectDeclare.h:122
retain count logic for ZFFramework
listener data used by ZFListener
Definition ZFArgs.h:20
ZFObject's class info.
Definition ZFClass.h:63
light weight array
Definition ZFCoreArray.h:292
ZFDI_Wrapper zfself
class ref to self
Definition ZFDynamicInvoker.h:21
ValueType valueType(void)
valueType
Definition ZFDynamicInvoker.h:39
const zfstring & valueString(void)
access string value
Definition ZFDynamicInvoker.h:47
virtual zfbool objectIsInternalPrivate(void)
whether this object is internal private object or its class is ZFClass::classIsInternalPrivate
Definition ZFDynamicInvoker.h:193
virtual zfbool objectIsInternal(void)
whether this object is internal object or its class is ZFClass::classIsInternal
Definition ZFDynamicInvoker.h:191
void value(const zflong &v)
change value
Definition ZFDynamicInvoker.h:143
zfdouble valueFloat(void)
helper method
Definition ZFDynamicInvoker.h:130
virtual void objectOnInit(const zflong &v)
init with value
Definition ZFDynamicInvoker.h:209
void value(const zfstring &v)
change value
Definition ZFDynamicInvoker.h:137
virtual zfidentity objectHashImpl(void)
see objectHash
Definition ZFDynamicInvoker.h:157
virtual ZFCompareResult objectCompareImpl(ZFObject *anotherObj)
see objectCompare
Definition ZFDynamicInvoker.h:171
virtual void objectOnInit(const zfstring &v)
init with value
Definition ZFDynamicInvoker.h:207
virtual void objectInfoImpl(zfstring &ret)
see objectInfo
Definition ZFDynamicInvoker.h:195
zfbool valueFloatT(zfdouble &ret)
helper method
Definition ZFDynamicInvoker.h:103
ValueType
value type
Definition ZFDynamicInvoker.h:25
@ TypeFloat
float type
Definition ZFDynamicInvoker.h:28
@ TypeString
string type
Definition ZFDynamicInvoker.h:26
@ TypeInt
int type
Definition ZFDynamicInvoker.h:27
virtual void objectOnInit(void)
override this to init your object
Definition ZFDynamicInvoker.h:205
virtual void objectOnInit(const zfdouble &v)
init with value
Definition ZFDynamicInvoker.h:211
void value(const zfdouble &v)
change value
Definition ZFDynamicInvoker.h:149
zflong valueInt(void)
helper method
Definition ZFDynamicInvoker.h:97
zfbool valueIntT(zflong &ret)
helper method
Definition ZFDynamicInvoker.h:67
base class of all objects
Definition ZFObjectCore.h:195
virtual void objectOnInit(void)
override this to init your object
general output callback
Definition ZFIOCallback_output.h:37
a ZFObject holder which would release content object automatically when destroyed
Definition zfautoFwd.h:34
#define zfcast(T_To, obj)
safely cast ZFObject types, return null if not desired type
Definition zfcast.h:24