IIUC, nowadays one can easily do static reflection of POD classes/structs using, e.g., boost::hana or magic_get.
How can I perform similar static reflection of some member functions of non-POD classes? (I'm looking for a solution which doesn't require using a compiler fork which implements proposed static reflection extensions.)
In my specific use case, I have existing classes each having some setters. I'd like to wrap those classes with an adapter which pulls values of the appropriate types from some input generators and calls the matching setters on the adapted instance.
class A {
public:
void setParam1(double); // To be annotated, e.g. REFLECT_MEM_FUNC(void setParam1(double);)
void setParam2(Param); // To be annotated, e.g. REFLECT_MEM_FUNC(void setParam2(Param);)
void anotherFunc(); // Not annotated
private:
double param1;
Param param2;
// ...
};
class B {
public:
void mySetParam(int); // To be annotated, e.g. REFLECT_MEM_FUNC(void mySetParam(int);)
private:
SomeOtherClass inner; // parameter passed on to this member
// ...
};
template<
typename AdaptedT,
// Tuple of references to classes, each implementing AppropriateParam get();
// AppropriateParam return type of each getter must match the matching reflected setter.
// To be clear, it's the instantiator's responsibility to provide matching Inputs; Inputs is not an auto-generated type since it can be anything providing the expected static interface.
typename Inputs
> class Adapter {
public:
Adapter(AdaptedT, Inputs);
// Calls adapted's reflected setters with the values provided by inputs.
setParametersFromInputs();
private:
AdaptedT adapted;
Inputs inputs;
// ...
};
// Just to demonstrate the static interface expected from an input
template<typename ParamT>
class Input {
public:
ParamT get();
// ...
};
Input<double> inputDouble { /* ... */ };
Input<Param> inputParam { /* ... */ };
Adapted<A, std::tuple<Input<double>&, Input<Param>&>> adapter {
{ /* ... */ },
{inputDouble, inputParam}
};
// Calls:
// adapted.setParam1(std::get<0>(inputs).get());
// adapted.setParam2(std::get<1>(inputs).get());
adapter.setParametersFromInputs();
Aucun commentaire:
Enregistrer un commentaire