Re: spirit::karma specialization for std::optional (C++17)
Michael Powell <[email protected]>
| Newsgroups | gmane.comp.parsers.spirit.general |
|---|---|
| Message-ID | <CAMEoF_EOMCuWFrWLfENdjwahN+1_tJVg6Eb0GKdLryR8PdwhAA@mail.gmail.com> |
Post more detail, like your grammar, and perhaps someone can help you. On Fri, Apr 21, 2017 at 11:21 AM, Emiliano Canedo <[email protected]> wrote: > Yes, but I need to handle this with the library. When a optional don't have > a value, karma must skip this attribute. The same behavior like with > boost::optional (Karma handles boost::optional). > > For example, I can't do something like this: > static type call(std::experimental::optional<Attribute> const& attr, > Context& ctx) > { > if (attr) > return attr.value(); > else > return nullopt; > } > > The return type always must be Attribute. > > On Fri, Apr 21, 2017 at 12:13 PM, Michael Powell <[email protected]> > wrote: >> >> On Fri, Apr 21, 2017 at 11:06 AM, Emiliano Canedo >> <[email protected]> wrote: >> > I can omit the output when optional doesn't have a value, but I don't >> > know >> > how >> >> if (opt.has_value()) { >> /* output value */ >> } >> >> http://en.cppreference.com/w/cpp/utility/optional >> >> > On Fri, Apr 21, 2017 at 11:52 AM, Michael <[email protected]> wrote: >> >> >> >> >> >> >> >> On April 21, 2017 10:20:42 AM EDT, Emiliano Canedo >> >> <[email protected]> wrote: >> >> >Hello, >> >> > >> >> >I'm working in a spirit::karma specialization for std::optional >> >> >(C++17). I >> >> >have this code for now: >> >> > >> >> >//----------------------------------- >> >> > >> >> >namespace boost >> >> >{ >> >> >namespace spirit >> >> >{ >> >> >namespace traits >> >> >{ >> >> > >> >> >// This handles std::optional attributes >> >> >template <typename Attribute, typename Exposed> >> >> >struct extract_from_attribute<std::experimental::optional<Attribute>, >> >> >Exposed> >> >> >{ >> >> > typedef Attribute const& type; >> >> > template <typename Context> >> >> > static type call(std::experimental::optional<Attribute> const& >> >> > attr, >> >> >Context& ctx) >> >> > { >> >> > return attr.value(); >> >> > } >> >> >}; >> >> > >> >> >template <typename Attribute, typename Exposed> >> >> >struct extract_from_attribute<std::experimental::optional<Attribute >> >> >const>, >> >> >Exposed> >> >> >{ >> >> > typedef Attribute const& type; >> >> > template <typename Context> >> >> > static type call(std::experimental::optional<Attribute const> >> >> > const& >> >> >attr, Context& ctx) >> >> > { >> >> > return attr.value(); >> >> > } >> >> >}; >> >> > >> >> >} >> >> >} >> >> >} >> >> > >> >> >//----------------------------------- >> >> > >> >> >This works good to extract the value, but, I need to handle the calls >> >> >if >> >> >the optional have, or not, a value. I tried to understand how karma >> >> >handle >> >> >this for boost::optional, but I'm stuck with that. Any idea? >> >> >> >> Are you trying to invoke the output regardless of optional state? Or >> >> can >> >> you omit the output when optional does not have a value? >> >> >> >> >Thanks!! >> >> > >> >> > >> >> >> >> > >------------------------------------------------------------------------ >> >> > >> >> >> >> > >> >> > > >------------------------------------------------------------------------------ >> >> >Check out the vibrant tech community on one of the world's most >> >> >engaging tech sites, Slashdot.org! http://sdm.link/slashdot >> >> > >> >> >> >> > >------------------------------------------------------------------------ >> >> > >> >> >_______________________________________________ >> >> >Spirit-general mailing list >> >> >[email protected] >> >> >https://lists.sourceforge.net/lists/listinfo/spirit-general >> >> >> >> -- >> >> Sent from my Android device with K-9 Mail. Please excuse my brevity. >> >> >> >> >> >> >> >> ------------------------------------------------------------------------------ >> >> Check out the vibrant tech community on one of the world's most >> >> engaging tech sites, Slashdot.org! http://sdm.link/slashdot >> >> _______________________________________________ >> >> Spirit-general mailing list >> >> [email protected] >> >> https://lists.sourceforge.net/lists/listinfo/spirit-general >> > >> > >> > >> > >> > ------------------------------------------------------------------------------ >> > Check out the vibrant tech community on one of the world's most >> > engaging tech sites, Slashdot.org! http://sdm.link/slashdot >> > _______________________________________________ >> > Spirit-general mailing list >> > [email protected] >> > https://lists.sourceforge.net/lists/listinfo/spirit-general >> > >> >> >> ------------------------------------------------------------------------------ >> Check out the vibrant tech community on one of the world's most >> engaging tech sites, Slashdot.org! http://sdm.link/slashdot >> _______________________________________________ >> Spirit-general mailing list >> [email protected] >> https://lists.sourceforge.net/lists/listinfo/spirit-general > > > > ------------------------------------------------------------------------------ > Check out the vibrant tech community on one of the world's most > engaging tech sites, Slashdot.org! http://sdm.link/slashdot > _______________________________________________ > Spirit-general mailing list > [email protected] > https://lists.sourceforge.net/lists/listinfo/spirit-general > ------------------------------------------------------------------------------ Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot