2
0
mirror of https://github.com/boostorg/hana.git synced 2026-02-01 08:32:11 +00:00
Files
hana/example/struct.cpp
Louis Dionne f87919b94b [Record] Rename to Struct and refactor
- Define the notion of move-independence, thus relaxing the requirements on accessors.
- Rename `members` to `accessors` and use `members` to get the members only
- Document the different ways of providing a model
- Make the model of Foldable more consistent

Fixes #53
2015-04-21 16:32:33 -04:00

146 lines
3.1 KiB
C++

/*
@copyright Louis Dionne 2015
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/map.hpp>
#include <boost/hana/maybe.hpp>
#include <boost/hana/pair.hpp>
#include <boost/hana/string.hpp>
#include <boost/hana/struct.hpp>
#include <boost/hana/struct_macros.hpp>
#include <boost/hana/tuple.hpp>
#include <string>
using namespace boost::hana;
int main() {
//////////////////////////////////////////////////////////////////////////////
// Models
//////////////////////////////////////////////////////////////////////////////
{
//! [Comparable]
struct Person {
BOOST_HANA_DEFINE_STRUCT(Person,
(std::string, name),
(unsigned short, age)
);
};
Person john{"John", 30}, kevin{"Kevin", 20};
BOOST_HANA_RUNTIME_ASSERT(equal(john, john));
BOOST_HANA_RUNTIME_ASSERT(not_equal(john, kevin));
//! [Comparable]
}{
//! [Foldable]
struct Classroom {
BOOST_HANA_DEFINE_STRUCT(Classroom,
(unsigned short, boys),
(unsigned short, girls)
);
};
constexpr Classroom compsci{20, 3}; /* lol */
BOOST_HANA_CONSTEXPR_ASSERT(
fold.left(compsci, 0, [](auto total, auto member) {
// first(member) is the name of the member, here
// "boys" or "girls", and second(member) is its value.
return second(member) + total;
}) == 23
);
//! [Foldable]
}{
//! [to<Map>]
struct Person {
BOOST_HANA_DEFINE_STRUCT(Person,
(std::string, name),
(unsigned short, age)
);
};
Person john{"John", 30u};
BOOST_HANA_RUNTIME_ASSERT(to<Map>(john) == make<Map>(
make<Pair>(BOOST_HANA_STRING("name"), "John"),
make<Pair>(BOOST_HANA_STRING("age"), 30u)
));
//! [to<Map>]
}{
//! [Searchable]
struct Person {
BOOST_HANA_DEFINE_STRUCT(Person,
(std::string, name),
(unsigned short, age)
);
};
Person john{"John", 30};
BOOST_HANA_RUNTIME_ASSERT(
find(john, BOOST_HANA_STRING("name")) == just("John")
);
BOOST_HANA_CONSTANT_ASSERT(
find(john, BOOST_HANA_STRING("foobar")) == nothing
);
//! [Searchable]
}
//////////////////////////////////////////////////////////////////////////////
// Methods
//////////////////////////////////////////////////////////////////////////////
{
//! [accessors]
struct Person {
BOOST_HANA_DEFINE_STRUCT(Person,
(std::string, name),
(unsigned short, age)
);
};
BOOST_HANA_CONSTANT_ASSERT(
transform(accessors<Person>(), first) == make_tuple(
BOOST_HANA_STRING("name"), BOOST_HANA_STRING("age")
)
);
auto get_name = second(at_c<0>(accessors<Person>()));
auto get_age = second(at_c<1>(accessors<Person>()));
Person john{"John", 30};
BOOST_HANA_RUNTIME_ASSERT(get_name(john) == "John");
BOOST_HANA_RUNTIME_ASSERT(get_age(john) == 30);
//! [accessors]
}{
//! [members]
struct Person {
BOOST_HANA_DEFINE_STRUCT(Person,
(std::string, name),
(unsigned short, age)
);
};
Person john{"John", 30};
BOOST_HANA_RUNTIME_ASSERT(members(john) == make_tuple("John", 30));
//! [members]
}
}