Skip to content

Make pfr more friendly for integrating into other libraries? #100

@denzor200

Description

@denzor200

Description of issue
Now i'm working with Boost.Fusion extension for Boost.PFR, i decomposed it into 3 PR's and opened these PR's here: boostorg/fusion#241, here boostorg/fusion#242 and here boostorg/fusion#243.
Review is moving on, there is some wishes from reviewers(more on that later).
First, I would like to talk about such a problem like 3 PR's is too much code and the review takes too long.
I suggest to make pfr more adaptable and more friendly to adapt into other libraries(not only fusion).

Suggested solution

  1. To implement two functions:
  • boost::pfr::view
  • boost::pfr::view_fields
  1. To implement BOOST_PFR_REGISTRATE_REFLECTABLE macro.
  2. To add boost::pfr::is_reflectable trait. Example of using trait:
struct A {};
struct B {};
struct C {};
BOOST_PFR_REGISTRATE_REFLECTABLE(A)
using boost::pfr::is_reflectable;
is_reflectable<A>::value;                                       //< true
is_reflectable<decltype(boost::pfr::view_fields(B{}))>::value;  //< true
is_reflectable<C>::value;                                       //< false
  1. To use boost::pfr::is_reflectable trait for create overloading of compare/io operators and specialization of std::hash function. Examples of using these overloads:
#include <boost/pfr/ops.hpp>

template <class T>
struct uniform_comparator_less {
    bool operator()(const T& lhs, const T& rhs) const noexcept {
        // If T has operator< or conversion operator then it is used.
        return boost::pfr::view(lhs) < boost::pfr::view(rhs);
    }
};
#include <boost/pfr/functions_for.hpp>

struct pair_like {
    int first;
    short second;
};

BOOST_PFR_REGISTRATE_REFLECTABLE(pair_like)   // Added pair_like's specialization for is_reflectable trait.

// ...

assert(pair_like{1, 2} < pair_like{1, 3});
#include <boost/pfr/io_fields.hpp>

struct pair_like {
    int first;
    std::string second;
};

inline std::ostream& operator<<(std::ostream& os, const pair_like& x) {
    return os <<  bost::pfr::view_fields(x);
}
  1. To declare io, eq, ne, lt, gt, le, ge, hash_value, io_fields, eq_fields, ne_fields, lt_fields, gt_fields, le_fields, ge_fields, hash_fields, BOOST_PFR_FUNCTIONS_FOR as deprecated, and to actualize https://www.boost.org/doc/libs/1_75_0/doc/html/boost_pfr/tutorial.html#boost_pfr.tutorial.three_ways_of_getting_operators.

Conclusion
If everything planned can be done, then the PFR will be adapted as easily as the std::tuple, it will be just one small PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedPR with a fix would be appreciated

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions