ZFFramework
 
Loading...
Searching...
No Matches
ZFCorePointer.h
Go to the documentation of this file.
1
5#ifndef _ZFI_ZFCorePointer_h_
6#define _ZFI_ZFCorePointer_h_
7
8#include "ZFCoreTypeDef.h"
9#include "ZFCoreUtilMacro.h"
10#include "zfstr.h"
11
13
14// ============================================================
15template<typename T_Pointer>
16zfclassNotPOD _ZFP_ZFCorePointerHelper;
17template<typename T_Pointer>
18zfclassNotPOD _ZFP_ZFCorePointerHelper<T_Pointer *> {
19public:
20 static inline void *toNonConstRaw(ZF_IN T_Pointer *p) {
21 return p;
22 }
23 template<typename T_ZFCorePointer>
24 static inline T_Pointer *toPointer(ZF_IN T_ZFCorePointer p) {
25 return (T_Pointer *)p->pointerValueAccessNonConst();
26 }
27};
28template<typename T_Pointer>
29zfclassNotPOD _ZFP_ZFCorePointerHelper<const T_Pointer *> {
30public:
31 static inline void *toNonConstRaw(ZF_IN const T_Pointer *p) {
32 return zfnull;
33 }
34 template<typename T_ZFCorePointer>
35 static inline const T_Pointer *toPointer(ZF_IN T_ZFCorePointer p) {
36 return (const T_Pointer *)p->pointerValueAccess();
37 }
38};
39
40// ============================================================
48public:
50 ZFCorePointer(void) {}
51 virtual ~ZFCorePointer(void) {}
53
54public:
56 virtual void objectInfoT(ZF_IN_OUT zfstring &ret) const {
57 if(this->pointerValueAccess() == zfnull) {
58 ret += ZFTOKEN_zfnull;
59 }
60 else {
61 this->objectInfoOfContentT(ret);
62 }
63 }
64
65 virtual inline zfstring objectInfo(void) const {
66 zfstring ret;
67 this->objectInfoT(ret);
68 return ret;
69 }
70
71public:
77 virtual inline zfstring objectInfoOfContent(void) const {
78 zfstring ret;
79 this->objectInfoOfContentT(ret);
80 return ret;
81 }
82
83public:
87 virtual ZFCompareResult objectCompare(ZF_IN const ZFCorePointer &another) const {
88 return ((this->pointerValueAccess() == another.pointerValueAccess())
91 }
92
93public:
104 virtual const ZFCorePointer *refNew(void) const zfpurevirtual;
108 virtual void refDelete(void) const {
109 zfdelete(const_cast<ZFCorePointer *>(this));
110 }
111
114 virtual const void *pointerValueAccess(void) const zfpurevirtual;
118 virtual void *pointerValueAccessNonConst(void) const zfpurevirtual;
119
123 template<typename T_PointerDesired>
124 inline T_PointerDesired pointerValueT(void) const {
125 return _ZFP_ZFCorePointerHelper<T_PointerDesired>::toPointer(this);
126 }
127
132};
133ZFOUTPUT_TYPE(ZFCorePointer, {v.objectInfoT(s);})
134
135// ============================================================
136template<typename T_Pointer>
137zfclassNotPOD _ZFP_ZFCorePointerPrivate {
138public:
139 zfuint refCount;
140 T_Pointer pointerValue;
141public:
142 _ZFP_ZFCorePointerPrivate(void)
143 : refCount(1)
144 , pointerValue(zfnull)
145 {
146 }
147};
180template<typename T_Pointer, typename T_ZFCorePointerType>
182public:
186 inline void pointerValue(ZF_IN T_Pointer const &value) {
187 if(d->pointerValue != value) {
188 T_Pointer pointerValueOld = d->pointerValue;
189 d->pointerValue = value;
190 if(value) {
191 T_ZFCorePointerType::pointerOnRetain(d->pointerValue);
192 }
193 if(pointerValueOld != zfnull) {
194 T_ZFCorePointerType::pointerOnDelete(pointerValueOld);
195 }
196 }
197 }
198
201 inline T_Pointer const &pointerValue(void) const {
202 return d->pointerValue;
203 }
204
205public:
207 inline T_Pointer operator -> (void) const {
208 return d->pointerValue;
209 }
210 inline zfbool operator == (ZF_IN zfnullT const &p) const {
211 return (d->pointerValue == zfnull);
212 }
213 inline zfbool operator != (ZF_IN zfnullT const &p) const {
214 return (d->pointerValue != zfnull);
215 }
216 inline zfbool operator == (ZF_IN T_Pointer const &p) const {
217 return (d->pointerValue == p);
218 }
219 inline zfbool operator != (ZF_IN T_Pointer const &p) const {
220 return (d->pointerValue != p);
221 }
222 inline zfbool operator == (ZF_IN ZFCorePointerT<T_Pointer, T_ZFCorePointerType> const &ref) const {
223 return (d->pointerValue == ref.d->pointerValue);
224 }
225 inline zfbool operator != (ZF_IN ZFCorePointerT<T_Pointer, T_ZFCorePointerType> const &ref) const {
226 return (d->pointerValue == ref.d->pointerValue);
227 }
229
230public:
232 ZFCorePointerT(void)
233 : d(zfnew(_ZFP_ZFCorePointerPrivate<T_Pointer>))
234 {
235 }
236 ZFCorePointerT(ZF_IN zfnullT const &value)
237 : d(zfnew(_ZFP_ZFCorePointerPrivate<T_Pointer>))
238 {
239 }
240 ZFCorePointerT(ZF_IN T_Pointer const &value)
241 : d(zfnew(_ZFP_ZFCorePointerPrivate<T_Pointer>))
242 {
243 this->pointerValue(value);
244 }
246 : d(ref.d)
247 {
248 ++(d->refCount);
249 }
250 virtual ~ZFCorePointerT(void) {
251 --(d->refCount);
252 if(d->refCount == 0) {
253 if(d->pointerValue != zfnull) {
254 T_ZFCorePointerType::pointerOnDelete(d->pointerValue);
255 }
256 zfdelete(d);
257 }
258 }
260 _ZFP_ZFCorePointerPrivate<T_Pointer> *dTmp = d;
261 d = ref.d;
262 ++(ref.d->refCount);
263 --(dTmp->refCount);
264 if(dTmp->refCount == 0) {
265 if(dTmp->pointerValue != zfnull) {
266 T_ZFCorePointerType::pointerOnDelete(dTmp->pointerValue);
267 }
268 zfdelete(dTmp);
269 }
270 return *this;
271 }
272 ZFCorePointerT<T_Pointer, T_ZFCorePointerType> &operator = (ZF_IN T_Pointer const &value) {
273 this->pointerValue(value);
274 return *this;
275 }
276 operator T_Pointer (void) const {
277 return d->pointerValue;
278 }
280
281public:
283 virtual void objectInfoOfContentT(ZF_IN_OUT zfstring &ret) const {
284 this->objectInfoOfContentT(ret, zfnull);
285 }
286
290 ) const {
291 if(infoGetter != zfnull) {
292 infoGetter(ret, d->pointerValue);
293 }
294 else {
295 zftToStringT(ret, d->pointerValue);
296 }
297 }
298
299public:
301 virtual const ZFCorePointer *refNew(void) const {
303 }
305 virtual inline const void *pointerValueAccess(void) const {
306 return d->pointerValue;
307 }
309 virtual inline void *pointerValueAccessNonConst(void) const {
310 return _ZFP_ZFCorePointerHelper<T_Pointer>::toNonConstRaw(d->pointerValue);
311 }
312
314 virtual zfindex objectRetainCount(void) const {
315 return d->refCount;
316 }
317
319 template<typename T_PointerDesired>
320 inline T_PointerDesired pointerValueT(void) const {
321 return _ZFP_ZFCorePointerHelper<T_PointerDesired>::toPointer(this);
322 }
324
325private:
326 _ZFP_ZFCorePointerPrivate<T_Pointer> *d;
327};
328ZFOUTPUT_TYPE_TEMPLATE(ZFM_EXPAND(typename T_Pointer, typename T_ZFCorePointerType), ZFM_EXPAND(ZFCorePointerT<T_Pointer, T_ZFCorePointerType>), {v.objectInfoT(s);})
329
330// ============================================================
347#define ZFCOREPOINTER_DECLARE(T_ZFCorePointer, pointerRetainAction, pointerDeleteAction) \
348 template<typename T_Pointer> \
349 zfclassNotPOD _ZFP_CPT_##T_ZFCorePointer { \
350 public: \
351 static inline void pointerOnRetain(T_Pointer const &p) \
352 pointerRetainAction \
353 static inline void pointerOnDelete(T_Pointer const &p) \
354 pointerDeleteAction \
355 }; \ \
357 template<typename T_Pointer> \
359 public: \ \
361 T_ZFCorePointer(void) \
363 { \
364 } \
365 T_ZFCorePointer(ZF_IN T_Pointer const &value) \
367 { \
368 } \
369 T_ZFCorePointer(ZF_IN zfnullT const &value) \
371 { \
372 } \
373 T_ZFCorePointer(ZF_IN ZFCorePointerT<T_Pointer, _ZFP_CPT_##T_ZFCorePointer<T_Pointer> > const &ref) \
375 { \
376 } \
377 T_ZFCorePointer<T_Pointer> &operator = (ZF_IN T_Pointer const &value) { \
379 return *this; \
380 } \
381 T_ZFCorePointer<T_Pointer> &operator = (ZF_IN ZFCorePointerT<T_Pointer, _ZFP_CPT_##T_ZFCorePointer<T_Pointer> > const &ref) { \
383 return *this; \
384 } \
385 template<typename T_PointerDesired> \
386 inline T_PointerDesired pointerValueT(void) const { \
387 return _ZFP_ZFCorePointerHelper<T_PointerDesired>::toPointer(this); \
388 } \
389 inline T_Pointer operator -> (void) const { \
390 return this->pointerValue(); \
391 } \
392 template<typename T_Ref> \
393 inline T_Ref operator * (void) const { \
394 return *(this->pointerValue()); \
395 } \
396 inline zfbool operator == (ZF_IN zfnullT const &p) const { \
397 return (this->pointerValue() == zfnull); \
398 } \
399 inline zfbool operator != (ZF_IN zfnullT const &p) const { \
400 return (this->pointerValue() != zfnull); \
401 } \
402 inline zfbool operator == (ZF_IN T_Pointer const &p) const { \
403 return (this->pointerValue() == p); \
404 } \
405 inline zfbool operator != (ZF_IN T_Pointer const &p) const { \
406 return (this->pointerValue() != p); \
407 } \
408 inline zfbool operator == (ZF_IN T_ZFCorePointer<T_Pointer> const &ref) const { \
409 return (this->pointerValue() == ref.pointerValue()); \
410 } \
411 inline zfbool operator != (ZF_IN T_ZFCorePointer<T_Pointer> const &ref) const { \
412 return (this->pointerValue() == ref.pointerValue()); \
413 } \ \
415 }; \
416 ZFOUTPUT_TYPE_TEMPLATE(typename T_Pointer, T_ZFCorePointer<T_Pointer>, {v.objectInfoT(s);})
417
422
424#endif // #ifndef _ZFI_ZFCorePointer_h_
425
#define ZFLIB_ZFCore
used to export symbols
Definition ZFCoreEnvDef.h:30
#define ZFCOREPOINTER_DECLARE(T_ZFCorePointer, pointerRetainAction, pointerDeleteAction)
declare your custom ZFCorePointerT type
Definition ZFCorePointer.h:347
types for ZFFramework
#define zffinal
dummy macro shows that a method or class is designed must not to be overrided
Definition ZFCoreTypeDef_ClassType.h:63
#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 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 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 ZFCLASS_DISALLOW_COPY_CONSTRUCTOR(ClassName)
a macro to show that a class doesn't allow copy constructor
Definition ZFCoreTypeDef_ClassType.h:81
#define zfnew(Type,...)
same as new defined for future use
Definition ZFCoreTypeDef_ClassType.h:89
#define zfnullT
type for zfnull, can be used for function overload
Definition ZFCoreTypeDef_CoreType.h:85
_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 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
#define ZFOUTPUT_TYPE(T_Type, outputAction)
declare your custom type conversion to string, convenient for debug
Definition ZFCoreTypeDef_OtherType.h:221
#define ZFOUTPUT_TYPE_TEMPLATE(T_typenameList, T_Type, outputAction)
see ZFOUTPUT_TYPE
Definition ZFCoreTypeDef_OtherType.h:262
#define ZFTOKEN_zfnull
string tokens
Definition ZFCoreTypeDef_OtherType.h:16
ZFCompareResult
compare result of two ZFObjects
Definition ZFCoreTypeDef_OtherType.h:28
@ ZFCompareUncomparable
Definition ZFCoreTypeDef_OtherType.h:29
@ ZFCompareEqual
Definition ZFCoreTypeDef_OtherType.h:31
void zftToStringT(zfstring &s, T_Type const &v)
util function to obtain object info
Definition ZFCoreTypeDef_OtherType.h:182
zft_zfstring< zfchar > zfstring
see zft_zfstring
Definition ZFCoreTypeDef_StringType.h:15
#define ZFM_EXPAND(...)
macro to expand a macro
Definition ZFCoreUtilMacro.h:148
#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
void(* InfoGetter)(zfstring &ret, T_Type const &v)
proto type for obtain object info, see zftToStringT
Definition ZFCoreTypeDef_OtherType.h:151
see ZFCorePointerT
Definition ZFCorePointer.h:420
see ZFCorePointerT
Definition ZFCorePointer.h:419
see ZFCorePointerT
Definition ZFCorePointer.h:418
see ZFCorePointerT
Definition ZFCorePointer.h:421
abstract base class for ZFCorePointerT
Definition ZFCorePointer.h:46
virtual ZFCompareResult objectCompare(const ZFCorePointer &another) const
compare by internal pointer value
Definition ZFCorePointer.h:87
virtual const ZFCorePointer * refNew(void) const =0
create a new reference to the smart pointer
virtual void objectInfoT(zfstring &ret) const
see objectInfo
Definition ZFCorePointer.h:56
T_PointerDesired pointerValueT(void) const
util method to get and cast to desired type
Definition ZFCorePointer.h:124
virtual void objectInfoOfContentT(zfstring &ret) const =0
see objectInfoOfContent
virtual const void * pointerValueAccess(void) const =0
get the internal pointer
virtual zfstring objectInfoOfContent(void) const
get content info or ZFTOKEN_zftToStringNotAvailable if not available
Definition ZFCorePointer.h:77
virtual zfstring objectInfo(void) const
return object info
Definition ZFCorePointer.h:65
virtual void * pointerValueAccessNonConst(void) const =0
get the internal pointer
virtual void refDelete(void) const
see refNew
Definition ZFCorePointer.h:108
virtual zfindex objectRetainCount(void) const =0
get current retain count
pointer container with reference count logic
Definition ZFCorePointer.h:181
virtual zfindex objectRetainCount(void) const
get current retain count
Definition ZFCorePointer.h:314
void pointerValue(T_Pointer const &value)
set the pointer value
Definition ZFCorePointer.h:186
virtual const ZFCorePointer * refNew(void) const
create a new reference to the smart pointer
Definition ZFCorePointer.h:301
virtual void objectInfoOfContentT(zfstring &ret) const
see objectInfoOfContent
Definition ZFCorePointer.h:283
T_Pointer const & pointerValue(void) const
get the pointer value
Definition ZFCorePointer.h:201
virtual void * pointerValueAccessNonConst(void) const
get the internal pointer
Definition ZFCorePointer.h:309
virtual void objectInfoOfContentT(zfstring &ret, typename ZFCoreInfoGetter< T_Pointer >::InfoGetter infoGetter) const
see objectInfoOfContent
Definition ZFCorePointer.h:287
virtual const void * pointerValueAccess(void) const
get the internal pointer
Definition ZFCorePointer.h:305
string format util