typelist.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //#define CXXOMFORT_NOTICES 2
  2. #include <cxxomfort/cxxo-utils.hpp>
  3. #include <cxxomfort/backports.hpp>
  4. #include <cxxomfort/typeindex.hpp>
  5. #include <cxxomfort/library/type_name.hpp>
  6. #include <cstddef>
  7. #include <iostream>
  8. #include <vector>
  9. #include <list>
  10. typedef cxxomfort::typelist<unsigned int, std::string, float, bool * >
  11. TypeList;
  12. enum Eenum { E1= 1, E2= 2, E3= 3};
  13. void test_ops();
  14. void test_compose();
  15. void test_derive();
  16. void test_satisfy();
  17. int main () {
  18. using namespace std;
  19. using cxxomfort::library::type_name;
  20. using cxxomfort::library::typeid_demangle;
  21. cxxomfort::output_info(stdout); cout<< endl;
  22. cout<< "TL "<< cxxomfort::type_name<TypeList>()<< " sizeof "<< sizeof(TypeList)<< endl;
  23. cout<< "size : "<< cxxomfort::typelist_size<TypeList>::value<< endl;
  24. cout<< "find float : "<< cxxomfort::typelist_find<float, TypeList>::value<< endl;
  25. cout<< "has float : "<< (cxxomfort::typelist_find<float, TypeList>::value < cxxomfort::typelist_size<TypeList>::value) << endl;
  26. cout<< "find double : "<< cxxomfort::typelist_find<double, TypeList>::value<< endl;
  27. cout<< "has double : "<< (cxxomfort::typelist_find<double, TypeList>::value < cxxomfort::typelist_size<TypeList>::value) << endl;
  28. cout<< "check for fundamental"<< endl;
  29. cout<< cxxomfort::typelist_count_if< std::is_fundamental, TypeList >::value << endl;
  30. cout<< "check for pointer"<< endl;
  31. cout<< cxxomfort::typelist_count_if< std::is_pointer, TypeList >::value << endl;
  32. #if 0
  33. // the next line should fail to compile
  34. typedef typename
  35. cxxomfort::typelist_get< cxxomfort::typelist_size<TypeList>::value , TypeList>::type
  36. Wrong;
  37. cout<< cxxomfort::type_name<Wrong>()<< endl;
  38. #endif
  39. test_ops();
  40. test_satisfy();
  41. test_compose();
  42. test_derive();
  43. }
  44. void test_ops () {
  45. using namespace std;
  46. using namespace cxxomfort;
  47. cout<< "--\nPush/Pop test:"<< endl;
  48. typedef typename typelist_push< int, TypeList >::type TL2;
  49. typedef typename typelist_pop< TL2 >::type TL3;
  50. static_assert( (std::is_same<TypeList,TL3>::value), "push-pop on typelist failed!");
  51. cout<< "TL2 "<< type_name<TL2>()<< endl;
  52. cout<< "TL3 "<< type_name<TL3>()<< endl;
  53. #if (CXXOMFORT_CXX_STD>=2011)
  54. typedef typename typelist_cut<1, TypeList>::type TC1;
  55. typedef typename typelist_cut<2, TypeList>::type TC2;
  56. cout<< "TC1 "<< type_name<TC1>()<< endl;
  57. cout<< "TC2 "<< type_name<TC2>()<< endl;
  58. #endif
  59. }
  60. void test_satisfy () {
  61. using namespace std;
  62. using namespace cxxomfort;
  63. cout<< "--\nSatisfy test:"<< endl;
  64. #if (1 || CXXOMFORT_CXX_STD >= 2011)
  65. typedef typelist< int, bool, unsigned long, float > TNumerics;
  66. typedef typelist< int, bool, char*, size_t, long double, nullptr_t > TNonNumerics;
  67. cout<< "TL Numerics "<< cxxomfort::type_name<TNumerics>()<< endl;
  68. cout<< "integers? \t"<< (typelist_all_if<TNumerics, std::is_integral>::value)<< endl;
  69. cout<< "arithmetic? \t"<< (typelist_all_if<TNumerics, std::is_arithmetic>::value)<< endl;
  70. cout<< "TL NonNumerics "<< cxxomfort::type_name<TNonNumerics>()<< endl;
  71. cout<< "integers? \t"<< (typelist_all_if<TNonNumerics, std::is_integral>::value)<< endl;
  72. cout<< "arithmetic? \t"<< (typelist_all_if<TNonNumerics, std::is_arithmetic>::value)<< endl;
  73. #else
  74. cout<< "Not implemented"<< endl;
  75. #endif
  76. }
  77. void test_compose () {
  78. using namespace std;
  79. using namespace cxxomfort;
  80. cout<< "--\nWrap/Compose test:"<< endl;
  81. typedef typelist<std::pair<Eenum, std::string_view>, std::string, cxxomfort::Exactly<float>, bool* > TL1;
  82. cout<< "TL1 "<< cxxomfort::type_name<TL1>()<< endl;
  83. typedef typelist_wrap< std::reference_wrapper , TL1>::type TW1;
  84. cout<< "TW1 "<< cxxomfort::type_name<TW1>()<< endl;
  85. typedef typelist_compose< std::decay , TL1>::type TC1;
  86. cout<< "TC1 "<< cxxomfort::type_name<TC1>()<< endl;
  87. }
  88. // derived test
  89. // types need to be nonlocal for C++<11
  90. typedef cxxomfort::typelist< std::string, std::wstring > TL1;
  91. typedef cxxomfort::typelist_derive< TL1 >::type Deriveds;
  92. struct TL_Multistring: Deriveds {};
  93. void test_derive () {
  94. using namespace std;
  95. using namespace cxxomfort;
  96. cout<< "--\nDerive test:"<< endl;
  97. #if (1 || CXXOMFORT_CXX_STD>=2011)
  98. cout<< "TL1 "<< cxxomfort::type_name<TL1>()<< endl;
  99. cout<< "Deriveds: "<< cxxomfort::type_name<Deriveds>()<< endl;
  100. cout<< "TL2 "<< cxxomfort::type_name<TL_Multistring>()<< endl;
  101. static_assert((is_base_of<std::wstring, TL_Multistring>::value), "Base");
  102. #else
  103. cout<< "Not implemented"<< endl;
  104. #endif
  105. }