3 Defines `boost::hana::experimental::print`.
5 @copyright Louis Dionne 2013-2017
6 Distributed under the Boost Software License, Version 1.0.
7 (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
10 #ifndef BOOST_HANA_EXPERIMENTAL_PRINTABLE_HPP
11 #define BOOST_HANA_EXPERIMENTAL_PRINTABLE_HPP
13 #include <boost/hana/concept/constant.hpp>
14 #include <boost/hana/concept/product.hpp>
15 #include <boost/hana/concept/sequence.hpp>
16 #include <boost/hana/config.hpp>
17 #include <boost/hana/core/to.hpp>
18 #include <boost/hana/core/dispatch.hpp>
19 #include <boost/hana/first.hpp>
20 #include <boost/hana/for_each.hpp>
21 #include <boost/hana/intersperse.hpp>
22 #include <boost/hana/second.hpp>
23 #include <boost/hana/transform.hpp>
24 #include <boost/hana/tuple.hpp>
26 // models for different containers
27 #include <boost/hana/fwd/map.hpp>
28 #include <boost/hana/fwd/optional.hpp>
29 #include <boost/hana/fwd/set.hpp>
30 #include <boost/hana/fwd/string.hpp>
31 #include <boost/hana/fwd/type.hpp>
33 #include <boost/core/demangle.hpp>
43 BOOST_HANA_NAMESPACE_BEGIN namespace experimental {
48 template <typename T, typename = void>
49 struct print_impl : print_impl<T, hana::when<true>> { };
51 template <typename T, bool condition>
52 struct print_impl<T, hana::when<condition>> : hana::default_ {
53 template <typename ...Args>
54 static constexpr auto apply(Args&& ...) = delete;
58 //! @ingroup group-experimental
59 //! Returns a string representation of the given object.
61 //! This function is defined for most containers provided by Hana, and
62 //! also for objects that define an `operator<<` that can be used with
63 //! a `std::basic_ostream`. It can recursively print containers within
64 //! containers, but do not expect any kind of proper indentation.
66 //! This function requires (the rest of) Boost to be available on the
67 //! system. It also requires RTTI to be enabled.
68 #ifdef BOOST_HANA_DOXYGEN_INVOKED
69 auto print = [](auto const& x) -> std::string {
70 return tag-dispatched;
75 std::string operator()(T const& t) const {
76 using Tag = typename hana::tag_of<T>::type;
77 using Print = BOOST_HANA_DISPATCH_IF(print_impl<Tag>,
78 hana::experimental::Printable<Tag>::value
81 #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
82 static_assert(hana::experimental::Printable<Tag>::value,
83 "hana::experimental::print(t) requires 't' to be Printable");
86 return Print::apply(t);
90 constexpr print_t print{};
93 // Define the `Printable` concept
96 using Tag = typename hana::tag_of<T>::type;
97 static constexpr bool value = !hana::is_default<print_impl<Tag>>::value;
100 namespace print_detail {
101 std::string strip_type_junk(std::string const& str) {
102 return std::regex_replace(str, std::regex("^([a-z_]+::)*([a-z_]*)_t<"), "$2<");
106 // model for Sequences
107 template <typename S>
108 struct print_impl<S, hana::when<hana::Sequence<S>::value>> {
109 template <typename Xs>
110 static std::string apply(Xs const& xs) {
111 std::string result = "(";
112 auto comma_separated = hana::intersperse(xs, ", ");
113 hana::for_each(comma_separated, [&result](auto const& x) {
114 result += hana::experimental::print(x);
121 // model for OutputStreamable types
123 template <typename S>
124 struct print_impl<S, hana::when_valid<decltype(
125 std::declval<std::ostringstream&>() << std::declval<S const&>()
127 template <typename T>
128 static std::string apply(T const& t) {
129 std::ostringstream os;
136 // model for hana::optional
138 struct print_impl<hana::optional_tag> {
139 template <typename O>
140 static std::string apply(O const& optional) {
141 return hana::maybe("nothing",
143 return "just(" + hana::experimental::print(x) + ")";
148 // model for hana::maps
150 struct print_impl<hana::map_tag> {
151 template <typename M>
152 static std::string apply(M const& map) {
153 std::string result = "{";
154 auto pairs = hana::transform(hana::to_tuple(map),
155 [](auto const& pair) {
156 return hana::experimental::print(hana::first(pair))
158 + hana::experimental::print(hana::second(pair));
160 auto comma_separated = hana::intersperse(pairs, ", ");
161 hana::for_each(comma_separated, [&result](auto const& element) {
169 // model for hana::metafunctions
170 template <template <typename ...> class F>
171 struct print_impl<hana::metafunction_t<F>> {
172 template <typename T>
173 static std::string apply(T const&) {
174 return print_detail::strip_type_junk(boost::core::demangle(typeid(T).name()));
178 // model for hana::metafunction_classes
179 template <typename F>
180 struct print_impl<hana::metafunction_class_t<F>> {
181 template <typename T>
182 static std::string apply(T const&) {
183 return print_detail::strip_type_junk(boost::core::demangle(typeid(T).name()));
187 // model for Constants holding a `Printable`
188 template <typename C>
189 struct print_impl<C, hana::when<
190 hana::Constant<C>::value &&
191 Printable<typename C::value_type>::value
193 template <typename T>
194 static std::string apply(T const&) {
195 constexpr auto value = hana::value<T>();
196 return hana::experimental::print(value);
200 // model for Products
201 template <typename P>
202 struct print_impl<P, hana::when<hana::Product<P>::value>> {
203 template <typename T>
204 static std::string apply(T const& t) {
205 return '(' + hana::experimental::print(hana::first(t))
207 + hana::experimental::print(hana::second(t)) + ')';
211 // model for hana::strings
213 struct print_impl<hana::string_tag> {
214 template <typename S>
215 static std::string apply(S const& s) {
216 return '"' + std::string{hana::to<char const*>(s)} + '"';
220 // model for hana::sets
222 struct print_impl<hana::set_tag> {
223 template <typename S>
224 static std::string apply(S const& set) {
225 std::string result = "{";
226 auto as_tuple = hana::transform(hana::to_tuple(set),
227 hana::experimental::print);
228 auto comma_separated = hana::intersperse(as_tuple, ", ");
229 hana::for_each(comma_separated, [&result](auto const& element) {
237 // model for hana::templates
238 template <template <typename ...> class F>
239 struct print_impl<template_t<F>> {
240 template <typename T>
241 static std::string apply(T const&) {
242 return print_detail::strip_type_junk(boost::core::demangle(typeid(T).name()));
246 // model for hana::types
248 struct print_impl<hana::type_tag> {
249 template <typename T>
250 static std::string apply(T const&) {
251 using Type = typename T::type;
252 return "type<" + boost::core::demangle(typeid(Type).name()) + '>';
255 } BOOST_HANA_NAMESPACE_END
257 #endif // !BOOST_HANA_EXPERIMENTAL_PRINTABLE_HPP