In the good old days we used to adapt a struct
into a Boost.Fusion container or an associative container with
#include <boost/fusion/adapted/struct/adapt_struct.hpp>
#include <boost/fusion/adapted/struct/adapt_assoc_struct.hpp>
struct Person{
std::string name;
int age;
};
BOOST_FUSION_ADAPT_STRUCT(
Person,
(std::string, name)
(int, age)
);
struct tag_name;
struct tag_age;
BOOST_FUSION_ADAPT_ASSOC_STRUCT(
Person,
(std::string, name, tag_name)
(int, age, tag_age));
int main(){}
Now with Boost.Hana we can also adapt
#include <boost/hana/adapt_struct.hpp>
BOOST_HANA_ADAPT_STRUCT(Person,
name,
age
);
The question: Is there a sort of BOOST_HANA_ADAPT_ASSOC_STRUCT
(equivalent to BOOST_FUSION_ADAPT_ASSOC_STRUCT
) in Boost.Hana? Or is this done differently now?
Bonus question: Is there a BOOST_HANA_ADAPT_TPL
?
According to the documentation of Boost.Fusion the BOOST_FUSION_ADAPT_ASSOC_STRUCT
macro generates boiler plate for modeling
Random Access Sequence and Associative Sequence.
With Boost.Hana these additional macros are not needed as you get additional functionality for free without any boiler plate.
For "Associative Sequence", Boost.Hana has the Searchable
concept that provides the at_key
function.
While Boost.Hana does not have iterators for "Random Access Sequence", it does have the functions at
and at_c
in Boost.Hana's Iterable
concept.
Unfortunately Boost.Hana's Struct
does not implement these although I believe it could. Perhaps that could be added.
As for BOOST_HANA_ADAPT_TPL
, Boost.Hana has no macro for facilitating template structs, but users can still implement hana::accessors
to make a Struct
without any macro.
You can search boost::hana::Struct
s members by their name with a compile-time string:
#define BOOST_HANA_CONFIG_ENABLE_STRING_UDL
#include <boost/hana.hpp>
#include <string>
namespace hana = boost::hana;
using namespace hana::literals;
struct Person {
std::string name;
int age;
};
BOOST_HANA_ADAPT_STRUCT(
Person,
name,
age
);
int main()
{
Person person{"Joe", 42};
// hana::Searchable search by key
hana::at_key(person, "name"_s);
hana::at_key(person, "age"_s);
}