3

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?

seertaak
  • 1,087
  • 1
  • 9
  • 17

1 Answers1

1

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.....

DarthRubik
  • 3,927
  • 1
  • 18
  • 54
  • BTW, I believe the usual name for this sort of thing is 'proxy type'. – golvok Dec 11 '19 at 17:15
  • To make it clear: in general you would need to create such a proxy class for every property of the inner class, because you need to distinguish between properties of the same type. – Dmitrii Bulashevich Aug 07 '23 at 08:57