is_cstring.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // (C) Copyright Gennadiy Rozental 2001.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // See http://www.boost.org/libs/test for the library home page.
  6. //
  7. //! @file
  8. //! Defines the is_cstring type trait
  9. // ***************************************************************************
  10. #ifndef BOOST_TEST_UTILS_IS_CSTRING_HPP
  11. #define BOOST_TEST_UTILS_IS_CSTRING_HPP
  12. // Boost
  13. #include <boost/mpl/bool.hpp>
  14. #include <boost/type_traits/is_same.hpp>
  15. #include <boost/type_traits/decay.hpp>
  16. #include <boost/type_traits/remove_pointer.hpp>
  17. #include <boost/type_traits/remove_const.hpp>
  18. #include <boost/type_traits/add_const.hpp>
  19. #include <boost/test/utils/basic_cstring/basic_cstring_fwd.hpp>
  20. #include <string>
  21. #if defined(BOOST_TEST_STRING_VIEW)
  22. #include <string_view>
  23. #endif
  24. //____________________________________________________________________________//
  25. namespace boost {
  26. namespace unit_test {
  27. // ************************************************************************** //
  28. // ************** is_cstring ************** //
  29. // ************************************************************************** //
  30. namespace ut_detail {
  31. template<typename T>
  32. struct is_cstring_impl : public mpl::false_ {};
  33. template<typename T>
  34. struct is_cstring_impl<T const*> : public is_cstring_impl<T*> {};
  35. template<typename T>
  36. struct is_cstring_impl<T const* const> : public is_cstring_impl<T*> {};
  37. template<>
  38. struct is_cstring_impl<char*> : public mpl::true_ {};
  39. template<>
  40. struct is_cstring_impl<wchar_t*> : public mpl::true_ {};
  41. template <typename T, bool is_cstring = is_cstring_impl<typename boost::decay<T>::type>::value >
  42. struct deduce_cstring_transform_impl;
  43. template <typename T, bool is_cstring >
  44. struct deduce_cstring_transform_impl<T&, is_cstring> : public deduce_cstring_transform_impl<T, is_cstring>{};
  45. template <typename T, bool is_cstring >
  46. struct deduce_cstring_transform_impl<T const, is_cstring> : public deduce_cstring_transform_impl<T, is_cstring>{};
  47. template <typename T>
  48. struct deduce_cstring_transform_impl<T, true> {
  49. typedef typename boost::add_const<
  50. typename boost::remove_pointer<
  51. typename boost::decay<T>::type
  52. >::type
  53. >::type U;
  54. typedef boost::unit_test::basic_cstring<U> type;
  55. };
  56. template <typename T>
  57. struct deduce_cstring_transform_impl< T, false > {
  58. typedef typename
  59. boost::remove_const<
  60. typename boost::remove_reference<T>::type
  61. >::type type;
  62. };
  63. template <typename T>
  64. struct deduce_cstring_transform_impl< std::basic_string<T, std::char_traits<T> >, false > {
  65. typedef boost::unit_test::basic_cstring<typename boost::add_const<T>::type> type;
  66. };
  67. #if defined(BOOST_TEST_STRING_VIEW)
  68. template <typename T>
  69. struct deduce_cstring_transform_impl< std::basic_string_view<T, std::char_traits<T> >, false > {
  70. private:
  71. using sv_t = std::basic_string_view<T, std::char_traits<T> > ;
  72. public:
  73. using type = stringview_cstring_helper<typename boost::add_const<T>::type, sv_t>;
  74. };
  75. #endif
  76. } // namespace ut_detail
  77. template<typename T>
  78. struct is_cstring : public ut_detail::is_cstring_impl<typename decay<T>::type> {};
  79. template<typename T, bool is_cstring = is_cstring<typename boost::decay<T>::type>::value >
  80. struct is_cstring_comparable: public mpl::false_ {};
  81. template<typename T>
  82. struct is_cstring_comparable< T, true > : public mpl::true_ {};
  83. template<typename T>
  84. struct is_cstring_comparable< std::basic_string<T, std::char_traits<T> >, false > : public mpl::true_ {};
  85. #if defined(BOOST_TEST_STRING_VIEW)
  86. template<typename T>
  87. struct is_cstring_comparable< std::basic_string_view<T, std::char_traits<T> >, false > : public mpl::true_ {};
  88. #endif
  89. template<typename T>
  90. struct is_cstring_comparable< boost::unit_test::basic_cstring<T>, false > : public mpl::true_ {};
  91. template <class T>
  92. struct deduce_cstring_transform {
  93. typedef typename
  94. boost::remove_const<
  95. typename boost::remove_reference<T>::type
  96. >::type U;
  97. typedef typename ut_detail::deduce_cstring_transform_impl<typename boost::decay<U>::type>::type type;
  98. };
  99. } // namespace unit_test
  100. } // namespace boost
  101. #endif // BOOST_TEST_UTILS_IS_CSTRING_HPP