Re: spirit::karma specialization for std::optional (C++17)
Emiliano Canedo <[email protected]>
| Newsgroups | gmane.comp.parsers.spirit.general |
|---|---|
| Message-ID | <CAMnrcdyC85hVh_dr5CQnN-HxNZvjt=m4vMj5AHjWrMBpFUYYEw@mail.gmail.com> |
A complete example:
//--------------------------------------------------------------------------------------------
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();
}
};
}
}
}
struct MyStruct
{
std::experimental::optional<int> foo;
std::experimental::optional<float> flt;
struct Other
{
std::experimental::optional<std::string> bar;
};
std::vector<Other> other;
};
BOOST_FUSION_ADAPT_STRUCT
(RTB::MyStruct::Other,
bar)
BOOST_FUSION_ADAPT_STRUCT
(RTB::MyStruct,
foo,
flt,
other)
template<typename Iterator>
struct BidRequestSerializer : boost::spirit::karma::grammar<Iterator,
MyStruct()>
{
BidRequestSerializer() : BidRequestSerializer::base_type(start_)
{
using boost::spirit::karma::int_;
using boost::spirit::karma::float_;
using boost::spirit::karma::string;
using boost::spirit::karma::lit;
other_obj_ = '{' << string << '}';
other_ = lit("other:") << '[' << other_obj_ % ',' << ']';
start_ = '{' << int_ << ',' << float_ << ',' << other_ << '}';
}
private:
boost::spirit::karma::rule<Iterator, MyStruct::Other()> other_obj_;
boost::spirit::karma::rule<Iterator, std::vector<MyStruct::Other>()>
other_;
boost::spirit::karma::rule<Iterator, MyStruct()> start_;
};
//-------------------------------------------------------------------------------------------------------
On Fri, Apr 21, 2017 at 12:26 PM, Michael Powell <[email protected]>
wrote:
> 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
> _______________________________________________
> 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