ZFFramework
 
Loading...
Searching...
No Matches
ZFValueHolder.h
Go to the documentation of this file.
1
5
6#ifndef _ZFI_ZFValueHolder_h_
7#define _ZFI_ZFValueHolder_h_
8
9#include "ZFObjectCore.h"
10#include "ZFObjectRetain.h"
12
13// ============================================================
14// ZFValueHolder
25typedef void (*ZFValueHolderType)(ZF_IN void *value);
26
30zffinal zfclass ZFLIB_ZFCore ZFValueHolder : zfextend ZFObject {
31 // ============================================================
32public:
33 ZFOBJECT_DECLARE_WITH_CUSTOM_CTOR(ZFValueHolder, ZFObject)
34
35protected:
36 ZFValueHolder(void) : _value(zfnull), _type(zfnull) {}
37
39 cache->_cleanup();
40 })
41private:
42 void *_value;
44 void _cleanup(void) {
45 if(_value && _type) {
46 void *value = _value;
47 ZFValueHolderType type = _type;
48 _value = zfnull;
49 _type = zfnull;
50 type(value);
51 }
52 else {
53 _value = zfnull;
54 _type = zfnull;
55 }
56 }
57
58protected:
62 virtual void objectOnInit(
63 ZF_IN void *value
65 ) {
66 _value = value;
67 _type = type;
68 }
70 virtual void objectOnInit(void) {
72 }
74 virtual void objectOnDeallocPrepare(void) {
75 this->_cleanup();
77 }
78
79public:
84 if(_value && _type) {
85 void *value = _value;
86 ZFValueHolderType type = _type;
87 _value = value;
88 _type = type;
89 type(value);
90 }
91 else {
92 _value = value;
93 _type = type;
94 }
95 }
96
97public:
104 void *value(void) {return _value;}
108 ZFValueHolderType type(void) {return _type;}
109
110public:
112 template<typename T_RawType>
113 inline T_RawType valueT(void) {
114 return (T_RawType)_value;
115 }
116
117 template<typename T_RawType>
118 inline T_RawType valueRef(void) {
119 typedef typename zftTraits<T_RawType>::TrPtr T_RawTypePointer;
120 return *(T_RawTypePointer)_value;
121 }
122
123protected:
126public:
128 virtual ZFCompareResult objectCompareValueImpl(ZF_IN ZFObject *anotherObj);
129};
130zfclassNotPOD ZFLIB_ZFCore _ZFP_ZFValueHolderType {
131public:
132 static void TypePOD(ZF_IN void *value) {
133 zffree(value);
134 }
135 template<typename T_Object>
136 static void TypeObject(ZF_IN void *value) {
137 zfdelete((T_Object)value);
138 }
139 template<typename T_Object>
140 static void TypePoolObject(ZF_IN void *value) {
141 zfpoolDelete((T_Object)value);
142 }
143};
145#define ZFValueHolderTypePointerRef() zfnull
147#define ZFValueHolderTypePOD() _ZFP_ZFValueHolderType::TypePOD
149#define ZFValueHolderTypeObject(T_Object) _ZFP_ZFValueHolderType::TypeObject<T_Object>
151#define ZFValueHolderTypePoolObject(T_Object) _ZFP_ZFValueHolderType::TypePoolObject<T_Object>
152
154#endif // #ifndef _ZFI_ZFValueHolder_h_
155
#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:91
#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:112
#define ZF_IN
dummy macro that shows the param used as required input
Definition ZFCoreTypeDef_ClassType.h:180
#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
#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:38
#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:142
retain count logic for ZFFramework
#define ZFALLOC_CACHE_RELEASE(action)
mark class that it should apply cache logic when alloc
Definition ZFObjectRetain.h:186
void(* ZFValueHolderType)(void *value)
type for ZFValueHolder
Definition ZFValueHolder.h:25
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:758
used to hold a non ZFObject type for performance
Definition ZFValueHolder.h:30
ZFValueHolderType type(void)
delete callback to clear the value
Definition ZFValueHolder.h:108
virtual void objectOnInit(void)
override this to init your object
Definition ZFValueHolder.h:70
T_RawType valueRef(void)
util method to cast value
Definition ZFValueHolder.h:118
virtual void objectOnInit(void *value, ZFValueHolderType type)
init with object
Definition ZFValueHolder.h:62
virtual void objectInfoImplAppend(zfstring &ret)
see objectInfo
virtual void objectOnDeallocPrepare(void)
called before objectOnDealloc, safe to call virtual functions here
Definition ZFValueHolder.h:74
void value(void *value, ZFValueHolderType type)
change the value
Definition ZFValueHolder.h:83
virtual ZFCompareResult objectCompareValueImpl(ZFObject *anotherObj)
see objectCompareValue
void * value(void)
used to hold the raw pointer
Definition ZFValueHolder.h:104
T_RawType valueT(void)
util method to cast value
Definition ZFValueHolder.h:113
T_Type * TrPtr
pointer type
Definition ZFCoreUtilTemplate.h:108