ZFFramework
Loading...
Searching...
No Matches
ZFCoreValue.h
Go to the documentation of this file.
1
5#ifndef _ZFI_ZFCoreValue_h_
6#define _ZFI_ZFCoreValue_h_
7
8#include "ZFCoreTypeDef.h"
9#include "ZFMemPool.h"
10
12
16template<typename T_Value>
18public:
22 typedef T_Value ValueType;
23
24public:
28 void refPrepare(void) {if(d == zfnull) {d = zfpoolNew(D);}}
32 void refDelete(void) {
33 if(d) {
34 D *dTmp = d;
35 d = zfnull;
36 if(--(dTmp->refCount) == 0) {
37 zfpoolDelete(dTmp);
38 }
39 }
40 }
41
45 zfbool valid(void) const {
46 return d;
47 }
48
50 T_Value &value(void) {
51 if(d) {
52 return d->v;
53 }
54 else {
55 d = zfpoolNew(D);
56 return d->v;
57 }
58 }
59
60 T_Value const &value(void) const {
61 if(d) {
62 return d->v;
63 }
64 else {
65 static T_Value t;
66 return t;
67 }
68 }
69
70 ZFCoreValue<T_Value> &value(ZF_IN T_Value const &v) {
71 if(d) {
72 d->v = v;
73 }
74 else {
75 d = zfpoolNew(D, v);
76 }
77 return *this;
78 }
79
80public:
82 ZFCoreValue(void) : d(zfnull) {}
83 ZFCoreValue(ZF_IN ZFCoreValue<T_Value> const &ref) : d(ref.d) {
84 if(ref.d) {
85 ++(ref.d->refCount);
86 }
87 }
88 ~ZFCoreValue(void) {
89 if(d && --(d->refCount) == 0) {
90 zfpoolDelete(d);
91 }
92 }
93 zfbool operator == (ZF_IN ZFCoreValue<T_Value> const &ref) const {return d == ref.d;}
94 zfbool operator != (ZF_IN ZFCoreValue<T_Value> const &ref) const {return d != ref.d;}
95 ZFCoreValue<T_Value> &operator = (ZF_IN ZFCoreValue<T_Value> const &ref) {
96 if(d != ref.d) {
97 D *dTmp = d;
98 d = ref.d;
99 if(d) {
100 ++(d->refCount);
101 }
102 if(dTmp && --(dTmp->refCount) == 0) {
103 zfpoolDelete(dTmp);
104 }
105 }
106 return *this;
107 }
109
110private:
111 zfclassNotPOD D {
112 public:
113 zfuint refCount;
114 T_Value v;
115 public:
116 D(void)
117 : refCount(1)
118 , v()
119 {
120 }
121 D(ZF_IN T_Value const &v)
122 : refCount(1)
123 , v(v)
124 {
125 }
126 };
127private:
128 D *d;
129};
130
132#endif // #ifndef _ZFI_ZFCoreValue_h_
133
types for ZFFramework
#define zfclassLikePOD
shows the class is not a POD type, but you may use it like a POD except memset it to 0
Definition ZFCoreTypeDef_ClassType.h:41
#define ZF_IN
dummy macro that shows the param used as required input
Definition ZFCoreTypeDef_ClassType.h:184
#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
_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
_ZFT_t_zfuint zfuint
same as unsigned int, see zfindex
Definition ZFCoreTypeDef_CoreType.h:169
memory pool
#define zfpoolDelete(obj)
see zfpoolNew
Definition ZFMemPool.h:38
#define zfpoolNew(T_Type,...)
internal use only, for allocating internal types for performance
Definition ZFMemPool.h:37
#define ZF_NAMESPACE_GLOBAL_BEGIN
begin namespace ZFFramework
Definition ZFNamespace.h:97
#define ZF_NAMESPACE_GLOBAL_END
end namespace ZFFramework
Definition ZFNamespace.h:98
light weight value holder with ref count logic
Definition ZFCoreValue.h:17
void refPrepare(void)
prepare instance to make it able to be shared between each copy
Definition ZFCoreValue.h:28
ZFCoreValue< T_Value > & value(T_Value const &v)
change the value, all reference would also be changed
Definition ZFCoreValue.h:70
void refDelete(void)
delete reference
Definition ZFCoreValue.h:32
T_Value const & value(void) const
access the value
Definition ZFCoreValue.h:60
zfbool valid(void) const
whether the value has been initialized
Definition ZFCoreValue.h:45
T_Value & value(void)
access the value
Definition ZFCoreValue.h:50
T_Value ValueType
value type
Definition ZFCoreValue.h:22