简体   繁体   中英

Alternative implementation for BOOST_FUSION_ADAPT_STRUCT

From link , the usage of BOOST_FUSION_ADAPT_STRUCT usage which is

namespace demo
{
    struct employee
    {
        std::string name;
        int age;
    };
}

BOOST_FUSION_ADAPT_STRUCT(
    demo::employee,
    (std::string, name)
    (int, age))

What will be the actual implementation to replace the use of the BOOST_FUSION_ADAPT_STRUCT macro.

I might depend on compiler/flags. On mine:

namespace boost { namespace fusion { namespace traits { template< > struct tag_of<demo::employee > { typedef struct_tag type; }; template< > struct tag_of<demo::employee const> { typedef struct_tag type; }; } namespace extension { template< > struct access::struct_member< demo::employee , 0 > { typedef std::string attribute_type; typedef attribute_type type; template<typename Seq> struct apply { typedef typename add_reference< typename mpl::eval_if< is_const<Seq> , add_const<attribute_type> , mpl::identity<attribute_type> >::type >::type type; constexpr static type call(Seq& seq) { return seq. name; } }; }; template< > struct struct_member_name< demo::employee , 0 > { typedef char const* type; constexpr static type call() { return "name"; } }; template< > struct access::struct_member< demo::employee , 1 > { typedef int attribute_type; typedef attribute_type type; template<typename Seq> struct apply { typedef typename add_reference< typename mpl::eval_if< is_const<Seq> , add_const<attribute_type> , mpl::identity<attribute_type> >::type >::type type; constexpr static type call(Seq& seq) { return seq. age; } }; }; template< > struct struct_member_name< demo::employee , 1 > { typedef char const* type; constexpr static type call() { return "age"; } }; template< > struct struct_size<demo::employee> : mpl::int_<2> {}; template< > struct struct_is_view< demo::employee > : mpl::false_ {}; } } namespace mpl { template<typename> struct sequence_tag; template< > struct sequence_tag<demo::employee> { typedef fusion::fusion_sequence_tag type; }; template< > struct sequence_tag< demo::employee const > { typedef fusion::fusion_sequence_tag type; }; } }

Just for readability:

#include <string>
#include <boost/fusion/adapted/struct.hpp>

namespace demo {
    struct employee
    {
        std::string name;
        int age;
    };
}

namespace boost {
    namespace fusion {
        namespace traits {
            template <> struct tag_of<demo::employee> { typedef struct_tag type; };
            template <> struct tag_of<demo::employee const> { typedef struct_tag type; };
        } // namespace traits
        namespace extension {
            template <> struct access::struct_member<demo::employee, 0> {
                typedef std::string attribute_type;
                typedef attribute_type type;
                template <typename Seq> struct apply {
                    typedef typename add_reference<
                        typename mpl::eval_if<is_const<Seq>, add_const<attribute_type>,
                                 mpl::identity<attribute_type>>::type>::type
                                     type;
                    constexpr static type call(Seq& seq) { return seq.name; }
                };
            };
            template <> struct struct_member_name<demo::employee, 0> {
                typedef char const* type;
                constexpr static type call() { return "name"; }
            };
            template <> struct access::struct_member<demo::employee, 1> {
                typedef int attribute_type;
                typedef attribute_type type;
                template <typename Seq> struct apply {
                    typedef typename add_reference<
                        typename mpl::eval_if<is_const<Seq>, add_const<attribute_type>,
                                 mpl::identity<attribute_type>>::type>::type
                                     type;
                    constexpr static type call(Seq& seq) { return seq.age; }
                };
            };
            template <> struct struct_member_name<demo::employee, 1> {
                typedef char const* type;
                constexpr static type call() { return "age"; }
            };
            template <> struct struct_size<demo::employee> : mpl::int_<2> {};
            template <> struct struct_is_view<demo::employee> : mpl::false_ {};
        } // namespace extension
    } // namespace fusion
    namespace mpl {
        template <typename> struct sequence_tag;
        template <> struct sequence_tag<demo::employee> {
            typedef fusion::fusion_sequence_tag type;
        };
        template <> struct sequence_tag<demo::employee const> {
            typedef fusion::fusion_sequence_tag type;
        };
    } // namespace mpl
} // namespace boost

For actual alternatives, see

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM