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 
16 #include "CLHEP/Utility/noncopyable.h"
17 #include "CLHEP/Utility/type_traits.h"
18 
19 #include <cassert>
20 #include <iostream>
21 
22 
23 using namespace CLHEP;
24 
25 
26 // primary type category codes:
27 unsigned _unknown = 0uL ;
28 unsigned _void = 1uL << 0;
29 unsigned _integral = 1uL << 1;
30 unsigned _floating_point = 1uL << 2;
31 unsigned _array = 1uL << 3;
32 unsigned _pointer = 1uL << 4;
33 unsigned _lvalue_reference = 1uL << 5;
34 unsigned _rvalue_reference = 1uL << 6;
35 unsigned _member_object_pointer = 1uL << 7;
36 unsigned _member_function_pointer = 1uL << 8;
37 unsigned _enum = 1uL << 9;
38 unsigned _union = 1uL << 10; // Help, compiler!
39 unsigned _class = 1uL << 11;
40 unsigned _function = 1uL << 12;
41 
42 // composite type category codes:
43 unsigned _reference = 1uL << 13;
44 unsigned _arithmetic = 1uL << 14;
45 unsigned _fundamental = 1uL << 15;
46 unsigned _object = 1uL << 16;
47 unsigned _scalar = 1uL << 17;
48 unsigned _compound = 1uL << 18;
49 unsigned _member_pointer = 1uL << 19;
50 
51 
52 // evaluate categories:
53 template< 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;
62  if( is_array <T>::value ) ans += _array;
63  if( is_pointer <T>::value ) ans += _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 
76  if( is_object <T>::value ) ans += _object;
77  if( is_scalar <T>::value ) ans += _scalar;
78  if( is_compound <T>::value ) ans += _compound;
80 
81  return ans;
82 }
83 
84 
85 unsigned cat_void
87 unsigned cat_int
89 unsigned cat_flt
91 unsigned cat_arr
93 unsigned cat_ptr
95 unsigned cat_lref
97 unsigned cat_rref
99 unsigned cat_mem_obj_ptr
101 unsigned cat_mbr_fctn_ptr
103 unsigned cat_enum
105 unsigned cat_union
107 unsigned cat_class
109 unsigned cat_fctn
111 
112 
113 // define some test types:
114 
115 // class type:
116 struct mytype
117 {
118  int a;
119  int * p;
120  int f();
121 }; // mytype
122 
123 
124 // enum type:
125 enum myenum { a=12, b=16 };
126 
127 
128 // union type:
129 union myblend
130 {
131  int a;
132  int * p;
133  int f();
134 };
135 
136 enum enum_UDT{ one, two, three };
137 struct 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 
151 typedef void(*f1)();
152 typedef int(*f2)(int);
153 typedef int(*f3)(int, bool);
154 typedef void (UDT::*mf1)();
155 typedef int (UDT::*mf2)();
156 typedef int (UDT::*mf3)(int);
157 typedef int (UDT::*mf4)(int, float);
158 typedef int (UDT::*mp);
159 typedef 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 
169 typedef int& r_type;
170 #if ! defined(_MSC_VER)
171 typedef const r_type cr_type;
172 #endif // _MSC_VER
173 
174 struct POD_UDT { int x; };
175 struct empty_UDT
176 {
177  empty_UDT() { };
178  empty_UDT(const empty_UDT&) { };
179  ~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 };
201 {
203 };
205 
207 {
209  nothrow_copy_UDT(const nothrow_copy_UDT&)throw();
211  nothrow_copy_UDT& operator=(const nothrow_copy_UDT&){ return *this; }
212  bool operator==(const nothrow_copy_UDT&)const
213  { return true; }
214 };
215 
217 {
221  nothrow_assign_UDT& operator=(const nothrow_assign_UDT&)throw(){ return *this; }
222  bool operator==(const nothrow_assign_UDT&)const
223  { return true; }
224 };
225 
227 {
228  nothrow_construct_UDT()throw();
233  { return true; }
234 };
235 
236 class Base { };
237 
238 class Derived : public Base { };
239 class Derived2 : public Base { };
240 class MultiBase : public Derived, public Derived2 { };
241 class PrivateBase : private Base { };
242 
243 class NonDerived { };
244 
245 enum enum1
246 { one_,two_ };
247 
248 enum enum2
250 
251 struct VB
252 { virtual ~VB() { }; };
253 
254 struct 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 };
269 { int_constructible(int); };
271 { operator int(); };
272 
273 // struct non_empty:
274 // used to verify that is_empty does not emit
275 // spurious warnings or errors.
276 
277 struct non_empty : private noncopyable
278 { int i; };
279 
280 // abstract base classes:
281 struct test_abc1
282 {
283  test_abc1();
284  virtual ~test_abc1();
285  test_abc1(const test_abc1&);
286  test_abc1& operator=(const test_abc1&);
287  virtual void foo() = 0;
288  virtual void foo2() = 0;
289 };
290 
291 struct test_abc2
292 {
293  virtual ~test_abc2();
294  virtual void foo() = 0;
295  virtual void foo2() = 0;
296 };
297 
298 struct test_abc3 : public test_abc1
299 { virtual void foo3() = 0; };
300 
301 struct incomplete_type;
302 
304 {
305  virtual ~polymorphic_base();
306  virtual void method();
307 };
308 
310 { };
311 
313 { virtual void method(); };
314 
315 struct virtual_inherit1 : virtual Base { };
317 struct virtual_inherit3 : private virtual Base { };
318 struct virtual_inherit4 : virtual noncopyable { };
319 struct virtual_inherit5 : virtual int_convertible { };
320 struct virtual_inherit6 : virtual Base { virtual ~virtual_inherit6()throw(); };
321 
322 typedef void foo0_t();
323 typedef void foo1_t(int);
324 typedef void foo2_t(int&, double);
325 typedef void foo3_t(int&, bool, int, int);
326 typedef void foo4_t(int, bool, int*, int[], int, int, int, int, int);
327 
329 {
331  int i;
332 };
333 
335 {
337  int i;
338 };
339 
341 {
343  int i;
344 };
345 
347 {
348  trivial_except_assign& operator=(trivial_except_assign const&);
349  int i;
350 };
351 
352 template <class T>
353 struct wrap
354 {
355  T t;
356  int j;
357 protected:
358  wrap();
359  wrap(const wrap&);
360  wrap& operator=(const wrap&);
361 };
362 
363 
365 { operator char*() const; };
366 
367 
368 typedef const double (UDT::*mp2) ;
369 
370 
371 int
373 {
374  // void types
375  assert(evaluate<void >() == cat_void);
376  assert(evaluate<void const >() == cat_void);
377  assert(evaluate<void volatile >() == cat_void);
378  assert(evaluate<void const volatile >() == cat_void);
379 
380  // integral types
381  assert(evaluate<bool >() == cat_int);
382  assert(evaluate<bool const >() == cat_int);
383  assert(evaluate<bool volatile >() == cat_int);
384  assert(evaluate<bool const volatile >() == cat_int);
385  assert(evaluate<char >() == cat_int);
386  assert(evaluate<char const >() == cat_int);
387  assert(evaluate<char volatile >() == cat_int);
388  assert(evaluate<char const volatile >() == cat_int);
389  typedef signed char schar;
390  assert(evaluate<schar >() == cat_int);
391  assert(evaluate<schar const >() == cat_int);
392  assert(evaluate<schar volatile >() == cat_int);
393  assert(evaluate<schar const volatile >() == cat_int);
394  typedef unsigned char uchar;
395  assert(evaluate<uchar >() == cat_int);
396  assert(evaluate<uchar const >() == cat_int);
397  assert(evaluate<uchar volatile >() == cat_int);
398  assert(evaluate<uchar const volatile >() == cat_int);
399  assert(evaluate<short >() == cat_int);
400  assert(evaluate<short const >() == cat_int);
401  assert(evaluate<short volatile >() == cat_int);
402  assert(evaluate<short const volatile >() == cat_int);
403  typedef unsigned short ushort;
404  assert(evaluate<ushort >() == cat_int);
405  assert(evaluate<ushort const >() == cat_int);
406  assert(evaluate<ushort volatile >() == cat_int);
407  assert(evaluate<ushort const volatile >() == cat_int);
408  assert(evaluate<int >() == cat_int);
409  assert(evaluate<int const >() == cat_int);
410  assert(evaluate<int volatile >() == cat_int);
411  assert(evaluate<int const volatile >() == cat_int);
412  typedef unsigned int uint;
413  assert(evaluate<uint >() == cat_int);
414  assert(evaluate<uint const >() == cat_int);
415  assert(evaluate<uint volatile >() == cat_int);
416  assert(evaluate<uint const volatile >() == cat_int);
417  assert(evaluate<long >() == cat_int);
418  assert(evaluate<long const >() == cat_int);
419  assert(evaluate<long volatile >() == cat_int);
420  assert(evaluate<long const volatile >() == cat_int);
421  typedef unsigned long ulong;
422  assert(evaluate<ulong >() == cat_int);
423  assert(evaluate<ulong const >() == cat_int);
424  assert(evaluate<ulong volatile >() == cat_int);
425  assert(evaluate<ulong const volatile >() == cat_int);
426 
427  // floating-point types
428  assert(evaluate<float >() == cat_flt);
429  assert(evaluate<float const >() == cat_flt);
430  assert(evaluate<float volatile >() == cat_flt);
431  assert(evaluate<float const volatile >() == cat_flt);
432  assert(evaluate<double >() == cat_flt);
433  assert(evaluate<double const >() == cat_flt);
434  assert(evaluate<double volatile >() == cat_flt);
435  assert(evaluate<double const volatile >() == cat_flt);
436  typedef long double ldouble;
437  assert(evaluate<ldouble >() == cat_flt);
438  assert(evaluate<ldouble const >() == cat_flt);
439  assert(evaluate<ldouble volatile >() == cat_flt);
440  assert(evaluate<ldouble const volatile >() == 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);
456  assert(evaluate<int volatile * >() == cat_ptr);
457  assert(evaluate<int * * >() == cat_ptr);
458  assert(evaluate<int * * volatile >() == cat_ptr);
459  assert(evaluate<uint volatile * >() == cat_ptr);
460  assert(evaluate<long volatile * const >() == cat_ptr);
461  assert(evaluate<double * const * >() == cat_ptr);
462  assert(evaluate<float * const * const >() == 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);
472  assert(evaluate<int volatile & >() == cat_lref);
473  assert(evaluate<int const volatile & >() == 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);
483  assert(evaluate<volatile UDT & >() == cat_lref);
484  assert(evaluate<const volatile UDT & >() == cat_lref);
485 
486  // member object pointer types
487  assert(evaluate<int mytype::* >() == cat_mem_obj_ptr);
488  assert(evaluate<int mytype::* const >() == cat_mem_obj_ptr);
489  assert(evaluate<int mytype::* volatile >() == cat_mem_obj_ptr);
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);
505  assert(evaluate<myenum const >() == cat_enum);
506  assert(evaluate<myenum volatile >() == cat_enum);
507  assert(evaluate<myenum const volatile >() == cat_enum);
508  assert(evaluate<enum_UDT >() == cat_enum);
509 
510  // union types
511  #if 0
512  assert(evaluate<myblend >() == cat_union);
513  assert(evaluate<myblend const >() == cat_union);
514  assert(evaluate<myblend volatile >() == cat_union);
515  assert(evaluate<myblend const volatile >() == cat_union);
516  assert(evaluate<union_UDT >() == cat_union);
517  assert(evaluate<POD_union_UDT >() == cat_union);
518  assert(evaluate<empty_union_UDT >() == cat_union);
519  assert(evaluate<empty_POD_union_UDT >() == cat_union);
520  assert(evaluate<union_UDT const >() == cat_union);
521  assert(evaluate<POD_union_UDT volatile >() == cat_union);
522  assert(evaluate<empty_union_UDT const volatile>() == cat_union);
523  assert(evaluate<empty_POD_union_UDT const>() == cat_union);
524  #endif // 0
525 
526  // class types
527  assert(evaluate<UDT >() == cat_class);
528  assert(evaluate<UDT const >() == cat_class);
529  assert(evaluate<UDT volatile >() == cat_class);
530  assert(evaluate<UDT const volatile >() == cat_class);
531  assert(evaluate<empty_UDT >() == cat_class);
532  assert(evaluate<test_abc1 >() == cat_class);
533  assert(evaluate<test_abc1 const >() == cat_class);
534  assert(evaluate<std::iostream >() == 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 }
unsigned cat_mem_obj_ptr
unsigned cat_ptr
#define double(obj)
Definition: excDblThrow.cc:32
bool operator==(const nothrow_construct_UDT &) const
bool operator==(const nothrow_assign_UDT &) const
void foo1_t(int)
int(UDT::* mf2)()
nothrow_copy_UDT & operator=(const nothrow_copy_UDT &)
int(* f3)(int, bool)
empty_UDT(const empty_UDT &)
int(UDT::* mf3)(int)
unsigned _member_pointer
bool operator==(const nothrow_copy_UDT &) const
unsigned cat_arr
bool operator==(const empty_POD_UDT &) const
unsigned _member_function_pointer
const doubleUDT::* mp2
unsigned _floating_point
unsigned int uint
Definition: testDistCopy.cc:43
unsigned cat_fctn
unsigned cat_enum
unsigned _class
nothrow_construct_UDT & operator=(const nothrow_construct_UDT &)
unsigned _arithmetic
unsigned _object
unsigned cat_mbr_fctn_ptr
nothrow_assign_UDT & operator=(const nothrow_assign_UDT &)
unsigned _integral
unsigned _member_object_pointer
intUDT::* mp
int & r_type
unsigned _rvalue_reference
unsigned cat_union
unsigned cat_class
unsigned _scalar
unsigned _enum
empty_UDT & operator=(const empty_UDT &)
int(* f2)(int)
virtual ~VB()
unsigned _void
unsigned _lvalue_reference
unsigned _array
void(UDT::* mf1)()
void f(void g())
Definition: excDblThrow.cc:38
bool operator==(const empty_UDT &) const
unsigned _function
unsigned _reference
enum_UDT
unsigned _compound
myenum
unsigned _fundamental
void foo4_t(int, bool, int *, int[], int, int, int, int, int)
void foo0_t()
unsigned _union
unsigned cat_flt
void foo2_t(int &, double)
int(UDT::* cmf)(int) const
void foo3_t(int &, bool, int, int)
unsigned _pointer
unsigned _unknown
unsigned cat_lref
const r_type cr_type
int main()
Definition: testBug66214.cc:30
unsigned cat_void
void(* f1)()
unsigned evaluate()
int(UDT::* mf4)(int, float)
unsigned cat_int
unsigned cat_rref