My original answer was shit; sorry for that. It corresponded here that actually answers the question.
I just added a macro to easily define Struct with custom accessories. Here is how you could do it:
#include <boost/hana/adapt_adt.hpp> #include <boost/hana/at_key.hpp> #include <boost/hana/string.hpp> #include <cassert> #include <string> namespace hana = boost::hana; struct Person { Person(std::string const& name, int age) : name_(name), age_(age) { } std::string const& get_name() const { return name_; } int get_age() const { return age_; } private: std::string name_; int age_; }; BOOST_HANA_ADAPT_ADT(Person, (name, [](auto const& p) { return p.get_name(); }), (age, [](auto const& p) { return p.get_age(); }) ); int main() { Person bob{"Bob", 30}; assert(hana::at_key(bob, BOOST_HANA_STRING("name")) == "Bob"); assert(hana::at_key(bob, BOOST_HANA_STRING("age")) == 30); }
This code should work on master . Otherwise, if you need more control over the definition of your accessories or the keys used to match them, you can also define all of these manually:
namespace boost { namespace hana { template <> struct accessors_impl<Person> { static auto apply() { return hana::make_tuple( hana::make_pair(BOOST_HANA_STRING("name"), [](auto&& p) -> std::string const& { return p.get_name(); }), hana::make_pair(BOOST_HANA_STRING("age"), [](auto&& p) { return p.get_age(); }) ); } }; }}
You can find more information on how to define Struct in the link for the Struct concept.
Louis dionne
source share