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:
103 virtual const ZFCorePointer *refNew(void) const zfpurevirtual;
107 virtual void refDelete(void) const {
108 zfdelete(const_cast<ZFCorePointer *>(this));
109 }
110
113 virtual const void *pointerValueAccess(void) const zfpurevirtual;
117 virtual void *pointerValueAccessNonConst(void) const zfpurevirtual;
118
122 template<typename T_PointerDesired>
123 inline T_PointerDesired pointerValueT(void) const {
124 return _ZFP_ZFCorePointerHelper<T_PointerDesired>::toPointer(this);
125 }
126
131};
132ZFOUTPUT_TYPE(ZFCorePointer, {v.objectInfoT(s);})
133
134// ============================================================
135template<typename T_Pointer>
136zfclassNotPOD _ZFP_ZFCorePointerPrivate {
137public:
138 zfuint refCount;
139 T_Pointer pointerValue;
140public:
141 _ZFP_ZFCorePointerPrivate(void)
142 : refCount(1)
143 , pointerValue(zfnull)
144 {
145 }
146};
179template<typename T_Pointer, typename T_ZFCorePointerType>
181public:
185 inline void pointerValue(ZF_IN T_Pointer const &value) {
186 if(d->pointerValue != value) {
187 T_Pointer pointerValueOld = d->pointerValue;
188 d->pointerValue = value;
189 if(value) {
190 T_ZFCorePointerType::pointerOnRetain(d->pointerValue);
191 }
192 if(pointerValueOld != zfnull) {
193 T_ZFCorePointerType::pointerOnDelete(pointerValueOld);
194 }
195 }
196 }
197
200 inline T_Pointer const &pointerValue(void) const {
201 return d->pointerValue;
202 }
203
204public:
206 inline T_Pointer operator -> (void) const {
207 return d->pointerValue;
208 }
209 inline zfbool operator == (ZF_IN zfnullT const &p) const {
210 return (d->pointerValue == zfnull);
211 }
212 inline zfbool operator != (ZF_IN zfnullT const &p) const {
213 return (d->pointerValue != zfnull);
214 }
215 inline zfbool operator == (ZF_IN T_Pointer const &p) const {
216 return (d->pointerValue == p);
217 }
218 inline zfbool operator != (ZF_IN T_Pointer const &p) const {
219 return (d->pointerValue != p);
220 }
221 inline zfbool operator == (ZF_IN ZFCorePointerT<T_Pointer, T_ZFCorePointerType> const &ref) const {
222 return (d->pointerValue == ref.d->pointerValue);
223 }
224 inline zfbool operator != (ZF_IN ZFCorePointerT<T_Pointer, T_ZFCorePointerType> const &ref) const {
225 return (d->pointerValue == ref.d->pointerValue);
226 }
228
229public:
231 ZFCorePointerT(void)
232 : d(zfnew(_ZFP_ZFCorePointerPrivate<T_Pointer>))
233 {
234 }
235 ZFCorePointerT(ZF_IN zfnullT const &value)
236 : d(zfnew(_ZFP_ZFCorePointerPrivate<T_Pointer>))
237 {
238 }
239 ZFCorePointerT(ZF_IN T_Pointer const &value)
240 : d(zfnew(_ZFP_ZFCorePointerPrivate<T_Pointer>))
241 {
242 this->pointerValue(value);
243 }
245 : d(ref.d)
246 {
247 ++(d->refCount);
248 }
249 virtual ~ZFCorePointerT(void) {
250 --(d->refCount);
251 if(d->refCount == 0) {
252 if(d->pointerValue != zfnull) {
253 T_ZFCorePointerType::pointerOnDelete(d->pointerValue);
254 }
255 zfdelete(d);
256 }
257 }
259 _ZFP_ZFCorePointerPrivate<T_Pointer> *dTmp = d;
260 d = ref.d;
261 ++(ref.d->refCount);
262 --(dTmp->refCount);
263 if(dTmp->refCount == 0) {
264 if(dTmp->pointerValue != zfnull) {
265 T_ZFCorePointerType::pointerOnDelete(dTmp->pointerValue);
266 }
267 zfdelete(dTmp);
268 }
269 return *this;
270 }
271 ZFCorePointerT<T_Pointer, T_ZFCorePointerType> &operator = (ZF_IN T_Pointer const &value) {
272 this->pointerValue(value);
273 return *this;
274 }
275 operator T_Pointer (void) const {
276 return d->pointerValue;
277 }
279
280public:
282 virtual void objectInfoOfContentT(ZF_IN_OUT zfstring &ret) const {
283 this->objectInfoOfContentT(ret, zfnull);
284 }
285
289 ) const {
290 if(infoGetter != zfnull) {
291 infoGetter(ret, d->pointerValue);
292 }
293 else {
294 zftToStringT(ret, d->pointerValue);
295 }
296 }
297
298public:
300 virtual const ZFCorePointer *refNew(void) const {
301 return zfnew(ZFCorePointerT<T_Pointer ZFM_COMMA() T_ZFCorePointerType>, *this);
302 }
304 virtual inline const void *pointerValueAccess(void) const {
305 return d->pointerValue;
306 }
308 virtual inline void *pointerValueAccessNonConst(void) const {
309 return _ZFP_ZFCorePointerHelper<T_Pointer>::toNonConstRaw(d->pointerValue);
310 }
311
313 virtual zfindex objectRetainCount(void) const {
314 return d->refCount;
315 }
316
318 template<typename T_PointerDesired>
319 inline T_PointerDesired pointerValueT(void) const {
320 return _ZFP_ZFCorePointerHelper<T_PointerDesired>::toPointer(this);
321 }
323
324private:
325 _ZFP_ZFCorePointerPrivate<T_Pointer> *d;
326};
327ZFOUTPUT_TYPE_TEMPLATE(ZFM_EXPAND(typename T_Pointer, typename T_ZFCorePointerType), ZFM_EXPAND(ZFCorePointerT<T_Pointer, T_ZFCorePointerType>), {v.objectInfoT(s);})
328
329// ============================================================
346#define ZFCOREPOINTER_DECLARE(T_ZFCorePointer, pointerRetainAction, pointerDeleteAction) \
347 template<typename T_Pointer> \
348 zfclassNotPOD _ZFP_CPT_##T_ZFCorePointer { \
349 public: \
350 static inline void pointerOnRetain(T_Pointer const &p) \
351 pointerRetainAction \
352 static inline void pointerOnDelete(T_Pointer const &p) \
353 pointerDeleteAction \
354 }; \ \
356 template<typename T_Pointer> \
358 public: \ \
360 T_ZFCorePointer(void) \
362 { \
363 } \
364 T_ZFCorePointer(ZF_IN T_Pointer const &value) \
366 { \
367 } \
368 T_ZFCorePointer(ZF_IN zfnullT const &value) \
370 { \
371 } \
372 T_ZFCorePointer(ZF_IN ZFCorePointerT<T_Pointer, _ZFP_CPT_##T_ZFCorePointer<T_Pointer> > const &ref) \
374 { \
375 } \
376 T_ZFCorePointer<T_Pointer> &operator = (ZF_IN T_Pointer const &value) { \
378 return *this; \
379 } \
380 T_ZFCorePointer<T_Pointer> &operator = (ZF_IN ZFCorePointerT<T_Pointer, _ZFP_CPT_##T_ZFCorePointer<T_Pointer> > const &ref) { \
382 return *this; \
383 } \
384 template<typename T_PointerDesired> \
385 inline T_PointerDesired pointerValueT(void) const { \
386 return _ZFP_ZFCorePointerHelper<T_PointerDesired>::toPointer(this); \
387 } \
388 inline T_Pointer operator -> (void) const { \
389 return this->pointerValue(); \
390 } \
391 template<typename T_Ref> \
392 inline T_Ref operator * (void) const { \
393 return *(this->pointerValue()); \
394 } \
395 inline zfbool operator == (ZF_IN zfnullT const &p) const { \
396 return (this->pointerValue() == zfnull); \
397 } \
398 inline zfbool operator != (ZF_IN zfnullT const &p) const { \
399 return (this->pointerValue() != zfnull); \
400 } \
401 inline zfbool operator == (ZF_IN T_Pointer const &p) const { \
402 return (this->pointerValue() == p); \
403 } \
404 inline zfbool operator != (ZF_IN T_Pointer const &p) const { \
405 return (this->pointerValue() != p); \
406 } \
407 inline zfbool operator == (ZF_IN T_ZFCorePointer<T_Pointer> const &ref) const { \
408 return (this->pointerValue() == ref.pointerValue()); \
409 } \
410 inline zfbool operator != (ZF_IN T_ZFCorePointer<T_Pointer> const &ref) const { \
411 return (this->pointerValue() == ref.pointerValue()); \
412 } \ \
414 }; \
415 ZFOUTPUT_TYPE_TEMPLATE(typename T_Pointer, T_ZFCorePointer<T_Pointer>, {v.objectInfoT(s);})
416
421
423#endif // #ifndef _ZFI_ZFCorePointer_h_
424
#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:346
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: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: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
#define ZF_IN_OUT
dummy macro that shows the param used as required input and output
Definition ZFCoreTypeDef_ClassType.h:200
#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:107
#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 ZFM_COMMA()
macro to a comma
Definition ZFCoreUtilMacro.h:143
#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:419
see ZFCorePointerT
Definition ZFCorePointer.h:418
see ZFCorePointerT
Definition ZFCorePointer.h:417
see ZFCorePointerT
Definition ZFCorePointer.h:420
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:123
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:107
virtual zfindex objectRetainCount(void) const =0
get current retain count
pointer container with reference count logic
Definition ZFCorePointer.h:180
virtual zfindex objectRetainCount(void) const
get current retain count
Definition ZFCorePointer.h:313
void pointerValue(T_Pointer const &value)
set the pointer value
Definition ZFCorePointer.h:185
virtual const ZFCorePointer * refNew(void) const
create a new reference to the smart pointer
Definition ZFCorePointer.h:300
virtual void objectInfoOfContentT(zfstring &ret) const
see objectInfoOfContent
Definition ZFCorePointer.h:282
T_Pointer const & pointerValue(void) const
get the pointer value
Definition ZFCorePointer.h:200
virtual void * pointerValueAccessNonConst(void) const
get the internal pointer
Definition ZFCorePointer.h:308
virtual void objectInfoOfContentT(zfstring &ret, typename ZFCoreInfoGetter< T_Pointer >::InfoGetter infoGetter) const
see objectInfoOfContent
Definition ZFCorePointer.h:286
virtual const void * pointerValueAccess(void) const
get the internal pointer
Definition ZFCorePointer.h:304
string format util