]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/hana/example/misc/printf.cpp
1 // Copyright Louis Dionne 2013-2017
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
5 #include <boost/hana/adjust_if.hpp>
6 #include <boost/hana/at_key.hpp>
7 #include <boost/hana/core/is_a.hpp>
8 #include <boost/hana/core/to.hpp>
9 #include <boost/hana/filter.hpp>
10 #include <boost/hana/functional/compose.hpp>
11 #include <boost/hana/functional/partial.hpp>
12 #include <boost/hana/map.hpp>
13 #include <boost/hana/not.hpp>
14 #include <boost/hana/pair.hpp>
15 #include <boost/hana/prepend.hpp>
16 #include <boost/hana/string.hpp>
17 #include <boost/hana/sum.hpp>
18 #include <boost/hana/tuple.hpp>
19 #include <boost/hana/type.hpp>
20 #include <boost/hana/unpack.hpp>
23 namespace hana
= boost::hana
;
26 constexpr auto formats
= hana::make_map(
27 hana::make_pair(hana::type_c
<int>, hana::string_c
<'%', 'd'>),
28 hana::make_pair(hana::type_c
<float>, hana::string_c
<'%', 'f'>),
29 hana::make_pair(hana::type_c
<char const*>, hana::string_c
<'%', 's'>)
32 template <typename
...Tokens
>
33 constexpr auto format(Tokens
...tokens_
) {
34 auto tokens
= hana::make_tuple(tokens_
...);
36 // If you don't care about constexpr-ness of `format`, you can use
37 // this lambda instead of `compose(partial(...), typeid_)`:
40 // return formats[typeid_(token)];
42 auto format_string_tokens
= hana::adjust_if(tokens
,
43 hana::compose(hana::not_
, hana::is_a
<hana::string_tag
>),
44 hana::compose(hana::partial(hana::at_key
, formats
), hana::typeid_
)
47 auto format_string
= hana::sum
<hana::string_tag
>(format_string_tokens
);
48 auto variables
= hana::filter(tokens
, hana::compose(hana::not_
, hana::is_a
<hana::string_tag
>));
49 return hana::prepend(variables
, format_string
);
55 char const* c
= "abcdef";
58 BOOST_HANA_STRING("first="), a
59 , BOOST_HANA_STRING(" second="), b
60 , BOOST_HANA_STRING(" third="), c
63 hana::unpack(args
, [](auto fmt
, auto ...args
) {
64 std::printf(hana::to
<char const*>(fmt
), args
...);