type_traits.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #ifndef BOOST_BEAST_HTTP_DETAIL_TYPE_TRAITS_HPP
  10. #define BOOST_BEAST_HTTP_DETAIL_TYPE_TRAITS_HPP
  11. #include <boost/beast/core/detail/type_traits.hpp>
  12. #include <boost/optional.hpp>
  13. #include <cstdint>
  14. namespace boost {
  15. namespace beast {
  16. namespace http {
  17. template<bool isRequest, class Fields>
  18. class header;
  19. template<bool, class, class>
  20. class message;
  21. template<bool isRequest, class Body, class Fields>
  22. class parser;
  23. namespace detail {
  24. template<class T>
  25. class is_header_impl
  26. {
  27. template<bool b, class F>
  28. static std::true_type check(
  29. header<b, F> const*);
  30. static std::false_type check(...);
  31. public:
  32. using type = decltype(check((T*)0));
  33. };
  34. template<class T>
  35. using is_header = typename is_header_impl<T>::type;
  36. template<class T>
  37. struct is_parser : std::false_type {};
  38. template<bool isRequest, class Body, class Fields>
  39. struct is_parser<parser<isRequest, Body, Fields>> : std::true_type {};
  40. struct fields_model
  41. {
  42. struct writer;
  43. string_view method() const;
  44. string_view reason() const;
  45. string_view target() const;
  46. protected:
  47. string_view get_method_impl() const;
  48. string_view get_target_impl() const;
  49. string_view get_reason_impl() const;
  50. bool get_chunked_impl() const;
  51. bool get_keep_alive_impl(unsigned) const;
  52. bool has_content_length_impl() const;
  53. void set_method_impl(string_view);
  54. void set_target_impl(string_view);
  55. void set_reason_impl(string_view);
  56. void set_chunked_impl(bool);
  57. void set_content_length_impl(boost::optional<std::uint64_t>);
  58. void set_keep_alive_impl(unsigned, bool);
  59. };
  60. template<class T, class = beast::detail::void_t<>>
  61. struct has_value_type : std::false_type {};
  62. template<class T>
  63. struct has_value_type<T, beast::detail::void_t<
  64. typename T::value_type
  65. > > : std::true_type {};
  66. /** Determine if a <em>Body</em> type has a size
  67. This metafunction is equivalent to `std::true_type` if
  68. Body contains a static member function called `size`.
  69. */
  70. template<class T, class = void>
  71. struct is_body_sized : std::false_type {};
  72. template<class T>
  73. struct is_body_sized<T, beast::detail::void_t<
  74. typename T::value_type,
  75. decltype(
  76. std::declval<std::uint64_t&>() =
  77. T::size(std::declval<typename T::value_type const&>())
  78. )>> : std::true_type {};
  79. template<class T>
  80. struct is_fields_helper : T
  81. {
  82. template<class U = is_fields_helper>
  83. static auto f1(int) -> decltype(
  84. std::declval<string_view&>() = std::declval<U const&>().get_method_impl(),
  85. std::true_type());
  86. static auto f1(...) -> std::false_type;
  87. using t1 = decltype(f1(0));
  88. template<class U = is_fields_helper>
  89. static auto f2(int) -> decltype(
  90. std::declval<string_view&>() = std::declval<U const&>().get_target_impl(),
  91. std::true_type());
  92. static auto f2(...) -> std::false_type;
  93. using t2 = decltype(f2(0));
  94. template<class U = is_fields_helper>
  95. static auto f3(int) -> decltype(
  96. std::declval<string_view&>() = std::declval<U const&>().get_reason_impl(),
  97. std::true_type());
  98. static auto f3(...) -> std::false_type;
  99. using t3 = decltype(f3(0));
  100. template<class U = is_fields_helper>
  101. static auto f4(int) -> decltype(
  102. std::declval<bool&>() = std::declval<U const&>().get_chunked_impl(),
  103. std::true_type());
  104. static auto f4(...) -> std::false_type;
  105. using t4 = decltype(f4(0));
  106. template<class U = is_fields_helper>
  107. static auto f5(int) -> decltype(
  108. std::declval<bool&>() = std::declval<U const&>().get_keep_alive_impl(
  109. std::declval<unsigned>()),
  110. std::true_type());
  111. static auto f5(...) -> std::false_type;
  112. using t5 = decltype(f5(0));
  113. template<class U = is_fields_helper>
  114. static auto f6(int) -> decltype(
  115. std::declval<bool&>() = std::declval<U const&>().has_content_length_impl(),
  116. std::true_type());
  117. static auto f6(...) -> std::false_type;
  118. using t6 = decltype(f6(0));
  119. template<class U = is_fields_helper>
  120. static auto f7(int) -> decltype(
  121. void(std::declval<U&>().set_method_impl(std::declval<string_view>())),
  122. std::true_type());
  123. static auto f7(...) -> std::false_type;
  124. using t7 = decltype(f7(0));
  125. template<class U = is_fields_helper>
  126. static auto f8(int) -> decltype(
  127. void(std::declval<U&>().set_target_impl(std::declval<string_view>())),
  128. std::true_type());
  129. static auto f8(...) -> std::false_type;
  130. using t8 = decltype(f8(0));
  131. template<class U = is_fields_helper>
  132. static auto f9(int) -> decltype(
  133. void(std::declval<U&>().set_reason_impl(std::declval<string_view>())),
  134. std::true_type());
  135. static auto f9(...) -> std::false_type;
  136. using t9 = decltype(f9(0));
  137. template<class U = is_fields_helper>
  138. static auto f10(int) -> decltype(
  139. void(std::declval<U&>().set_chunked_impl(std::declval<bool>())),
  140. std::true_type());
  141. static auto f10(...) -> std::false_type;
  142. using t10 = decltype(f10(0));
  143. template<class U = is_fields_helper>
  144. static auto f11(int) -> decltype(
  145. void(std::declval<U&>().set_content_length_impl(
  146. std::declval<boost::optional<std::uint64_t>>())),
  147. std::true_type());
  148. static auto f11(...) -> std::false_type;
  149. using t11 = decltype(f11(0));
  150. template<class U = is_fields_helper>
  151. static auto f12(int) -> decltype(
  152. void(std::declval<U&>().set_keep_alive_impl(
  153. std::declval<unsigned>(),
  154. std::declval<bool>())),
  155. std::true_type());
  156. static auto f12(...) -> std::false_type;
  157. using t12 = decltype(f12(0));
  158. using type = std::integral_constant<bool,
  159. t1::value && t2::value && t3::value &&
  160. t4::value && t5::value && t6::value &&
  161. t7::value && t8::value && t9::value &&
  162. t10::value && t11::value && t12::value>;
  163. };
  164. } // detail
  165. } // http
  166. } // beast
  167. } // boost
  168. #endif