ZFFramework
 
Loading...
Searching...
No Matches
ZFEnum.h
Go to the documentation of this file.
1
5
6#ifndef _ZFI_ZFEnum_h_
7#define _ZFI_ZFEnum_h_
8
9#include "ZFTypeId.h"
10#include "ZFEnumDeclare.h"
11#include "ZFEnumDeclare_inner.h"
13
14// ============================================================
18#define ZFEnumInvalid() ((zfuint)-1)
22#define ZFEnumNameInvalid() _ZFP_ZFEnumNameInvalid()
23extern ZFLIB_ZFCore const zfstring &_ZFP_ZFEnumNameInvalid(void);
24
25// ============================================================
38zfabstract ZFLIB_ZFCore ZFEnum : zfextend ZFTypeIdWrapper {
40
41protected:
43 ZFEnum(void) : _ZFP_ZFEnum_value(ZFEnumInvalid()) {}
45
46protected:
49 ZF_IN const ZFSerializableData &serializableData
50 , ZF_OUT_OPT zfstring *outErrorHint = zfnull
51 , ZF_OUT_OPT ZFSerializableData *outErrorPos = zfnull
52 );
55 ZF_IN_OUT ZFSerializableData &serializableData
56 , ZF_OUT_OPT zfstring *outErrorHint = zfnull
57 , ZF_IN_OPT ZFSerializable *refOwner = zfnull
58 );
59
60protected:
62 virtual void copyableOnCopyFrom(ZF_IN ZFObject *anotherObj) {
63 zfsuperI(ZFCopyable)::copyableOnCopyFrom(anotherObj);
64 this->enumValue(zfcast(zfself *, anotherObj)->enumValue());
65 }
66
67protected:
71 virtual void objectOnInit(ZF_IN zfuint value);
75 virtual void objectOnInit(ZF_IN ZFEnum *another);
76
78 virtual void objectOnInit(void) {zfsuper::objectOnInit();}
79
80public:
82 virtual void objectInfoImpl(ZF_IN_OUT zfstring &ret);
83
85 virtual inline zfidentity objectHashImpl(void) {
86 return zfidentityCalc(this->enumValue());
87 }
88
93 virtual ZFCompareResult objectCompareImpl(ZF_IN ZFObject *anotherObj);
94
95public:
96 /* use ZFObject version instead of ZFTypeIdWrapper version */
98 virtual zfbool objectIsInternal(void) {
100 }
105
106public:
108 virtual void *wrappedValue(void);
110 virtual void wrappedValue(ZF_IN const void *v);
112 virtual void wrappedValueCopy(ZF_IN void *v);
113public:
115 virtual void zfvReset(void);
117 virtual zfbool zfvIsInit(void);
118public:
121 return zftrue;
122 }
125 ZF_IN const ZFSerializableData &serializableData
126 , ZF_OUT_OPT zfstring *outErrorHint = zfnull
127 , ZF_OUT_OPT ZFSerializableData *outErrorPos = zfnull
128 );
131 ZF_OUT ZFSerializableData &serializableData
132 , ZF_OUT_OPT zfstring *outErrorHint = zfnull
133 );
136 ZF_IN const zfchar *src
137 , ZF_IN_OPT zfindex srcLen = zfindexMax()
138 , ZF_OUT_OPT zfstring *errorHint = zfnull
139 );
143 , ZF_OUT_OPT zfstring *errorHint = zfnull
144 );
145
146public:
167
176
182
187
191 virtual zfuint const &enumValue(void) {
192 return _ZFP_ZFEnum_value;
193 }
194
200 virtual const zfstring &enumName(void) {
201 return this->enumNameForValue(_ZFP_ZFEnum_value);
202 }
203
204public:
210 virtual void enumValue(ZF_IN zfuint const &value) {
211 if(!this->enumIsFlags() && !this->enumValueContain(value)) {
212 _ZFP_ZFEnum_value = ZFEnumInvalid();
213 return;
214 }
215 _ZFP_ZFEnum_value = value;
216 }
217
218public:
219 void _ZFP_enumValue(ZF_IN zfuint value) {
220 this->enumValue(value);
221 }
222public:
223 zfuint _ZFP_ZFEnum_value;
224public:
226 virtual void wrappedValueOnAssign(ZF_IN ZFTypeIdWrapper *ref) {
227 zfself *refTmp = zfcast(zfself *, ref);
228 if(refTmp != zfnull) {
229 this->_ZFP_ZFEnum_value = refTmp->_ZFP_ZFEnum_value;
230 }
231 }
232};
233
234// ============================================================
235// zfflags conversion
240 ZF_OUT zfflags &ret
241 , ZF_IN const ZFClass *enumClass
242 , ZF_IN const zfchar *src
243 , ZF_IN_OPT zfindex srcLen = zfindexMax()
244 , ZF_IN_OPT zfchar separatorToken = '|'
245 );
250 ZF_IN const ZFClass *enumClass
251 , ZF_IN const zfchar *src
252 , ZF_IN_OPT zfindex srcLen = zfindexMax()
253 , ZF_IN_OPT zfchar separatorToken = '|'
254 ) {
255 zfflags ret = 0;
256 zfflagsFromStringT(ret, enumClass, src, srcLen, separatorToken);
257 return ret;
258}
259
264 , ZF_IN const ZFClass *enumClass
265 , ZF_IN zfflags const &value
266 , ZF_IN_OPT zfbool includeNotConverted = zftrue
267 , ZF_IN_OPT zfbool exclusiveMode = zffalse
268 , ZF_OUT_OPT zfflags *notConverted = zfnull
269 , ZF_IN_OPT zfchar separatorToken = '|'
270 );
273 ZF_IN const ZFClass *enumClass
274 , ZF_IN zfflags const &value
275 , ZF_IN_OPT zfbool includeNotConverted = zftrue
276 , ZF_IN_OPT zfbool exclusiveMode = zffalse
277 , ZF_OUT_OPT zfflags *notConverted = zfnull
278 , ZF_IN_OPT zfchar separatorToken = '|'
279 ) {
280 zfstring ret;
281 zfflagsToStringT(ret, enumClass, value, includeNotConverted, exclusiveMode, notConverted, separatorToken);
282 return ret;
283}
284
285// ============================================================
286zfclassFwd _ZFP_ZFEnumDataPrivate;
287zfclassNotPOD ZFLIB_ZFCore _ZFP_ZFEnumData {
288public:
289 _ZFP_ZFEnumData(void);
290 ~_ZFP_ZFEnumData(void);
291public:
292 zfbool needInitFlag;
293 const ZFClass *ownerClass;
294 zfuint enumDefault;
295 zfbool enumIsFlags;
296 void add(
297 ZF_IN zfbool isEnableDuplicateValue
298 , ZF_IN zfuint value
299 , ZF_IN const zfstring &name
300 );
301 zfindex enumCount(void) const;
302 zfindex enumIndexForValue(ZF_IN zfuint value) const;
303 zfuint enumValueAt(ZF_IN zfindex index) const;
304 const zfstring &enumNameAt(ZF_IN zfindex index) const;
305 zfbool enumValueContain(ZF_IN zfuint value) const;
306 zfuint enumValueForName(ZF_IN const zfstring &name) const;
307 const zfstring &enumNameForValue(ZF_IN zfuint value) const;
308private:
309 _ZFP_ZFEnumDataPrivate *d;
310};
311extern ZFLIB_ZFCore _ZFP_ZFEnumData *_ZFP_ZFEnumDataAccess(ZF_IN const ZFClass *ownerClass);
312extern ZFLIB_ZFCore void _ZFP_ZFEnumDataCleanup(ZF_IN const ZFClass *ownerClass);
313extern ZFLIB_ZFCore void _ZFP_ZFEnumDataCleanup(ZF_IN const _ZFP_ZFEnumData *d);
314zfclassNotPOD _ZFP_ZFEnumDataHolder {
315public:
316 _ZFP_ZFEnumDataHolder(ZF_IN const _ZFP_ZFEnumData *d)
317 : d(d)
318 {
319 }
320 _ZFP_ZFEnumDataHolder(void) {
321 _ZFP_ZFEnumDataCleanup(d);
322 }
323public:
324 const _ZFP_ZFEnumData *d;
325};
326extern ZFLIB_ZFCore const _ZFP_ZFEnumData *_ZFP_ZFEnumDataFind(ZF_IN const ZFClass *enumClass);
327extern ZFLIB_ZFCore void _ZFP_ZFEnumMethodReg(
329 , ZF_IN const _ZFP_ZFEnumData *d
330 );
331
333#endif // #ifndef _ZFI_ZFEnum_h_
334
#define ZFLIB_ZFCore
used to export symbols
Definition ZFCoreEnvDef.h:30
_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:192
#define zfextend
dummy macro shows class inherit from another
Definition ZFCoreTypeDef_ClassType.h:53
#define zfpurevirtual
dummy macro shows that a method is pure virtual method
Definition ZFCoreTypeDef_ClassType.h:68
#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:180
#define ZF_IN_OPT
dummy macro that shows the param used as optional input
Definition ZFCoreTypeDef_ClassType.h:184
#define ZF_OUT
dummy macro that shows the param used as required output
Definition ZFCoreTypeDef_ClassType.h:188
#define zfclassNotPOD
shows the class is not a POD type, you should not memset it or declare it in stack or copy value by c...
Definition ZFCoreTypeDef_ClassType.h:48
#define ZF_IN_OUT
dummy macro that shows the param used as required input and output
Definition ZFCoreTypeDef_ClassType.h:196
#define zfclassFwd
forward declaration of a class type
Definition ZFCoreTypeDef_ClassType.h:31
_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
#define zffalse
bool false type
Definition ZFCoreTypeDef_CoreType.h:111
#define zfnull
same as NULL, defined for future use
Definition ZFCoreTypeDef_CoreType.h:88
_ZFT_t_zfuint zfuint
same as unsigned int, see zfindex
Definition ZFCoreTypeDef_CoreType.h:169
ZFCompareResult
compare result of two ZFObjects
Definition ZFCoreTypeDef_OtherType.h:28
zft_zfstring< zfchar > zfstring
see zft_zfstring
Definition ZFCoreTypeDef_StringType.h:15
zfflags zfflagsFromString(const ZFClass *enumClass, const zfchar *src, zfindex srcLen=((zfindex) -1), zfchar separatorToken='|')
zfflags conversion using ZFEnum as source, see zfflagsFromStringT
Definition ZFEnum.h:249
zfstring zfflagsToString(const ZFClass *enumClass, zfflags const &value, zfbool includeNotConverted=_ZFT_t_zftrue, zfbool exclusiveMode=_ZFT_t_zffalse, zfflags *notConverted=zft_zfnull, zfchar separatorToken='|')
see zfflagsToStringT
Definition ZFEnum.h:272
zfbool zfflagsToStringT(zfstring &ret, const ZFClass *enumClass, zfflags const &value, zfbool includeNotConverted=_ZFT_t_zftrue, zfbool exclusiveMode=_ZFT_t_zffalse, zfflags *notConverted=zft_zfnull, zfchar separatorToken='|')
zfflags conversion using ZFEnum as source, see zfflagsToString
#define ZFEnumInvalid()
invalid value for ZFEnum
Definition ZFEnum.h:18
zfbool zfflagsFromStringT(zfflags &ret, const ZFClass *enumClass, const zfchar *src, zfindex srcLen=((zfindex) -1), zfchar separatorToken='|')
zfflags conversion using ZFEnum as source, see zfflagsFromString
ZFEnum declare impl.
ZFEnum declare impl.
zfidentity zfidentityCalc(T_Type const &v)
calculate identity from POD object
Definition ZFIdentityUtil.h:92
#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 zfcast(T_To, obj)
safely cast ZFObject types, return null if not desired type
Definition ZFObjectCast.h:28
#define zfabstract
typename for class showing that its abstract
Definition ZFObjectClassTypeFwd.h:42
#define ZFOBJECT_DECLARE_ABSTRACT_WITH_CUSTOM_CTOR(ChildClass, SuperClass,...)
see ZFOBJECT_DECLARE_WITH_CUSTOM_CTOR
Definition ZFObjectDeclare.h:160
#define zfsuperI(T_SuperType)
class ref to proper super type, see ZFObject for more info
Definition ZFObjectDeclare.h:26
reflectable type define
ZFObject's class info.
Definition ZFClass.h:67
light weight array
Definition ZFCoreArray.h:331
virtual zfbool zfvIsInit(void)
true if the value is in init value state
virtual zfbool zfvToData(ZFSerializableData &serializableData, zfstring *outErrorHint=zft_zfnull)
convert to serializable data
virtual ZFCompareResult objectCompareImpl(ZFObject *anotherObj)
compare result of two enum values as int, or return ZFCompareUncomparable if any of them are ZFEnumIn...
virtual void zfvReset(void)
reset the value to it's init value
virtual void wrappedValueOnAssign(ZFTypeIdWrapper *ref)
see zfvAssign
Definition ZFEnum.h:226
virtual zfbool objectIsInternalPrivate(void)
whether this object is internal private object or its class is ZFClass::classIsInternalPrivate
Definition ZFEnum.h:102
virtual zfindex enumCount(void)=0
get value num
virtual void objectOnInit(zfuint value)
init with value or ZFEnumInvalid if invalid
virtual zfbool wrappedValuePreferStringConverter(void)
whether prefer string converter during serialization, which would result much shorter result
Definition ZFEnum.h:120
virtual void wrappedValue(const void *v)
set the value, no type safe check
virtual zfuint enumValueForName(const zfstring &name)=0
get the value with specified name, or ZFEnumInvalid if not exist
virtual void copyableOnCopyFrom(ZFObject *anotherObj)
called by copy to copy contents from anotherObj
Definition ZFEnum.h:62
virtual const zfstring & enumNameForValue(zfuint value)=0
get the name with specified value, or ZFEnumNameInvalid if not exist
virtual zfbool zfvFromData(const ZFSerializableData &serializableData, zfstring *outErrorHint=zft_zfnull, ZFSerializableData *outErrorPos=zft_zfnull)
convert from serializable data
virtual zfuint enumDefault(void)=0
default value of this enum
virtual const zfstring & enumNameAt(zfindex index)=0
get the name at index, or ZFEnumNameInvalid if not exist
virtual zfbool enumIsFlags(void)=0
true if this enum type is flags type (which support bit operation)
virtual zfbool serializableOnSerializeFromData(const ZFSerializableData &serializableData, zfstring *outErrorHint=zft_zfnull, ZFSerializableData *outErrorPos=zft_zfnull)
for serializable data that has "category" attribute, ZFSerializable would ignore it and leave it to s...
virtual zfbool objectIsInternal(void)
whether this object is internal object or its class is ZFClass::classIsInternal
Definition ZFEnum.h:98
virtual zfindex enumIndexForValue(zfuint value)=0
get the index of enum value, or zfindexMax() if no such enum value
virtual zfbool enumValueContain(zfuint value)=0
return true if contain the specified value
virtual const zfstring & enumName(void)
get current value's name, or ZFEnumNameInvalid if invalid
Definition ZFEnum.h:200
virtual zfbool zfvToString(zfstring &s, zfstring *errorHint=zft_zfnull)
convert to string
virtual zfbool serializableOnSerializeToData(ZFSerializableData &serializableData, zfstring *outErrorHint=zft_zfnull, ZFSerializable *refOwner=zft_zfnull)
corresponding to serializableOnSerializeFromData, return whether the task is success,...
virtual zfuint const & enumValue(void)
get the enum value, or ZFEnumInvalid if invalid
Definition ZFEnum.h:191
virtual void enumValue(zfuint const &value)
set the enum value
Definition ZFEnum.h:210
virtual void wrappedValueCopy(void *v)
get the value, no type safe check
virtual void objectOnInit(void)
override this to init your object
Definition ZFEnum.h:78
virtual void * wrappedValue(void)
access the value's address
ZFEnum zfself
class ref to self
Definition ZFEnum.h:39
virtual zfbool zfvFromString(const zfchar *src, zfindex srcLen=((zfindex) -1), zfstring *errorHint=zft_zfnull)
convert from string
virtual zfuint enumValueAt(zfindex index)=0
get the value at index, or ZFEnumInvalid if not exist
virtual void objectInfoImpl(zfstring &ret)
see objectInfo
virtual void objectOnInit(ZFEnum *another)
init with ZFEnum
virtual zfidentity objectHashImpl(void)
see objectHash
Definition ZFEnum.h:85
virtual void objectOnInit(void)
override this to init your object
virtual zfbool objectIsInternal(void)
whether this object is internal object or its class is ZFClass::classIsInternal
virtual zfbool objectIsInternalPrivate(void)
whether this object is internal private object or its class is ZFClass::classIsInternalPrivate
ZFSerializable's data container, see ZFSerializable.
Definition ZFSerializableData.h:74