io.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright (c) 2016-2025 Antony Polukhin
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_PFR_IO_HPP
  6. #define BOOST_PFR_IO_HPP
  7. #pragma once
  8. #include <boost/pfr/detail/config.hpp>
  9. #if !defined(BOOST_USE_MODULES) || defined(BOOST_PFR_INTERFACE_UNIT)
  10. #include <boost/pfr/detail/detectors.hpp>
  11. #include <boost/pfr/io_fields.hpp>
  12. /// \file boost/pfr/io.hpp
  13. /// Contains IO stream manipulator \forcedlink{io} for types.
  14. /// If type is streamable using its own operator or its conversion operator, then the types operator is used.
  15. ///
  16. /// \b Example:
  17. /// \code
  18. /// #include <boost/pfr/io.hpp>
  19. /// struct comparable_struct { // No operators defined for that structure
  20. /// int i; short s; char data[7]; bool bl; int a,b,c,d,e,f;
  21. /// };
  22. /// // ...
  23. ///
  24. /// comparable_struct s1 {0, 1, "Hello", false, 6,7,8,9,10,11};
  25. /// std::cout << boost::pfr::io(s1); // Outputs: {0, 1, H, e, l, l, o, , , 0, 6, 7, 8, 9, 10, 11}
  26. /// \endcode
  27. ///
  28. /// \podops for other ways to define operators and more details.
  29. ///
  30. /// \b Synopsis:
  31. namespace boost { namespace pfr {
  32. namespace detail {
  33. ///////////////////// Helper typedefs
  34. template <class Stream, class Type>
  35. using enable_not_ostreamable_t = std::enable_if_t<
  36. not_applicable<ostreamable_detector, Stream&, const std::remove_reference_t<Type>&>::value,
  37. Stream&
  38. >;
  39. template <class Stream, class Type>
  40. using enable_not_istreamable_t = std::enable_if_t<
  41. not_applicable<istreamable_detector, Stream&, Type&>::value,
  42. Stream&
  43. >;
  44. template <class Stream, class Type>
  45. using enable_ostreamable_t = std::enable_if_t<
  46. !not_applicable<ostreamable_detector, Stream&, const std::remove_reference_t<Type>&>::value,
  47. Stream&
  48. >;
  49. template <class Stream, class Type>
  50. using enable_istreamable_t = std::enable_if_t<
  51. !not_applicable<istreamable_detector, Stream&, Type&>::value,
  52. Stream&
  53. >;
  54. ///////////////////// IO impl
  55. template <class T>
  56. struct io_impl {
  57. T value;
  58. };
  59. BOOST_PFR_BEGIN_MODULE_EXPORT
  60. template <class Char, class Traits, class T>
  61. enable_not_ostreamable_t<std::basic_ostream<Char, Traits>, T> operator<<(std::basic_ostream<Char, Traits>& out, io_impl<T>&& x) {
  62. return out << boost::pfr::io_fields(std::forward<T>(x.value));
  63. }
  64. template <class Char, class Traits, class T>
  65. enable_ostreamable_t<std::basic_ostream<Char, Traits>, T> operator<<(std::basic_ostream<Char, Traits>& out, io_impl<T>&& x) {
  66. return out << x.value;
  67. }
  68. template <class Char, class Traits, class T>
  69. enable_not_istreamable_t<std::basic_istream<Char, Traits>, T> operator>>(std::basic_istream<Char, Traits>& in, io_impl<T>&& x) {
  70. return in >> boost::pfr::io_fields(std::forward<T>(x.value));
  71. }
  72. template <class Char, class Traits, class T>
  73. enable_istreamable_t<std::basic_istream<Char, Traits>, T> operator>>(std::basic_istream<Char, Traits>& in, io_impl<T>&& x) {
  74. return in >> x.value;
  75. }
  76. BOOST_PFR_END_MODULE_EXPORT
  77. } // namespace detail
  78. BOOST_PFR_BEGIN_MODULE_EXPORT
  79. /// IO manipulator to read/write \aggregate `value` using its IO stream operators or using \forcedlink{io_fields} if operators are not available.
  80. ///
  81. /// \b Example:
  82. /// \code
  83. /// struct my_struct { int i; short s; };
  84. /// my_struct x;
  85. /// std::stringstream ss;
  86. /// ss << "{ 12, 13 }";
  87. /// ss >> boost::pfr::io(x);
  88. /// assert(x.i == 12);
  89. /// assert(x.s == 13);
  90. /// \endcode
  91. ///
  92. /// \customio
  93. template <class T>
  94. auto io(T&& value) noexcept {
  95. return detail::io_impl<T>{std::forward<T>(value)};
  96. }
  97. BOOST_PFR_END_MODULE_EXPORT
  98. }} // namespace boost::pfr
  99. #endif // #if !defined(BOOST_USE_MODULES) || defined(BOOST_PFR_INTERFACE_UNIT)
  100. #endif // BOOST_PFR_IO_HPP