简体   繁体   中英

How to define get/set pair with BOOST_HANA_ADAPT_ADT?

I would like to introspect on an third-party ADT which defines pairs of getters/setters for access to "properties" of the class. For example:

struct Echo {
    float mix;  // read-only, don't ask why.
    void set_mix(float mix);
};

I would like to write:

BOOST_HANA_ADAPT_ADT(Echo, 
    (mix, 
     [] (const auto& self) { return self.mix; }, 
     [] (auto& self, float x) { self.set_mix(x); })
);

Is this possible?

I am not sure exactly what you are trying to do, but could you do something with using a dummy type like so:

#include "boost/hana.hpp"


struct Echo {
    float mix;  // read-only, don't ask why.
    void set_mix(float mix);
};

//Getter and setter functionality moved into here
struct FakeType
{
    Echo* const inner;
    FakeType(Echo* inner) : inner(inner){}
    operator float() { return inner->mix; }
    FakeType& operator=(const float& value) { 
        inner->set_mix(value); 
        return *this;
    }
};



BOOST_HANA_ADAPT_ADT(Echo,
    //Now returns a "FakeType" instead of the float directly
    (mix, [](Echo& p) { return FakeType(&p); })  

);

The FakeType class handles all of the "getter" and "setter" type stuff.....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM