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
20template<typename T_Type>
22public:
24 T_Type zfv;
25public:
27 zftValue(void) : zfv() {}
28 zftValue(ZF_IN T_Type const &ref) : zfv(ref) {}
29 zftValue(ZF_IN zftValue<T_Type> const &ref) : zfv(ref.zfv) {}
30 template<typename T_Ref> zftValue(ZF_IN T_Ref const &ref) : zfv(ref) {}
31 inline zftValue<T_Type> &operator = (ZF_IN T_Type const &ref) {zfv = ref; return *this;}
32 inline zftValue<T_Type> &operator = (ZF_IN zftValue<T_Type> const &ref) {zfv = ref.zfv; return *this;}
33 template<typename T_Ref> inline zftValue<T_Type> &operator = (ZF_IN T_Ref const &ref) {zfv = ref; return *this;}
34 inline operator T_Type const & (void) const {return zfv;}
35 inline operator T_Type & (void) {return zfv;}
37};
38
39// ============================================================
51#define zftType(...) \
52 _ZFP_zftType<void(__VA_ARGS__)>::Type
53template<typename T>
54zfclassNotPOD _ZFP_zftType;
55template<typename T_Ret, typename T_Type>
56zfclassNotPOD _ZFP_zftType<T_Ret (T_Type)> {
57public:
58 typedef T_Type Type;
59};
60
61// ============================================================
62// zftTraits
75
76#define zftTraitsModifierName_N "N"
78#define zftTraitsModifierName_R "R"
80#define zftTraitsModifierName_CR "CR"
82#define zftTraitsModifierName_P "P"
84#define zftTraitsModifierName_PR "PR"
86#define zftTraitsModifierName_PCR "PCR"
88#define zftTraitsModifierName_CP "CP"
90#define zftTraitsModifierName_CPR "CPR"
92#define zftTraitsModifierName_CPCR "CPCR"
99template<typename T_Type>
101public:
102 enum {
103 TrIsPtr = 0,
106 };
107 typedef T_Type TrType;
108 typedef T_Type & TrRef;
109 typedef T_Type const & TrConstRef;
110 typedef T_Type * TrPtr;
111 typedef const T_Type * TrConstPtr;
112 typedef T_Type TrNoRef;
126 static const zfchar *ModifierName(void) {return zftTraitsModifierName_N;}
127};
128
129/*
130 * here is a list
131 * Type
132 * Type &
133 * Type const &
134 * Type *
135 * Type *&
136 * Type * const &
137 * const Type *
138 * const Type *&
139 * const Type * const &
140 *
141 * void
142 * void *
143 * const void *
144 * void *&
145 * void * const &
146 * const void *&
147 * const void * const &
148 */
149template<typename T_Type>
151public:
152 enum {
153 TrIsPtr = 0,
154 TrIsRef = 1,
155 TrModifier = (int)zftTraitsModifier_R,
156 };
157 typedef T_Type TrType;
158 typedef T_Type & TrRef;
159 typedef T_Type const & TrConstRef;
160 typedef T_Type * TrPtr;
161 typedef const T_Type * TrConstPtr;
162 typedef T_Type TrNoRef;
163 static const zfchar *ModifierName(void) {return zftTraitsModifierName_R;}
164};
165template<typename T_Type>
167public:
168 enum {
169 TrIsPtr = 0,
170 TrIsRef = 1,
171 TrModifier = (int)zftTraitsModifier_CR,
172 };
173 typedef T_Type TrType;
174 typedef T_Type & TrRef;
175 typedef T_Type const & TrConstRef;
176 typedef T_Type * TrPtr;
177 typedef const T_Type * TrConstPtr;
178 typedef T_Type TrNoRef;
179 static const zfchar *ModifierName(void) {return zftTraitsModifierName_CR;}
180};
181template<typename T_Type>
183public:
184 enum {
185 TrIsPtr = 1,
186 TrIsRef = 0,
187 TrModifier = (int)zftTraitsModifier_P,
188 };
189 typedef T_Type TrType;
190 typedef T_Type & TrRef;
191 typedef T_Type const & TrConstRef;
192 typedef T_Type * TrPtr;
193 typedef const T_Type * TrConstPtr;
194 typedef T_Type * TrNoRef;
195 static const zfchar *ModifierName(void) {return zftTraitsModifierName_P;}
196};
197template<typename T_Type>
199public:
200 enum {
201 TrIsPtr = 1,
202 TrIsRef = 1,
203 TrModifier = (int)zftTraitsModifier_PR,
204 };
205 typedef T_Type TrType;
206 typedef T_Type & TrRef;
207 typedef T_Type const & TrConstRef;
208 typedef T_Type * TrPtr;
209 typedef const T_Type * TrConstPtr;
210 typedef T_Type * TrNoRef;
211 static const zfchar *ModifierName(void) {return zftTraitsModifierName_PR;}
212};
213template<typename T_Type>
215public:
216 enum {
217 TrIsPtr = 1,
218 TrIsRef = 1,
219 TrModifier = (int)zftTraitsModifier_PCR,
220 };
221 typedef T_Type TrType;
222 typedef T_Type & TrRef;
223 typedef T_Type const & TrConstRef;
224 typedef T_Type * TrPtr;
225 typedef const T_Type * TrConstPtr;
226 typedef T_Type * TrNoRef;
227 static const zfchar *ModifierName(void) {return zftTraitsModifierName_PCR;}
228};
229template<typename T_Type>
231public:
232 enum {
233 TrIsPtr = 1,
234 TrIsRef = 0,
235 TrModifier = (int)zftTraitsModifier_CP,
236 };
237 typedef T_Type TrType;
238 typedef T_Type & TrRef;
239 typedef T_Type const & TrConstRef;
240 typedef T_Type * TrPtr;
241 typedef const T_Type * TrConstPtr;
242 typedef const T_Type * TrNoRef;
243 static const zfchar *ModifierName(void) {return zftTraitsModifierName_CP;}
244};
245template<typename T_Type>
247public:
248 enum {
249 TrIsPtr = 1,
250 TrIsRef = 1,
251 TrModifier = (int)zftTraitsModifier_CPR,
252 };
253 typedef T_Type TrType;
254 typedef T_Type & TrRef;
255 typedef T_Type const & TrConstRef;
256 typedef T_Type * TrPtr;
257 typedef const T_Type * TrConstPtr;
258 typedef const T_Type * TrNoRef;
259 static const zfchar *ModifierName(void) {return zftTraitsModifierName_CPR;}
260};
261template<typename T_Type>
263public:
264 enum {
265 TrIsPtr = 1,
266 TrIsRef = 1,
267 TrModifier = (int)zftTraitsModifier_CPCR,
268 };
269 typedef T_Type TrType;
270 typedef T_Type & TrRef;
271 typedef T_Type const & TrConstRef;
272 typedef T_Type * TrPtr;
273 typedef const T_Type * TrConstPtr;
274 typedef const T_Type * TrNoRef;
275 static const zfchar *ModifierName(void) {return zftTraitsModifierName_CPCR;}
276};
277
278template<>
280public:
281 enum {
282 TrIsPtr = 0,
283 TrIsRef = 0,
284 TrModifier = (int)zftTraitsModifier_N,
285 };
286 typedef void TrType;
287 typedef void TrRef;
288 typedef void TrConstRef;
289 typedef void * TrPtr;
290 typedef const void * TrConstPtr;
291 typedef void TrNoRef;
292 static const zfchar *ModifierName(void) {return zftTraitsModifierName_N;}
293};
294template<>
296public:
297 enum {
298 TrIsPtr = 0,
299 TrIsRef = 0,
300 TrModifier = (int)zftTraitsModifier_N,
301 };
302 typedef const void TrType;
303 typedef const void TrRef;
304 typedef const void TrConstRef;
305 typedef const void * TrPtr;
306 typedef const void * TrConstPtr;
307 typedef const void TrNoRef;
308 static const zfchar *ModifierName(void) {return zftTraitsModifierName_N;}
309};
310template<>
312public:
313 enum {
314 TrIsPtr = 1,
315 TrIsRef = 0,
316 TrModifier = (int)zftTraitsModifier_P,
317 };
318 typedef void TrType;
319 typedef void TrRef;
320 typedef void * const & TrConstRef;
321 typedef void * TrPtr;
322 typedef const void * TrConstPtr;
323 typedef void * TrNoRef;
324 static const zfchar *ModifierName(void) {return zftTraitsModifierName_P;}
325};
326template<>
328public:
329 enum {
330 TrIsPtr = 1,
331 TrIsRef = 0,
332 TrModifier = (int)zftTraitsModifier_CP,
333 };
334 typedef void TrType;
335 typedef void TrRef;
336 typedef const void * const & TrConstRef;
337 typedef void * TrPtr;
338 typedef const void * TrConstPtr;
339 typedef const void * TrNoRef;
340 static const zfchar *ModifierName(void) {return zftTraitsModifierName_CP;}
341};
342template<>
344public:
345 enum {
346 TrIsPtr = 1,
347 TrIsRef = 1,
348 TrModifier = (int)zftTraitsModifier_PR,
349 };
350 typedef void TrType;
351 typedef void TrRef;
352 typedef void * const & TrConstRef;
353 typedef void * TrPtr;
354 typedef const void * TrConstPtr;
355 typedef void * TrNoRef;
356 static const zfchar *ModifierName(void) {return zftTraitsModifierName_PR;}
357};
358template<>
360public:
361 enum {
362 TrIsPtr = 1,
363 TrIsRef = 1,
364 TrModifier = (int)zftTraitsModifier_PCR,
365 };
366 typedef void TrType;
367 typedef void TrRef;
368 typedef void * const & TrConstRef;
369 typedef void * TrPtr;
370 typedef const void * TrConstPtr;
371 typedef void * TrNoRef;
372 static const zfchar *ModifierName(void) {return zftTraitsModifierName_PCR;}
373};
374template<>
376public:
377 enum {
378 TrIsPtr = 1,
379 TrIsRef = 1,
380 TrModifier = (int)zftTraitsModifier_CPR,
381 };
382 typedef void TrType;
383 typedef void TrRef;
384 typedef void * const & TrConstRef;
385 typedef void * TrPtr;
386 typedef const void * TrConstPtr;
387 typedef const void * TrNoRef;
388 static const zfchar *ModifierName(void) {return zftTraitsModifierName_CPR;}
389};
390template<>
392public:
393 enum {
394 TrIsPtr = 1,
395 TrIsRef = 1,
396 TrModifier = (int)zftTraitsModifier_CPCR,
397 };
398 typedef void TrType;
399 typedef void TrRef;
400 typedef void * const & TrConstRef;
401 typedef void * TrPtr;
402 typedef const void * TrConstPtr;
403 typedef const void * TrNoRef;
404 static const zfchar *ModifierName(void) {return zftTraitsModifierName_CPCR;}
405};
407
408// ============================================================
410template<int cond = 0, typename T_Fix = void>
413
414template<>
416public:
418 typedef void Value;
419};
420
421// ============================================================
423template<typename T0, typename T1>
425public:
426 enum {
429 };
430};
431
432template<typename T>
434public:
435 enum {
438 };
439};
440
441// ============================================================
443template<typename T>
445private:
446 template<typename U>
447 static char _test(int U::*);
448 template<typename U>
449 static int _test(...);
450public:
451 enum {
453 Value = ((sizeof(_test<T>(0)) == sizeof(char)) ? 1 : 0)
454 };
455};
456
457// ============================================================
459template<typename TChild, typename TBase,
462private:
463 template<typename T_Dummy>
464 static char _test(TChild const &, T_Dummy);
465 static int _test(TBase const &, int);
466 zfclassNotPOD _Conv {
467 public:
468 operator TChild const & (void);
469 operator TBase const & (void) const;
470 };
471public:
472 enum {
474 Value = ((sizeof(_test(_Conv(), 0)) == sizeof(char)) ? 1 : 0)
475 };
476};
477
478template<typename TChild, typename TBase>
486
487template<typename T>
489public:
490 enum {
493 };
494};
495
496// ============================================================
498template<typename T_Type>
500public:
501 enum {
504 };
505};
506
508template<typename T_Type>
510public:
512 static zfidentity value(ZF_IN const T_Type &v) {
513 return zfidentityCalc(v);
514 }
515};
516
517template<typename T_Type>
518inline zfidentity zfhash(ZF_IN const T_Type &v) {
519 return zftHash<T_Type>::value(v);
520}
521
522#define ZFHASH_DECLARE(Type, action) \
523 \
524 template<> \
525 zfclassNotPOD zftHash<Type > { \
526 public: \
527 static zfidentity value(ZF_IN const Type &v) { \
528 action \
529 } \
530 }; \
531
532
533#define ZFHASH_DECLARE_TEMPLATE(T_Type, Type, action) \
534 \
535 template<T_Type > \
536 zfclassNotPOD zftHash<Type > { \
537 public: \
538 static zfidentity value(ZF_IN const Type &v) { \
539 action \
540 } \
541 }; \
542
543
547template<typename T_Element>
549 ZF_IN T_Element const &v0
550 , ZF_IN T_Element const &v1
551 ) {
552 return zfmemcmp(&v0, &v1, sizeof(T_Element));
553}
554
556#define ZFCORE_POD_DECLARE(Type) \
557 _ZFP_ZFCORE_POD_DECLARE(template<>, _ZFP_ZFCORE_POD_DECLARE_EXPAND(Type)) \
558 _ZFP_ZFCORE_POD_DECLARE_COMPARER(_ZFP_ZFCORE_POD_DECLARE_EMPTY(), _ZFP_ZFCORE_POD_DECLARE_EXPAND(Type))
559
560#define ZFCORE_POD_DECLARE_TEMPLATE(T_Type, Type) \
561 _ZFP_ZFCORE_POD_DECLARE(_ZFP_ZFCORE_POD_DECLARE_EXPAND(template< T_Type >),_ZFP_ZFCORE_POD_DECLARE_EXPAND(Type)) \
562 _ZFP_ZFCORE_POD_DECLARE_COMPARER(_ZFP_ZFCORE_POD_DECLARE_EXPAND(template< T_Type >), _ZFP_ZFCORE_POD_DECLARE_EXPAND(Type))
563
565#define ZFCORE_POD_DECLARE_NO_COMPARER(Type) \
566 _ZFP_ZFCORE_POD_DECLARE(template<>,_ZFP_ZFCORE_POD_DECLARE_EXPAND(Type))
567
568#define ZFCORE_POD_DECLARE_TEMPLATE_NO_COMPARER(T_Type, Type) \
569 _ZFP_ZFCORE_POD_DECLARE(_ZFP_ZFCORE_POD_DECLARE_EXPAND(template< T_Type >),_ZFP_ZFCORE_POD_DECLARE_EXPAND(Type))
570
571#define _ZFP_ZFCORE_POD_DECLARE_EMPTY(...)
572#define _ZFP_ZFCORE_POD_DECLARE_EXPAND(...) __VA_ARGS__
573#define _ZFP_ZFCORE_POD_DECLARE(classFix, Type) \
574 \
575 classFix \
576 zfclassNotPOD zftIsPOD<Type > { \
577 public: \
578 enum { \
579 Value = 1, \
580 }; \
581 }; \
582 classFix \
583 zfclassNotPOD zftHash<Type > { \
584 public: \
585 static zfidentity value(ZF_IN const Type &v) { \
586 return zfidentityCalc(v); \
587 } \
588 }; \
589
590#define _ZFP_ZFCORE_POD_DECLARE_COMPARER(funcFix, Type) \
591 \
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 funcFix inline zfbool operator > (ZF_IN const Type &v0, ZF_IN const Type &v1) {return zfcmpPOD(v0, v1) > 0;} \
597 funcFix inline zfbool operator >= (ZF_IN const Type &v0, ZF_IN const Type &v1) {return zfcmpPOD(v0, v1) >= 0;} \
598
599
601
602#endif // #ifndef _ZFI_ZFCoreUtilTemplate_h_
603
_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:92
#define zftTraitsModifierName_N
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:76
#define zftTraitsModifierName_PR
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:84
zftTraitsModifier
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:64
@ zftTraitsModifier_CP
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:71
@ zftTraitsModifier_CPR
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:72
@ zftTraitsModifier_R
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:66
@ zftTraitsModifier_N
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:65
@ zftTraitsModifier_P
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:68
@ zftTraitsModifier_PR
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:69
@ zftTraitsModifier_CPCR
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:73
@ zftTraitsModifier_PCR
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:70
@ zftTraitsModifier_CR
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:67
#define zftTraitsModifierName_CR
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:80
#define zftTraitsModifierName_CPR
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:90
#define zftTraitsModifierName_P
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:82
zfint zfcmpPOD(T_Element const &v0, T_Element const &v1)
util method to compare two POD type
Definition ZFCoreUtilTemplate.h:548
#define zftTraitsModifierName_R
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:78
#define zftTraitsModifierName_PCR
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:86
#define zftTraitsModifierName_CP
see zftTraits::ModifierName
Definition ZFCoreUtilTemplate.h:88
zfidentity zfhash(const T_Type &v)
std::hash wrapper
Definition ZFCoreUtilTemplate.h:518
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:418
std::enable_if wrapper
Definition ZFCoreUtilTemplate.h:411
std::hash wrapper
Definition ZFCoreUtilTemplate.h:509
static zfidentity value(const T_Type &v)
std::hash wrapper
Definition ZFCoreUtilTemplate.h:512
std::is_class wrapper
Definition ZFCoreUtilTemplate.h:444
@ Value
std::is_class wrapper
Definition ZFCoreUtilTemplate.h:453
std::is_pod wrapper
Definition ZFCoreUtilTemplate.h:499
@ Value
std::is_pod wrapper
Definition ZFCoreUtilTemplate.h:503
@ Value
std::is_same wrapper
Definition ZFCoreUtilTemplate.h:437
std::is_same wrapper
Definition ZFCoreUtilTemplate.h:424
@ Value
std::is_same wrapper
Definition ZFCoreUtilTemplate.h:428
@ Value
std::is_base_of wrapper
Definition ZFCoreUtilTemplate.h:492
@ Value
std::is_base_of wrapper
Definition ZFCoreUtilTemplate.h:483
std::is_base_of wrapper
Definition ZFCoreUtilTemplate.h:461
@ Value
std::is_base_of wrapper
Definition ZFCoreUtilTemplate.h:474
type traits similar to STL's traits
Definition ZFCoreUtilTemplate.h:100
T_Type TrNoRef
remove reference
Definition ZFCoreUtilTemplate.h:112
static const zfchar * ModifierName(void)
type modifier name
Definition ZFCoreUtilTemplate.h:126
const T_Type * TrConstPtr
const pointer type
Definition ZFCoreUtilTemplate.h:111
T_Type & TrRef
reference type
Definition ZFCoreUtilTemplate.h:108
T_Type TrType
normal type
Definition ZFCoreUtilTemplate.h:107
T_Type * TrPtr
pointer type
Definition ZFCoreUtilTemplate.h:110
@ TrModifier
true if the type is reference type
Definition ZFCoreUtilTemplate.h:105
@ TrIsRef
true if the type is pointer type
Definition ZFCoreUtilTemplate.h:104
T_Type const & TrConstRef
const reference type
Definition ZFCoreUtilTemplate.h:109
util wrapper class to hold a type
Definition ZFCoreUtilTemplate.h:21
T_Type zfv
the wrapped value
Definition ZFCoreUtilTemplate.h:24