Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#231 Added pfr extension #232

Closed
wants to merge 9 commits into from
Closed

Conversation

denzor200
Copy link
Contributor

So, here is my implementation of pfr extension for boost fusion library. This extension may be useful for some boost spirit users.
I am ready to answer any question you may have to facilitate the consideration of this PR. Thank you for attention.

@denzor200 denzor200 marked this pull request as draft March 27, 2021 19:36
@daminetreg
Copy link
Contributor

Looks interesting 🙂 I have to accustomate myself with pfr first before reviewing here.

@djowel
Copy link
Collaborator

djowel commented Mar 29, 2021

I haven't inspected... Why are the checks failing?

@denzor200
Copy link
Contributor Author

I haven't inspected... Why are the checks failing?

Yes! There is some checks failing..
All on my side - I'll fix it during this or next week.
But i can't fix AppVeyor-side fails, just like this:
'cl' is not recognized as an internal or external command

@denzor200 denzor200 marked this pull request as ready for review June 21, 2021 20:26
@denzor200
Copy link
Contributor Author

@djowel i'm done. Appveyor's checks was fixed. Travis was fixed too, but now we cant verify this))
It was tested with boost.spirt.x3 in my project, in my opiniton this extension is good.
You can start to inspect, if you wish.

@djowel
Copy link
Collaborator

djowel commented Jun 21, 2021

I'll look and study tomorrow

@denzor200
Copy link
Contributor Author

Hello everyone! This PR will be extended in the future. I suppose, we need an alternative way to tell fusion that structure must be serialized by pfr, it's way without 'adapt' macro.

#include <iostream>
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/adapted/pfr.hpp>
 
constexpr std::string_view str = "100 200";
 
struct A
{
    int first;
    int second;
};

/// No BOOST_FUSION_ADAPT_STRUCT here! And no BOOST_FUSION_ADAPT_PFR too..
 
int main()
{
    A rec;
    auto iter = str.begin();
    auto const end = str.end();
    using boost::spirit::x3::int_;
    bool r = boost::spirit::x3::parse(iter, end, int_ >> ' ' >> int_, boost::fusion::pfr(rec));
    std::cout << "done (" << std::boolalpha << (r && iter == end) << ")" << std::endl;
    return 0;
}
#include <iostream>
#include <boost/spirit/home/x3.hpp>
#include <boost/fusion/adapted/pfr.hpp>

constexpr std::string_view str = "100 200";

struct A
{
    int first;
    int second;
};

boost::spirit::x3::rule<class test, boost::fusion::pfr_reference<A>> const test = "test";
auto const test_def = boost::spirit::x3::int_ >> ' ' >> boost::spirit::x3::int_;
BOOST_SPIRIT_DEFINE(test);

int main()
{
    A rec;
    auto iter = str.begin();
    auto const end = str.end();
    bool r = boost::spirit::x3::parse(iter, end, test, boost::fusion::pfr(rec));
    std::cout << "done (" << std::boolalpha << (r && iter == end) << ")" << std::endl;
    return 0;
}

@djowel
Copy link
Collaborator

djowel commented Sep 4, 2021

All checks have failed? What's up with that?

Still having 'cl' is not recognized as an internal or external command... it seems. Hrmmm...

@denzor200
Copy link
Contributor Author

Hello everyone! This PR will be extended in the future. I suppose, we need an alternative way to tell fusion that structure must be serialized by pfr, it's way without 'adapt' macro.
...

Addition to my previous comment.. Consider this simple fusion-able structure:

struct A
{
    int first;
    int second;
    int third;
};

BOOST_FUSION_ADAPT_STRUCT(A, first, second/*, third*/);

I suggest giving the user of the library the ability to choose from two ways:
1. boost::fusion::pfr approach

auto a = A{100, 200, 300};
boost::fusion::for_each(boost::fusion::pfr(a), [](auto val) {
	std::cout << val << " ";
});

Will print "100 200 ", because boost::fusion::pfr is intended to indicate that boost.fusion SHOULD serialize value using boost.pfr library.

2. boost::fusion::pfr_fields approach

auto a = A{100, 200, 300};
boost::fusion::for_each(boost::fusion::pfr_fields(a), [](auto val) {
	std::cout << val << " ";
}); 

Will print "100 200 300 " because boost::fusion::pfr_fields is intended to indicate that boost.fusion MUST serialize value using boost.pfr library. This approach do ignore another ways to serialize value and work only with pfr library.

For a structure that is not fusion-able, it makes no difference which way to choose, because boost::fusion::pfr and boost::fusion::pfr_fields behave the same in this case - both look to pfr for serialization.

@djowel what do you say about this?
PS Inspired by boost::pfr::io && boost::pfr::io_fields pair from the pfr library.
https://www.boost.org/doc/libs/1_75_0/doc/html/boost_pfr/tutorial.html#boost_pfr.tutorial.three_ways_of_getting_operators

@denzor200
Copy link
Contributor Author

Duplicate of the #243

@denzor200 denzor200 closed this Jan 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants