Automatic deducing of Attribute

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

Automatic deducing of Attribute

Orient
I tried to exclude the need of BOOST_FUSION_ADAPT_STRUCT and it results in the following proof of concept:

#include <type_traits>
#include <utility>

struct filler { template< typename type > operator type (); };

template< typename A, typename index_sequence = std::index_sequence< 0 >, typename = void >
struct aggregate_arity
    : std::integral_constant< std::size_t, (index_sequence::size() - 1) >
{
    
};

template< typename A, std::size_t ...indices >
struct aggregate_arity< A, std::index_sequence< indices... >, std::__void_t< decltype(A{(indices, filler{})...}) > >
    : aggregate_arity< A, std::index_sequence< indices..., sizeof...(indices) > >
{
    
};

#include <sstream>

template< typename Iterator, typename Sentinel = Iterator >
struct parser
{
    
    Iterator beg;
    Sentinel const end;
    
    template< typename Attribute, std::size_t ...indices >
    Attribute
    parse(std::index_sequence< indices... >)
    {
        parser & parser_ = *this;
        return {(static_cast< void >(indices), parser_)...};
    }
    
    template< typename Attribute >
    std::enable_if_t< !std::is_arithmetic< Attribute >::value, Attribute >
    parse()
    {
        return parse< Attribute >(std::make_index_sequence< aggregate_arity< Attribute >::value >{});
    }
    
    template< typename Attribute >
    std::enable_if_t< std::is_arithmetic< Attribute >::value, Attribute >
    parse()
    {
        Attribute attr;
        std::istringstream iss_({beg, end});
        iss_ >> attr;
        std::advance(beg, iss_.tellg());
        return attr;
    }
    
    template< typename Attribute >
    operator Attribute ()
    {
        return parse< Attribute >();
    }
    
};

#include <iostream> 

#include <cstdlib>

struct ast
{
    
    int a;
    int b;
    
};

int
main()
{
    std::string input = "123 321";
    parser< typename std::string::const_iterator > parser_{std::cbegin(input), std::cend(input)};
    ast const ast_ = parser_;
    std::cout << ast_.a << ' ' << ast_.b << std::endl;
    return EXIT_SUCCESS;
}

[Live example](http://coliru.stacked-crooked.com/a/cda79dc371cf5950).

Does it buy us anything?
Reply | Threaded
Open this post in threaded view
|

Re: Automatic deducing of Attribute

Joel de Guzman
On 2/18/16 12:15 PM, Orient wrote:
> I tried to exclude the need of BOOST_FUSION_ADAPT_STRUCT and it results in
> the following proof of concept:
>
>
>
> [Live example](http://coliru.stacked-crooked.com/a/cda79dc371cf5950).
>
> Does it buy us anything?

Seems like an interesting idea. Not sure how this can be applied though.

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


------------------------------------------------------------------------------
Site24x7 APM Insight: Get Deep Visibility into Application Performance
APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month
Monitor end-to-end web transactions and take corrective actions now
Troubleshoot faster and improve end-user experience. Signup Now!
http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140
_______________________________________________
Spirit-general mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/spirit-general