tuple_get.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Simple test file for cxxomfort's
  3. * "get<type> from std::tuple" feature extension.
  4. *
  5. * Interfaces tested in this example:
  6. *
  7. * * std::tuple
  8. * * std::get<type>(std::tuple<...>)
  9. * * cxxomfort::type_name
  10. * * static_assert
  11. *
  12. * A tuple type is selected, under the name 'TupleType'.
  13. * Invariant about the tuple (its status and types) are tested via static_assert.
  14. * After that, a variable of that tuple type is created, under the name 'tu'.
  15. * The get<index> and get<type> functions are used to obtain the member
  16. * components of this tuple, demonstrating the correctness of the latter.
  17. *
  18. * This and other examples assume cxxomfort is installed in
  19. * include library path; if otherwise, compile with
  20. * -I /path/to/cxxomfort .
  21. */
  22. #include <cxxomfort/base.hpp>
  23. #include <cxxomfort/tuple.hpp>
  24. #include <cxxomfort/library/tuple.hpp> // is_tuple
  25. #include <cxxomfort/library/utility.hpp>
  26. //#include <cxxomfort/library/type_traits.hpp>
  27. #include <cxxomfort/library/type_name.hpp>
  28. #include <cxxomfort/array.hpp>
  29. #include <cxxomfort/ostream.hpp>
  30. #include <cassert>
  31. #include <utility>
  32. #include <string>
  33. #include <iostream>
  34. #if (!defined(__cpp_lib_tuples_by_type))
  35. #error "std::get<T> for std::tuple not detected or implemented"
  36. #endif
  37. // A tuple type is selected, under the name 'TupleType'.
  38. typedef std::tuple<long int, std::pair<std::string, std::string>, cxxomfort::info, bool*, float >
  39. TupleType;
  40. size_t const TS = std::tuple_size<TupleType>::value;
  41. namespace cxlt= cxxomfort::library::tuple;
  42. //namespace cxltt = cxxomfort::library::type_traits;
  43. int main () {
  44. using namespace std;
  45. cxxomfort::output_info(); cout<< endl;
  46. using cxxomfort::library::utility::is_std_pair;
  47. cout<< "Implementación de tuple get<type>: ";
  48. switch (CXXOMFORT_IMPLEMENTS_tuple_get_type) {
  49. case CXXO_IMPLSTATUS_NATIVE():
  50. cout<< "c++ native"; break;
  51. case CXXO_IMPLSTATUS_BACKPORT():
  52. cout<< "backported"; break;
  53. case CXXO_IMPLSTATUS_EMULATION():
  54. cout<< "emulated"; break;
  55. default: {
  56. cout<< "unknown?";
  57. break;
  58. }
  59. }
  60. cout<< endl;
  61. // Invariant about the tuple (its status and types) are tested via static_assert.
  62. static_assert (cxlt::is_tuple<TupleType>::value, "TupleType declaration must be a std::tuple<...>");
  63. static_assert (tuple_size<TupleType>::value > 2, "TupleType must have at least three (3) elements");
  64. cout<< "TupleType == "<< cxxomfort::type_name<TupleType>()<< endl;
  65. // After that, a variable of that tuple type is created, under the name 'tu'.
  66. TupleType tu = make_tuple(-1, make_pair("abc", "def"), cxxomfort::_, nullptr, 0);
  67. enum { e0assert = is_same< tuple_element<0,TupleType>::type, long int>::value };
  68. enum { e1assert = is_pointer< tuple_element<3,TupleType>::type >::value };
  69. static_assert (e0assert, "TupleType's first element (index <0>) must be 'long int'");
  70. static_assert (e1assert, "TupleType's fourth element (index <3>) must be a pointer type");
  71. // we get the type of the second element (index <1> )
  72. typedef tuple_element<0,TupleType>::type Tu0; // should be pair<string,string> here
  73. cout<< "Internal type with index<0> == "<< cxxomfort::type_name<Tu0>()<< endl;
  74. typedef tuple_element<1,TupleType>::type Tu1; // should be pair<string,string> here
  75. cout<< "Internal type with index<1> == "<< cxxomfort::type_name<Tu1>()<< endl;
  76. static_assert (is_std_pair<Tu1>::value, "TupleType's second element (index <1>) must be a std::pair<...>");
  77. // We check how many times the type appears in the tuple
  78. cout<< "Appears in tuple "<< cxlt::tuple_count_type<Tu1, TupleType>::value<< " times."<< endl;
  79. // The get<index> and get<type> functions are used to obtain the member
  80. // components of this tuple, demonstrating the correctness of the latter.
  81. long int g1 = get<0>(tu);
  82. long int t1 = get<long int>(tu);
  83. (void)g1;
  84. (void)t1;
  85. assert (g1 == t1 && "Obtained long ints from tuple are equal");
  86. std::string const& g2 = get<1>(tu).second;
  87. std::string const& t2 = get<Tu1>(tu).second;
  88. assert (g2 == t2 && "Obtained strings from tuple are equal");
  89. cout<< "Obtaining second string in the second element of tuple: ";
  90. cout<< g2<< endl;
  91. float& f1= get<float>(tu);
  92. cout<< "float: "<< f1<< endl;
  93. /*
  94. * When enabled, the following test *must* fail
  95. */
  96. #if 0
  97. #endif
  98. /*
  99. * When enabled, the following test *must* fail
  100. */
  101. #if 0
  102. tuple<bool, int, double, int, char> repeated;
  103. get<3>(repeated); // gets the second int
  104. get<int>(repeated); // <-- must FAIL, there's more than one int in tuple
  105. #endif
  106. }