find_option.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright (c) 2006-2018 Maxim Khizhinsky
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #include <type_traits>
  6. #include <cds/opt/options.h>
  7. // Value options
  8. namespace {
  9. template <int Val>
  10. struct int_opt {
  11. static const int value = Val;
  12. };
  13. template <bool Val>
  14. struct bool_opt {
  15. static const bool value = Val;
  16. };
  17. enum user_enum {
  18. val_zero, val_one, val_two, val_three, val_four, val_five
  19. };
  20. template <user_enum Val>
  21. struct enum_opt {
  22. static const user_enum value = Val;
  23. };
  24. }
  25. // Declare necessary cds::opt::find_option specialization for user-provided enum type
  26. CDS_DECLARE_FIND_OPTION_INTEGRAL_SPECIALIZATION( user_enum )
  27. void find_option_compiler_test()
  28. {
  29. // *************************************************
  30. // Type options
  31. //
  32. struct tag_default;
  33. struct tag_a;
  34. struct tag_b;
  35. // Option not found
  36. static_assert( (std::is_same<
  37. cds::opt::find_option< cds::opt::tag<tag_default>, cds::opt::stat<tag_a>, bool_opt<false> >::type,
  38. cds::opt::tag<tag_default>
  39. >::value), "Result != tag_default" );
  40. // Option found once
  41. static_assert( (std::is_same<
  42. cds::opt::find_option< cds::opt::tag<tag_default>, cds::opt::tag<tag_a> >::type,
  43. cds::opt::tag<tag_a>
  44. >::value), "Result != tag_a" );
  45. static_assert( (std::is_same<
  46. cds::opt::find_option< cds::opt::tag<tag_default>, cds::opt::stat<tag_a>, cds::opt::tag<tag_a> >::type,
  47. cds::opt::tag<tag_a>
  48. >::value), "Result != tag_a" );
  49. // First option
  50. static_assert( (std::is_same<
  51. cds::opt::find_option< cds::opt::tag<tag_default>
  52. ,cds::opt::tag<tag_a> // desired
  53. ,cds::opt::stat<tag_a>
  54. ,cds::opt::stat<tag_a>
  55. ,cds::opt::stat<tag_a>
  56. ,cds::opt::stat<tag_a>
  57. ,cds::opt::stat<tag_a>
  58. >::type,
  59. cds::opt::tag<tag_a>
  60. >::value), "Result != tag_a" );
  61. // Last option
  62. static_assert( (std::is_same<
  63. cds::opt::find_option< cds::opt::tag<tag_default>
  64. ,cds::opt::stat<tag_a>
  65. ,cds::opt::stat<tag_a>
  66. ,cds::opt::stat<tag_a>
  67. ,cds::opt::stat<tag_a>
  68. ,cds::opt::stat<tag_a>
  69. ,cds::opt::tag<tag_a> // desired
  70. >::type,
  71. cds::opt::tag<tag_a>
  72. >::value), "Result != tag_a" );
  73. // Middle option
  74. static_assert( (std::is_same<
  75. cds::opt::find_option< cds::opt::tag<tag_default>
  76. ,cds::opt::stat<tag_a>
  77. ,cds::opt::stat<tag_a>
  78. ,cds::opt::stat<tag_a>
  79. ,cds::opt::tag<tag_a> // desired
  80. ,cds::opt::stat<tag_a>
  81. ,cds::opt::stat<tag_a>
  82. >::type,
  83. cds::opt::tag<tag_a>
  84. >::value), "Result != tag_a" );
  85. // Option not found
  86. static_assert( (std::is_same<
  87. cds::opt::find_option< cds::opt::tag<tag_default>
  88. ,cds::opt::stat<tag_a>
  89. ,cds::opt::stat<tag_a>
  90. ,cds::opt::stat<tag_a>
  91. ,cds::opt::stat<tag_default>
  92. ,cds::opt::stat<tag_a>
  93. ,cds::opt::stat<tag_a>
  94. >::type,
  95. cds::opt::tag<tag_default>
  96. >::value), "Result != tag_default" );
  97. // Multiple options
  98. static_assert( (std::is_same<
  99. cds::opt::find_option< cds::opt::tag<tag_default>, cds::opt::tag<tag_a>, cds::opt::tag<tag_b> >::type,
  100. cds::opt::tag<tag_a>
  101. >::value), "Result != tag_a" );
  102. static_assert( (std::is_same<
  103. cds::opt::find_option< cds::opt::tag<tag_default>
  104. ,cds::opt::tag<tag_a> // desired - first accepted
  105. ,cds::opt::stat<tag_a>
  106. ,cds::opt::stat<tag_a>
  107. ,cds::opt::stat<tag_b>
  108. ,cds::opt::stat<tag_a>
  109. ,cds::opt::stat<tag_a>
  110. ,cds::opt::tag<tag_b> // desired
  111. >::type,
  112. cds::opt::tag<tag_a>
  113. >::value), "Result != tag_a" );
  114. // *****************************************************
  115. // Value options
  116. // Not found
  117. static_assert( (std::is_same<
  118. cds::opt::find_option< int_opt<15>, bool_opt<false>, cds::opt::stat<tag_a> >::type,
  119. int_opt<15>
  120. >::value), "Result != int_opt<15>" );
  121. static_assert( (std::is_same<
  122. cds::opt::find_option< int_opt<15>, int_opt<100>, cds::opt::stat<tag_a> >::type,
  123. int_opt<100>
  124. >::value), "Result != int_opt<100>" );
  125. static_assert( (std::is_same<
  126. cds::opt::find_option< int_opt<15>, int_opt<100>, cds::opt::stat<tag_a>, bool_opt<true>, int_opt<200> >::type,
  127. int_opt<100>
  128. >::value), "Result != int_opt<100>" );
  129. // User-provided enum type
  130. static_assert( (std::is_same<
  131. cds::opt::find_option< enum_opt<val_zero>, int_opt<100>, cds::opt::stat<tag_a>, int_opt<200> >::type,
  132. enum_opt<val_zero>
  133. >::value), "Result != enum_opt<val_zero>" );
  134. static_assert( (std::is_same<
  135. cds::opt::find_option< enum_opt<val_zero>, int_opt<100>, cds::opt::stat<tag_a>, enum_opt<val_three>, int_opt<200> >::type,
  136. enum_opt<val_three>
  137. >::value), "Result != enum_opt<val_three>" );
  138. }
  139. void test_extracting_option_value()
  140. {
  141. struct tag_a;
  142. // Define option
  143. typedef cds::opt::tag< tag_a > tag_option;
  144. // What is the value of the tag_option?
  145. // How we can extract tag_a from tag_option?
  146. // Here is a solution:
  147. typedef cds::opt::value< tag_option >::tag tag_option_value;
  148. // tag_option_value is the same as tag_a
  149. static_assert( (std::is_same< tag_option_value, tag_a >::value), "Error getting the value of option: tag_option_value != tag_a" );
  150. // Value-option
  151. typedef cds::opt::alignment< 16 > align_option;
  152. static_assert( cds::opt::value< align_option >::alignment == 16, "Error getting the value of option: option value != 16" );
  153. }