io.hpp 3.5 KB

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