Header lexy/callback/aggregate.hpp

Callback and sink lexy::as_aggregate

lexy/callback/aggregate.hpp
namespace lexy
{
    template <typename T>
    constexpr auto as_aggregate;
}

Callback and sink to construct the aggregate T from the lexy::member and value pairs produced by lexy::dsl::member.

As a callback it has the following overloads:

(lexy::nullopt)

Returns a value constructed T.

(T&& t)

Forwards an existing object unchanged.

(Args&& …​ args)

Requires that the arguments are pairs of lexy::member instantiations and arbitrary types. It will create a value constructed T and repeatedly invoke the function of the lexy::member object on the aggregate object and the value. The result is an aggregate where all members specified as the first argument of pair are set to the value of the second argument; if the same member is given multiple times, it is repeatedly updated. This is then returned.

(T&& result, Args&&…​ args)

Same as the overload above, but it starts with result instead of a new object. This allows other rules to set/override members of result.

As a sink, it value initializes an object of type T. The sink callback has only one overload that accepts a lexy::member object and value; it will invoke the associated function on the result object and the value. The final aggregate is returned at the end.

Example 1. Parse a point
struct point
{
    int x, y;
};

struct production
{
    static constexpr auto rule = [] {
        auto value = dsl::integer<int>(dsl::digits<>);

        // Parse an integer into the x/y member of point.
        auto x_coord = (dsl::member<& point::x> = value);
        auto y_coord = (dsl::member<& point::y> = value);

        return x_coord + dsl::comma + y_coord;
    }();

    // `lexy::as_aggregate` accepts the `lexy::member<Fn>` + value pairs.
    static constexpr auto value = lexy::as_aggregate<point>;
};
Tip
See email.cpp (productions fields and message) for a complex example that makes use of as_aggregate.

See also