]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/hana/example/tutorial/quickstart.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 // Make sure `assert` always triggers an assertion
10 //! [additional_setup]
15 struct Fish
{ std::string name
; };
16 struct Cat
{ std::string name
; };
17 struct Dog
{ std::string name
; };
18 //! [additional_setup]
21 #include <boost/hana.hpp>
22 namespace hana
= boost::hana
;
29 auto animals
= hana::make_tuple(Fish
{"Nemo"}, Cat
{"Garfield"}, Dog
{"Snoopy"});
33 using namespace hana::literals
;
35 // Access tuple elements with operator[] instead of std::get.
36 Cat garfield
= animals
[1_c
];
38 // Perform high level algorithms on tuples (this is like std::transform)
39 auto names
= hana::transform(animals
, [](auto a
) {
43 assert(hana::reverse(names
) == hana::make_tuple("Snoopy", "Garfield", "Nemo"));
48 auto animal_types
= hana::make_tuple(hana::type_c
<Fish
*>, hana::type_c
<Cat
&>, hana::type_c
<Dog
>);
50 auto no_pointers
= hana::remove_if(animal_types
, [](auto a
) {
51 return hana::traits::is_pointer(a
);
54 static_assert(no_pointers
== hana::make_tuple(hana::type_c
<Cat
&>, hana::type_c
<Dog
>), "");
59 auto has_name
= hana::is_valid([](auto&& x
) -> decltype((void)x
.name
) { });
61 static_assert(has_name(garfield
), "");
62 static_assert(!has_name(1), "");
67 auto serialize
= [](std::ostream
& os
, auto const& object
) {
68 hana::for_each(os
, [&](auto member
) {
70 os
<< member
<< std::endl
;
77 // 1. Give introspection capabilities to 'Person'
79 BOOST_HANA_DEFINE_STRUCT(Person
,
85 // 2. Write a generic serializer (bear with std::ostream for the example)
86 auto serialize
= [](std::ostream
& os
, auto const& object
) {
87 hana::for_each(hana::members(object
), [&](auto member
) {
88 os
<< member
<< std::endl
;
93 Person john
{"John", 30};
94 serialize(std::cout
, john
);