io_fields.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright (c) 2016-2024 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_FIELDS_HPP
  6. #define BOOST_PFR_IO_FIELDS_HPP
  7. #pragma once
  8. #include <boost/pfr/detail/config.hpp>
  9. #include <boost/pfr/detail/core.hpp>
  10. #include <type_traits>
  11. #include <utility> // metaprogramming stuff
  12. #include <boost/pfr/detail/sequence_tuple.hpp>
  13. #include <boost/pfr/detail/io.hpp>
  14. #include <boost/pfr/detail/make_integer_sequence.hpp>
  15. #include <boost/pfr/tuple_size.hpp>
  16. /// \file boost/pfr/io_fields.hpp
  17. /// Contains IO manipulator \forcedlink{io_fields} to read/write any \aggregate field-by-field.
  18. ///
  19. /// \b Example:
  20. /// \code
  21. /// struct my_struct {
  22. /// int i;
  23. /// short s;
  24. /// };
  25. ///
  26. /// std::ostream& operator<<(std::ostream& os, const my_struct& x) {
  27. /// return os << boost::pfr::io_fields(x); // Equivalent to: os << "{ " << x.i << " ," << x.s << " }"
  28. /// }
  29. ///
  30. /// std::istream& operator>>(std::istream& is, my_struct& x) {
  31. /// return is >> boost::pfr::io_fields(x); // Equivalent to: is >> "{ " >> x.i >> " ," >> x.s >> " }"
  32. /// }
  33. /// \endcode
  34. ///
  35. /// \podops for other ways to define operators and more details.
  36. ///
  37. /// \b Synopsis:
  38. namespace boost { namespace pfr {
  39. namespace detail {
  40. template <class T>
  41. struct io_fields_impl {
  42. T value;
  43. };
  44. template <class Char, class Traits, class T>
  45. std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& out, io_fields_impl<const T&>&& x) {
  46. const T& value = x.value;
  47. constexpr std::size_t fields_count_val = boost::pfr::detail::fields_count<T>();
  48. out << '{';
  49. #if BOOST_PFR_USE_CPP17 || BOOST_PFR_USE_LOOPHOLE
  50. detail::print_impl<0, fields_count_val>::print(out, detail::tie_as_tuple(value));
  51. #else
  52. ::boost::pfr::detail::for_each_field_dispatcher(
  53. value,
  54. [&out](const auto& val) {
  55. // We can not reuse `fields_count_val` in lambda because compilers had issues with
  56. // passing constexpr variables into lambdas. Computing is again is the most portable solution.
  57. constexpr std::size_t fields_count_val_lambda = boost::pfr::detail::fields_count<T>();
  58. detail::print_impl<0, fields_count_val_lambda>::print(out, val);
  59. },
  60. detail::make_index_sequence<fields_count_val>{}
  61. );
  62. #endif
  63. return out << '}';
  64. }
  65. template <class Char, class Traits, class T>
  66. std::basic_ostream<Char, Traits>& operator<<(std::basic_ostream<Char, Traits>& out, io_fields_impl<T>&& x) {
  67. return out << io_fields_impl<const std::remove_reference_t<T>&>{x.value};
  68. }
  69. template <class Char, class Traits, class T>
  70. std::basic_istream<Char, Traits>& operator>>(std::basic_istream<Char, Traits>& in, io_fields_impl<T&>&& x) {
  71. T& value = x.value;
  72. constexpr std::size_t fields_count_val = boost::pfr::detail::fields_count<T>();
  73. const auto prev_exceptions = in.exceptions();
  74. in.exceptions( typename std::basic_istream<Char, Traits>::iostate(0) );
  75. const auto prev_flags = in.flags( typename std::basic_istream<Char, Traits>::fmtflags(0) );
  76. char parenthis = {};
  77. in >> parenthis;
  78. if (parenthis != '{') in.setstate(std::basic_istream<Char, Traits>::failbit);
  79. #if BOOST_PFR_USE_CPP17 || BOOST_PFR_USE_LOOPHOLE
  80. detail::read_impl<0, fields_count_val>::read(in, detail::tie_as_tuple(value));
  81. #else
  82. ::boost::pfr::detail::for_each_field_dispatcher(
  83. value,
  84. [&in](const auto& val) {
  85. // We can not reuse `fields_count_val` in lambda because compilers had issues with
  86. // passing constexpr variables into lambdas. Computing is again is the most portable solution.
  87. constexpr std::size_t fields_count_val_lambda = boost::pfr::detail::fields_count<T>();
  88. detail::read_impl<0, fields_count_val_lambda>::read(in, val);
  89. },
  90. detail::make_index_sequence<fields_count_val>{}
  91. );
  92. #endif
  93. in >> parenthis;
  94. if (parenthis != '}') in.setstate(std::basic_istream<Char, Traits>::failbit);
  95. in.flags(prev_flags);
  96. in.exceptions(prev_exceptions);
  97. return in;
  98. }
  99. template <class Char, class Traits, class T>
  100. std::basic_istream<Char, Traits>& operator>>(std::basic_istream<Char, Traits>& in, io_fields_impl<const T&>&& ) {
  101. static_assert(sizeof(T) && false, "====================> Boost.PFR: Attempt to use istream operator on a boost::pfr::io_fields wrapped type T with const qualifier.");
  102. return in;
  103. }
  104. template <class Char, class Traits, class T>
  105. std::basic_istream<Char, Traits>& operator>>(std::basic_istream<Char, Traits>& in, io_fields_impl<T>&& ) {
  106. static_assert(sizeof(T) && false, "====================> Boost.PFR: Attempt to use istream operator on a boost::pfr::io_fields wrapped temporary of type T.");
  107. return in;
  108. }
  109. } // namespace detail
  110. /// IO manipulator to read/write \aggregate `value` field-by-field.
  111. ///
  112. /// \b Example:
  113. /// \code
  114. /// struct my_struct {
  115. /// int i;
  116. /// short s;
  117. /// };
  118. ///
  119. /// std::ostream& operator<<(std::ostream& os, const my_struct& x) {
  120. /// return os << boost::pfr::io_fields(x); // Equivalent to: os << "{ " << x.i << " ," << x.s << " }"
  121. /// }
  122. ///
  123. /// std::istream& operator>>(std::istream& is, my_struct& x) {
  124. /// return is >> boost::pfr::io_fields(x); // Equivalent to: is >> "{ " >> x.i >> " ," >> x.s >> " }"
  125. /// }
  126. /// \endcode
  127. ///
  128. /// Input and output streaming operators for `boost::pfr::io_fields` are symmetric, meaning that you get the original value by streaming it and
  129. /// reading back if each fields streaming operator is symmetric.
  130. ///
  131. /// \customio
  132. template <class T>
  133. auto io_fields(T&& value) noexcept {
  134. return detail::io_fields_impl<T>{std::forward<T>(value)};
  135. }
  136. }} // namespace boost::pfr
  137. #endif // BOOST_PFR_IO_FIELDS_HPP