summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/hana/test/concept/struct/at_key.cpp
blob: 2a9d757be7bdc187e04fb9b71fa555f71b8b932c (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
// 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)

#include <boost/hana/assert.hpp>
#include <boost/hana/at_key.hpp>
#include <boost/hana/define_struct.hpp>
#include <boost/hana/string.hpp>

#include <string>
namespace hana = boost::hana;


struct Person {
    BOOST_HANA_DEFINE_STRUCT(Person,
        (std::string, name),
        (std::string, last_name),
        (int, age)
    );
};

int main() {
    // non-const ref
    {
        Person john{"John", "Doe", 30};
        std::string& name = hana::at_key(john, BOOST_HANA_STRING("name"));
        std::string& last_name = hana::at_key(john, BOOST_HANA_STRING("last_name"));
        int& age = hana::at_key(john, BOOST_HANA_STRING("age"));

        name = "Bob";
        last_name = "Foo";
        age = 99;

        BOOST_HANA_RUNTIME_CHECK(john.name == "Bob");
        BOOST_HANA_RUNTIME_CHECK(john.last_name == "Foo");
        BOOST_HANA_RUNTIME_CHECK(john.age == 99);
    }

    // const ref
    {
        Person john{"John", "Doe", 30};
        Person const& const_john = john;
        std::string const& name = hana::at_key(const_john, BOOST_HANA_STRING("name"));
        std::string const& last_name = hana::at_key(const_john, BOOST_HANA_STRING("last_name"));
        int const& age = hana::at_key(const_john, BOOST_HANA_STRING("age"));

        john.name = "Bob";
        john.last_name = "Foo";
        john.age = 99;

        BOOST_HANA_RUNTIME_CHECK(name == "Bob");
        BOOST_HANA_RUNTIME_CHECK(last_name == "Foo");
        BOOST_HANA_RUNTIME_CHECK(age == 99);
    }
}