ZFFramework
Loading...
Searching...
No Matches
ZFValue.h
Go to the documentation of this file.
1
5
6#ifndef _ZFI_ZFValue_h_
7#define _ZFI_ZFValue_h_
8
9#include "ZFObjectCore.h"
10#include "ZFObjectRetain.h"
12
13zfclassNotPOD ZFLIB_ZFCore _ZFP_ZFValueType {
14public:
15 static void Type_zffree(ZF_IN void *value) {
16 zffree(value);
17 }
18 template<typename T_Object>
19 static void Type_zfdelete(ZF_IN void *value) {
20 zfdelete((T_Object *)value);
21 }
22 template<typename T_Object>
23 static void Type_zfpoolDelete(ZF_IN void *value) {
24 zfpoolDelete((T_Object *)value);
25 }
26};
28#define ZFValueType_none() zfnull
30#define ZFValueType_zffree() _ZFP_ZFValueType::Type_zffree
32#define ZFValueType_zfdelete(T_Object) _ZFP_ZFValueType::Type_zfdelete<T_Object >
34#define ZFValueType_zfpoolDelete(T_Object) _ZFP_ZFValueType::Type_zfpoolDelete<T_Object >
35
36// ============================================================
37// ZFValue
48typedef void (*ZFValueType)(ZF_IN void *value);
49
54 // ============================================================
55public:
57
58protected:
59 ZFValue(void) : _value(zfnull), _type(zfnull) {}
60
61private:
62 void *_value;
63 ZFValueType _type;
64 void _cleanup(void) {
65 if(_value && _type) {
66 void *value = _value;
67 ZFValueType type = _type;
68 _value = zfnull;
69 _type = zfnull;
70 type(value);
71 }
72 else {
73 _value = zfnull;
74 _type = zfnull;
75 }
76 }
77
78protected:
82 virtual void objectOnInit(
83 ZF_IN void *value
85 ) {
86 _value = value;
87 _type = type;
88 }
89
92 template<typename T_Value>
93 void objectOnInit(ZF_IN T_Value const &value) {
94 _value = zfpoolNew(T_Value, value);
95 _type = ZFValueType_zfpoolDelete(T_Value);
96 }
98 virtual void objectOnInit(void) {
100 }
102 virtual void objectOnDeallocPrepare(void) {
103 this->_cleanup();
105 }
106
107public:
112 if(_value && _type) {
113 void *valueOld = _value;
114 ZFValueType typeOld = _type;
115 _value = value;
116 _type = type;
117 typeOld(valueOld);
118 }
119 else {
120 _value = value;
121 _type = type;
122 }
123 return this;
124 }
125
128 template<typename T_Value>
129 ZFValue *value(ZF_IN T_Value const &value) {
130 if(_value && _type) {
131 void *valueOld = _value;
132 ZFValueType typeOld = _type;
133 _value = zfpoolNew(T_Value, value);
134 _type = ZFValueType_zfpoolDelete(T_Value);
135 typeOld(valueOld);
136 }
137 else {
138 _value = zfpoolNew(T_Value, value);
139 _type = ZFValueType_zfpoolDelete(T_Value);
140 }
141 return this;
142 }
143
146 template<typename T_Value>
147 T_Value &valueCreate(void) {
148 if(_value && _type) {
149 void *valueOld = _value;
150 ZFValueType typeOld = _type;
151 _value = zfpoolNew(T_Value);
152 _type = ZFValueType_zfpoolDelete(T_Value);
153 typeOld(valueOld);
154 }
155 else {
156 _value = zfpoolNew(T_Value);
157 _type = ZFValueType_zfpoolDelete(T_Value);
158 }
159 return *(T_Value *)_value;
160 }
161
162public:
166 zfbool valid(void) {return _value != zfnull;}
170 void *valuePtr(void) {return _value;}
174 ZFValueType type(void) {return _type;}
175
176public:
182 template<typename T_Value>
183 inline T_Value &value(void) {
184 return *(T_Value *)_value;
185 }
186
191 template<typename T_Value>
192 inline T_Value *valuePtr(void) {
193 return (T_Value *)_value;
194 }
195
196protected:
199public:
201 virtual ZFCompareResult objectCompareValueImpl(ZF_IN ZFObject *anotherObj);
202};
203
205#endif // #ifndef _ZFI_ZFValue_h_
206
#define ZFLIB_ZFCore
used to export symbols
Definition ZFCoreEnvDef.h:30
#define zffinal
dummy macro shows that a method or class is designed must not to be overrided
Definition ZFCoreTypeDef_ClassType.h:63
#define zfextend
dummy macro shows class inherit from another
Definition ZFCoreTypeDef_ClassType.h:53
#define zfdelete(instance)
same as delete defined for future use
Definition ZFCoreTypeDef_ClassType.h:110
#define zfoverride
dummy macro shows that method override parent's method
Definition ZFCoreTypeDef_ClassType.h:58
#define zffree(ptr)
same as free defined for future use, do nothing if ptr is NULL
Definition ZFCoreTypeDef_ClassType.h:104
#define ZF_IN
dummy macro that shows the param used as required input
Definition ZFCoreTypeDef_ClassType.h:191
#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:207
#define zfclassFwd
forward declaration of a class type
Definition ZFCoreTypeDef_ClassType.h:31
_ZFT_t_zfbool zfbool
bool type
Definition ZFCoreTypeDef_CoreType.h:103
#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
zft_zfstring< zfchar > zfstring
see zft_zfstring
Definition ZFCoreTypeDef_StringType.h:15
#define zfpoolDelete(obj)
see zfpoolNew
Definition ZFMemPool.h:36
#define zfpoolNew(T_Type,...)
internal use only, for allocating internal types for performance
Definition ZFMemPool.h:35
#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_WITH_CUSTOM_CTOR(ChildClass, SuperClass,...)
declare object which allow custom constructor
Definition ZFObjectDeclare.h:139
retain count logic for ZFFramework
void(* ZFValueType)(void *value)
type for ZFValue
Definition ZFValue.h:48
#define ZFValueType_zfpoolDelete(T_Object)
see ZFValueType
Definition ZFValue.h:34
virtual void objectOnInit(void)
override this to init your object
virtual void objectOnDeallocPrepare(void)
called before objectOnDealloc, safe to call virtual functions here
Definition ZFObjectCore.h:746
used to hold a non ZFObject type for performance, for internal use only
Definition ZFValue.h:53
virtual void objectOnDeallocPrepare(void)
called before objectOnDealloc, safe to call virtual functions here
Definition ZFValue.h:102
ZFValueType type(void)
delete callback to clear the value
Definition ZFValue.h:174
T_Value * valuePtr(void)
util method to access value as reference
Definition ZFValue.h:192
virtual void objectOnInit(void *value, ZFValueType type)
init with value and type
Definition ZFValue.h:82
T_Value & valueCreate(void)
change the value
Definition ZFValue.h:147
virtual ZFCompareResult objectCompareValueImpl(ZFObject *anotherObj)
see objectCompareValue
ZFValue * value(void *value, ZFValueType type)
change the value
Definition ZFValue.h:111
virtual void objectOnInit(void)
override this to init your object
Definition ZFValue.h:98
virtual void objectInfoImplAppend(zfstring &ret)
see objectInfo
T_Value & value(void)
util method to access value as reference
Definition ZFValue.h:183
zfbool valid(void)
whether value valid
Definition ZFValue.h:166
ZFValue * value(T_Value const &value)
change the value
Definition ZFValue.h:129
void * valuePtr(void)
used to hold the raw pointer
Definition ZFValue.h:170
void objectOnInit(T_Value const &value)
init and copy value
Definition ZFValue.h:93