ZFFramework
 
Loading...
Searching...
No Matches
ZFCoreUtilTemplate.h
Go to the documentation of this file.
1
5
6#ifndef _ZFI_ZFCoreUtilTemplate_h_
7#define _ZFI_ZFCoreUtilTemplate_h_
8
9#include "ZFIdentityUtil.h"
10
12
13// ============================================================
14// zftValue
18template<typename T_Type>
20public:
22 T_Type zfv;
23public:
25 zftValue(void) : zfv() {}
26 zftValue(ZF_IN T_Type const &ref) : zfv(ref) {}
27 zftValue(ZF_IN zftValue<T_Type> const &ref) : zfv(ref.zfv) {}
28 template<typename T_Ref> zftValue(ZF_IN T_Ref const &ref) : zfv(ref) {}
29 inline zftValue<T_Type> &operator = (ZF_IN T_Type const &ref) {zfv = ref; return *this;}
30 inline zftValue<T_Type> &operator = (ZF_IN zftValue<T_Type> const &ref) {zfv = ref.zfv; return *this;}
31 template<typename T_Ref> inline zftValue<T_Type> &operator = (ZF_IN T_Ref const &ref) {zfv = ref; return *this;}
32 inline operator T_Type const & (void) const {return zfv;}
33 inline operator T_Type & (void) {return zfv;}
35};
36
37// ============================================================
49#define zftType(...) \
50 _ZFP_zftType<void(__VA_ARGS__)>::Type
51template<typename T>
52zfclassNotPOD _ZFP_zftType;
53template<typename T_Ret, typename T_Type>
54zfclassNotPOD _ZFP_zftType<T_Ret (T_Type)> {
55public:
56 typedef T_Type Type;
57};
58
59// ============================================================
60// zftTraits
73
74#define zftTraitsModifierName_N "N"
76#define zftTraitsModifierName_R "R"
78#define zftTraitsModifierName_CR "CR"
80#define zftTraitsModifierName_P "P"
82#define zftTraitsModifierName_PR "PR"
84#define zftTraitsModifierName_PCR "PCR"
86#define zftTraitsModifierName_CP "CP"
88#define zftTraitsModifierName_CPR "CPR"
90#define zftTraitsModifierName_CPCR "CPCR"
97template<typename T_Type>
99public:
100 enum {
101 TrIsPtr = 0,
104 };
105 typedef T_Type TrType;
106 typedef T_Type & TrRef;
107 typedef T_Type const & TrConstRef;
108 typedef T_Type * TrPtr;
109 typedef const T_Type * TrConstPtr;
110 typedef T_Type TrNoRef;
124 static const zfchar *ModifierName(void) {return zftTraitsModifierName_N;}
125};
126
127/*
128 * here is a list
129 * Type
130 * Type &
131 * Type const &
132 * Type *
133 * Type *&
134 * Type * const &
135 * const Type *
136 * const Type *&
137 * const Type * const &
138 *
139 * void
140 * void *
141 * const void *
142 * void *&
143 * void * const &
144 * const void *&
145 * const void * const &
146 */
147template<typename T_Type>
149public:
150 enum {
151 TrIsPtr = 0,
152 TrIsRef = 1,
153 TrModifier = (int)zftTraitsModifier_R,
154 };
155 typedef T_Type TrType;
156 typedef T_Type & TrRef;
157 typedef T_Type const & TrConstRef;
158 typedef T_Type * TrPtr;
159 typedef const T_Type * TrConstPtr;
160 typedef T_Type TrNoRef;
161 static const zfchar *ModifierName(void) {return zftTraitsModifierName_R;}
162};
163template<typename T_Type>
165public:
166 enum {
167 TrIsPtr = 0,
168 TrIsRef = 1,
169 TrModifier = (int)zftTraitsModifier_CR,
170 };
171 typedef T_Type TrType;
172 typedef T_Type & TrRef;
173 typedef T_Type const & TrConstRef;
174 typedef T_Type * TrPtr;
175 typedef const T_Type * TrConstPtr;
176 typedef T_Type TrNoRef;
177 static const zfchar *ModifierName(void) {return zftTraitsModifierName_CR;}
178};
179template<typename T_Type>
181public:
182 enum {
183 TrIsPtr = 1,
184 TrIsRef = 0,
185 TrModifier = (int)zftTraitsModifier_P,
186 };
187 typedef T_Type TrType;
188 typedef T_Type & TrRef;
189 typedef T_Type const & TrConstRef;
190 typedef T_Type * TrPtr;
191 typedef const T_Type * TrConstPtr;
192 typedef T_Type * TrNoRef;
193 static const zfchar *ModifierName(void) {return zftTraitsModifierName_P;}
194};
195template<typename T_Type>
197public:
198 enum {
199 TrIsPtr = 1,
200 TrIsRef = 1,
201 TrModifier = (int)zftTraitsModifier_PR,
202 };
203 typedef T_Type TrType;
204 typedef T_Type & TrRef;
205 typedef T_Type const & TrConstRef;
206 typedef T_Type * TrPtr;
207 typedef const T_Type * TrConstPtr;
208 typedef T_Type * TrNoRef;
209 static const zfchar *ModifierName(void) {return zftTraitsModifierName_PR;}
210};
211template<typename T_Type>
213public:
214 enum {
215 TrIsPtr = 1,
216 TrIsRef = 1,
217 TrModifier = (int)zftTraitsModifier_PCR,
218 };
219 typedef T_Type TrType;
220 typedef T_Type & TrRef;
221 typedef T_Type const & TrConstRef;
222 typedef T_Type * TrPtr;
223 typedef const T_Type * TrConstPtr;
224 typedef T_Type * TrNoRef;
225 static const zfchar *ModifierName(void) {return zftTraitsModifierName_PCR;}
226};
227template<typename T_Type>
229public:
230 enum {
231 TrIsPtr = 1,
232 TrIsRef = 0,
233 TrModifier = (int)zftTraitsModifier_CP,
234 };
235 typedef T_Type TrType;
236 typedef T_Type & TrRef;
237 typedef T_Type const & TrConstRef;
238 typedef T_Type * TrPtr;
239 typedef const T_Type * TrConstPtr;
240 typedef const T_Type * TrNoRef;
241 static const zfchar *ModifierName(void) {return zftTraitsModifierName_CP;}
242};
243template<typename T_Type>
245public:
246 enum {
247 TrIsPtr = 1,
248 TrIsRef = 1,
249 TrModifier = (int)zftTraitsModifier_CPR,
250 };
251 typedef T_Type TrType;
252 typedef T_Type & TrRef;
253 typedef T_Type const & TrConstRef;
254 typedef T_Type * TrPtr;
255 typedef const T_Type * TrConstPtr;
256 typedef const T_Type * TrNoRef;
257 static const zfchar *ModifierName(void) {return zftTraitsModifierName_CPR;}
258};
259template<typename T_Type>
261public:
262 enum {
263 TrIsPtr = 1,
264 TrIsRef = 1,
265 TrModifier = (int)zftTraitsModifier_CPCR,
266 };
267 typedef T_Type TrType;
268 typedef T_Type & TrRef;
269 typedef T_Type const & TrConstRef;
270 typedef T_Type * TrPtr;
271 typedef const T_Type * TrConstPtr;
272 typedef const T_Type * TrNoRef;
273 static const zfchar *ModifierName(void) {return zftTraitsModifierName_CPCR;}
274};
275
276template<>
278public:
279 enum {
280 TrIsPtr = 0,
281 TrIsRef = 0,
282 TrModifier = (int)zftTraitsModifier_N,
283 };
284 typedef void TrType;
285 typedef void TrRef;
286 typedef void TrConstRef;
287 typedef void * TrPtr;
288 typedef const void * TrConstPtr;
289 typedef void TrNoRef;
290 static const zfchar *ModifierName(void) {return zftTraitsModifierName_N;}
291};
292template<>
294public:
295 enum {
296 TrIsPtr = 0,
297 TrIsRef = 0,
298 TrModifier = (int)zftTraitsModifier_N,
299 };
300 typedef const void TrType;
301 typedef const void TrRef;
302 typedef const void TrConstRef;
303 typedef const void * TrPtr;
304 typedef const void * TrConstPtr;
305 typedef const void TrNoRef;
306 static const zfchar *ModifierName(void) {return zftTraitsModifierName_N;}
307};
308template<>
310public:
311 enum {
312 TrIsPtr = 1,
313 TrIsRef = 0,
314 TrModifier = (int)zftTraitsModifier_P,
315 };
316 typedef void TrType;
317 typedef void TrRef;
318 typedef void * const & TrConstRef;
319 typedef void * TrPtr;
320 typedef const void * TrConstPtr;
321 typedef void * TrNoRef;
322 static const zfchar *ModifierName(void) {return zftTraitsModifierName_P;}
323};
324template<>
326public:
327 enum {
328 TrIsPtr = 1,
329 TrIsRef = 0,
330 TrModifier = (int)zftTraitsModifier_CP,
331 };
332 typedef void TrType;
333 typedef void TrRef;
334 typedef const void * const & TrConstRef;
335 typedef void * TrPtr;
336 typedef const void * TrConstPtr;
337 typedef const void * TrNoRef;
338 static const zfchar *ModifierName(void) {return zftTraitsModifierName_CP;}
339};
340template<>
342public:
343 enum {
344 TrIsPtr = 1,
345 TrIsRef = 1,
346 TrModifier = (int)zftTraitsModifier_PR,
347 };
348 typedef void TrType;
349 typedef void TrRef;
350 typedef void * const & TrConstRef;
351 typedef void * TrPtr;
352 typedef const void * TrConstPtr;
353 typedef void * TrNoRef;
354 static const zfchar *ModifierName(void) {return zftTraitsModifierName_PR;}
355};
356template<>
358public:
359 enum {
360 TrIsPtr = 1,
361 TrIsRef = 1,
362 TrModifier = (int)zftTraitsModifier_PCR,
363 };
364 typedef void TrType;
365 typedef void TrRef;
366 typedef void * const & TrConstRef;
367 typedef void * TrPtr;
368 typedef const void * TrConstPtr;
369 typedef void * TrNoRef;
370 static const zfchar *ModifierName(void) {return zftTraitsModifierName_PCR;}
371};
372template<>
374public:
375 enum {
376 TrIsPtr = 1,
377 TrIsRef = 1,
378 TrModifier = (int)zftTraitsModifier_CPR,
379 };
380 typedef void TrType;
381 typedef void TrRef;
382 typedef void * const & TrConstRef;
383 typedef void * TrPtr;
384 typedef const void * TrConstPtr;
385 typedef const void * TrNoRef;
386 static const zfchar *ModifierName(void) {return zftTraitsModifierName_CPR;}
387};
388template<>
390public:
391 enum {
392 TrIsPtr = 1,
393 TrIsRef = 1,
394 TrModifier = (int)zftTraitsModifier_CPCR,
395 };
396 typedef void TrType;
397 typedef void TrRef;
398 typedef void * const & TrConstRef;
399 typedef void * TrPtr;
400 typedef const void * TrConstPtr;
401 typedef const void * TrNoRef;
402 static const zfchar *ModifierName(void) {return zftTraitsModifierName_CPCR;}
403};
405
406// ============================================================
408template<int cond = 0, typename T_Fix = void>
411
412template<>
414public:
416 typedef void Value;
417};
418
419// ============================================================
421template<typename T0, typename T1>
423public:
424 enum {
427 };
428};
429
430template<typename T>
432public:
433 enum {
436 };
437};
438
439// ============================================================
441template<typename T>
443private:
444 template<typename U>
445 static char _test(int U::*);
446 template<typename U>
447 static int _test(...);
448public:
449 enum {
451 Value = ((sizeof(_test<T>(0)) == sizeof(char)) ? 1 : 0)
452 };
453};
454
455// ============================================================
457template<typename TChild, typename TBase,
460private:
461 template<typename T_Dummy>
462 static char _test(TChild const &, T_Dummy);
463 static int _test(TBase const &, int);
464 zfclassNotPOD _Conv {
465 public:
466 operator TChild const & (void);
467 operator TBase const & (void) const;
468 };
469public:
470 enum {
472 Value = ((sizeof(_test(_Conv(), 0)) == sizeof(char)) ? 1 : 0)
473 };
474};
475
476template<typename TChild, typename TBase>
484
485template<typename T>
487public:
488 enum {
491 };
492};
493
494// ============================================================
496template<typename T_Type>
498public:
499 enum {
502 };
503};
504
506template<typename T_Type>
508public:
510 static zfidentity value(ZF_IN const T_Type &v) {
511 return zfidentityCalc(v);
512 }
513};
514
515template<typename T_Type>
516inline zfidentity zfhash(ZF_IN const T_Type &v) {
517 return zftHash<T_Type>::value(v);
518}
519
520#define ZFHASH_DECLARE(Type, action) \
521 \
522 template<> \
523 zfclassNotPOD zftHash<Type > { \
524 public: \
525 static zfidentity value(ZF_IN const Type &v) { \
526 action \
527 } \
528 }; \
529
530
531#define ZFHASH_DECLARE_TEMPLATE(T_Type, Type, action) \
532 \
533 template<T_Type > \
534 zfclassNotPOD zftHash<Type > { \
535 public: \
536 static zfidentity value(ZF_IN const Type &v) { \
537 action \
538 } \
539 }; \
540
541
545template<typename T_Element>
547 ZF_IN T_Element const &v0
548 , ZF_IN T_Element const &v1
549 ) {
550 return zfmemcmp(&v0, &v1, sizeof(T_Element));
551}
552
554#define ZFCORE_POD_DECLARE(Type) \
555 _ZFP_ZFCORE_POD_DECLARE(template<>, _ZFP_ZFCORE_POD_DECLARE_EXPAND(Type)) \
556 _ZFP_ZFCORE_POD_DECLARE_COMPARER(_ZFP_ZFCORE_POD_DECLARE_EMPTY(), _ZFP_ZFCORE_POD_DECLARE_EXPAND(Type))
557
558#define ZFCORE_POD_DECLARE_TEMPLATE(T_Type, Type) \
559 _ZFP_ZFCORE_POD_DECLARE(_ZFP_ZFCORE_POD_DECLARE_EXPAND(template< T_Type >),_ZFP_ZFCORE_POD_DECLARE_EXPAND(Type)) \
560 _ZFP_ZFCORE_POD_DECLARE_COMPARER(_ZFP_ZFCORE_POD_DECLARE_EXPAND(template< T_Type >), _ZFP_ZFCORE_POD_DECLARE_EXPAND(Type))
561
563#define ZFCORE_POD_DECLARE_NO_COMPARER(Type) \
564 _ZFP_ZFCORE_POD_DECLARE(template<>,_ZFP_ZFCORE_POD_DECLARE_EXPAND(Type))
565
566#define ZFCORE_POD_DECLARE_TEMPLATE_NO_COMPARER(T_Type, Type) \
567 _ZFP_ZFCORE_POD_DECLARE(_ZFP_ZFCORE_POD_DECLARE_EXPAND(template< T_Type >),_ZFP_ZFCORE_POD_DECLARE_EXPAND(Type))
568
569#define _ZFP_ZFCORE_POD_DECLARE_EMPTY(...)
570#define _ZFP_ZFCORE_POD_DECLARE_EXPAND(...) __VA_ARGS__
571#define _ZFP_ZFCORE_POD_DECLARE(classFix, Type) \
572 \
573 classFix \
574 zfclassNotPOD zftIsPOD<Type > { \
575 public: \
576 enum { \
577 Value = 1, \
578 }; \
579 }; \
580 classFix \
581 zfclassNotPOD zftHash<Type > { \
582 public: \
583 static zfidentity value(ZF_IN const Type &v) { \
584 return zfidentityCalc(v); \
585 } \
586 }; \
587
588#define _ZFP_ZFCORE_POD_DECLARE_COMPARER(funcFix, Type) \
589 \
590 funcFix inline zfbool operator == (ZF_IN const Type &v0, ZF_IN const Type &v1) {return zfcmpPOD(v0, v1) == 0;} \
591 funcFix inline zfbool operator != (ZF_IN const Type &v0, ZF_IN const Type &v1) {return zfcmpPOD(v0, v1) != 0;} \
592 funcFix inline zfbool operator < (ZF_IN const Type &v0, ZF_IN const Type &v1) {return zfcmpPOD(v0, v1) < 0;} \
593 funcFix inline zfbool operator <= (ZF_IN const Type &v0, ZF_IN const Type &v1) {return zfcmpPOD(v0, v1) <= 0;} \
594 funcFix inline zfbool operator > (ZF_IN const Type &v0, ZF_IN const Type &v1) {return zfcmpPOD(v0, v1) > 0;} \
595 funcFix inline zfbool operator >= (ZF_IN const Type &v0, ZF_IN const Type &v1) {return zfcmpPOD(v0, v1) >= 0;} \
596
597
599
600#endif // #ifndef _ZFI_ZFCoreUtilTemplate_h_
601
_ZFT_t_zfchar zfchar
char wrapper
Definition ZFCoreTypeDef_CharType.h:17
#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:180
zfint zfmemcmp(const void *p1, const void *p2, zfindex size)
wrapper to memcmp
Definition ZFCoreTypeDef_ClassType.h:144
#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_zfint zfint
same as int, see zfindex
Definition ZFCoreTypeDef_CoreType.h:165
_zft_zfidentity zfidentity
identity type, ensured at least 32 bit, ensured unsigned
Definition ZFCoreTypeDef_CoreType.h:225
#define zftTraitsModifierName_CPCR
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:90
#define zftTraitsModifierName_N
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:74
#define zftTraitsModifierName_PR
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:82
zftTraitsModifier
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:62
@ zftTraitsModifier_CP
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:69
@ zftTraitsModifier_CPR
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:70
@ zftTraitsModifier_R
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:64
@ zftTraitsModifier_N
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:63
@ zftTraitsModifier_P
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:66
@ zftTraitsModifier_PR
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:67
@ zftTraitsModifier_CPCR
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:71
@ zftTraitsModifier_PCR
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:68
@ zftTraitsModifier_CR
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:65
#define zftTraitsModifierName_CR
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:78
#define zftTraitsModifierName_CPR
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:88
#define zftTraitsModifierName_P
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:80
zfint zfcmpPOD(T_Element const &v0, T_Element const &v1)
util method to compare two POD type
Definition ZFCoreUtilTemplate.h:546
#define zftTraitsModifierName_R
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:76
#define zftTraitsModifierName_PCR
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:84
#define zftTraitsModifierName_CP
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:86
zfidentity zfhash(const T_Type &v)
std::hash wrapper
Definition ZFCoreUtilTemplate.h:516
identity generator utility
zfidentity zfidentityCalc(T_Type const &v)
calculate identity from POD object
Definition ZFIdentityUtil.h:92
#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 Value
std::enable_if wrapper
Definition ZFCoreUtilTemplate.h:416
std::enable_if wrapper
Definition ZFCoreUtilTemplate.h:409
std::hash wrapper
Definition ZFCoreUtilTemplate.h:507
static zfidentity value(const T_Type &v)
std::hash wrapper
Definition ZFCoreUtilTemplate.h:510
std::is_class wrapper
Definition ZFCoreUtilTemplate.h:442
@ Value
std::is_class wrapper
Definition ZFCoreUtilTemplate.h:451
std::is_pod wrapper
Definition ZFCoreUtilTemplate.h:497
@ Value
std::is_pod wrapper
Definition ZFCoreUtilTemplate.h:501
@ Value
std::is_same wrapper
Definition ZFCoreUtilTemplate.h:435
std::is_same wrapper
Definition ZFCoreUtilTemplate.h:422
@ Value
std::is_same wrapper
Definition ZFCoreUtilTemplate.h:426
@ Value
std::is_base_of wrapper
Definition ZFCoreUtilTemplate.h:490
@ Value
std::is_base_of wrapper
Definition ZFCoreUtilTemplate.h:481
std::is_base_of wrapper
Definition ZFCoreUtilTemplate.h:459
@ Value
std::is_base_of wrapper
Definition ZFCoreUtilTemplate.h:472
type traits similar to STL's traits
Definition ZFCoreUtilTemplate.h:98
T_Type TrNoRef
remove reference
Definition ZFCoreUtilTemplate.h:110
static const zfchar * ModifierName(void)
type modifier name
Definition ZFCoreUtilTemplate.h:124
const T_Type * TrConstPtr
const pointer type
Definition ZFCoreUtilTemplate.h:109
T_Type & TrRef
reference type
Definition ZFCoreUtilTemplate.h:106
T_Type TrType
normal type
Definition ZFCoreUtilTemplate.h:105
T_Type * TrPtr
pointer type
Definition ZFCoreUtilTemplate.h:108
@ TrModifier
true if the type is reference type
Definition ZFCoreUtilTemplate.h:103
@ TrIsRef
true if the type is pointer type
Definition ZFCoreUtilTemplate.h:102
T_Type const & TrConstRef
const reference type
Definition ZFCoreUtilTemplate.h:107
util wrapper class to hold a type
Definition ZFCoreUtilTemplate.h:19
T_Type zfv
the wrapped value
Definition ZFCoreUtilTemplate.h:22