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