Boost logo

Boost Users :

Subject: Re: [Boost-users] [Proto] grammar matching terminal or pointer to terminal
From: Eric Niebler (eric_at_[hidden])
Date: 2009-09-28 17:44:15


joel wrote:
> Eric Niebler wrote:
>> I don't know. Try telling me again what you're trying to do.
> note to self: don't post without coffee first v_v
>
> I should match : char, float, char*..* or float*..* , pointer being any
> level of indirection

Try this:

   #include <boost/proto/core.hpp>
   #include <boost/proto/transform/arg.hpp>

   namespace mpl = boost::mpl;
   namespace proto = boost::proto;

   template<typename T>
   struct is_ok : mpl::false_ {};

   template<>
   struct is_ok<char> : mpl::true_ {};

   template<>
   struct is_ok<float> : mpl::true_ {};

   template<typename T>
   struct is_ok<T *> : is_ok<T> {};

   struct IsOK
     : proto::and_<
           proto::terminal<proto::_>
         , proto::if_<is_ok<proto::_value>()>
>
   {};

   int main()
   {
       BOOST_MPL_ASSERT((
           proto::matches<proto::terminal<char>::type, IsOK>));
       BOOST_MPL_ASSERT((
           proto::matches<proto::terminal<float>::type, IsOK>));
       BOOST_MPL_ASSERT((
           proto::matches<proto::terminal<char*>::type, IsOK>));
       BOOST_MPL_ASSERT((
           proto::matches<proto::terminal<char**>::type, IsOK>));

       BOOST_MPL_ASSERT_NOT((
           proto::matches<proto::terminal<short>::type, IsOK>));
       BOOST_MPL_ASSERT_NOT((
           proto::matches<proto::terminal<short*>::type, IsOK>));

       return 0;
   }

-- 
Eric Niebler
BoostPro Computing
http://www.boostpro.com

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net