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