X3: Terse ADAPT_STRUCT for C++11

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

X3: Terse ADAPT_STRUCT for C++11

Mario Lang
Hi.

This is actually rather convenient:

#define ADAPT_STRUCT_(_, type, member)                                         \
  (decltype(std::declval<type>().member), member)

#define ADAPT_STRUCT(type, ...)                                                \
  BOOST_FUSION_ADAPT_STRUCT(type,                                              \
    BOOST_PP_SEQ_FOR_EACH(ADAPT_STRUCT_, type,                                 \
                          BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)))


// Use like this:
namespace client {
    struct data {
        int member1;
        char member2, member3;
    };
}
ADAPT_STRUCT(client::data,
  member1, member2, member3
)

* Less typing.
* No duplication of member types.
* Avoid the slightly strange foo(a, (b, c) (d, e) ...) syntax.

I wonder if Fusion or Spirit should provide this version of the
ADAPT_STRUCT macro by default.

--
Enjoy,
  ⡍⠁⠗⠊⠕

------------------------------------------------------------------------------
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: X3: Terse ADAPT_STRUCT for C++11

Michael Powell-2
On Mon, Jun 15, 2015 at 8:33 AM, Mario Lang <[hidden email]> wrote:

> Hi.
>
> This is actually rather convenient:
>
> #define ADAPT_STRUCT_(_, type, member)                                         \
>   (decltype(std::declval<type>().member), member)
>
> #define ADAPT_STRUCT(type, ...)                                                \
>   BOOST_FUSION_ADAPT_STRUCT(type,                                              \
>     BOOST_PP_SEQ_FOR_EACH(ADAPT_STRUCT_, type,                                 \
>                           BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)))
>
>
> // Use like this:
> namespace client {
>     struct data {
>         int member1;
>         char member2, member3;
>     };
> }
> ADAPT_STRUCT(client::data,
>   member1, member2, member3
> )
>
> * Less typing.
> * No duplication of member types.
> * Avoid the slightly strange foo(a, (b, c) (d, e) ...) syntax.
>
> I wonder if Fusion or Spirit should provide this version of the
> ADAPT_STRUCT macro by default.

Personally, I like it. +1

> --
> Enjoy,
>   ⡍⠁⠗⠊⠕
>
> ------------------------------------------------------------------------------
> _______________________________________________
> Spirit-general mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/spirit-general

------------------------------------------------------------------------------
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: X3: Terse ADAPT_STRUCT for C++11

damien.buhl@lecbna.org
Hi,

This is already in Boost 1.58.0 and is possible to use in C++03, C++98
compilers too.

http://www.boost.org/doc/libs/1_58_0/libs/fusion/doc/html/fusion/adapted/adapt_struct.html

> namespace demo
> {
>     struct employee
>     {
>         std::string name;
>         int age;
>     };
> }
>
> // demo::employee is now a Fusion sequence
> BOOST_FUSION_ADAPT_STRUCT(
>     demo::employee,
>     name,
>     age)

I used to do this in my projects for long and brought it to Boost.Fusion
mainline in 1.58.0.

In my version you can even mix it : type deduction, type specification.

Cheers,
--
Damien Buhl

On 15/06/2015 14:39, Michael Powell wrote:

> On Mon, Jun 15, 2015 at 8:33 AM, Mario Lang <[hidden email]> wrote:
>> Hi.
>>
>> This is actually rather convenient:
>>
>> #define ADAPT_STRUCT_(_, type, member)                                         \
>>   (decltype(std::declval<type>().member), member)
>>
>> #define ADAPT_STRUCT(type, ...)                                                \
>>   BOOST_FUSION_ADAPT_STRUCT(type,                                              \
>>     BOOST_PP_SEQ_FOR_EACH(ADAPT_STRUCT_, type,                                 \
>>                           BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)))
>>
>>
>> // Use like this:
>> namespace client {
>>     struct data {
>>         int member1;
>>         char member2, member3;
>>     };
>> }
>> ADAPT_STRUCT(client::data,
>>   member1, member2, member3
>> )
>>
>> * Less typing.
>> * No duplication of member types.
>> * Avoid the slightly strange foo(a, (b, c) (d, e) ...) syntax.
>>
>> I wonder if Fusion or Spirit should provide this version of the
>> ADAPT_STRUCT macro by default.
>
> Personally, I like it. +1
>
>> --
>> Enjoy,
>>   ⡍⠁⠗⠊⠕
>>
>> ------------------------------------------------------------------------------
>> _______________________________________________
>> Spirit-general mailing list
>> [hidden email]
>> https://lists.sourceforge.net/lists/listinfo/spirit-general
>
> ------------------------------------------------------------------------------
> _______________________________________________
> Spirit-general mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/spirit-general
>

------------------------------------------------------------------------------
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: X3: Terse ADAPT_STRUCT for C++11

sehe
On 06/15/2015 04:49 PM, Damien Buhl wrote:
> I used to do this in my projects for long and brought it to Boost.Fusion
> mainline in 1.58.0.
+1 wow. I didn't notice this. I can now simplify my own code :)

------------------------------------------------------------------------------
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: X3: Terse ADAPT_STRUCT for C++11

Mario Lang
In reply to this post by damien.buhl@lecbna.org
Damien Buhl <[hidden email]> writes:

> Hi,
>
> This is already in Boost 1.58.0 and is possible to use in C++03, C++98
> compilers too.
>
> http://www.boost.org/doc/libs/1_58_0/libs/fusion/doc/html/fusion/adapted/adapt_struct.html
>
>> namespace demo
>> {
>>     struct employee
>>     {
>>         std::string name;
>>         int age;
>>     };
>> }
>>
>> // demo::employee is now a Fusion sequence
>> BOOST_FUSION_ADAPT_STRUCT(
>>     demo::employee,
>>     name,
>>     age)

Oh!  That is even better, how come we didn't notice :-)  Thanks for the hint!

--
CYa,
  ⡍⠁⠗⠊⠕ | Debian Developer <URL:http://debian.org/>
  .''`. | Get my public key via finger mlang/[hidden email]
 : :' : | 1024D/7FC1A0854909BCCDBE6C102DDFFC022A6B113E44
 `. `'
   `-      <URL:http://delysid.org/>  <URL:http://www.staff.tugraz.at/mlang/>

------------------------------------------------------------------------------
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: X3: Terse ADAPT_STRUCT for C++11

Joel de Guzman
In reply to this post by sehe
On 6/15/15 11:25 PM, Seth wrote:
> On 06/15/2015 04:49 PM, Damien Buhl wrote:
>> I used to do this in my projects for long and brought it to Boost.Fusion
>> mainline in 1.58.0.
> +1 wow. I didn't notice this. I can now simplify my own code :)

Yes, Damien did a wonderful job on this one! He also cleaned up a lot of
the ADAPT gunk that has been bothering me for quite some time.

Oh, and this is all documented, BTW:

   http://www.boost.org/doc/libs/1_58_0/libs/fusion/doc/html/fusion/adapted/adapt_struct.html

Notice too that this is consistent across all the adapt macros.

Let's all give Damien a wonderful round of +1 :-)


Regards,
--
Joel de Guzman
http://www.ciere.com
http://boost-spirit.com
http://www.cycfi.com/


------------------------------------------------------------------------------
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general
Reply | Threaded
Open this post in threaded view
|

Re: X3: Terse ADAPT_STRUCT for C++11

damien.buhl@lecbna.org
On 16/06/2015 00:00, Joel de Guzman wrote:

> On 6/15/15 11:25 PM, Seth wrote:
>> On 06/15/2015 04:49 PM, Damien Buhl wrote:
>>> I used to do this in my projects for long and brought it to Boost.Fusion
>>> mainline in 1.58.0.
>> +1 wow. I didn't notice this. I can now simplify my own code :)
>
> Yes, Damien did a wonderful job on this one! He also cleaned up a lot of
> the ADAPT gunk that has been bothering me for quite some time.
>
> Oh, and this is all documented, BTW:
>
>    http://www.boost.org/doc/libs/1_58_0/libs/fusion/doc/html/fusion/adapted/adapt_struct.html
>
> Notice too that this is consistent across all the adapt macros.
>
> Let's all give Damien a wonderful round of +1 :-)

Thanks Joel :) Thank to you for reviewing my changes. Don't give me too
much +1 I could become awfully full of myself. :p

------------------------------------------------------------------------------
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general