summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/hana/example/tutorial/quickstart.cpp
blob: 6358e503bb552551661e395cd6b6d47ab912a5ba (plain)
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
// Copyright Louis Dionne 2013-2017
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)

// Make sure `assert` always triggers an assertion
#ifdef NDEBUG
#   undef NDEBUG
#endif

//! [additional_setup]
#include <cassert>
#include <iostream>
#include <string>

struct Fish { std::string name; };
struct Cat  { std::string name; };
struct Dog  { std::string name; };
//! [additional_setup]

//! [includes]
#include <boost/hana.hpp>
namespace hana = boost::hana;
//! [includes]


int main() {

//! [animals]
auto animals = hana::make_tuple(Fish{"Nemo"}, Cat{"Garfield"}, Dog{"Snoopy"});
//! [animals]

//! [algorithms]
using namespace hana::literals;

// Access tuple elements with operator[] instead of std::get.
Cat garfield = animals[1_c];

// Perform high level algorithms on tuples (this is like std::transform)
auto names = hana::transform(animals, [](auto a) {
  return a.name;
});

assert(hana::reverse(names) == hana::make_tuple("Snoopy", "Garfield", "Nemo"));
//! [algorithms]


//! [type-level]
auto animal_types = hana::make_tuple(hana::type_c<Fish*>, hana::type_c<Cat&>, hana::type_c<Dog>);

auto no_pointers = hana::remove_if(animal_types, [](auto a) {
  return hana::traits::is_pointer(a);
});

static_assert(no_pointers == hana::make_tuple(hana::type_c<Cat&>, hana::type_c<Dog>), "");
//! [type-level]


//! [has_name]
auto has_name = hana::is_valid([](auto&& x) -> decltype((void)x.name) { });

static_assert(has_name(garfield), "");
static_assert(!has_name(1), "");
//! [has_name]

#if 0
//! [screw_up]
auto serialize = [](std::ostream& os, auto const& object) {
  hana::for_each(os, [&](auto member) {
    //           ^^ oopsie daisy!
    os << member << std::endl;
  });
};
//! [screw_up]
#endif

//! [serialization]
// 1. Give introspection capabilities to 'Person'
struct Person {
  BOOST_HANA_DEFINE_STRUCT(Person,
    (std::string, name),
    (int, age)
  );
};

// 2. Write a generic serializer (bear with std::ostream for the example)
auto serialize = [](std::ostream& os, auto const& object) {
  hana::for_each(hana::members(object), [&](auto member) {
    os << member << std::endl;
  });
};

// 3. Use it
Person john{"John", 30};
serialize(std::cout, john);

// output:
// John
// 30
//! [serialization]

}