mercredi 1 mars 2017

Generic class method computation based on variable input

I roughly got the following setup:

#include <iostream>
using namespace std;

template<typename T>
class Element{
    T getX() const;
    T getY() const;
 private:
    T x,y;
    std::vector<float> handling_times;
    float cost;
};

template<typename T, typename Tnext>
class Bloc {
     T getX() const;
     T getY() const;
 private:
     T x,y;
     float effort;
     float damage_done;
};

template<typename T>
class Measurements {
void calcMeasurements(const std::vector<T*> &data);
     float getMean() const;
 private:
     float mean;
};


int main() {
     std::vector<Element<int>*> elements;
     // fill with elements
     std::vector<Bloc<float>*> blocs;
     // fill with blocs

     // calculate mean of blocs effort
     Measurements<Bloc<float>> bloc_mean_effort_measurement;
     bloc_mean_effort_measurement.calcMeasurements(blocs);

     return 0;
 }

So two classes Element and Bloc which hold some data I'd like to perform Measurements on. For example, I'd like to measure the getMean() of an Element's handling_times which is of type std::vector<float>. Another case would be to measure the mean of std::vector<Bloc<float>*> blocs based on the effort stored in each Bloc. As you can see the input types for an Measurement vary but the functionality behind the mean calculation always stays the same. I'd like to have this functionality only implemented once (mean is only the simplest example I could think of) and use it on different types. Furthermore, I can't get my head around, how to pass the Measurement object based on which entity (e.g. Element costs or Bloc effort) the measure should be computed. Would it make sense to have an enum PossibleMeasurements in Element with HANDLING_TIMES and COSTS. So to say for each private variable I would like to be able to compute measures on.

Aucun commentaire:

Enregistrer un commentaire