Search code examples
c++boost-spiritboost-fusion

Can spirit use a Factory instead of ADAPT_STRUCT to process rule?


I'd like to use a factory function in a semantic action, but I haven't been able to find the right recipe using phoenix or fusion.

My spirit-qi rule that would look something like:

object = type_identifier >> arg_list;

and I'd like to have Fusion perform something like:

object = Factory(type_identifier)(arg_list)

instead of using BOOST_FUSION_ADAPT_STRUCT to create a structure containing type_identifier and arg_list.

How can something like this be done? Thanks.


Solution

  • Yes. In principle there are three approaches:

    1. rely on the public constructor
    2. use a semantic action (e.g. qi::int_ [ qi::_val = boost::phoenix::construct<type_identifier>(qi::_1) ])
    3. use attribute transformation traits [See Customization of Spirit's Attribute Handling in the docs). In this case you'd simply have

       qi::rule<It, Mytype()> r = qi::_int;
      

      and the corresponding trait (assign_to_attribute_from_value<MyType, imt>) handles the assignment