CLHEP VERSION Reference Documentation
   
CLHEP Home Page     CLHEP Documentation     CLHEP Bug Reports

testCategories.cc
Go to the documentation of this file.
1// ======================================================================
2//
3// Test basic functionality of primary type traits
4//
5// Author: W. E. Brown, 2010-03-27, adapted in part from the boost
6// library's type_traits and related functionality whose internal
7// attributions bear the following various notices:
8//
9// (C) Copyright John Maddock 2000.
10// Distributed under the Boost Software License, Version 1.0.
11// See http://www.boost.org/LICENSE_1_0.txt
12//
13// ======================================================================
14
15
18
19#include <cassert>
20#include <iostream>
21
22
23using namespace CLHEP;
24
25
26// primary type category codes:
27unsigned _unknown = 0uL ;
28unsigned _void = 1uL << 0;
29unsigned _integral = 1uL << 1;
30unsigned _floating_point = 1uL << 2;
31unsigned _array = 1uL << 3;
32unsigned _pointer = 1uL << 4;
33unsigned _lvalue_reference = 1uL << 5;
34unsigned _rvalue_reference = 1uL << 6;
35unsigned _member_object_pointer = 1uL << 7;
36unsigned _member_function_pointer = 1uL << 8;
37unsigned _enum = 1uL << 9;
38unsigned _union = 1uL << 10; // Help, compiler!
39unsigned _class = 1uL << 11;
40unsigned _function = 1uL << 12;
41
42// composite type category codes:
43unsigned _reference = 1uL << 13;
44unsigned _arithmetic = 1uL << 14;
45unsigned _fundamental = 1uL << 15;
46unsigned _object = 1uL << 16;
47unsigned _scalar = 1uL << 17;
48unsigned _compound = 1uL << 18;
49unsigned _member_pointer = 1uL << 19;
50
51
52// evaluate categories:
53template< typename T >
54 unsigned
56{
57 unsigned ans = _unknown;
58
59 if( is_void <T>::value ) ans += _void;
60 if( is_integral <T>::value ) ans += _integral;
61 if( is_floating_point <T>::value ) ans += _floating_point;
62 if( is_array <T>::value ) ans += _array;
63 if( is_pointer <T>::value ) ans += _pointer;
64 if( is_lvalue_reference <T>::value ) ans += _lvalue_reference;
65 if( is_rvalue_reference <T>::value ) ans += _rvalue_reference;
66 if( is_member_object_pointer <T>::value ) ans += _member_object_pointer;
68 if( is_enum <T>::value ) ans += _enum;
69 if( is_union <T>::value ) ans += _union;
70 if( is_class <T>::value ) ans += _class;
71 if( is_function <T>::value ) ans += _function;;
72
73 if( is_reference <T>::value ) ans += _reference;
74 if( is_arithmetic <T>::value ) ans += _arithmetic;
75 if( is_fundamental <T>::value ) ans += _fundamental;
76 if( is_object <T>::value ) ans += _object;
77 if( is_scalar <T>::value ) ans += _scalar;
78 if( is_compound <T>::value ) ans += _compound;
79 if( is_member_pointer <T>::value ) ans += _member_pointer;
80
81 return ans;
82}
83
84
85unsigned cat_void
87unsigned cat_int
89unsigned cat_flt
91unsigned cat_arr
93unsigned cat_ptr
95unsigned cat_lref
97unsigned cat_rref
103unsigned cat_enum
105unsigned cat_union
107unsigned cat_class
109unsigned cat_fctn
111
112
113// define some test types:
114
115// class type:
116struct mytype
117{
118 int a;
119 int * p;
120 int f();
121}; // mytype
122
123
124// enum type:
125enum myenum { a=12, b=16 };
126
127
128// union type:
130{
131 int a;
132 int * p;
133 int f();
134};
135
137struct UDT
138{
139 UDT() { };
140 ~UDT() { };
141 UDT(const UDT&);
142 UDT& operator=(const UDT&);
143 int i;
144
145 void f1();
146 int f2();
147 int f3(int);
148 int f4(int, float);
149};
150
151typedef void(*f1)();
152typedef int(*f2)(int);
153typedef int(*f3)(int, bool);
154typedef void (UDT::*mf1)();
155typedef int (UDT::*mf2)();
156typedef int (UDT::*mf3)(int);
157typedef int (UDT::*mf4)(int, float);
158typedef int (UDT::*mp);
159typedef int (UDT::*cmf)(int) const;
160
161// cv-qualifiers applied to reference types should have no effect
162
163// This is intentional:
164// r_type and cr_type should be the same type
165// but some compilers wrongly apply cv-qualifiers
166// to reference types (this may generate a warning
167// on some compilers):
168
169typedef int& r_type;
170#if ! defined(_MSC_VER)
171typedef const r_type cr_type;
172#endif // _MSC_VER
173
174struct POD_UDT { int x; };
176{
178 empty_UDT(const empty_UDT&) { };
180 empty_UDT& operator=(const empty_UDT&){ return *this; }
181 bool operator==(const empty_UDT&)const
182 { return true; }
183};
185{
186 bool operator==(const empty_POD_UDT&)const
187 { return true; }
188};
190{
191 int x;
192 double y;
194};
196{
197 int x;
198 double y;
199};
205
207{
212 bool operator==(const nothrow_copy_UDT&)const
213 { return true; }
214};
215
225
235
236class Base { };
237
238class Derived : public Base { };
239class Derived2 : public Base { };
240class MultiBase : public Derived, public Derived2 { };
241class PrivateBase : private Base { };
242
243class NonDerived { };
244
247
250
251struct VB
252{ virtual ~VB() { }; };
253
254struct VD : VB
255{ ~VD() { }; };
256
257// struct non_pointer:
258// used to verify that is_pointer does not return
259// true for class types that implement operator void*()
260
262{ operator void*(){return this;} };
264{
265 int i;
266 operator int*(){return &i;}
267};
271{ operator int(); };
272
273// struct non_empty:
274// used to verify that is_empty does not emit
275// spurious warnings or errors.
276
277struct non_empty : private noncopyable
278{ int i; };
279
280// abstract base classes:
282{
284 virtual ~test_abc1();
287 virtual void foo() = 0;
288 virtual void foo2() = 0;
289};
290
292{
293 virtual ~test_abc2();
294 virtual void foo() = 0;
295 virtual void foo2() = 0;
296};
297
298struct test_abc3 : public test_abc1
299{ virtual void foo3() = 0; };
300
301struct incomplete_type;
302
304{
306 virtual void method();
307};
308
311
314
315struct virtual_inherit1 : virtual Base { };
317struct virtual_inherit3 : private virtual Base { };
318struct virtual_inherit4 : virtual noncopyable { };
320struct virtual_inherit6 : virtual Base { virtual ~virtual_inherit6()throw(); };
321
322typedef void foo0_t();
323typedef void foo1_t(int);
324typedef void foo2_t(int&, double);
325typedef void foo3_t(int&, bool, int, int);
326typedef void foo4_t(int, bool, int*, int[], int, int, int, int, int);
327
333
339
345
351
352template <class T>
353struct wrap
354{
355 T t;
356 int j;
357protected:
359 wrap(const wrap&);
361};
362
363
365{ operator char*() const; };
366
367
368typedef const double (UDT::*mp2) ;
369
370
371int
373{
374 // void types
375 assert(evaluate<void >() == cat_void);
376 assert(evaluate<void const >() == cat_void);
379
380 // integral types
381 assert(evaluate<bool >() == cat_int);
382 assert(evaluate<bool const >() == cat_int);
385 assert(evaluate<char >() == cat_int);
386 assert(evaluate<char const >() == cat_int);
389 typedef signed char schar;
390 assert(evaluate<schar >() == cat_int);
391 assert(evaluate<schar const >() == cat_int);
394 typedef unsigned char uchar;
395 assert(evaluate<uchar >() == cat_int);
396 assert(evaluate<uchar const >() == cat_int);
399 assert(evaluate<short >() == cat_int);
400 assert(evaluate<short const >() == cat_int);
403 typedef unsigned short ushort;
404 assert(evaluate<ushort >() == cat_int);
405 assert(evaluate<ushort const >() == cat_int);
408 assert(evaluate<int >() == cat_int);
409 assert(evaluate<int const >() == cat_int);
410 assert(evaluate<int volatile >() == cat_int);
412 typedef unsigned int uint;
413 assert(evaluate<uint >() == cat_int);
414 assert(evaluate<uint const >() == cat_int);
417 assert(evaluate<long >() == cat_int);
418 assert(evaluate<long const >() == cat_int);
421 typedef unsigned long ulong;
422 assert(evaluate<ulong >() == cat_int);
423 assert(evaluate<ulong const >() == cat_int);
426
427 // floating-point types
428 assert(evaluate<float >() == cat_flt);
429 assert(evaluate<float const >() == cat_flt);
432 assert(evaluate<double >() == cat_flt);
433 assert(evaluate<double const >() == cat_flt);
436 typedef long double ldouble;
437 assert(evaluate<ldouble >() == cat_flt);
441
442 // array types
443 assert(evaluate<double [1]>() == cat_arr);
444 assert(evaluate<char const [2]>() == cat_arr);
445 assert(evaluate<float volatile [3]>() == cat_arr);
446 assert(evaluate<long const volatile [4]>() == cat_arr);
447 assert(evaluate<mytype * [6]>() == cat_arr);
448 assert(evaluate<unsigned * [6]>() == cat_arr);
449 assert(evaluate<long double [7][7]>() == cat_arr);
450 assert(evaluate<UDT [2]>() == cat_arr);
451
452 // pointer types
453 assert(evaluate<char * >() == cat_ptr);
454 assert(evaluate<int * >() == cat_ptr);
455 assert(evaluate<int const * >() == cat_ptr);
457 assert(evaluate<int * * >() == cat_ptr);
463 assert(evaluate<int (*)(int) >() == cat_ptr);
464 assert(evaluate<non_pointer* >() == cat_ptr);
465 assert(evaluate<f1 >() == cat_ptr);
466 assert(evaluate<f2 >() == cat_ptr);
467 assert(evaluate<f3 >() == cat_ptr);
468
469 // lvalue reference types
470 assert(evaluate<int & >() == cat_lref);
471 assert(evaluate<int const & >() == cat_lref);
474 assert(evaluate<int * & >() == cat_lref);
475 assert(evaluate<int (&)(int) >() == cat_lref);
476 assert(evaluate<int (&)[2] >() == cat_lref);
477 assert(evaluate<r_type >() == cat_lref);
478 #if ! defined(_MSC_VER)
479 assert(evaluate<cr_type >() == cat_lref);
480 #endif // _MSC_VER
481 assert(evaluate<UDT & >() == cat_lref);
482 assert(evaluate<const UDT & >() == cat_lref);
485
486 // member object pointer types
490 assert(evaluate<mp >() == cat_mem_obj_ptr);
491 assert(evaluate<mp2 >() == cat_mem_obj_ptr);
492
493 // member function pointer types
494 assert(evaluate<int (mytype::*)() >() == cat_mbr_fctn_ptr);
495 assert(evaluate<int (mytype::*)()const >() == cat_mbr_fctn_ptr);
496 assert(evaluate<int (mytype::*)(int) >() == cat_mbr_fctn_ptr);
497 assert(evaluate<mf1 >() == cat_mbr_fctn_ptr);
498 assert(evaluate<mf2 >() == cat_mbr_fctn_ptr);
499 assert(evaluate<mf3 >() == cat_mbr_fctn_ptr);
500 assert(evaluate<mf4 >() == cat_mbr_fctn_ptr);
501 assert(evaluate<cmf >() == cat_mbr_fctn_ptr);
502
503 // enum types
504 assert(evaluate<myenum >() == cat_enum);
508 assert(evaluate<enum_UDT >() == cat_enum);
509
510 // union types
511 #if 0
512 assert(evaluate<myblend >() == cat_union);
516 assert(evaluate<union_UDT >() == cat_union);
524 #endif // 0
525
526 // class types
527 assert(evaluate<UDT >() == cat_class);
528 assert(evaluate<UDT const >() == cat_class);
531 assert(evaluate<empty_UDT >() == cat_class);
532 assert(evaluate<test_abc1 >() == cat_class);
535
536 // function types
537 assert(evaluate<int (int) >() == cat_fctn);
538 assert(evaluate<void (int,float, long) >() == cat_fctn);
539 assert(evaluate<foo0_t >() == cat_fctn);
540 assert(evaluate<foo1_t >() == cat_fctn);
541 assert(evaluate<foo2_t >() == cat_fctn);
542 assert(evaluate<foo3_t >() == cat_fctn);
543 assert(evaluate<foo4_t >() == cat_fctn);
544
545 return 0;
546}
#define double(obj)
void f1()
int f3(int)
int f4(int, float)
UDT(const UDT &)
UDT & operator=(const UDT &)
int f2()
virtual ~VB()
bool operator==(const empty_POD_UDT &) const
empty_UDT & operator=(const empty_UDT &)
bool operator==(const empty_UDT &) const
empty_UDT(const empty_UDT &)
int f()
nothrow_assign_UDT(const nothrow_assign_UDT &)
nothrow_assign_UDT & operator=(const nothrow_assign_UDT &)
bool operator==(const nothrow_assign_UDT &) const
bool operator==(const nothrow_construct_UDT &) const
nothrow_construct_UDT & operator=(const nothrow_construct_UDT &)
nothrow_copy_UDT & operator=(const nothrow_copy_UDT &)
bool operator==(const nothrow_copy_UDT &) const
nothrow_copy_UDT(const nothrow_copy_UDT &)
virtual ~polymorphic_base()
virtual void method()
virtual void method()
virtual void foo()=0
virtual ~test_abc1()
test_abc1 & operator=(const test_abc1 &)
test_abc1(const test_abc1 &)
virtual void foo2()=0
virtual void foo()=0
virtual void foo2()=0
virtual ~test_abc2()
virtual void foo3()=0
trivial_except_assign & operator=(trivial_except_assign const &)
trivial_except_copy(trivial_except_copy const &)
virtual ~virtual_inherit6()
wrap(const wrap &)
wrap & operator=(const wrap &)
unsigned _unknown
@ two_
@ one_
unsigned cat_ptr
unsigned _scalar
unsigned _rvalue_reference
unsigned _function
unsigned evaluate()
unsigned _pointer
unsigned _floating_point
unsigned cat_int
int(UDT::* mf2)()
unsigned cat_lref
int(UDT::* cmf)(int) const
int(UDT::* mf4)(int, float)
unsigned _member_pointer
unsigned _enum
int(* f2)(int)
int(* f3)(int, bool)
unsigned cat_union
void(* f1)()
const doubleUDT::* mp2
unsigned _member_object_pointer
unsigned cat_arr
unsigned _compound
void foo1_t(int)
unsigned _integral
@ three_
@ four_
intUDT::* mp
unsigned cat_enum
void foo0_t()
unsigned cat_fctn
unsigned _object
unsigned _class
void foo3_t(int &, bool, int, int)
unsigned _void
unsigned _member_function_pointer
unsigned cat_flt
unsigned cat_rref
unsigned _union
void foo2_t(int &, double)
void foo4_t(int, bool, int *, int[], int, int, int, int, int)
void(UDT::* mf1)()
unsigned cat_mbr_fctn_ptr
int & r_type
unsigned cat_mem_obj_ptr
unsigned _reference
int(UDT::* mf3)(int)
unsigned _lvalue_reference
unsigned cat_class
unsigned _arithmetic
unsigned _fundamental
int main()
unsigned _array
unsigned cat_void
const r_type cr_type
@ one
@ two
@ three
unsigned int uint
int f()